This may not be a good way to process the sequences, especially if your file is large. However, just to show another way, you can read the whole file into
$_ and pull the protein name/sequence pairs out using a regular expression. Then
split them on newline in a
map, pass into a
sort so as to get the pairs in ascending sequence length and finally
map the pairs out and assign to a hash. Thus, if there are any duplicate proteins the longest sequence will be assigned to the hash last, overwriting any previous shorter sequence.
use strict;
use warnings;
use Data::Dumper;
{
local $/;
$_ = <DATA>;
}
my %sequences =
map { $_->[0], $_->[1] }
sort { length $a->[1] <=> length $b->[1] }
map { [split m{\n}] }
m { > ( [^\n]+ \n [^\n]+ ) }gx;
print Dumper(\%sequences);
__END__
>protein1
ASFGTHTRHTHRHTHTRHTRHTR
>protein2
ERYRYTRYHTRHTGEFEWWFEEFFFFREFRGRE
>protein3
AWEERERGRGRGREGRGREGRRRRRRRRTTHTHTRHRHTRHTR
>protein2
AASEFEFEFE
>protein4
REYTRHTRGRVEVCREVR
Here's the output
$VAR1 = {
'protein4' => 'REYTRHTRGRVEVCREVR',
'protein2' => 'ERYRYTRYHTRHTGEFEWWFEEFFFFREFRGRE',
'protein1' => 'ASFGTHTRHTHRHTHTRHTRHTR',
'protein3' => 'AWEERERGRGRGREGRGREGRRRRRRRRTTHTHTRHRHTRHTR'
};
I hope this is of use.
Cheers,
JohnGG
Update: Used the x modifier and spaced things out a bit to aid readability.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.