joluito has asked for the wisdom of the Perl Monks concerning the following question:
Hello everybody,
I'm trying to extract a set of diferent ID lists from a FASTA file in a batch.
I have a code that allows me to do it but it only extracts the first list of the loop (even though it reads the other lists and tell how many od each list's ID are found in the FASTA file).
There must be an issue with the OUTFILE opening/closing in the for loop, but I seem unable to find what the problem is.
Any help would be appreciated.
Here is the code (and I can provide dummy test files if needed):
#!usr/bin/perl -w use strict; use diagnostics; use Getopt::Long; #usage example: perl /path_to_script/listret.pl -p /path_to_lists_dir +-f /path_to_fasta/FastaFile.faa my ($path,$fasta); GetOptions( 'path=s' => \$path, 'fasta=s' => \$fasta, ); #print "$path\n"; chdir $path or die "ERROR: Unable to enter $path: $!\n"; opendir (TEMP , "."); my @files = readdir (TEMP); closedir TEMP; my $name; my $found=0; my $totalist=0; for my $file (@files) { if($file=~/(\w+)\.fasta/){ $name = "$1"; open (INFILE2, "$path/$name.txt") || die ("cannot open input file"); chomp(my @lista = <INFILE2>); $/ = "\>"; open (INFILE, "$path/$name.fasta") || die ("cannot open input file"); chomp(my @data = <INFILE>); open OUT,'>'."$path/$name.out" or die "ERROR: Unable to open $file $! +\n"; foreach my $li (@lista){ chomp $li; print"$li\n"; $totalist++; for(@data){ if(/$li/){ $found++; print ">"."$_\n"; print OUT ">"."$_\n"; } } } print "For $name we found $found of a total $totalist\n"; } }
Example of FASTA format:
>VFG000033(gb|WP_002208793) MPPAARLSLLQRSSTMSDFLPFALPDIGEAEIQAVTESMRSGWLTTGPNAREFEREFAAY IGADVEAVAVNSATAGLHLALEAIGVGPGDEVITTTHTFTASAEVARYLGAEPVLVDIDP ATLCISPAAIERAITPRTRAIVPVHYGGLSCDMDSILEIARKHGLKVIEDAAHALPASWQ GRRIGSLESDLTVYSFYATKTLATGEGGMVVTRDPALAKRCRVMRLHGIDRDAFDRFTSK KPAWYYEIVAPGFKYNMTDTAAAMGRVQLQRVQQMRDRRAQIAAAYDQAFADLPLTLPPG PGRTPGVERVAHRDDDEHSWHLYAIRIHPQAPLKCDDFIVRMTENGIGCSVHYVPLHLQP YWRDRYGLTPDMYPHSQAAFEGMASLPIYSRMTDADVQRVIASVRQLLRP >VFG000036(gb|NP_490509) MQFIDLKTQYQALRDTINPRIQAVLDHGQFIMGPEVKELEAALCAYTGAKHCITVASGTE ALLISLMALGVKAGDEVITTSFTFVATAEVIALLGAKPVFVDVEPDTCNIKVSEIEAKIT PRTKAIIPVSLYGQCGDMDEV
Example of ID list:
WP_002208793 WP_002208792 WP_002211763 WP_002211762 NP_490508 NP_490509 NP_459538 NP_459540
I expect to get a new FASTA file (for each of the ID lists) containing only the sequences matching to the ID on the specific list. The code expects to, basically, be a kind of Sequences retriever in batch. I mean, I expect the script to:
-open the original FASTA file
-open each of the ID lists
-read and chomp it
-match & retrieve the correspondent sequences from the original FASTA
-write them to individual files (named like the lists but in FASTA format)
The output should ne in FASTA format (the same format in the above example).
Leaving aside the format specifications, I think the issue here is why it only works in the first "for" loop iteration and not in the following ones. Can you spot any error I can't, regarding the outfile opening or how I try to write my results in it inside the loop?
Thank you very much in advance.
|
|---|