#!/usr/bin/perl -w

`rm /u/home/praktikum/PRAK15/Proteinortho/ortho_fasta.fa`;

open(DATEI, "< /u/home/praktikum/PRAK15/Proteinortho/no_dpl_orthos.txt");
open(DATEI3, ">> /u/home/praktikum/PRAK15/Proteinortho/ortho_fasta.fa");

my $gene="undefined";
my $org="undefined";
my $found=0;

while(<DATEI>)
{

	my @columns = split(" ", $_);
	
	for($i=0; $i<scalar(@columns); $i++)
	{
		if($columns[$i] =~ /(\S*\_)(\S*)#/)
		{
			$org=$1;
			$gene=$2;
			
			if($org =~ /mci_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Melitaea_cinxia.MelCinx1.0.24_edited.pep.all.fa");
			}
			elsif($org =~ /api_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Acyrthosiphon_pisum.GCA_000142985.2.24_edited.pep.all.fa");
			}
			elsif($org =~ /rpr_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Rhodnius_prolixus.RproC1.24_edited.pep.all.fa");
			}
			elsif($org =~ /dpo_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Dendroctonus_ponderosae.GCA_000355655.1.24_edited.pep.all.fa");
			}
			elsif($org =~ /bmo_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Bombyx_mori.GCA_000151625.1.24_edited.pep.all.fa");
			}
			elsif($org =~ /hme_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Heliconius_melpomene.Hmel1.24_edited.pep.all.fa");
			}
			elsif($org =~ /dpl1_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Danaus_plexippus.DanPle_1.0.24_edited.pep.all.fa");
			}
			elsif($org =~ /dpl3_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Danaus_Plexippus_OGS2_pep_edited.fa");
			}
			elsif($org =~ /ame_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Apis_mellifera.GCA_000002195.1.24_edited.pep.all.fa");
			}
			elsif($org =~ /tca_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Tribolium_castaneum.Tcas3.24_edited.pep.all.fa");
			}
			elsif($org =~ /pxy_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/P.xylostella.gene_edited.fa");
			}
			elsif($org =~ /nvi_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Nasonia_vitripennis.GCA_000002325.2.24_edited.pep.all.fa");
			}
			elsif($org =~ /aga_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Anopheles_gambiae.AgamP4.24_edited.pep.all.fa");
			}
			elsif($org =~ /mse_/)
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Manduca_Sexta_OGS2_20140407_proteins_final_edited-66.fa");
			}
			else
			{
				open(DATEI2, "< /u/home/praktikum/PRAK15/Proteinortho/FASTA/Drosophila_melanogaster.BDGP5.24_edited.pep.all.fa");
			}

				#print DATEI3 $gene."\n";
				$found=0;
				while(<DATEI2>)
				{
					if($_ =~ /$gene/)
					{
						print DATEI3 $_;
						$found=1;
					}
					elsif($found==1 && $_ !~ />/)
					{
						print DATEI3 $_;
					}
					elsif($found==1)
					{
						last;
					}
				}
				#seek(DATEI2,0,0);
				close(DATEI2);
		}
	}

	
}



close(DATEI);
#close(DATEI2);
close(DATEI3);

