#!/usr/bin/perl

use Switch;


$optionId = $ARGV[0];

if($optionId eq "-co" && ($#ARGV == 2 || $#ARGV == 3)){
	$in_folder = $ARGV[1];
	$out_folder = $ARGV[2];
	$slash_string = $ARGV[3];
	if($slash_string eq ""){
		$slash_string = '\\';	
	}
	process_folders($in_folder,$out_folder,"",$slash_string);
}

elsif($optionId eq "-fi" && ($#ARGV == 3 || $#ARGV == 4)){
	$in_folder = $ARGV[1];
	$out_folder = $ARGV[2];
	$max_rate = $ARGV[3];
	$slash_string = $ARGV[4];
	if($max_rate =~ /[^0-9\.]/){
		print "\nmax_rate supplied: '$max_rate', is not a number. check if you supplied all 4 params - option(-fi), an input directory, output directory and a max_rate \n";
		showUsage();
	}
	if($slash_string eq ""){
		$slash_string = '\\';	
	}
	process_folders($in_folder,$out_folder,$max_rate,$slash_string);
}

else{
	print "Incorrect parameters were supplied\n";
	showUsage();
}

sub showUsage
{
	print "\nUsage: \n";
	print "convert fastq to fasta format:\t./illumina_parse -co <input_path> <output_path> [<slash_string>]\n";
	print "filter low quality entries:\t./illumina_parse -fi <input_path> <output_path> <max_rate> [<slash_string>]\n";
	print "[] = optional parameter\n";
	die "";	
}

sub isnan { ! defined( $_[0] <=> 9**9**9 ) }

sub filterRead {
        my $seq         =shift;
        my $maxRate     =shift;
        my $length      =length($seq);
        my $count       =0;
        $count++ while $seq=~m/N/g;
	$count++ while $seq=~m/\./g;
        return 1 if(($count/$length)<=$maxRate);
        return 0;
}

sub process_folders{
	$in_folder = $_[0];
	$out_folder = $_[1];
	$max_rate = $_[2];
	$slash_string = $_[3];
	opendir(DIR, $in_folder) or die "can't open directory $in_folder: $!\n";
	unless(-d $out_folder){
 	   mkdir $out_folder or die "can't create directory $out_folder: $! \n";
	}
	@files = readdir DIR;
	$y_entries = 0;
	$total_entries = 0;
	$high_qual_entries = 0;
	foreach $fileName (@files) {
		if(index($fileName,'.fastq') != -1){
	 		open(in_file, "<","$in_folder/$fileName") || die "Error: file '$in_folder/$fileName' can not be opened\n";
			print "processing file $in_folder/$fileName\n";
			$fileName =~ s/.fastq/.fa/;
			open(out_file,">>","$out_folder/$fileName") || die "Error: file $out_folder/$fileName can not be opened\n";
			until(eof(in_file)){
	 	      		$h1=<in_file>;
				$seq=<in_file>;
		        	$h2=<in_file>;
        			$qual=<in_file>;
				$total_entries = $total_entries + 1;
        			chomp $h1;
	        		chomp $seq;
        			$h1=~s/\@/>/;
				if(index($h1, ' 1:Y') != -1 || index($h1,' 2:Y') != -1){
                                        $y_entries = $y_entries + 1;
                                        next;
				}
				if(index($h1, ' 1:N') != -1){
					#$temp = $slash_string."1:N";
					$temp = ":N";
					$h1 =~ s/ 1:N/$temp/;
					$h1 = $h1.$slash_string."1";
				}
				elsif(index($h1, ' 2:N') != -1){
					#$temp = $slash_string."2:N";
					$temp = ":N";
					$h1 =~ s/ 2:N/$temp/;
					$h1 = $h1.$slash_string."2";
				}
							
				if($max_rate eq ""){
					print out_file "$h1\n$seq\n";
				}
				elsif(filterRead($seq,$max_rate)){
					print out_file "$h1\n$seq\n";
					$high_qual_entries = $high_qual_entries + 1;
				}
			}
		close in_file;
		close out_file;
		print "successfully written $fileName\n";
 		}
	} 	
	print "total entries: $total_entries\n";
	print "low quality entries(entries containing 1:Y or 2:Y in the header): $y_entries\n";
	$qual_entries = $total_entries - $high_qual_entries;
	print "entries removed due to exceeding max_rate: $qual_entries \n";
	closedir DIR;
}
