File Coverage

blib/lib/Bio/DOOP/Util/Run/Mofext.pm
Criterion Covered Total %
statement 9 140 6.4
branch 0 20 0.0
condition n/a
subroutine 3 13 23.0
pod 10 10 100.0
total 22 183 12.0


line stmt bran cond sub pod time code
1             package Bio::DOOP::Util::Run::Mofext;
2              
3 1     1   5 use strict;
  1         2  
  1         31  
4 1     1   6 use warnings;
  1         1  
  1         29  
5 1     1   6 use Carp qw(cluck carp verbose);
  1         1  
  1         1695  
6              
7             =head1 NAME
8              
9             Bio::DOOP::Util::Run::Mofext - Mofext module
10              
11             =head1 VERSION
12              
13             Version 0.17
14              
15             =cut
16              
17             our $VERSION = '0.17';
18              
19             =head1 SYNOPSIS
20              
21             #!/usr/bin/perl -w
22              
23             use Bio::DOOP::DOOP;
24              
25             $db = Bio::DOOP::DBSQL->connect("user","pass","doop-plant-1_5","localhost");
26              
27             @list = ("81001020","81001110","81001200","81001225","81001230","81001290","81001470","81001580","81001610","81001620","81001680","81001680","81001690");
28              
29             $mofext = Bio::DOOP::Util::Run::Mofext->new($db,'500','M',\@list);
30              
31             $mofext->set_tmp_file_name("/data/DOOP/dummy.txt");
32              
33             print $mofext->get_tmp_file_name,"\n";
34              
35             $error = $mofext->write_to_tmp;
36              
37             if($error != 0){
38             die "Write error!\n";
39             }
40              
41             $error = $mofext->run('TTGGGC' , 6 , 0.6 , '/data/default_matrix' );
42              
43             if ($error == -1){
44             die "No results or error!\n";
45             }
46              
47             @res = @{$mofext->get_results};
48              
49             # Returns the motif objects, score and extended score.
50             for $result (@res){
51             print $$result[0]->get_id," ",$$result[1],"$$result[2]","\n";
52             }
53              
54             =head1 DESCRIPTION
55              
56             Mofext is a fuzzy sequence pattern search tool developed by Tibor Nagy. This module
57             is a wrapper object for mofext. It allows the user to search for similar motifs in the
58             DOOP database.
59              
60             =head1 AUTHORS
61              
62             Tibor Nagy, Godollo, Hungary and Endre Sebestyen, Martonvasar, Hungary
63              
64             =head1 METHODS
65              
66             =head2 new
67              
68             Create new Mofext object.
69              
70             Arguments:
71              
72             1. Bio::DOOP::DBSQL object
73             2. promoter type (500, 1000, 3000)
74             3. subset type (depends on reference species)
75             4. arrayref of cluster ids
76              
77             $mofext = Bio::DOOP::Util::Run::Mofext->new($db,500,'B',\@list);
78              
79             =cut
80              
81             sub new {
82 0     0 1   my $self = {};
83 0           my $dummy = shift;
84 0           my $db = shift;
85 0           my $promo_type = shift;
86 0           my $subset_type = shift;
87 0           my $cluster_id_list = shift;
88 0           my @motif_collection;
89              
90 0           for my $cl_id (@{$cluster_id_list}){
  0            
91 0           my $cl = Bio::DOOP::Cluster->new($db,$cl_id,$promo_type);
92 0 0         if ($cl == -1){ next }
  0            
93 0           my $subset = $cl->get_subset_by_type($subset_type);
94 0 0         if ($subset == -1){ next }
  0            
95 0           my $motifs = $subset->get_all_motifs;
96 0 0         if($motifs == -1){ next }
  0            
97 0           for my $motif (@$motifs){
98 0           push @motif_collection, [$motif->get_id,$motif->seq];
99             }
100             }
101              
102 0           $self->{DB} = $db;
103 0           $self->{CLLIST} = $cluster_id_list;
104             # TODO use File::Temp module
105 0           $self->{TMP_FILE} = "/tmp/mofext_run.txt";
106 0           $self->{MOTIF_COLL} = \@motif_collection;
107              
108 0           bless $self;
109 0           return($self);
110             }
111              
112             =head2 new_by_file
113              
114             Create a new Mofext object from query file, containing cluster ids, one per line.
115              
116             Arguments:
117              
118             1. Bio::DOOP::DBSQL object
119             2. promoter type (500, 1000, 3000)
120             3. subset type (depends on reference species)
121             4. name of file with cluster ids
122              
123             $mofext = Bio::DOOP::Util::Run::Mofext->new_by_file($db,500,'B','/tmp/clusters.txt');
124              
125             =cut
126              
127             sub new_by_file {
128 0     0 1   my $self = {};
129 0           my $dummy = shift;
130 0           my $db = shift;
131 0           my $promo_type = shift;
132 0           my $subset_type = shift;
133 0           my $filename = shift;
134 0           my @motif_collection;
135             my @cluster_id_list;
136              
137 0 0         open CLUSTER_ID_FILE,$filename or cluck("No such file or directory!\n");
138 0           while(){
139 0           chomp;
140 0           my $cl_id = $_;
141 0           push @cluster_id_list,$cl_id;
142 0           my $cl = Bio::DOOP::Cluster->new($db,$cl_id,$promo_type);
143 0           my $subset = $cl->get_subset_by_type($subset_type);
144 0 0         if ($subset == -1) { next }
  0            
145 0           my $motifs = $subset->get_all_motifs;
146 0 0         if($motifs == -1){ next }
  0            
147 0           for my $motif (@$motifs){
148 0           push @motif_collection, [$motif->get_id,$motif->seq];
149             }
150             }
151 0           close CLUSTER_ID_FILE;
152              
153 0           $self->{DB} = $db;
154 0           $self->{CLLIST} = \@cluster_id_list;
155             # TODO use File::Temp module
156 0           $self->{TMP_FILE} = "/tmp/mofext_run.txt";
157 0           $self->{MOTIF_COLL} = \@motif_collection;
158              
159 0           bless $self;
160 0           return($self);
161             }
162              
163             =head2 new_by_tmp
164              
165             Create a new Mofext object from an existing temporary file containing conserved motifs. It is useful in some cases,
166             because the new constructor is very slow when you use large cluster lists. If you use this constructor, you don't
167             need to use the set_tmp_file_name and write_to_tmp methods.
168              
169             Arguments:
170              
171             1. Bio::DOOP::DBSQL object
172             2. temporary file name
173              
174             $mofext = Bio::DOOP::Util::Run::Mofext->new_by_tmp($db,"/tmp/motifs.txt");
175              
176             =cut
177              
178             sub new_by_tmp {
179 0     0 1   my $self = {};
180 0           my $dummy = shift;
181 0           $self->{DB} = shift;
182 0           $self->{TMP_FILE} = shift;
183              
184 0           bless $self;
185 0           return($self);
186             }
187              
188              
189             =head2 get_tmp_file_name
190              
191             Get the name of the temporary file containing the motifs.
192              
193             $tmp_name = $mofext->get_tmp_file_name;
194              
195             =cut
196              
197             sub get_tmp_file_name {
198 0     0 1   my $self = shift;
199 0           return($self->{TMP_FILE});
200             }
201              
202             =head2 set_tmp_file_name
203              
204             Set the temporary file name.
205              
206             $mofext->set_tmp_file_name('/tmp/motifs.txt');
207              
208             =cut
209              
210             sub set_tmp_file_name {
211 0     0 1   my $self = shift;
212 0           my $file_name = shift;
213 0           $self->{TMP_FILE} = $file_name;
214             }
215              
216             =head2 write_to_tmp
217              
218             Write out the collected motifs to the temporary file.
219              
220             $write_error = $mofext->write_to_tmp;
221              
222             =cut
223              
224             sub write_to_tmp {
225 0     0 1   my $self = shift;
226              
227 0 0         open OUT,">".$self->{TMP_FILE} or return(-1);
228 0           for my $motif (@{$self->{MOTIF_COLL}}) {
  0            
229 0           print OUT $$motif[0]," ",$$motif[1]," ",length($$motif[1]),"\n";
230             }
231 0           close OUT;
232              
233 0           return(0);
234             }
235              
236             =head2 run
237              
238             Runs mofext, returns 0 on success, otherwise -1.
239              
240             Arguments:
241              
242             1. query sequence
243             2. wordsize
244             3. cutoff
245             4. matrix file path/name
246              
247             A typical matrix looks like this:
248              
249             11 A T G C S W R Y K M N
250             A 5 -4 -4 -4 -4 1 1 -4 -4 1 -2
251             T -4 5 -4 -4 -4 1 -4 1 1 -4 -2
252             G -4 -4 5 -4 1 -4 1 -4 1 -4 -2
253             C -4 -4 -4 5 1 -4 -4 1 -4 1 -2
254             S -4 -4 1 1 -1 -4 -2 -2 -2 -2 -1
255             W 1 1 -4 -4 -4 -1 -2 -2 -2 -2 -1
256             R 1 -4 1 -4 -2 -2 -1 -4 -2 -2 -1
257             Y -4 1 -4 1 -2 -2 -4 -1 -2 -2 -1
258             K -4 1 1 -4 -2 -2 -2 -2 -1 -4 -1
259             M 1 -4 -4 1 -2 -2 -2 -2 -4 -1 -1
260             N -2 -2 -2 -2 -1 -1 -1 -1 -1 -1 -1
261              
262             $mofext_error = $mofext->run('AAGTKSAAT','7','90','/data/run/matrix.txt')
263              
264             =cut
265              
266             sub run {
267 0     0 1   my $self = shift;
268 0           my $query = shift;
269 0           my $wordsize = shift;
270 0           my $cutoff = shift;
271 0           my $matrix_file = shift;
272              
273 0           my %seen;
274              
275 0           my $params = "-q $query -m $matrix_file -w $wordsize -c $cutoff -d ".$self->get_tmp_file_name." -o iseqDfF";
276 0           my @results = `mofext $params`;
277              
278 0           my @id_uniq = grep { ! $seen{ $_ }++ } @results;
  0            
279              
280 0 0         if ($#id_uniq == -1){return(-1)} # No result.
  0            
281              
282 0           $self->{RESULT} = \@id_uniq; # Arrayref of motif ids.
283 0           return(0);
284             }
285              
286             =head2 run_background
287              
288             Runs mofext in background, returns the process id.
289              
290             Arguments:
291              
292             1. query sequence
293             2. wordsize
294             3. cutoff
295             4. matrix file path/name
296             5. output file path/name
297              
298             $mofext_pid = $mofext->run_background('AAGTKSAAT','7','90','/data/run/matrix.txt','/data/run/mofext_results.txt');
299              
300             =cut
301              
302             sub run_background {
303 0     0 1   my $self = shift;
304 0           my $query = shift;
305 0           my $wordsize = shift;
306 0           my $cutoff = shift;
307 0           my $matrix_file = shift;
308 0           my $outfile = shift;
309 0           my $pid;
310              
311 0 0         unless($pid = fork){
312              
313 0           my $params = "-q $query -m $matrix_file -w $wordsize -c $cutoff -d ".$self->get_tmp_file_name." -o iseqDfF";
314 0           my @results = `mofext $params | sort | uniq >$outfile`;
315             }
316              
317 0           return($pid);
318             }
319              
320             =head2 get_results
321              
322             Returns an arrayref of arrays with motif objects and other information of the results.
323              
324             The results contain the following:
325              
326             1. Bio::DOOP::Motif object
327             2. motif score
328             3. motif extended score
329             4. full hit sequence
330             5. alignment start position in the query sequence
331             6. alignment start position in the hit sequence
332              
333             @result = @{$mofext->get_results};
334              
335             =cut
336              
337             sub get_results {
338 0     0 1   my $self = shift;
339              
340 0           my $res = $self->{RESULT};
341 0           my @mofext_res;
342             my $id;
343 0           my $score;
344 0           my $extscore;
345 0           my $fullhit;
346 0           my $querystart;
347 0           my $hitstart;
348 0           my $querysub;
349              
350 0           for my $line (@{$res}) {
  0            
351 0           chomp($line);
352 0           ($id,$score,$extscore,$fullhit,$querystart,$hitstart) = split(/ /,$line);
353 0           my $motif = Bio::DOOP::Motif->new($self->{DB},$id);
354 0           push @mofext_res, [$motif,$score,$extscore,$querysub,$fullhit,$querystart,$hitstart];
355             }
356              
357 0           return(\@mofext_res);
358             }
359              
360             =head2 get_results_from_file
361              
362             Returns an arrayref of arrays with motif objects and other information of the results
363             from a results file. With this method you can fetch the results of different mofext objects.
364              
365             The results contain the following:
366              
367             1. Bio::DOOP::Motif object
368             2. motif score
369             3. motif extended score
370             4. full hit sequence
371             5. alignment start position in the query sequence
372             6. alignment start position in the hit sequence
373              
374             @result = @{$mofext->get_results_from_file};
375              
376             =cut
377              
378             sub get_results_from_file {
379 0     0 1   my $self = shift;
380 0           my $filename = shift;
381              
382 0           my @mofext_res;
383             my $id;
384 0           my $score;
385 0           my $extscore;
386 0           my $fullhit;
387 0           my $querystart;
388 0           my $hitstart;
389 0           my $querysub;
390              
391 0 0         open RES,$filename or return(-1);
392 0           while(){
393 0           my $line = $_;
394 0           chomp($line);
395 0           ($id,$score,$extscore,$fullhit,$querystart,$hitstart) = split(/ /,$line);
396 0           my $motif = Bio::DOOP::Motif->new($self->{DB},$id);
397 0           push @mofext_res, [$motif,$score,$extscore,$querysub,$fullhit,$querystart,$hitstart];
398             }
399 0           close RES;
400 0           return(\@mofext_res);
401             }
402              
403             1;