#!/usr/bin/perl -w

=head1 NAME

fastq-filter_mono_nucleotide.pl - Remove mononucleotide sequences from fastq file 

=head1 SYNOPSIS

fastq-filter_extract_reads.pl -f sequence_reads.fastq [-help] 

 Required arguments:
    -f        Fastq file (with each sequence reprensented in four lines)

 Options:
    -help -h  Help
    
 Example usage:
    fastq-filter_mono_nucleotide.pl -f sequence_reads.fastq > filtered_reads.fastq

=head1 DESCRIPTION

Filter out reads that consist only of a single nucleotide and/or N's.  For example:
 Pass filter:
    AAATCGATCGG
    TGGGGGGGGGG
    GGGGGGGGNNT
 Fail filter (not printed):
    AAAAAAAAAAA
    AAAAAAANNNN
    NNNNNNNNNNN
	
=head1 AUTHOR

Lance Parsons <lparsons@princeton.edu>

=head1 LICENSE

This script is licensed by the Simplified BSD License
See LICENSE.TXT and <http://www.opensource.org/licenses/bsd-license>

Copyright (c) 2011, Lance Parsons
All rights reserved.

=cut

use strict;
use Getopt::Long;
use Pod::Usage;

# Variables set in response to command line arguments
# (with defaults)

my $needsHelp = '';
my $fastqfilename;

my $options_okay = &Getopt::Long::GetOptions(
	'fastq|f=s' => \$fastqfilename,
	'help|h'    => \$needsHelp
);

# Check to make sure options are specified correctly and files exist
&check_opts();

open( my $fastqfile, "<", $fastqfilename );
while (<$fastqfile>) {
	my $id   = $_;
	my $read = <$fastqfile>;
	chomp($read);
	my $id2  = <$fastqfile>;
	my $qual = <$fastqfile>;

	if (   $read =~ /^[N|A]*$/
		|| $read =~ /^[N|C]*$/
		|| $read =~ /^[N|G]*$/
		|| $read =~ /^[N|T]*$/ )
	{

	} else {
		print $id. $read . "\n" . $id2 . $qual;
	}
}

# Check for problem with the options or if user requests help
sub check_opts {
	if ($needsHelp) {
		pod2usage( -verbose => 2 );
	}

	if ( !$options_okay ) {
		pod2usage(
			-exitval => 2,
			-verbose => 1,
			-message => "Error specifying options."
		);
	}
	if ( !-e $fastqfilename ) {
		pod2usage(
			-exitval => 2,
			-verbose => 1,
			-message => "Cannot read fastq file: '$fastqfilename!'\n"
		);
	}
}
