-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateProteinFunctionsTmp.pl
executable file
·276 lines (178 loc) · 6.85 KB
/
updateProteinFunctionsTmp.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/perl
use FindBin qw($Bin);
use JSON;
use Data::Dumper;
use lib "$Bin";
use SolrAPI;
my $solrServer = $ENV{PATRIC_SOLR};
my $solrFormat="&wt=csv&csv.separator=%09&csv.mv.separator=;";
my $solrh = SolrAPI->new();
my $json = JSON->new->allow_nonref;
# Get reference data
my $ecRef = $solrh->getECRef();
my $pathwayRef = $solrh->getPathwayRef();
my $infile = $ARGV[0];
# process functions and prepare update files
processFunctions($infile);
sub processFunctions {
my ($infile) = @_;
open IN, "$infile";
my $prev_genome_id = "";
while (my $row = <IN>){
chomp $row;
my ($patric_id, $pgfam_id, $product) = $row=~/(.*)\t(.*)\t(.*)/;
next unless ($product);
my ($genome_id) = $patric_id=~/fig\|(.*).peg/;
if ($genome_id ne $prev_genome_id || !$prev_genome_id){
# post updates for the last genome
prepareUpdateFiles($prev_genome_id, \@update_features, \@update_spgenes, \@delete_pathways, \@add_pathways) if $prev_genome_id;
print "$genome_id\n";
# global arrays to record all the updates
@update_features = ();
# new genome, get primary identifiers and existing features
$fids = getFeatureIDs($genome_id);
}
# process entry, compare functions and prepare updates
my ($feature, @ec_no, @ec, @go, @pathways, @ecpathways);
my $feature_id = $fids->{$patric_id};
next unless $feature_id;
$product=~s/^\s*|\s*$//g;
@ec_no = $product=~/\( *EC[: ]([\d-\.]+) *\)/g;
foreach my $ec_number (@ec_no){
my $ec_description = $ecRef->{$ec_number}->{ec_description};
push @ec, $ec_number.'|'.$ec_description unless (grep {$_ eq $ec_number.'|'.$ec_description} @ec);
foreach my $go_term (@{$ecRef->{$ec_number}->{go}}){
push @go, $go_term unless (grep {$_ eq $go_term} @go);
}
foreach my $pathway (@{$pathwayRef->{$ec_number}->{pathway}}){
my ($pathway_id, $pathway_name, $pathway_class) = split(/\t/, $pathway);
push @pathways, $pathway_id.'|'.$pathway_name unless (grep {$_ eq $pathway_id.'|'.$pathway_name} @pathways);
$ecpathway = "$ec_number\t$ec_description\t$pathway_id\t$pathway_name\t$pathway_class";
push @ecpathways, $ecpathway unless (grep {$_ eq $ecpathway} @ecpathways);
}
}
# update genome_feature
$feature->{feature_id} = $feature_id;
$feature->{ec}->{set} = \@ec unless scalar @ec;
$feature->{go}->{set} = \@go unless scalar @go;
$feature->{pathway}->{set} = \@pathways unless scalar @pathways;
push @update_features, $feature unless (scalar @ec && scalar @go && scalar @pathways);
$prev_genome_id = $genome_id;
}
prepareUpdateFiles($prev_genome_id, \@update_features, \@update_spgenes, \@delete_pathways, \@add_pathways);
close IN;
}
sub prepareUpdateFiles{
my($genome_id, $update_features, $update_spgenes, $delete_pathways, $add_pathways) = @_;
my $feature_json = $json->pretty->encode($update_features);
open GF, ">$genome_id.genome_feature.json";
print GF "$feature_json";
close GF;
=pod
my $spgenes_json = $json->pretty->encode($update_spgenes);
open SPG, ">$genome_id.sp_gene.json";
print SPG "$spgenes_json";
close SPG;
open DEL_PATH, ">$genome_id.delete_pathway.sh";
print DEL_PATH join("", @$delete_pathways);
close DEL_PATH;
my $pathway_json = $json->pretty->encode($add_pathways);
open PATH, ">$genome_id.pathway.json";
print PATH "$pathway_json";
close PATH;
=cut
}
sub getFeatureIDs {
my ($genome_id) = @_;
my $core = "/genome_feature";
my $query = "/select?q=annotation:PATRIC AND feature_type:CDS AND genome_id:$genome_id";
my $fields = "&fl=patric_id,feature_id";
my $rows = "&rows=1000000";
my $sort = "";
my $solrQuery = $solrServer.$core.$query.$fields.$rows.$sort.$solrFormat;
my @features = `wget -q -O - "$solrQuery" | grep -v patric_id`;
my %FID = ();
foreach my $feature (@features){
my ($patric_id, $feature_id) = $feature=~/(.*)\t(.*)/;
$FID{$patric_id} = $feature_id;
}
return \%FID;
}
sub getFeatures {
my ($genome_id) = @_;
my $core = "/genome_feature";
my $query = "/select?q=annotation:PATRIC AND feature_type:CDS AND genome_id:$genome_id AND ec:*";
my $fields = "&fl=genome_id,genome_name,taxon_id,sequence_id,accession,annotation,feature_id,patric_id,alt_locus_tag,refseq_locus_tag,gene,product,owner,public";
my $rows = "&rows=1000000";
my $sort = "";
my $solrFormat="&wt=json";
my $solrQuery = $solrServer.$core.$query.$fields.$rows.$sort.$solrFormat;
my $result = `wget -q -O - "$solrQuery"`;
my $resultObj = decode_json($result);
my $features;
foreach my $record(@{$resultObj->{'response'}->{'docs'}}){
$features->{$record->{patric_id}} = $record;
}
return $features;
}
sub getPathwayIDs {
my ($genome_id) = @_;
my $core = "/pathway";
my $query = "/select?q=annotation:PATRIC AND genome_id:$genome_id";
my $fields = "&fl=patric_id,id";
my $rows = "&rows=1000000";
my $sort = "";
my $solrQuery = $solrServer.$core.$query.$fields.$rows.$sort.$solrFormat;
my @result = `wget -q -O - "$solrQuery" | grep -v patric_id`;
my %pathway_ids = ();
foreach my $record (@result){
my ($patric_id, $id) = $record=~/(.*)\t(.*)/;
push @{$pathway_ids{$patric_id}}, $id;
}
return \%pathway_ids;
}
sub getSPGeneIDs {
my ($genome_id) = @_;
my $core = "/sp_gene";
my $query = "/select?q=genome_id:$genome_id";
my $fields = "&fl=patric_id,id";
my $rows = "&rows=1000000";
my $sort = "";
my $solrQuery = $solrServer.$core.$query.$fields.$rows.$sort.$solrFormat;
my @result = `wget -q -O - "$solrQuery" | grep -v patric_id`;
my %spgids = ();
foreach my $record (@result){
my ($patric_id, $id) = $record=~/(.*)\t(.*)/;
push @{$spgids{$patric_id}}, $id;
}
return \%spgids;
}
sub preparePathways {
my ($feature, $ecpathways) = @_;
my @pathways = ();
foreach my $ecpathway (@$ecpathways){
my $pathway;
my ($ec_number, $ec_description, $pathway_id, $pathway_name, $pathway_class) = split /\t/, $ecpathway;
$pathway->{owner} = $feature->{owner};
$pathway->{public} = $feature->{public};
$pathway->{genome_id} = $feature->{genome_id};
$pathway->{genome_name} = $feature->{genome_name};
$pathway->{taxon_id} = $feature->{taxon_id};
$pathway->{sequence_id} = $feature->{sequence_id};
$pathway->{accession} = $feature->{accession};
$pathway->{annotation} = $feature->{annotation};
$pathway->{feature_id} = $feature->{feature_id};
$pathway->{patric_id} = $feature->{patric_id};
$pathway->{alt_locus_tag} = $feature->{alt_locus_tag};
$pathway->{refseq_locus_tag} = $feature->{refseq_locus_tag};
$pathway->{ec_number} = $ec_number;
$pathway->{ec_description} = $ec_description;
$pathway->{pathway_id} = $pathway_id;
$pathway->{pathway_name} = $pathway_name;
$pathway->{pathway_class} = $pathway_class;
$pathway->{genome_ec} = $feature->{genome_id}.'_'.$ec_number;
$pathway->{pathway_ec} = $pathway_id.'_'.$ec_number;
push @pathways, $pathway;
}
return @pathways;
}