Parent Directory
|
Revision Log
Revision 1.4 - (view) (download) (as text)
1 : | olson | 1.1 | |
2 : | package FFserver; | ||
3 : | |||
4 : | use LWP::UserAgent; | ||
5 : | use Data::Dumper; | ||
6 : | olson | 1.3 | use YAML; |
7 : | olson | 1.1 | |
8 : | use strict; | ||
9 : | |||
10 : | sub new | ||
11 : | { | ||
12 : | my($class, $server_url) = @_; | ||
13 : | |||
14 : | olson | 1.3 | $server_url = "http://servers.nmpdr.org/figfam/server.cgi" unless $server_url; |
15 : | olson | 1.1 | |
16 : | |||
17 : | my $self = { | ||
18 : | server_url => $server_url, | ||
19 : | ua => LWP::UserAgent->new(), | ||
20 : | }; | ||
21 : | |||
22 : | return bless $self, $class; | ||
23 : | } | ||
24 : | |||
25 : | olson | 1.3 | sub members_of_families |
26 : | { | ||
27 : | my($self, @ids) = @_; | ||
28 : | olson | 1.4 | return $self->run_query('members_of_families', @ids); |
29 : | } | ||
30 : | |||
31 : | sub should_be_member | ||
32 : | { | ||
33 : | my($self, @id_seq_pairs) = @_; | ||
34 : | return $self->run_query('should_be_member', @id_seq_pairs); | ||
35 : | } | ||
36 : | |||
37 : | sub all_families | ||
38 : | { | ||
39 : | my($self) = @_; | ||
40 : | return $self->run_query('all_families'); | ||
41 : | } | ||
42 : | |||
43 : | sub run_query | ||
44 : | { | ||
45 : | my($self, $function, @args ) = @_; | ||
46 : | my $form = [function => $function, | ||
47 : | args => YAML::Dump(@args), | ||
48 : | ]; | ||
49 : | olson | 1.3 | return $self->run_query_form($form); |
50 : | } | ||
51 : | |||
52 : | sub run_query_form | ||
53 : | { | ||
54 : | my($self, $form) = @_; | ||
55 : | |||
56 : | my $res = $self->{ua}->post($self->{server_url}, $form); | ||
57 : | |||
58 : | if ($res->is_success) | ||
59 : | { | ||
60 : | return Load($res->content); | ||
61 : | } | ||
62 : | else | ||
63 : | { | ||
64 : | die "error on post " . $res->content; | ||
65 : | } | ||
66 : | } | ||
67 : | |||
68 : | olson | 1.1 | sub assign_function_to_prot |
69 : | { | ||
70 : | my($self, $input) = @_; | ||
71 : | |||
72 : | olson | 1.2 | my $wq; |
73 : | |||
74 : | olson | 1.1 | if (ref($input) eq 'ARRAY') |
75 : | { | ||
76 : | olson | 1.2 | $wq = SequenceListWorkQueue->new($input); |
77 : | olson | 1.1 | } |
78 : | else | ||
79 : | { | ||
80 : | olson | 1.2 | $wq = FastaWorkQueue->new($input); |
81 : | olson | 1.1 | } |
82 : | olson | 1.2 | |
83 : | return ResultHandler->new($wq, $self->{server_url}, 'assign_function_to_prot', \&id_seq_pair_bundler, \&tab_delimited_output_parser); | ||
84 : | olson | 1.1 | } |
85 : | |||
86 : | olson | 1.2 | sub assign_functions_to_dna |
87 : | olson | 1.1 | { |
88 : | olson | 1.2 | my($self, $input) = @_; |
89 : | olson | 1.1 | |
90 : | olson | 1.2 | my $wq; |
91 : | olson | 1.1 | |
92 : | olson | 1.2 | if (ref($input) eq 'ARRAY') |
93 : | olson | 1.1 | { |
94 : | olson | 1.2 | $wq = SequenceListWorkQueue->new($input); |
95 : | olson | 1.1 | } |
96 : | else | ||
97 : | { | ||
98 : | olson | 1.2 | $wq = FastaWorkQueue->new($input); |
99 : | olson | 1.1 | } |
100 : | olson | 1.2 | |
101 : | olson | 1.3 | return ResultHandler->new($wq, $self->{server_url}, 'assign_functions_to_DNA', \&id_seq_pair_bundler, \&tab_delimited_output_parser); |
102 : | olson | 1.2 | } |
103 : | |||
104 : | sub id_seq_pair_bundler | ||
105 : | { | ||
106 : | my($item) = @_; | ||
107 : | my($id, $seq) = @$item[0,2]; | ||
108 : | return "id_seq", join(",", $id, (ref($seq) eq 'SCALAR' ? $$seq : $seq)); | ||
109 : | } | ||
110 : | |||
111 : | sub tab_delimited_output_parser | ||
112 : | { | ||
113 : | my($line) = @_; | ||
114 : | chomp $line; | ||
115 : | my @cols = split(/\t/, $line); | ||
116 : | return \@cols; | ||
117 : | } | ||
118 : | |||
119 : | |||
120 : | sub tab_delimited_dna_data_output_parser | ||
121 : | { | ||
122 : | my($line) = @_; | ||
123 : | chomp $line; | ||
124 : | my ($id, $idbe, $fam) = split(/\t/, $line); | ||
125 : | my ($beg, $end) = $idbe =~ /_(\d+)_(\d+)$/; | ||
126 : | return [$id, $beg, $end, $fam]; | ||
127 : | olson | 1.1 | } |
128 : | |||
129 : | olson | 1.2 | package ResultHandler; |
130 : | olson | 1.1 | use strict; |
131 : | olson | 1.2 | use Data::Dumper; |
132 : | olson | 1.1 | |
133 : | sub new | ||
134 : | { | ||
135 : | olson | 1.2 | my($class, $work_queue, $server_url, $function, $input_bundler, $output_parser) = @_; |
136 : | |||
137 : | olson | 1.1 | my $self = { |
138 : | olson | 1.2 | work_queue => $work_queue, |
139 : | server_url => $server_url, | ||
140 : | function => $function, | ||
141 : | input_bundler => $input_bundler, | ||
142 : | output_parser => $output_parser, | ||
143 : | ua => LWP::UserAgent->new(), | ||
144 : | cur_result => undef, | ||
145 : | olson | 1.1 | }; |
146 : | return bless $self, $class; | ||
147 : | } | ||
148 : | |||
149 : | sub get_next | ||
150 : | { | ||
151 : | my($self) = @_; | ||
152 : | |||
153 : | olson | 1.2 | if ($self->{cur_result}) |
154 : | { | ||
155 : | return $self->get_next_from_result(); | ||
156 : | } | ||
157 : | else | ||
158 : | { | ||
159 : | my @inp = $self->{work_queue}->get_next_n_bytes(16000); | ||
160 : | if (@inp) | ||
161 : | { | ||
162 : | my $form = [function => $self->{function}, | ||
163 : | map { &{$self->{input_bundler}}($_) } @inp ]; | ||
164 : | # print "Invoke " .Dumper($form); | ||
165 : | |||
166 : | my $res = $self->{ua}->post($self->{server_url}, $form); | ||
167 : | if ($res->is_success) | ||
168 : | { | ||
169 : | $self->{cur_result} = $res->content; | ||
170 : | #print "res: $self->{cur_result}\n"; | ||
171 : | return $self->get_next_from_result(); | ||
172 : | } | ||
173 : | else | ||
174 : | { | ||
175 : | olson | 1.3 | die "error " . $res->status_line . " on post " . $res->content; |
176 : | olson | 1.2 | } |
177 : | } | ||
178 : | else | ||
179 : | { | ||
180 : | return; | ||
181 : | } | ||
182 : | } | ||
183 : | } | ||
184 : | |||
185 : | sub get_next_from_result | ||
186 : | { | ||
187 : | my($self) = @_; | ||
188 : | if ($self->{cur_result} =~ s/^([^\n]*)\n//) | ||
189 : | { | ||
190 : | return &{$self->{output_parser}}($1); | ||
191 : | } | ||
192 : | } | ||
193 : | |||
194 : | package SequenceWorkQueue; | ||
195 : | use strict; | ||
196 : | |||
197 : | sub new | ||
198 : | { | ||
199 : | my($class) = @_; | ||
200 : | |||
201 : | my $self = {}; | ||
202 : | |||
203 : | return bless $self, $class; | ||
204 : | } | ||
205 : | olson | 1.1 | |
206 : | olson | 1.2 | sub get_next_n |
207 : | { | ||
208 : | my($self, $n) = @_; | ||
209 : | my @out; | ||
210 : | |||
211 : | for (my $i = 0;$i < $n; $i++) | ||
212 : | olson | 1.1 | { |
213 : | olson | 1.2 | my($id, $com, $seqp) = $self->get_next(); |
214 : | if (defined($id)) | ||
215 : | { | ||
216 : | push(@out, [$id, $com, $seqp]); | ||
217 : | } | ||
218 : | else | ||
219 : | { | ||
220 : | last; | ||
221 : | } | ||
222 : | olson | 1.1 | } |
223 : | olson | 1.2 | return @out; |
224 : | } | ||
225 : | |||
226 : | sub get_next_n_bytes | ||
227 : | { | ||
228 : | my($self, $n) = @_; | ||
229 : | my @out; | ||
230 : | |||
231 : | my $size = 0; | ||
232 : | while ($size < $n) | ||
233 : | olson | 1.1 | { |
234 : | olson | 1.2 | my($id, $com, $seqp) = $self->get_next(); |
235 : | if (defined($id)) | ||
236 : | { | ||
237 : | push(@out, [$id, $com, $seqp]); | ||
238 : | $size += (ref($seqp) eq 'SCALAR') ? length($$seqp) : length($seqp); | ||
239 : | } | ||
240 : | else | ||
241 : | { | ||
242 : | last; | ||
243 : | } | ||
244 : | olson | 1.1 | } |
245 : | olson | 1.2 | return @out; |
246 : | olson | 1.1 | } |
247 : | |||
248 : | olson | 1.2 | package FastaWorkQueue; |
249 : | olson | 1.1 | use strict; |
250 : | olson | 1.2 | use base 'SequenceWorkQueue'; |
251 : | olson | 1.1 | use FileHandle; |
252 : | use FIG; | ||
253 : | |||
254 : | sub new | ||
255 : | { | ||
256 : | olson | 1.2 | my($class, $input) = @_; |
257 : | olson | 1.1 | |
258 : | my $fh; | ||
259 : | if (ref($input)) | ||
260 : | { | ||
261 : | $fh = $input; | ||
262 : | } | ||
263 : | else | ||
264 : | { | ||
265 : | $fh = new FileHandle("<$input"); | ||
266 : | } | ||
267 : | olson | 1.2 | |
268 : | my $self = $class->SUPER::new(); | ||
269 : | |||
270 : | $self->{fh} = $fh; | ||
271 : | |||
272 : | olson | 1.1 | return bless $self, $class; |
273 : | } | ||
274 : | |||
275 : | sub get_next | ||
276 : | { | ||
277 : | my($self) = @_; | ||
278 : | |||
279 : | olson | 1.2 | my($id, $seqp, $com) = &FIG::read_fasta_record($self->{fh}); |
280 : | return defined($id) ? ($id, $com, $seqp) : (); | ||
281 : | } | ||
282 : | |||
283 : | package SequenceListWorkQueue; | ||
284 : | use strict; | ||
285 : | use base 'SequenceWorkQueue'; | ||
286 : | olson | 1.1 | |
287 : | olson | 1.2 | sub new |
288 : | { | ||
289 : | my($class, $input) = @_; | ||
290 : | |||
291 : | my $fh; | ||
292 : | if (ref($input) ne 'ARRAY') | ||
293 : | olson | 1.1 | { |
294 : | olson | 1.2 | die "SequenceWorkQueue requires a list as input"; |
295 : | olson | 1.1 | } |
296 : | olson | 1.2 | |
297 : | my $self = $class->SUPER::new(); | ||
298 : | |||
299 : | $self->{list} = $input; | ||
300 : | |||
301 : | return bless $self, $class; | ||
302 : | } | ||
303 : | |||
304 : | sub get_next | ||
305 : | { | ||
306 : | my($self) = @_; | ||
307 : | |||
308 : | my $top = shift @{$self->{list}}; | ||
309 : | |||
310 : | return defined($top) ? @$top : (); | ||
311 : | olson | 1.1 | } |
312 : | olson | 1.2 | |
313 : | |||
314 : | olson | 1.1 | 1; |
315 : | olson | 1.2 |
MCS Webmaster | ViewVC Help |
Powered by ViewVC 1.0.3 |