Rob Rob - 5 months ago 27
Perl Question

Nested if statements: Swapping headers and sequences in fasta files

I am opening a directory and processing each file. A sample file looks like this when opened:


For the above sample file, I am trying to make them look like this:


I need to find the "header" in next line sequence, take flanks on either side of the match, and then flip them. I want to print each file's worth of contents to another separate file.
Here is my code so far. It runs without errors, but doesn't generate any output. My guess is this is probably related to the nested if statements. I have never worked with those before.

use strict;
use warnings;

my ($directory) = @ARGV;
my $dir = "$directory";
my @ArrayofFiles = glob "$dir/*";
my $count = 0;

open(OUT, ">", "/path/to/output_$count.txt") or die $!;

foreach my $file(@ArrayofFiles){
open(my $fastas, $file) or die $!;
while (my $line = <$fastas>){
if ($line =~ m/(^>)([a-z]{5})/i){
my $header = $2;
if ($line !~ /^>/){
my $sequence .= $line;
if ($sequence =~ m/(([a-z]{1})($header)([a-z]{1}))/i){
my $matchplusflanks = $1;
print OUT ">", $matchplusflanks, "\n", $header, "\n";

How can I fix this code? Thanks.


Try this

foreach my $file(@ArrayofFiles)
    open my $fh," <", $file or die"error opening $!\n";
    while(my $head=<$fh>)
        chomp $head;
        my $next_line = <$fh>;
        my($extract) = $next_line =~m/(.$head.)/;
        print ">$extract\n$head\n";