1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | my @columns = (); |
---|
7 | my $del = ""; |
---|
8 | my @in = (); |
---|
9 | my @out = (); |
---|
10 | my $command = ""; |
---|
11 | my $field = 0; |
---|
12 | |
---|
13 | # a wrapper for cut for use in galaxy |
---|
14 | # cutWrapper.pl [filename] [columns] [delim] [output] |
---|
15 | |
---|
16 | die "Check arguments\n" unless @ARGV == 4; |
---|
17 | |
---|
18 | $ARGV[1] =~ s/\s+//g; |
---|
19 | foreach ( split /,/, $ARGV[1] ) { |
---|
20 | if (m/^c\d{1,}$/i) { |
---|
21 | push (@columns, $_); |
---|
22 | $columns[@columns-1] =~s/c//ig; |
---|
23 | } |
---|
24 | } |
---|
25 | |
---|
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; |
---|
27 | |
---|
28 | my $column_delimiters_href = { |
---|
29 | 'T' => q{\t}, |
---|
30 | 'C' => ",", |
---|
31 | 'D' => "-", |
---|
32 | 'U' => "_", |
---|
33 | 'P' => q{\|}, |
---|
34 | 'Dt' => q{\.}, |
---|
35 | 'Sp' => q{\s+} |
---|
36 | }; |
---|
37 | |
---|
38 | $del = $column_delimiters_href->{$ARGV[2]}; |
---|
39 | |
---|
40 | open (OUT, ">$ARGV[3]") or die "Cannot create $ARGV[2]:$!\n"; |
---|
41 | open (IN, "<$ARGV[0]") or die "Cannot open $ARGV[0]:$!\n"; |
---|
42 | while (<IN>) { |
---|
43 | chop; |
---|
44 | @in = split /$del/; |
---|
45 | foreach $field (@columns) { |
---|
46 | if (defined($in[$field-1])) { |
---|
47 | push(@out, $in[$field-1]); |
---|
48 | } else { |
---|
49 | push(@out, "."); |
---|
50 | } |
---|
51 | } |
---|
52 | print OUT join("\t",@out), "\n"; |
---|
53 | @out = (); |
---|
54 | } |
---|
55 | close IN; |
---|
56 | |
---|
57 | close OUT; |
---|
58 | |
---|