G'day BeckyLynn,
Welcome to the monastery.
I was unable to determine, from either your code or description, whether your input file contained one or many ">Sequence n" lines.
The following code handles any number of such lines.
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
my ($in_file, $out_file) = qw{temp_in.txt temp_out.txt};
open my $in_fh, '<', $in_file;
open my $out_fh, '>', $out_file;
my @protein;
while (<$in_fh>) {
chomp;
if (/^>(.*)$/) {
print_reverse_decoy(\@protein, $out_fh);
print $out_fh ">Reverse Decoy $1\n";
}
else {
push @protein, $_;
}
}
print_reverse_decoy(\@protein, $out_fh);
sub print_reverse_decoy {
my ($protein, $out_fh) = @_;
print $out_fh "$_\n" for map { scalar reverse split '' } reverse @
+$protein;
@$protein = ();
return;
}
A test run with this input:
$ cat temp_in.txt
>Sequence 1
ABCDEFG
HIJKLMN
>Sequence 2
OPQRST
UVWXYZ
>Sequence N
1234567890
!@#$%^&*()
Produces this output:
$ cat temp_out.txt
>Reverse Decoy Sequence 1
NMLKJIH
GFEDCBA
>Reverse Decoy Sequence 2
ZYXWVU
TSRQPO
>Reverse Decoy Sequence N
)(*&^%$#@!
0987654321
Notes:
-
Your shebang line looks wrong. I think you want '#!/usr/bin/perl', not '#! usr/bin/perl'.
-
See perlrun for preference of warnings pragma over -w switch.
-
Consider using the autodie pragma.
It saves having to repeatedly type '... or die "Some custom error message: $!";' code.
It also saves having to maintain the messages or check whether any have been omitted.
-
Use the 3-argument form of open.
There are a number of benefits from doing this, including being able to pass filehandles to a subroutine (as I've done in the code I've shown here).
-
See how I've used chomp.
Note that I've needed no other code to match or remove newlines with regular expressions.
-
See perlre: Modifiers for usage of the 'g' modifier.
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.