Ed Tickle
Ed Tickle

Reputation: 87

Carry across multiple capture groups in array but match only one group

I am trying to make a simple modification to some existing code without much luck, i want to carry across one more capture group from FILE1: $2, before comparing $1 as usual with data in FILE2 and printing both if a successful match occurs. Please keep the answer similar to my attempt if possible, so i am able to 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!

Upvotes: 3

Views: 86

Answers (1)

Borodin
Borodin

Reputation: 126732

It's frustrating that you don't seem to be learning from the many solutions and pieces of advice you've been given.

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;
    }
  }
}

Upvotes: 1

Related Questions