Since reading and writing is probably the slowest part of your program, it makes sense to read the file only once. And because the file might be big, you shouldn't read it all at once into memory, but rather line by line.

Here's something to get you started. Since you didn't include any example input and expected output data, I can't really help you with the processing step, but maybe it helps anyway:

#! C:/Perl/bin use strict; use warnings; use File::Path; # This script processes a fasta file containing DNA sequences # Part 1: declare variables, constants, ... # forward (F) barcodes my @forward = ("AGCCTAAGCT", "TCAAGTTAGC", "AGCCTGGCAT", "ACGGTCCATG", "ACTTGCCGAT", "ACGGTGGATC", "ATCCGCCTAG", "ATGGCGGTAC"); # reverse (R) barcodes my @reverse = ("AGCTTAGGCT", "TAGCCTAAGC", "AGCTTGCCAT", "ACGTTCAATG", "ACTGGCGGAT", "ACGTTGAATC", "ATCGGCAAGT", "ATGCCGTTAC"); # primers used for Variable Region 1 (V1) and Variable Region 3 (V3) o +f 16S rRNA # forward primer (V1 region) my $V1 = 'AGAGTTTGATCCTGGCTCAG'; # reverse primer (V3 region) my $V3 = 'GTATTACCGCGGCTGCTGGCA'; # locate the import-file with data my $input_file = "C:/../input.txt"; # concatenate primer to bar codes: my @processed = map { $_ . $V1 } @forwards; # construct a regex to search for my $search_for = join '|', @processed; # compile it: $search_for = qr{$search_for}; open my $FASTA_IN, '<', $input_file or die $!; open my $MATCHED_OUT, '>', 'matched.txt' or die $! open my $NOT_MATCHED_OUT, '>', 'notmatched.txt' or die $' # don't read all the data at once, # rather process it line by line while (my $line = <$FASTA_IN>) { if ($line =~ $search_for) { print $MATCHED_OUT $line; } else { print $NOT_MATCHED_OUT $line; } } close $FASTA_IN; close $MATCHED_OUT; close $NOT_MATCHED_OUT;

In reply to Re: remove part of string (DNA) by moritz
in thread remove part of string (DNA) by Furor

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.