G'day Sofie,

I normally handle fasta files by making the record terminator the '>'; note that the first record is a zero-length string which you'll typically want to discard. In your particular case here, you can split that record on a newline: the sequence will be the second element. Look at the code below and then some explanatory notes at the end.

#!/usr/bin/env perl use strict; use warnings; use constant MIN_LENGTH => 10; my $long_seqs = 0; { local $/ = '>'; while (<DATA>) { next if $. == 1; my $seq = (split /\n/)[1]; next if MIN_LENGTH > length $seq; ++$long_seqs; print $seq, "\n"; } } if (not $long_seqs) { print 'No sequence is over ', MIN_LENGTH-1, "\n"; } __DATA__ >NM_001 Homo sapiens ADA2 (CECR1) GATCCAA >NM_002 Homo sapiens IKBKG GGAGGTCTTTAGCTTTAGGGAAACCC > sequence length = 9 -- should be discarded ABCDEFGHI > sequence length = 10 -- should be kept ABCDEFGHIJ > sequence length = 11 -- should be kept ABCDEFGHIJK

Sample run:

$ ./pm_11113575_fasta_extract.pl GGAGGTCTTTAGCTTTAGGGAAACCC ABCDEFGHIJ ABCDEFGHIJK

Changing the MIN_LENGTH constant to 100:

$ ./pm_11113575_fasta_extract.pl No sequence is over 99

Notes:

— Ken


In reply to Re: Size of sequences in fastafile by kcott
in thread Size of sequences in fastafile by Sofie

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.