#!/usr/bin/perl -w
use strict;

#person in charge of this script
#this should be the person currently in charge of scripts for the SGN project
my $script_maintainer='Teri Solow <tms45@cornell.edu>';

use lib '/soldb/local_scripts/custom_packages';

#local packages to use
use runtime;


@ARGV or print "No input parameters, proceeding with default.\n";

my @arg_pairs = split (/\-/, (join ' ', @ARGV));

my %args=();

foreach (@arg_pairs){

    $_ or next;
    my ($flag, $val)=split /\s+/;
    $args{$flag}=$val;
}

my $bad_seq_out_file=$args{'l'};
my $seq_out_file=$args{'o'};
my $seq_in_file=$args{'s'};
my $qual_in_file=$args{'q'};
my $mask_treshold=15;

my $script_file=__FILE__;


#main body of script
#####################

my $start_time=time;

my %seq_hash=();
my %qual_hash=();

#get the sequences and quality and hash them

my ($defline, $dataline);

open SEQ_FILEIN, $seq_in_file;
foreach (<SEQ_FILEIN>){
    chomp;
    if (/^>([^\s]+)\s/){ 
	if ($defline){
	    $seq_hash{$defline}=$dataline;
	}
	$defline=$1;
	$dataline='';
	next;
    }

    s/^\s*([^\s]+)\s*$/$1/;
    $dataline.=$_;
}
$seq_hash{$defline}=$dataline;
close SEQ_FILEIN;

($defline,$dataline)=('','');

open QUAL_FILEIN, $qual_in_file;

foreach (<QUAL_FILEIN>){
    chomp;
    if (/^>([^\s]+)\s/){ 

	if ($defline){
	    $qual_hash{$defline}=$dataline;
	}
	$defline=$1;
	$dataline='';
	next;
    }

    $dataline.=$_;
}
$qual_hash{$defline}=$dataline;
close QUAL_FILEIN;


my @low_complexity;

foreach (keys %qual_hash){

    my $seq_length=length $seq_hash{$_};

#drop short seq
	if($seq_length < 150){
	    delete $seq_hash{$_};
	    next;
	}

    my @qual=split(/\s/,$qual_hash{$_});
    my @seq=split(//,$seq_hash{$_});

#side test - find out how complex the sequence is
#count instances of each letter
#mark as low complexity if one letter comprises over half the sequence or two > 3/4

	my %letter_count=();
	my $bad_seq;
	foreach (@seq){
	    $letter_count{$_}++;
	}

	my $first_letter;
	foreach $first_letter (keys %letter_count){
	    if ($letter_count{$first_letter}>(0.5 * $seq_length)){
		my $first_pct=int(($letter_count{$first_letter} * 100)/$seq_length);
		push @low_complexity, ">$_ ($first_letter-$first_pct%)\n$seq_hash{$_}";
		$bad_seq='true';
		last;
	    }
	    my $second_letter;
	    foreach $second_letter (keys %letter_count){
		$second_letter eq $first_letter and next;
		if (($letter_count{$first_letter}+$letter_count{$second_letter})>(0.75 * $seq_length)){
		    my $first_pct=int(($letter_count{$first_letter} * 100)/$seq_length);
		    my $second_pct=int(($letter_count{$second_letter} * 100)/$seq_length);
		    push @low_complexity, ">$_ ($first_letter-$first_pct%,$second_letter-$second_pct%)\n$seq_hash{$_}" ;
		    $bad_seq='true';
		    last;
		}
	    }
	    $bad_seq and last;
	}


	if($bad_seq){
	    delete $seq_hash{$_};
	    next;
	}

#mask out individual low quality nucleotide
    my $i=0;
    while ($i<@qual){
	($qual[$i]<=$mask_treshold) and $seq[$i]='N';
	$i++;
    }
    $seq_hash{$_}=join('',@seq);
}


print "Done parsing inputs, looking for repeats\n";

open SEQ_FILEOUT, ">$seq_out_file";

#increment nr_repeats in steps of 3 and find poly-A locations
my $nr_repeats;

for ($nr_repeats=9;$nr_repeats<=18;$nr_repeats+=3){

    my $seq_id;
    foreach $seq_id (keys %seq_hash){

	my $seq_string=$seq_hash{$seq_id};

	my $internal_repeats=$nr_repeats-2;

	my $seq_length=length $seq_string;

	$seq_string=~s/(A([NA]{$internal_repeats,})A)/\#$2\a/g;
	my @seq=split(//,$seq_string);

	my @substr;
	my $i=0;
	while ($i<@seq){
	    $seq[$i] eq '#'
		and push @substr,$i;
	    $seq[$i] eq "\a"
		and push @substr,$i;
	    $i++;
	}

	while(@substr){
	    my $start=shift @substr;
	    my $end=shift @substr;

#check to see that there are more A than Ns in the substring
#to avoid artefacts resulting form low quality regions that got all changed to N
	    my ($nr_A, $nr_N)=(0,0);
	    foreach(@seq[$start..$end]){
		$_ eq 'A' and $nr_A++;
		$_ eq 'N' and $nr_N++;
	    }

	    if (($nr_A > $nr_N) and ($end-$start+1) >= $nr_repeats){
		print SEQ_FILEOUT "min ln=$nr_repeats: Seq $seq_id ($seq_length bp) has polyA from $start to $end\n";
	    } 
	}
    }



}


close SEQ_FILEOUT;
#close QUAL_FILEOUT;


open (FILEOUT, ">$bad_seq_out_file");

print FILEOUT "Low complexity sequences:\n";
foreach (@low_complexity){
    print FILEOUT "$_\n";
}

close FILEOUT;


 runtime::runtime_print($start_time, "PolyA evaluation");


