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