#!/usr/bin/perl

use strict;use warnings;
use List::MoreUtils qw(any); #import of any-function

use feature qw(say);
use Data::Dumper;


system(`for i in \`seq 0 63\`; do t=\`echo "obase=2;\$i" | bc\` t=\`printf %06d \$t\` > \$t.pattern ; done`);
#111111.pattern not existing....... WHY??? (script working)
system(`touch 111111.pattern`);
system(`for i in *.pattern; do echo "#mir\tpxy\tbmo\tmse\tmci\thme\tdpl3\tpos.others\tall.others\tpattern\tother_names" >> \$i; done`);

open (FILE, 'mir_orthodata');
my $j=0;
my $temp;
my $temp2;
my $temp3;
my @A;
my @B;
my $sub;

while (<FILE>) {
    chomp;
# Species	Genes	Alg.-Conn.	tca	bmo	dme	ame	pxy	api	mci	mse	(lmi)	dpo	hme	rpr	nvi	aga	dpl3	(dpl)
(my $spec,my $genes,my $conn,$B[0],$A[$j][1],$B[1],$B[2],$A[$j][0], $B[3],$A[$j][3],$A[$j][2],my $lmi,$B[4],$A[$j][4],$B[5], $B[6], $B[7],$A[$j][5],my $dpl1) = split("\t");
#Erzeugen der Pattern, Speichern in $A[][6], mir-string in $A[][7], pos.others in $A[][8], all.others in $A[][9], mir-xxx in $A[][10], output-str in $A[][11]
$A[$j][6]="";
$A[$j][7]="";
$A[$j][8]=0;
$A[$j][9]=8;
$A[$j][10]="";
$A[$j][11]="";
#aka
$A[$j][12]="";
#trash
#$A[$j][13]="";
my $fam = "";
my@AKA;
#Durchlauf der Lepidoptera
for (my $n=0;$n<=5;$n++) {
	#Anfuegen an mir-string
	$A[$j][7] .= $A[$j][$n]."\t";
	#Pattern auslesen
	if ($A[$j][$n] eq "*") {$A[$j][6].="0";}
	else {$A[$j][6].="1";
#Eintraege nach Komma separieren, in Arr(@mir) stecken, den nach anderen mir-Unterarten durchsuchen, Ausgabe in aka-Spalte ([12])
		my @mir = split(',',$A[$j][$n]);
		#falls mir-xxx_1 -> entfernen des underscore
		if ($A[$j][10] eq "") {
			my@T;
			if (index($mir[0],"dpl3") != -1) {@T = split('_',substr $mir[0],5)}
			else {@T = split('_',substr $mir[0],4)}
			$A[$j][10] = $T[0];
		}
		
		foreach my $z (@mir) {
			my@T;
			if (index($z,"dpl3") != -1) {@T = split('_',substr $z,5)}
			else {@T = split('_',(substr $z,4))}
			if ($A[$j][10] ne $T[0] && index($A[$j][12],$T[0]) == -1) {
				$A[$j][12].=$T[0]."\t"}
			#else {$A[$j][13] .= $T[0]}
		}
	}
}

#Durchlauf der sonstigen species
for (my $m=0;$m<=7;$m++) {
	if ($B[$m] ne "*") {
		$A[$j][8]++;	#Zaehlen
		
#Eintraege nach Komma separieren, in Arr(@mir) stecken, den nach anderen mir-Unterarten durchsuchen, Ausgabe in aka-Spalte ([12])
		my @mir = split(',',$B[$m]);
		if ($A[$j][10] eq "") {
			my@T;
			#if (index($mir[0],"dpl3") != -1) {@T = split('_',substr $mir[0],5)}
			#else {
				@T = split('_',substr $mir[0],4);#}
			$A[$j][10] = $T[0];
		}
		foreach my $z (@mir) {
			$sub = substr $z,4;
			if ($A[$j][10] ne $sub && index($A[$j][12],$sub) == -1) {$A[$j][12].=$sub."\t"}
			#else {$A[$j][13] .= $sub}
		}
	}
}

if ($A[$j][10] eq "") {$A[$j][10] = "N/A"}
#Erzeugen des Ausgabestring
$A[$j][11].=$A[$j][10]."\t".$A[$j][7]."".$A[$j][8]."\t".$A[$j][9]."\t".$A[$j][6]."\t".$A[$j][12]."\n";
#printf"%d\t%s\n",$A[$j][8],$A[$j][7];

#Ausgabestring >> .pattern
my $filename = $A[$j][6]; 
open (OUTFILE, ">>$filename.pattern");
print OUTFILE $A[$j][11];
close (OUTFILE);

$j++;
}

#Delete empty *.pattern
system(`find -name "*.pattern" -size -75c -delete`);
#each file: generate entry for other_names if not already in list

my@FILES = glob("*.pattern");
foreach my $file (@FILES) {
    my @Data;my @Index;
#    system 'grep', '-v', "'#'", $file , '|', 'awk', '{printf"%s\n",$1}', ';', 'done', '|', 'sort > index.temp';
    open (PATTERN, "<", $file);
#discard first line (header)
    <PATTERN>; 
    my @all = <PATTERN>;

foreach my$line (@all) {
    chomp $line;
    my @Fields = split(/\t/, $line);
    push (@Index, $Fields[0]);
    
    #print @Index;
    }


#	system(`grep -v '#' $file | awk '{if (NF>10) {for (i=11;i<=NF;i++) {printf"\%s\t\%s\n", \$i, \$0 >> "searches.temp" }}}'`);
#	system(`awk '{printf"\%s\t\%s\t\%s\t\%s\t\%s\t\%s\t\%s\t\%s\t\%s\t\%s\t",\$1,\$3,\$4,\$5,\$6,\$7,\$8,\$9,\$10,\$11 >> "$file"}'`);
foreach my$line (@all) {
    chomp $line;
    my @Fields = split(/\t/, $line);

    if (scalar(@Fields) > 10) {
    for(my$z=10;$z<scalar(@Fields);$z++) {
	if (any { $Fields[$z] eq $_ } @Index) {}
	else { open (OUTFILE, ">>$file");
	       my$outstring = $Fields[$z]."\t";
	       for (my$k=1;$k<=9;$k++) {
		   $outstring .= $Fields[$k]."\t";
	       }
	       $outstring .= "\n";
	       print OUTFILE $outstring;
	       close (OUTFILE);
	}
	
    }
}
    close (PATTERN);}

}

#create index.pattern
#system(q(for i in *.pattern; do grep -v '#' \$i | awk '{print"\$1\t\$10\t\$11\n"}' ; done | sort > index.pattern));
system(`touch index`);
open (INDEX, ">", "index");
foreach my $file (@FILES) {
     open (PATTERN, "<", $file);
#discard first line (header)
    <PATTERN>; 
    my @all = <PATTERN>;

foreach my$line (@all) {
    chomp $line;
    my @Fields = split(/\t/, $line);
    my$out = $Fields[0]."\t".$Fields[9]."\n";
    print INDEX $out;
}
}
close (INDEX);
system(`sort index > index.pattern`);
system(`rm -r index`);


