Do multiple capture groups in an array, but only respond to one group

I am trying to do a simple modification of some existing code without much luck, I want to migrate one additional capture group from FILE1: $ 2 before comparing the $ 1 as usual with the data in FILE2 and print both if a successful match occurs, Please keep an answer similar to my attempt if possible so I can understand the changes.

FILE1 data:

abc 99269 +t
abc 550 -a
abc 100 +a
gdh 126477 +t 
hduf 1700 +c

      

FILE2 data:

517 1878 forward
2156 3289 forward
99000 100000 forward
22000 23000 backward
999555 999999 backward 

      

Desired output:

99269 +t 99000 100000 forward
550 -a 517 1878 forward
1700 +c 517 1878 forward 

      

Code:

#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

my $outputfile = "/Users/edwardtickle/Documents/CC22CDSpositive.txt"; 

open FILE1, "/Users/edwardtickle/Documents/CC22indelscc.txt";

open FILE2, "/Users/edwardtickle/Documents/CDS_rmmge.CC22.CORE.aln";

open (OUTPUTFILE, ">$outputfile");
my @file1list=();
my @indels=();

while (<FILE1>) {
    if (/^\S+\s+(\d+)\s+(\S+)/) {
        push @file1list, $1;
        push @indels, $2;
    }
}

close FILE1;

while ( my $line = <FILE2> ) {
    if ($line =~ /^>\S+\s+\S+\s+(\d+)\s+(\d+)\s+(\S+)/) {
        my $cds1 = $1;
        my $cds2 = $2;
        my $cds3 = $3;

        for my $cc22 (@file1list) {
            for my $indel (@indels) {
                if ( $cc22 > $cds1 && $cc22 < $cds2 ) {
                    print OUTPUTFILE "$cc22 $indel $cds1 $cds2 $cds3\n";
                }
            }
        }
    }
}

close FILE2;
close OUTPUTFILE;

      

Thanks in advance!

+3


source to share


1 answer


It's frustrating that you don't seem to learn from many of the solutions and tips that are given to you.

Here's a program that will do as you ask.



use strict;
use warnings;
use 5.010;
use autodie;

chdir '/Users/edwardtickle/Documents';

open my $fh, '<', 'CDS_rmmge.CC22.CORE.aln';

my @file2;
while (<$fh>) {
  next unless /\S/;
  push @file2, [ split ];
}

open my $out, '>', 'CC22CDSpositive.txt';

open $fh, '<', 'CC22indelscc.txt';

while (<$fh>) {

  my @line1 = split;

  for my $line2 (@file2) {

    if ( $line1[1] >= $line2->[0] and $line1[1] <= $line2->[1] ) {
      my @out = ( @line1[1,2], @$line2 );
      print $out "@out\n";
      last;
    }
  }
}

      

+1


source







All Articles