#!/usr/bin/perl -w

use strict;
use warnings;

# perl /Users/zilinyang/Desktop/曹最新基因组提交/f/delete_contatament.pl /Users/zilinyang/Desktop/曹最新基因组提交/f/fasta

my $file_dir = $ARGV[0];
my $list = $ARGV[1];

&trans($file_dir, $list);

sub trans {
    my ($f, $l) = @_;
    chdir $f;
    opendir(F, $f);
    my @f = readdir F;
    @f = grep($_ !~ /^\./, @f);
    close F;
 

    foreach my $fe(@f){
        my $nn = $fe;
        $nn =~ s/\_\_.*//g;
        system("mv $fe $nn"); 
        my %h = parse_fasta("$f/$nn");
        my $of = "$f/$nn (paired, trimmed pairs) assembly.fa";
        open(O, ">$of");
        
        my $l = "/Users/zilinyang/Desktop/曹最新基因组提交/f/RemainingContamination_"."$nn"."__paired__trimmed_pairs__assembly.txt";
        open (L, $l);
        my %f;
        my %d;
        while(<L>){
            chomp;
            next unless $_ =~ /\.\./;
            my @a = split /\t/, $_;
            my ($s, $e) = split /\.\./, $a[2];
            print "$a[0]\t$a[1]\t$s\t$e\n";
            if ($a[1]-($e-$s+1)<200){
                $d{$a[0]} = $a[2];
            }else{
                push @{$f{$a[0]}}, 0;
                push @{$f{$a[0]}}, $s-1;
                push @{$f{$a[0]}}, $e;
                push @{$f{$a[0]}}, $a[1]-$e;
            }

        }
        close L;

        foreach (keys %h){
            next unless !defined $d{$_};
            if (defined $f{$_}) {
                my $s1 = substr($h{$_}, ${$f{$_}}[0], ${$f{$_}}[1]);
                my $s2 = substr($h{$_}, ${$f{$_}}[2], ${$f{$_}}[3]);
                if (length($s1)>=200 && length($s2)>=200){
                    my $D1 = print_sequence_into_file($s1, 60);  # print_sequence_into_file($dna, 70);
                    my $D2 = print_sequence_into_file($s2, 60);  # print_sequence_into_file($dna, 70);
                    print O ">$_"."s1\n$D1\n", ">$_"."s2\n$D2\n";
                }
                elsif(length($s1)>=200 && length($s2)<200){
                    my $D1 = print_sequence_into_file($s1, 60);  # print_sequence_into_file($dna, 70);
                    print O ">$_"."s1\n$D1\n";
                }
                elsif(length($s1)<200 && length($s2)>=200){
                    my $D2 = print_sequence_into_file($s2, 60);  # print_sequence_into_file($dna, 70);
                    print O ">$_"."s2\n$D2\n";
                }else{

                }

                
            }else{
                my $D = print_sequence_into_file($h{$_}, 60);  # print_sequence_into_file($dna, 70);
                print O ">$_\n$D\n";
            }
        }
        close O;
        
    }
    
}



sub parse_fasta {

    my ($infile) = @_;
    my $term     = $/; # input record separator;
    my %seq_hash = (); # key:seqid, val:seq
    open my $INFILE, $infile or die "could not open infile '$infile' : $! \n"; 
    $/ = ">"; # input record separator;
    while(<$INFILE>) {
        chomp;
        next if($_ eq '');
        my ($id, @sequencelines) = split /\n/;
        foreach my $line (@sequencelines) {
            $id =~ s/ .*//g;
            $id =~ s/.*\|//g;
            $seq_hash{$id} .= $line;
        }
    }
    $/ = $term;
    return(%seq_hash);

} # end of parse_fasta



# print $sequence into $FILE with $length character per line
sub print_sequence_into_file{   

    my($sequence, $length) = @_;
    my $DNA;
    my $string;
    # Print sequence in lines of $length
    for (my $pos = 0; $pos < length($sequence); $pos += $length){   
        $DNA=substr($sequence, $pos, $length)."\n";
        $string.=$DNA;        
    }

    return $string;

}
