-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoid.pl
164 lines (152 loc) · 4.82 KB
/
geoid.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl
use strict;
my $root_dir;
BEGIN {
$root_dir = $0;
$root_dir =~ s/[^\/]*$//;
$root_dir = "./" unless $root_dir =~ /\//;
push @INC, $root_dir;
}
use Carp;
use Data::Dumper;
use Getopt::Long;
use Config::IniFiles;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Response;
use File::Temp;
use XML::Simple;
print "initializing...\n";
#default config
my $config;
$config = $root_dir . 'geoid.ini';
#get option, override default config if config parameter exists
my $option = GetOptions ("config=s" => \$config);
tie my %ini, 'Config::IniFiles', (-file => $config);
print "done.\n";
#output files
my $outdir = $ini{output}{geo};
my $gsefile = $ini{output}{gse};
my $gsmfile = $ini{output}{gsm};
#get xml result from entrez search
print "download entrez esearch results ...";
my $search_url = $ini{geo}{search_url} . "db=$ini{geo}{db}" . "&term=$ini{geo}{term}" . "[$ini{geo}{field}]" . "&retmax=$ini{geo}{retmax}" ;
my $searchfile = fetch($search_url);
print "done.\n";
#parse the xml file to get entrez UID for submissions
print "parsing esearch result xml file ...";
my $xs = new XML::Simple;
my $esearch = $xs->XMLin($searchfile);
my $ids = $esearch->{IdList}->{Id};
print "done.\n";
#use entrez esummary with input UID to fetch summary
print "download and parse esummary xml file for GEO UIDs ...\n";
my $xs = new XML::Simple;
open my $gsefh, ">", $gsefile;
open my $gsmfh, ">", $gsmfile;
print $gsefh "#submission_id project title GSE\n";
print $gsmfh "#submission_id project title GSM\n";
my %projects = ('lieb' => 'Jason Lieb',
'celniker' => 'Susan Celniker',
'henikoff' => 'Steven Henikoff',
'karpen' => 'Gary Karpen',
'lai' => 'Eric Lai',
'macalpine' => 'David MacAlpine',
'piano' => 'Fabio Piano',
'snyder' => 'Michael Snyder',
'waterston' => 'Robert Waterston',
'white' => 'Kevin White');
for my $id (@$ids) {
#print " GEO UID $id: downloading...";
my $summary_url = $ini{geo}{summary_url} . "db=$ini{geo}{db}" . "&id=$id";
my $geo_uid_file = $outdir . $id . '.xml';
my $summaryfile = fetch($summary_url, $geo_uid_file);
#print "done. parsing...";
print $summaryfile, "\n";
my $esummary = $xs->XMLin($summaryfile);
my ($type, $title, $summary, $gse, $gsm, $samples);
my $is_gse = 0;
for my $item (@{$esummary->{DocSum}->{Item}}) {
$type = $item->{content} if $item->{Name} eq 'entryType';
$type =~ s/^\s*//; $type =~ s/\s*$//;
if ($type eq 'GSE') {
$is_gse = 1 and last;
}
}
if ($is_gse) {
my @gsm;
for my $item (@{$esummary->{DocSum}->{Item}}) {
$title = $item->{content} if $item->{Name} eq 'title';
$summary = $item->{content} if $item->{Name} eq 'summary';
$gse = $item->{content} if $item->{Name} eq 'GSE';
$gsm = $item->{content} if $item->{Name} eq 'GSM_L';
#$samples = $item->{content} if $item->{Name} eq 'Samples';
$samples = $item if $item->{Name} eq 'Samples';
}
print Dumper($samples);
my $internal_id;
my $project;
for my $tmp (($title, $summary)) {
while (my ($k, $v) = each %projects) {
$project = $k and last if $tmp =~ /$v/;
}
}
for my $tmp (($title, $summary)) {
$internal_id = $1 and last if $tmp =~ /modENCODE[_ ]submission[_ ](\d*)?/i ;
}
$gse =~ s/^\s*//; $gse =~ s/\s*$//; $gse = 'GSE' . $gse;
if ($gsm ne '') {
@gsm = split(';', $gsm);
@gsm = map { $_ =~ s/^\s*//; $_ =~ s/\s*$//; 'GSM' . $_; } @gsm;
} else {
@gsm = get_gsm($samples);
}
warn "I could not find DCC internal submission id for this GSE number $gse." unless $internal_id;
$internal_id = $internal_id ? $internal_id : "Unknown id";
print "done. write out ...";
print $gsefh join("\t", ($internal_id, $project, $title, $gse)), "\n";
print $gsmfh join("\t", ($internal_id, $project, $title, @gsm)), "\n";
}
print "done\n";
}
close $gsefh;
close $gsmfh;
print "done\n";
exit 0;
sub fetch {
my ($url, $outfile) = @_;
my ($fh, $file);
if ($outfile) {
return $outfile if -e $outfile;
$file = $outfile;
open $fh, ">", $file;
} else {
($fh, $file) = File::Temp::tempfile();
}
my $ua = new LWP::UserAgent;
my $request = $ua->request(HTTP::Request->new('GET' => $url));
$request->is_success or die "$url: " . $request->message;
print $fh $request->content();
close $fh;
return $file;
}
sub get_gsm {
my $item = shift;
my @gsm;
my $xl = $item->{Item};
if (ref($xl) eq 'HASH') {
my $yl = $xl->{Item};
for my $y (@$yl) {
push @gsm, $y->{content} if $y->{Name} eq 'Accession';
}
}
if (ref($xl) eq 'ARRAY') {
for my $x (@$xl) {
my $yl = $x->{Item};
for my $y (@$yl) {
push @gsm, $y->{content} if $y->{Name} eq 'Accession';
}
}
}
return @gsm;
}