Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: how to merge the files of DNA sequences?

by graff (Chancellor)
on Jul 24, 2004 at 19:50 UTC ( #377159=note: print w/replies, xml ) Need Help??

in reply to how to merge the files of DNA sequences?

Are you confident that the two files have the same number of lines, the same number and set of individual names, and the same number of sequences per individual? And, are the individuals presented in the same order? That is, are the two files already aligned?

If there is a lack of alignment between the two, then some more specification is needed about what to do to reconcile differences between the two files.

But if the files are fully aligned, you could use a trivial perl script to join the two files line-by-line (or use the time-honored "paste" utility that has been in /usr/bin/ on unix systems since forever):

#!/usr/bin/perl open( F1, "file1" ) or die "file1: $!"; open( F2, "file2" ) or die "file2: $!"; while ( $s1 = <F1> ) { $s2 = <F2>; if ( $s1 =~ /^>/ ) { print $s1; } else { chomp $s1; print "$s1$s2"; # (update: added "$" on "s1") } }
or, using the "join" command and an even shorter perl script (which just removes duplicated individual names):
paste -d '' file1 file2 | perl -pe 's/(?<=\w)>.*//' > files.joined
(updates: mistakenly started with "join" command instead of "paste"; added the "-d ''" option on the paste command, to concatentate input strings with no separator. Regarding the (?<=\w)>.*, see the perlre man page about "zero-width look-behind assertion" -- this RE is checking for an angle bracket preceded by an alphanumeric, and replacing that angle bracket, and everything after it on the line, with an empty string. If a name ends in something other than a letter or digit, this will fail to remove the duplication.)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://377159]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2023-02-01 16:22 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (11 votes). Check out past polls.