I was considering posting another example of a parser, but having researched the question a bit more, I can only second what erix wrote!

Also, my understanding is that there are already existing modules to handle these things, such as BioPerl, which appears to have multiple existing parsers for this format, such as Bio::SeqIO::swiss. This is my first time trying out BioPerl so probably there's an even better way, but this seems to work:

use warnings; use strict; use HTTP::Tiny; use Bio::SeqIO; my $filename = '/tmp/Q94650.txt'; HTTP::Tiny->new->mirror( 'http://www.uniprot.org/uniprot/Q94650.txt', $filename)->{success} or die "Failed to fetch"; my $stream = Bio::SeqIO->new( -file => $filename, -format => 'swiss'); while ( my $seq = $stream->next_seq() ) { # just some examples... print $seq->accession_number, "\n"; print $seq->species->species, "\n"; my ($gene) = $seq->annotation->get_Annotations('gene_name'); print $gene->findval('Name'), "\n"; }

Or using the included bp_seqconvert utility:

$ wget -q http://www.uniprot.org/uniprot/Q94650.txt $ bp_seqconvert.pl --from swiss --to fasta <Q94650.txt >ARF1_PLAFA RecName: Full=ADP-ribosylation factor 1; Short=plARF; MGLYVSRLFNRLFQKKDVRILMVGLDAAGKTTILYKVKLGEVVTTIPTIGFNVETVEFRN ISFTVWDVGGQDKIRPLWRHYYSNTDGLIFVVDSNDRERIDDAREELHRMINEEELKDAI ILVFANKQDLPNAMSAAEVTEKLHLNTIRERNWFIQSTCATRGDGLYEGFDWLTTHLNNA K

In reply to Re: Converting Uniprot File to a Fasta File in Perl by haukex
in thread Converting Uniprot File to a Fasta File in Perl by pearllearner315

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.