# Giant Virus Finder, a software package to find giant viruses in metagenomes.
# Copyright (C) 2015, Csaba Kerepesi

# This file is part of Giant Virus Finder.

# Giant Virus Finder is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details: <http://www.gnu.org/licenses/>.

# For any other inquiries send an Email to Csaba Kerepesi: kerepesi@caesar.elte.hu
# Giant Virus Finder website: http://pitgroup.org/giant-virus-finder

# -in: single metagenome input
# -out: out dir
# Run: perl Phase1Blast.pl -out Giants-in-16Soil-megablast-2

my %args=();
for ($i=0; $i<@ARGV; $i+=2) {
    $args{$ARGV[$i]} = $ARGV[$i+1];
}

open IN0,"AllVirusGenomeFile.csv";
while (<IN0>) {
    chomp;
    $filename=$_;
    my @list=split(/\//);
    open IN,$filename;
    $first=1;
    while (<IN>) {
        chomp;
        if ($first==1) {
            my @split_line=split(/\|/,$_);
            $NC_filename{$split_line[3]}=$filename;
            $first=0;
        }
    }
    close IN;
}
close IN0;
open (IN, "TopGiants-300000-allacc.csv") || die "Can't open TopGiants-300000-allacc.csv";
while (<IN>) {
        chomp;
        if (exists $NC_filename{$_}) {
            push @Queries,$NC_filename{$_};
            $query_acc{$NC_filename{$_}}=$_;
        }
        else {
            push @Queries,"TopGiants-gb-300k/$_"; 
            $query_acc{"TopGiants-gb-300k/$_"}=$_;
        }
}
close IN;
if (exists $args{'-in'}) {
    push @mgmList,$args{'-in'};
}else {
    open (IN, "mgm.list") || die "Can't open mgm.list";
    while (<IN>) {
        chomp;
        push @mgmList,$_;
    }
    close IN;
}
foreach my $query_filename (@Queries) {
    foreach my $keymgm (@mgmList) {
        $time=localtime();
        print "$time, start: $args{'-out'}/phase1-results/$query_acc{$query_filename}-vs-$keymgm.blast\n";
        system("~/ncbi-blast-2.2.30+/bin/blastn -word_size 16 -evalue 0.01 -query $query_filename -db ~/ncbi-blast-2.2.30+/db/$keymgm -out $args{'-out'}/phase1-results/$query_acc{$query_filename}-vs-$keymgm.blast");
        $time=localtime();
        print "$time, end: $args{'-out'}/phase1-results/$query_acc{$query_filename}-vs-$keymgm.blast\n";
    }
}
