Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: compare two files on the basis of Two IDs

by marinersk (Priest)
on Sep 28, 2016 at 03:05 UTC ( [id://1172792]=note: print w/replies, xml ) Need Help??


in reply to Re^2: compare two files on the basis of Two IDs
in thread compare two files on the basis of Two IDs

This code works for me. Is there something wrong with the output?

a.dat:

chr17 69112551 chr1 67058869 chr7 151046672 chr7 151047369 chr1 66953654

b.dat:

chr1 66953622 66953654 chr1 67200451 67200472 chr1 67200475 67200478 chr1 67058869 67058880 chr1 67058881 67058885 chr1 67058887 67058895

Results:

S:\Steve\PerlMonks>compare.pl a.dat b.dat chr17 69112551 M chr1 67058869 M chr7 151046672 M chr7 151047369 M chr1 66953654 M S:\Steve\PerlMonks>

Replies are listed 'Best First'.
Re^4: compare two files on the basis of Two IDs
by genome (Novice) on Sep 28, 2016 at 13:39 UTC
    yes, Its a wrong code. It is giving the result with all M, but not E.. May be it is not considering the condition to print Es. Could you please help me in this. In original file, we have several entries, may give the Es.. Pleas have a look on the code

      Marshall wrote:
      "I couldn't see any way to get an "E" with your test data, so I added some extra data to my test cases. In the future, it is best if you can provide an example "desired output" that demo's the basic decisions which need to be made.
      show an example output and explain clearly how you arrived at that result.
        ok. Please consider again the input files, with both candidates, for E and M as well. File 1
        chr7 151046672 chr7 151047369 chr3 127680920 chr3 127680920
        File 2
        chr1 66953622 66953654 chr1 67200451 67200472 chr1 67200475 67200478 chr1 67058869 67058880 chr1 67058881 67058885 chr7 151046672 127680920 chr7 151047369 127680920 chr3 127680920 151046672 chr3 127680920 151047369
        Code for now.
        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $file1 = $ARGV[0]; open($infile1,$file1); my $file2 = $ARGV[1]; open($infile2,$file2); my %file2_hash; while (my $line = <$infile1>) { chomp $line; #so that output with E or M can be on same line next if $line =~ /^\s*$/; #skip blank lines (a common infile goof +) my ($chr, $val1, $val2) = split /\s+/,$line; } close $infile1; while (my $line = <$infile2>) { chomp $line; next if $line =~ /^\s*$/; #skip blank lines (a common infile goof) my ($key, $value1, $value2) = split /\s+/, $line; # use better "nam +es" I have # no idea of what a chr col $file2_hash{"$key:$value1:$value2"} = 1; # file handle closure is optional, but I'd do it. ### process each line in file2: ### If a line "matches" with any line in file1, then "E", else "M" ### I don't know that these numbers mean, come up with better comment close $infile2; if (exists $file2_hash{"$chr:$val1:$val2"}) { print "$line\tE\n"; # match exists with file 1 } else { print "$line\tM\n"; # match does NOT exist with file 1 } }
        Its not working, Since I want to print the output with respect to my file 1. If you can help with. I Know there is some error in 'If' statement. I could note understand that..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172792]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found