Hello onlyIDleft,

There are a number of problems with your code. For example, lines such as

%head_seqs = @headers_seqs;

make no sense. You need to read up on perl data structures to understand arrays and hashes.

I think the following script does what you need:

use strict; use warnings; open(my $in1, '<', $ARGV[0]) or die "Can't read from multifasta file '$ARGV[0]': $!"; my @multifasta = <$in1>; close $in1; open(my $in2, '<', $ARGV[1]) or die "Can't read from header replacement file '$ARGV[1]': $!"; my %headers; while (<$in2>) { my ($orig, $new) = split; warn "Duplicate replacement: $orig --> $new\n" if exists $headers{ +$orig}; $headers{$orig} = $new; } close $in2; my $destination = $ARGV[0] . '_headers-replaced.fasta'; open (my $out, '>', $destination) or die "Can't write to file '$destination': $!"; foreach my $line (@multifasta) { if ($line =~ /^\>/) # it is a header { foreach my $key (keys %headers) { if ($line =~ /$key/) { $line =~ s/$key/$headers{$key}/; last; } } } print $out $line; } close $out;

HTH,

Athanasius <°(((><contra mundum


In reply to Re: replace FASTA sequence headers by Athanasius
in thread replace FASTA sequence headers by onlyIDleft

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.