#!/usr/bin/perl -w
#perl new-gen-megan.pl ../live_wo_di1-di2-tri-arch_dUTPase_E001.csv > live_wo_di1-di2-tri-arch_dUTPase_E001.megan

my %have_genome;
my %have_dUTPase;
my %have_UNG;
my $sum=0;
my $sum1=0;
my $sum2=0;
my $sum3=0;

open IN,"/db2/data/kcsabadata/dUTPase/NC-GI-TAXID-wo-plasmid.csv";
while (<IN>) {
    if ($_ =~ /^(.+)\.\d+;.+;(\d+)/) {
        $taxid_to_NC{$1}=$2;
        $type_taxid{$2}=1;
        $sum++;
        #print"$1;$taxid_to_NC{$1}\n";
    }
}
close IN;

open IN,$ARGV[0];
while (<IN>) {
    if ($_ =~ /^(\w+);.+(N._\d+)/) {
        if (exists $type_taxid{$taxid_to_NC{$2}}) {
            if ($1 eq "no") {
                $type_taxid{$taxid_to_NC{$2}}=2;    #have not dUTPase and have not UNG
                $sum2++;
                #print "$taxid_to_NC{$2};$type_taxid{$taxid_to_NC{$2}}\n";
            }else {
                $type_taxid{$taxid_to_NC{$2}}=3;    #have not dUTPase and have UNG
                $sum3++;
                #print "$taxid_to_NC{$2};$type_taxid{$taxid_to_NC{$2}}\n";
            }
        }  
    }
}
close IN;

printf(qq~\@Creator\t
\@CreationDate\t
\@ContentType\tSummary4
\@Names\tdUTPase=yes\tdUTPase=no and UNG=no\tdUTPase=no and UNG=yes
\@Uids\t0\t0\t0
\@Sizes\t~);

printf("%d\t%d\t%d\n%s\t%d\n",$sum-$sum2-$sum3,$sum2,$sum3,'@TotalReads',$sum);

printf(qq~\@AdditionalReads\t0

\@Parameters mode=ABSOLUTE
\@NodeStyle\tTaxonomy\tPieChart
~);
foreach $taxid (keys %type_taxid) {
    if ($type_taxid{$taxid}==1) {
        print"TAX\t$taxid\t1\t0\t0\n";
    }elsif ($type_taxid{$taxid}==2) {
        print"TAX\t$taxid\t0\t1\t0\n";
    }elsif ($type_taxid{$taxid}==3) {
        print"TAX\t$taxid\t0\t0\t1\n";
    }
}
printf(qq~#EOF~);
