PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/filters/cutWrapper.pl

https://bitbucket.org/cistrome/cistrome-harvard/
Perl | 87 lines | 57 code | 13 blank | 17 comment | 10 complexity | 9c9d6785a0cfaafd6ba001c078fa7df8 MD5 | raw file
 1#!/usr/bin/perl -w
 2
 3use strict;
 4use warnings;
 5
 6my @columns = ();
 7my $del = "";
 8my @in = ();
 9my @out = ();
10my $command = "";
11my $field = 0;
12my $start = 0;
13my $end = 0;
14my $i = 0;
15
16# a wrapper for cut for use in galaxy
17# cutWrapper.pl [filename] [columns] [delim] [output]
18
19die "Check arguments\n" unless @ARGV == 4;
20
21$ARGV[1] =~ s/\s+//g;
22foreach ( split /,/, $ARGV[1] ) {
23  if (m/^c\d{1,}$/i) {
24    push (@columns, $_);
25    $columns[@columns-1] =~s/c//ig;
26  } elsif (m/^c\d{1,}-c\d{1,}$/i) {
27    ($start, $end)  = split(/-/, $_);
28    $start =~ s/c//ig;
29    $end =~ s/c//ig;
30    for $i ($start .. $end) {
31       push (@columns, $i); 
32    }
33  }
34}
35
36die "No columns specified, columns are not preceded with 'c', or commas are not used to separate column numbers: $ARGV[1]\n" if @columns == 0;
37
38my $column_delimiters_href = {
39  'T' => q{\t},
40  'C' => ",",
41  'D' => "-",
42  'U' => "_",
43  'P' => q{\|},
44  'Dt' => q{\.},
45  'Sp' => q{\s+}
46};
47
48$del = $column_delimiters_href->{$ARGV[2]};
49
50open (OUT, ">$ARGV[3]") or die "Cannot create $ARGV[2]:$!\n";
51open (IN,  "<$ARGV[0]") or die "Cannot open $ARGV[0]:$!\n";
52
53while (my $line=<IN>) {
54   if ($line =~ /^#/) {
55     #Ignore comment lines
56   } else {
57     chop($line);
58     @in = split(/$del/, $line);
59     foreach $field (@columns) {
60       if (defined($in[$field-1])) {
61         push(@out, $in[$field-1]);
62       } else {
63         push(@out, ".");
64       }
65     }    
66     print OUT join("\t",@out), "\n";
67     @out = ();
68   }
69}
70
71#while (<IN>) {
72#  chop;
73#  @in = split /$del/; 
74#  foreach $field (@columns) {
75#    if (defined($in[$field-1])) {
76#      push(@out, $in[$field-1]);
77#    } else {
78#      push(@out, ".");
79#    }
80#  }
81#  print OUT join("\t",@out), "\n";
82#  @out = ();
83#}
84close IN;
85
86close OUT;
87