#!/usr/bin/perl use strict; use warnings; use Getopt::Long; #set variables my ($optHelp, $optFile1, $optFile2, $optList); #get program options GetOptions ('h' => \$optHelp, 'a=s' => \$optFile1, 'b=s' => \$optFile2, 'l=s' => \$optList); # there are better ways to do this but I like this strategy :) if($optHelp || !$optFile1 || !$optFile2 || !$optList ){ print "Usege:\n\n"; print "./program -a file1 -b file2 -l list<.tsv> \n\n"; print "Note: sequences from file2 are replaced with those in file1 according to the list -l\n\n"; exit(0); } #allocate memory (well, more of set variables) my %hash1 =(); my %list = (); #open files open (ONE, "<", $optFile1) or die "$!"; open (TWO, "<", $optFile2) or die "$!"; open (LIST, "<", $optList) or die "$!"; #load list which should look like: #file1_id file2_id while(){ chomp; /^(.*?)\t(.*)/; $list{$1} = $2; #e } close LIST; #read file one into memory my $head; while(){ chomp; if(/>(.*)/){$head = $1; next;} #e $hash1{$head} .= $_; } close ONE; #replace fasta record from file to with the one in file one if id is set in list file my $x = 0; while(){ chomp; ## You can play with this if one line fasfa sequence is not what you can use if(/>(.*)/){if(defined $list{$1}){print $_. "\n" . $hash1{$list{$1}} . "\n"; $x = 1}else{$x = 0}} #e print $_ . "\n" if $x == 0; #e } close TWO;