
Kate
New User
Jan 26, 2014, 9:21 PM
Post #1 of 2
(2059 views)
|
Join two tables via harsh tables (test script and input files are attached)
|
Can't Post
|
|
Dear all, I found a script of joining two simple table files. The script works well, yet I can't understand few details. Any feedback or brief explanation will be appreciated. Thanks!! Kate ### input_dataset1.txt ### contig11 GO:100 other columns of data contig11 GO:289 other columns of data contig11 GO:113 other columns of data contig22 GO:388 other columns of data contig22 GO:101 other columns of data ### input_dataset2.txt ### contig11 3 N contig11 1 Y contig33 1 Y contig22 1 Y contig22 2 N ### output ### contig11 3 N GO:100 other columns of data contig11 3 N GO:289 other columns of data contig11 3 N GO:113 other columns of data contig11 1 Y GO:100 other columns of data contig11 1 Y GO:289 other columns of data contig11 1 Y GO:113 other columns of data contig22 1 Y GO:388 other columns of data contig22 1 Y GO:101 other columns of data contig22 2 N GO:388 other columns of data contig22 2 N GO:101 other columns of data ### script.pl ###
open(my $GOTERMS, $ARGV[0]) or die("Error opening GO terms file \"$ARGV[0]\": $!\n"); open(my $SNPS, $ARGV[1]) or die("Error opening SNP file \"$ARGV[1]\": $!\n"); my %goterm; while (<$GOTERMS>) { my ($id, $rest) = /^(\S++)(.*)/s; # -----> Question 1 push @{ $goterm{$id} }, $rest; # ------> Question 2 } while (my $row2 = <$SNPS>) { chomp($row2); my ($id) = $row2 =~ /^(\S+)/; # ------> Question 3 foreach my $rest (@{ $goterm{$id} }) { print("$row2$rest"); } } ### My questions ### Question 1. It saves the first field (key) to $id and other fields to $rest. a: why = not =~ b: why ^(\S++) not ^(\S+) ? c: why /s ? Question 2. I just declared the hash in the first line, so it should be an empty harsh %goterm. Since when $goterm{$id} became an array? Is "push @{ $goterm{$id} }, $rest"equal to "$goterm{$id}=$rest"? Question 3. a: why ($id) not $id b: why =~ not = c: why ^(\S+) not ^(\S++)
(This post was edited by FishMonger on Jan 27, 2014, 9:04 AM)
|