perl script not giving output
0
0
Entering edit mode
6.4 years ago
bioinfo355 • 0

The following program is suppose to read rs ID's of SNPs from the file 'testID' and then search them from file 'testSNP' and sends the output to testSNP.out. I through multiple print statements have debugged the program and found no error but it is not generating the output. what is the problem with this code?

#!/usr/bin/perl 
use warnings;
use strict;

my $rs_id;
open (my $id_file, '<testID') or die "Could not open the file: $!\n";
while ($rs_id = <$id_file>) {
findVariants($rs_id); }

close $id_file;
print "Done\n";

sub findVariants {
my ($rs_num) = @_;
open (my $in_file, '<testSNPs') or die "Could not open the file: $!\n";
my @snp = <$in_file>;
my @result= grep /$rs_num/, @snp; 
print @result;
open (my $out_file, '>>testSNPs.out');

print $out_file @result;

close $out_file;
close $in_file; 
}
perl SNPs • 2.6k views
ADD COMMENT
2
Entering edit mode

Share the contents of testID so that I can run the same at my end and troubleshoot. Will be happy to assist

ADD REPLY
0
Entering edit mode

I have added the 'chomp' statement and now it is working fine.

ADD REPLY
2
Entering edit mode

One thing to look for is that when reading from a file, the variable includes the end of line character(s), so in your case $rs_id contains the whole line including the \n character (assuming Linux), e.g. $rs_id contains 'rs1234\n'. So later when trying to match, you still have this end-of-line character. Try:

while ($rs_id = <$id_file>) {
  chomp($rs_id);
  ....
ADD REPLY
0
Entering edit mode

yes it works fine now. Thank you

ADD REPLY

Login before adding your answer.

Traffic: 1414 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6