iangibson has asked for the wisdom of the Perl Monks concerning the following question:
Esteemed colleagues: I have written a little program to extract relevant lines out of data files and format them nicely, as shown:
#!/usr/bin/perl use warnings; use strict; # Formats a PennCNV output file to remove extraneous information. Also + removes # CNVs containing less than 5 consecutive SNPs. Invoke this program wi +th the name # of the file to be modified. # Open input file and create output file, adding suffix '.truncated' t +o filename: open( my $file, '<', $ARGV[0] ) or die "Cannot open for reading, $!"; open( my $out, '>', $ARGV[0] . '.truncated' ) or die "Cannot open for writing, $!"; # Print headings to output file: printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s \n\n", "Samp +le I.D.", "Chromosome & coordinates", "Copy number", "No. SNPs", "CNV length (bp +)", "First SNP", "Last SNP", "Overlapping Gene(s)"; # Loop that matches each valid line of input file, using capturing par +entheses to # isolate each separate field. We also use '/x' flag to allow arbitrar +y whitespace # so we can break the regular expression up and add comments for reada +bility: while (<$file>) { if ( / (chr\d+:\d+-\d+) # $1 Chromosome & coordinates \s+ (numsnp=\d+) # $2 Number of SNPs \s+ (length=\S+) # $3 CNV length (bp) \s+ (state\d+) # $4 HMM state , (cn=\d+) # $5 Copy number \s+ (\S+) # $6 File directory \/ (\S+) # $7 Sample I.D. \s+ (startsnp=rs\d+) # $8 First SNP in CNV \s+ (endsnp=rs\d+) # $9 Last SNP in CNV \s+ (\S+) # $10 Gene(s) overlapping CNV \s+ (\S+) # $11 Distance of gene(s) from CNV /x and ( !/numsnp=[1-4]\s+/ ) # we also ignore CNVs with less tha +n 5 SNPs ) { # Print each line to output file using left-justified formatting: printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s \n", $7, $1, $5, $2, $3, $8, $9, $10; } } # Close the open filehandles: close($file); close($out);
My problem arises because I want to modify the program to write the rejected lines to a separate file, but when I try to use the capturing parentheses in an 'else' block inside the while loop right after the 'if', I end up with a blank file and perl gives me lots of errors relating to the $1, $2 etc. being out of scope.
So I'd like to know if there's an easy way of diverting any lines that don't match the 'if' patterns into a separate file.
Also, if anyone can spot other potential issues with my code, I'd appreciate constructive criticism. Thanks.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Capturing parentheses out of scope
by ikegami (Patriarch) on Jan 20, 2011 at 21:11 UTC | |
Re: Capturing parentheses out of scope
by toolic (Bishop) on Jan 20, 2011 at 21:03 UTC | |
by iangibson (Scribe) on Jan 20, 2011 at 21:12 UTC | |
Re: Capturing parentheses out of scope
by ELISHEVA (Prior) on Jan 20, 2011 at 21:27 UTC | |
Re: Capturing parentheses out of scope
by Anonyrnous Monk (Hermit) on Jan 20, 2011 at 21:04 UTC | |
by iangibson (Scribe) on Jan 20, 2011 at 21:10 UTC |