Need help comparing Base Pairs within PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help comparing Base Pairs within PERL
# 15  
Old 06-06-2012
when I make the change to '=~' i get the following errors:

Code:
 
Use of uninitialized value $1 in concatenation (.) or string at ddreggors.pl line 26, <FILE2> line 5.
Use of uninitialized value $1 in array element at ddreggors.pl line 26, <FILE2> line 5.
Use of uninitialized value in concatenation (.) or string at ddreggors.pl line 26, <FILE2> line 5.
Use of uninitialized value $1 in concatenation (.) or string at ddreggors.pl line 26, <FILE2> line 6.
Use of uninitialized value $1 in array element at ddreggors.pl line 26, <FILE2> line 6.
Use of uninitialized value in concatenation (.) or string at ddreggors.pl line 26, <FILE2> line 6.


Do you know what could be causing this?
Thanks again
# 16  
Old 06-06-2012
can you send me your code, or at minimum line 26 of your code?

---------- Post updated at 03:21 PM ---------- Previous update was at 03:19 PM ----------

Is that all you have changed?
When you change it back it works?

---------- Post updated at 03:27 PM ---------- Previous update was at 03:21 PM ----------

Never mind I see it....

I had to assign the 2 reference file columns to a variable and use the variable then it it works. Somehow after the match line $1 was getting unset, so the print line was failing on $1.

Code:
#!/usr/bin/perl


use strict;
use warnings;
my ($idx, $tmp, @data);
open(FILE1,"<","file3.txt") or die $!;
while (<FILE1>) {
        chomp;
        next unless $_ =~ /^\d/;
        ($idx, $tmp) = split(/\s+/,$_,2);
        $data[$idx] = $tmp;
}
open(FILE2,"<","file2.txt") or die $!;
while (<FILE2>) {
        chomp;
        next unless $_ =~ /^\d/;
        /^(\d*)\s*([a-z]*)/i;
        my @tmparr = split(/\s+/,$data[$1]);
        my $tmpref = $2;
        my $tmpidx = $1;
        next unless $tmparr[86] =~ /$tmpref/;
        print $tmpidx . "\t" . $tmparr[4] . "\t" . $tmparr[86] . "\n";
}

# 17  
Old 06-06-2012
This is what it currently is:

Code:
#!/usr/bin/perl


use strict;
use warnings;
my ($idx, $tmp, $geno, @ref, @data);
open(FILE1,"<","file1.txt") or die $!;
while (<FILE1>) {
        chomp;
        next unless $_ =~ /^\d/;
        ($idx, $tmp) = split(/\s+/,$_,2);
        $data[$idx] = $tmp;
}
open(FILE2,"<","file2.txt") or die $!;
while (<FILE2>) {
        chomp;
        next unless $_ =~ /^\d/;
        /^(\d*)\s*([a-z]*)/i;
        my @tmparr = split(/\s+/,$data[$1]);
        next unless $tmparr[1] =~ /$2/;
        print $1 . "\t" . $data[$1] . "\n";
}

# 18  
Old 06-07-2012
I found the issue.
See my above comment... I updated it to show what I did.

---------- Post updated at 04:09 PM ---------- Previous update was at 03:31 PM ----------

Actually, now that we have solved the original code... there is a better way to do this.
Here is a way smaller piece code that removes the need for second array (@ref) and I nest the loops.

Code:
#!/usr/bin/perl


use strict;
use warnings;
my (@data, $tmpidx, $tmpref);
open(FILE1,"<","file1.txt") or die $!;
while (<FILE1>) {
        chomp;
        next unless $_ =~ /^\d/;
        @data =  split(/\s+/,$_);
        open(FILE2,"<","file2.txt") or die $!;
        while (<FILE2>) {
                /^(\d*)\s*([a-z]*)/i;
                $tmpidx = $1;
                $tmpref = $2;
                next unless $_ =~ /^\d/;
                next unless $data[0] == $tmpidx;
                next unless $data[87] =~ /$tmpref/i;
                print $data[0] . "\t" . $data[5] . "\t" . $data[87] . "\n";
        }
        close FILE2;
}
close FILE1;


Result:
Code:
[user@host ~]$ ./test3.pl    
4       Exonic  AA
5       Intronic        GC

---------- Post updated 06-07-12 at 09:24 AM ---------- Previous update was 06-06-12 at 04:09 PM ----------

OK, after a bit more tweaking, here is the smallest and cleanest I could get the code.


Code:
#!/usr/bin/perl

use strict;
use warnings;
my (@ref, @data, $refidx, $ref, $row, @dataline, $refline);
open(FILE1,"<","file1.txt") or die $!; @data = <FILE1>; close(FILE1);
open(FILE2,"<","file2.txt") or die $!; @ref = <FILE2>; close(FILE2);
foreach $row (@data) {
        next unless $row =~ /^\d/;
        foreach $refline (@ref) {
                next unless $refline =~ /^\d/;
                ($refidx, $ref) = split('\s*', $refline);
                @dataline = split('\s',$row);
                next unless $dataline[0] == $refidx;
                next unless $dataline[87] =~ /$ref/i;
                print $dataline[0] . "\t" . $dataline[5] . "\t" . $dataline[87] . "\n";
        }
}

It weighs in at 18 lines, the files are read into arrays and closed immediately.
This User Gave Thanks to ddreggors For This Post:
# 19  
Old 06-07-2012
Thank you so much that is working great for me and I really appreciate the help.
Hopefully one my last questions but I now have a similar situation but the data and reference are both in the same file. For example:


Code:
Column 1     ....        Column 6     .....   Column 88      Column 89
(Index)                   (mutation)            (Genotype)      Reference
1                             Intronic                 TT                 A
2                             Frameshift             GT                 C
3                             Exonic                   AT                 C
4                             Exonic                   AA                 A
5                             Intronic                 GC                 G

So now rather than comparing the data to column 2 in another file, I would like to compare it to column 89 of the same file.
Is there a quick fix for this?
Thanks again!
# 20  
Old 06-07-2012
That is actually easier and least memory/disk intensive.

Here is a quick modification (untested) that should work...

Code:
#!/usr/bin/perl

use strict;
use warnings;
my (@data, $row, @dataline);
open(FILE1,"<","file1.txt") or die $!; @data = <FILE1>; close(FILE1);
foreach $row (@data) {
        next unless $row =~ /^\d/;
        @dataline = split('\s',$row);
        next unless $dataline[87] =~ /$dataline[88]/i;
        print $dataline[0] . "\t" . $dataline[5] . "\t" . $dataline[87] . "\n";
}

If you have any issues let me know and I will change as needed.
# 21  
Old 06-07-2012
That program appears to be running but no output is given when it is written as above. Do you know what could have happened?
thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl for comparing numbers from previous lines in a file?

Hi everyone I have a question for you, as I am trying to learn more about Perl and work with some weather data. I have an ascii file (shown below) that has 10 lines with different columns. What I would like is have Perl find an "anomalous" value by comparing a field with the values from the last... (2 Replies)
Discussion started by: lucshi09
2 Replies

2. Shell Programming and Scripting

Need help in comparing two files using shell or Perl

I have these two file that I am trying to compare using shell arrays. I need to find out the changed or the missing enteries from File2. For example. The line "f nsd1" in file2 is different from file1 and the line "g nsd6" is missing from file2. I dont want to use "for loop" because my files... (2 Replies)
Discussion started by: sags007_99
2 Replies

3. Shell Programming and Scripting

Perl: Need help comparing huge files

What do i need to do have the below perl program load 205 million record files into the hash. It currently works on smaller files, but not working on huge files. Any idea what i need to do to modify to make it work with huge files: #!/usr/bin/perl $ot1=$ARGV; $ot2=$ARGV; open(mfileot1,... (12 Replies)
Discussion started by: mrn6430
12 Replies

4. Shell Programming and Scripting

Perl: Comparing to two files and displaying the differences

Hi, I'm new to perl and i have to write a perl script that will compare to log/txt files and display the differences. Unfortunately I'm not allowed to use any complied binaries or applications like diff or comm. So far i've across a code like this: use strict; use warnings; my $list1;... (2 Replies)
Discussion started by: dont_be_hasty
2 Replies

5. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

6. Shell Programming and Scripting

comparing list values in Perl

Hi, I have tab separated list: KB0005 1019 T IFVATVPVI 0.691 PKC YES KB0005 1036 T YFLQTSQQL 0.785 PKC YES KB0005 1037 S FLQTSQQLK 0.585 DNAPK YES KB0005 508 S ENIISGVSY 0.507 cdc2 YES KB0005 511 S ... (1 Reply)
Discussion started by: karla
1 Replies

7. Shell Programming and Scripting

PERL name value pairs substituions

I have a main file with variable tokens like this: name: File1 =========== Destination/Company=@deploy.company@ Destination/Environment=@deploy.env@ Destination/Location=@deploy.location@ Destination/Domain=@deploy.location@ MIG_GatewayAddresses=@deploy.gwaddress@ MIG_URL=@deploy.mig_url@... (1 Reply)
Discussion started by: uandme2k2
1 Replies

8. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

9. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

10. Shell Programming and Scripting

perl search and replace pairs

Hello all im facing some kind of problem i have this string : functionA() $" "$ functionB("arg1") $" = "$ i will like to replace all the pairs of opening and closing "$" to be something like that functionA() <#" "#> functionB("arg1") <#" = "#> i cant of course do is with simple ... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question