#!/usr/bin/perl -w

#Typical run:
#First write the 3 input filename to the code
#perl time perl find-nohits.pl > live_wo_di1-di2-tri-arch_E1e-2.csv

my $act_dir="first";
my $is_hit=0;
my $act_acc=();
my $act_file=();
my $counter=0;
my $result_files;
my $firstline=();
my @act_acc_list=();
my $string=();
my $pre_act_dir=();
my $pre_act_acc=();
my %UNG=();
my $is_UNG;
my $file_name=();
my $UGI_SAUGI_P56=();
my $is_UGI_SAUGI_P56=();

open(IN1,"/db2/data/kcsabadata/dUTPase/blast-allgenomes/001-Evalue-UNG/list");
    while (<IN1>) {
        chomp;
        if ($_ =~ /(N.\S+)\.fna/) {
            $file_name=$1;
        }
        open(IN2,"/db2/data/kcsabadata/dUTPase/blast-allgenomes/001-Evalue-UNG/$file_name.fna");
        my $counter=0;
        while (<IN2>) {
            if ( $_ =~ /No hits found/ ) {
                $counter++;
            }
        }
        if ($counter == 0) {
            $UNG{$file_name}=1;
            #print"$file_name;$UNG{$file_name}\n";
        }else {
            $UNG{$file_name}=0;
            #print"$file_name;$UNG{$file_name}\n";
        }
        close IN2;
    }
close IN1;


open(IN1,"/db2/data/kcsabadata/dUTPase/blast-allgenomes/001-Evalue-UGI-SAUGI-P56/list");
    while (<IN1>) {
        chomp;
        if ($_ =~ /(N.\S+)\.fna/) {
            $file_name=$1;
        }
        open(IN2,"/db2/data/kcsabadata/dUTPase/blast-allgenomes/001-Evalue-UGI-SAUGI-P56/$file_name.fna");
        my $counter=0;
        while (<IN2>) {
            if ( $_ =~ /No hits found/ ) {
                $counter++;
            }
        }
        if ($counter <= 2) {
            $UGI_SAUGI_P56{$file_name}=1;
            #print"$file_name;$UGI_SAUGI_P56{$file_name}\n";
        }else {
            $UGI_SAUGI_P56{$file_name}=0;
            #print"$file_name;$UGI_SAUGI_P56{$file_name}\n";
        }
        close IN2;
    }
close IN1;

open (OUT, '>data.txt');
open(IN1,"AllGenomeFileNames-wo-plasmids.txt");
    while (<IN1>) {
        if ( $_ =~ /AllGenomes_14_Dec_3\/(\S+)\/(\S+)/ ) {
            $act_file=$_;
            $pre_act_dir=$1;
            $pre_act_acc=$2;
            if (($act_dir ne "first") && ($act_dir ne $1)) {
                if ($is_hit==0) {
                    $is_UNG=0;
                    foreach $acc (@act_acc_list) {
                        if ($UNG{$acc}) {
                            $is_UNG=1;
                        }
                    }
                    if ($is_UNG) {
                        print"yes;";
                    }else {
                        print"no;";
                    }
                    $is_UGI_SAUGI_P56=0;
                    foreach $acc (@act_acc_list) {
                        if ($UGI_SAUGI_P56{$acc}) {
                            $is_UGI_SAUGI_P56=1;
                        }
                    }
                    if ($is_UGI_SAUGI_P56) {
                        print"yes;";
                    }else {
                        print"no;";
                    }
                    print"$act_dir;";
                    foreach $acc (@act_acc_list) {
                        print"$acc;";
                        print OUT "$acc\n";
                        open(IN3,"/db2/data/kcsabadata/dUTPase/AllGenomes_14_Dec_3/$act_dir/$acc.fna");
                            $firstline=<IN3>;
                            chomp $firstline;
                            my @row = split(/\|/,$firstline);
                            print"$row[4];";
                        close IN3;
                    }
                    print"\n";
                }
                $is_hit=0;
                @act_acc_list=();
            }
            $act_dir=$pre_act_dir;
            $act_acc=$pre_act_acc;
            if ($act_acc =~ /(N.\S+)\.fna/) {
               push (@act_acc_list, $1);
            }
        }
        open(IN2,"/db2/data/kcsabadata/dUTPase/blast-allgenomes/001-Evalue-di1-di2-tri-arch/$act_acc");
            $counter=0;
            while (<IN2>) {
                if ( $_ =~ /No hits found/ ) {
                    $counter++;
                }
            }
            if ($counter<4) {
                $is_hit=1;
            }
        close IN2;
    }
close IN1;
close (OUT);


