annotate tools/filters/joinWrapper.pl @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #! /usr/bin/perl -w
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 use strict;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 use warnings;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 use File::Temp "tempfile";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 my ($input1, $input2, $field1, $field2, $mode, $OOption, $out_file1) = @ARGV;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 die "No arguments\n" unless @ARGV == 7;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 my ($fh1, $file1) = tempfile();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 my ($fh2, $file2) = tempfile();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 `sort -k $field1 $input1 > $file1`;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 `sort -k $field2 $input2 > $file2`;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 my $option = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 my @fields = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 my $line = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if ($OOption eq "Y") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 if (defined($fh1)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 $line = <$fh1>;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 die "Failed to create file $file1\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 @fields = split /\t/, $line;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 die "The field you selected does not exist in the input file" if (@fields < $field1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 my @optionO = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 my $i = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 foreach (@fields) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 ++$i;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 push(@optionO, "1.$i");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 $option = "-o " . join(",", @optionO);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 $option = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 $ENV{'LC_ALL'} = 'POSIX';
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if ($mode eq "V") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 `join -v 1 $option -1 $field1 -2 $field2 $file1 $file2 | tr " " "\t" > $out_file1`;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 `join $option -1 $field1 -2 $field2 $file1 $file2 | tr " " "\t" > $out_file1`;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 `rm $file1 ; rm $file2`;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51