目标如题,有多个fasta文件和一个文件名列表,将文件名列表中包含的文件匹配出来并提取第一条序列合并成一个fa文件。
这个采用perl实现,用法和代码如下:
1 #!/usr/bin/perl -w 2 use strict; 3 4 sub usage{ 5 die "usage:perl $0 <fa.list> <Fasta_Dir> <merged.fa>\n",unless(@ARGV==3); 6 } 7 usage(); 8 9 open LIST,"$ARGV[0]"; 10 open F,"$ARGV[1]"; 11 open OU,">$ARGV[2]"; 12 13 my @listName=<LIST>; 14 15 my @files=glob("$ARGV[1]/*.faa"); 16 my $matchNum=0; 17 18 foreach my $k(@files){ 19 my $fileName=(split /\//,$k)[-1]; 20 if(grep /^$fileName$/,@listName){ 21 $matchNum+=1; 22 open EACH,"$ARGV[1]/$fileName"; 23 $/=">"; 24 <EACH>; 25 while(<EACH>){ 26 chomp; 27 my @info=split /\n/,$_; 28 my $seqID=shift @info; 29 my $seq=join "",@info; 30 print OU ">$seqID\n$seq\n"; 31 last; 32 } 33 $/="\n"; 34 } 35 } 36 37 print "match: $matchNum files,and merge completed!\n"; 38 39 close LIST; 40 close F;
原文地址:https://www.cnblogs.com/mmtinfo/p/12113036.html
时间: 2024-10-02 07:32:51