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