PageRenderTime 17ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/human_genome_variation/ctd.pl

https://bitbucket.org/cistrome/cistrome-harvard/
Perl | 80 lines | 62 code | 6 blank | 12 comment | 7 complexity | 04478666150fd93b44a3d95f027a51a6 MD5 | raw file
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use LWP::UserAgent;
  4. require HTTP::Cookies;
  5. #######################################################
  6. # ctd.pl
  7. # Submit a batch query to CTD and fetch results into galaxy history
  8. # usage: ctd.pl inFile idCol inputType resultType actionType outFile
  9. #######################################################
  10. if (!@ARGV or scalar @ARGV != 6) {
  11. print "usage: ctd.pl inFile idCol inputType resultType actionType outFile\n";
  12. exit;
  13. }
  14. my $in = shift @ARGV;
  15. my $col = shift @ARGV;
  16. if ($col < 1) {
  17. print "The column number is with a 1 start\n";
  18. exit 1;
  19. }
  20. my $type = shift @ARGV;
  21. my $resType = shift @ARGV;
  22. my $actType = shift @ARGV;
  23. my $out = shift @ARGV;
  24. my @data;
  25. open(FH, $in) or die "Couldn't open $in, $!\n";
  26. while (<FH>) {
  27. chomp;
  28. my @f = split(/\t/);
  29. if (scalar @f < $col) {
  30. print "ERROR the requested column is not in the file $col\n";
  31. exit 1;
  32. }
  33. push(@data, $f[$col-1]);
  34. }
  35. close FH or die "Couldn't close $in, $!\n";
  36. my $url = 'http://ctd.mdibl.org/tools/batchQuery.go';
  37. #my $url = 'http://globin.bx.psu.edu/cgi-bin/print-query';
  38. my $d = join("\n", @data);
  39. #list maintains order, where hash doesn't
  40. #order matters at ctd
  41. #to use input file (gives error can't find file)
  42. #my @form = ('inputType', $type, 'inputTerms', '', 'report', $resType,
  43. #'queryFile', [$in, ''], 'queryFileColumn', $col, 'format', 'tsv', 'action', 'Submit');
  44. my @form = ('inputType', $type, 'inputTerms', $d, 'report', $resType,
  45. 'queryFile', '', 'format', 'tsv', 'action', 'Submit');
  46. if ($resType eq 'cgixns') { #only add if this type
  47. push(@form, 'actionTypes', $actType);
  48. }
  49. if ($resType eq 'go' or $resType eq 'go_enriched') {
  50. push(@form, 'ontology', 'go_bp', 'ontology', 'go_mf', 'ontology', 'go_cc');
  51. }
  52. my $ua = LWP::UserAgent->new;
  53. $ua->cookie_jar(HTTP::Cookies->new( () ));
  54. $ua->agent('Mozilla/5.0');
  55. my $page = $ua->post($url, \@form, 'Content_Type'=>'form-data');
  56. if ($page->is_success) {
  57. open(FH, ">", $out) or die "Couldn't open $out, $!\n";
  58. print FH "#";
  59. print FH $page->content, "\n";
  60. close FH or die "Couldn't close $out, $!\n";
  61. }else {
  62. print "ERROR failed to get page from CTD, ", $page->status_line, "\n";
  63. print $page->content, "\n";
  64. my $req = $page->request();
  65. print "Requested \n";
  66. foreach my $k(keys %$req) {
  67. if ($k eq '_headers') {
  68. my $t = $req->{$k};
  69. foreach my $k2 (keys %$t) { print "$k2 => $t->{$k2}\n"; }
  70. }else { print "$k => $req->{$k}\n"; }
  71. }
  72. exit 1;
  73. }
  74. exit;