annotate tools/filters/cutWrapper.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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 my @columns = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 my $del = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 my @in = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 my @out = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 my $command = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 my $field = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 # a wrapper for cut for use in galaxy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 # cutWrapper.pl [filename] [columns] [delim] [output]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 die "Check arguments\n" unless @ARGV == 4;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 $ARGV[1] =~ s/\s+//g;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 foreach ( split /,/, $ARGV[1] ) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 if (m/^c\d{1,}$/i) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 push (@columns, $_);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 $columns[@columns-1] =~s/c//ig;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 die "No columns specified, columns are not preceded with 'c', or commas are not used to separate column numbers: $ARGV[1]\n" if @columns == 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 my $column_delimiters_href = {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 'T' => q{\t},
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 'C' => ",",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 'D' => "-",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 'U' => "_",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 'P' => q{\|},
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 'Dt' => q{\.},
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 'Sp' => q{\s+}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 };
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 $del = $column_delimiters_href->{$ARGV[2]};
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 open (OUT, ">$ARGV[3]") or die "Cannot create $ARGV[2]:$!\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 open (IN, "<$ARGV[0]") or die "Cannot open $ARGV[0]:$!\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 while (my $line=<IN>) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if ($line =~ /^#/) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 #Ignore comment lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 chop($line);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 @in = split(/$del/, $line);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 foreach $field (@columns) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if (defined($in[$field-1])) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 push(@out, $in[$field-1]);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 push(@out, ".");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 print OUT join("\t",@out), "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 @out = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 #while (<IN>) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 # chop;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 # @in = split /$del/;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 # foreach $field (@columns) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 # if (defined($in[$field-1])) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 # push(@out, $in[$field-1]);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 # } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 # push(@out, ".");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 # }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 # }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 # print OUT join("\t",@out), "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 # @out = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 #}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 close IN;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 close OUT;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77