Grep/Backend/Agrep.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Grep/Filter/FilterRemoveDuplicates.pm ↑ Summary Grep/Backend/GUUGle.pm »
1 #############################################################################
2 # $Author: markus $
3 # $Date: 2009-11-12 19:54:03 +0100 (Thu, 12 Nov 2009) $
4 # $Revision: 1848 $
5 #############################################################################
6  
7 package Bio::Grep::Backend::Agrep;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use version; our $VERSION = qv('0.10.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use autodie qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Bio::Grep::SearchResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶use Bio::Grep::Backend::BackendI;
Subroutine BEGIN: called CC=1 SCAR=2.6
19  
20 use base 'Bio::Grep::Backend::BackendI';
21  
22 use IO::String;
23  
24 sub new {
25 my $self = shift;
26 $self = $self->SUPER::new;
27 $self->_init;
28 return $self;
29 }
30  
31 sub _init {
32 my ($self) = @_;
33 my %all_features = $self->features;
34  
35 # agrep implementation features
36 # TODO
37 delete $all_features{FILTERS};
38 delete $all_features{UPSTREAM};
39 delete $all_features{DOWNSTREAM};
40 delete $all_features{SORT};
41 delete $all_features{QUERY_FILE};
42 delete $all_features{QUERY_LENGTH};
43 delete $all_features{DIRECT_AND_REV_COM};
44 delete $all_features{NATIVE_D_A_REV_COM};
45  
46 # not available in any agrep implemenation
47 delete $all_features{MAXHITS};
48 delete $all_features{GUMISMATCHES};
49 delete $all_features{REVCOM_DEFAULT};
50 delete $all_features{NATIVE_ALIGNMENTS};
51  
52 # no vmatch features
53 delete $all_features{EVALUE};
54 delete $all_features{PERCENT_IDENTITY};
55 delete $all_features{COMPLETE};
56 delete $all_features{ONLINE};
57 delete $all_features{SHOWDESC};
58 delete $all_features{QSPEEDUP};
59 delete $all_features{HXDROP};
60 delete $all_features{EXDROP};
61 $self->features(%all_features);
62 return;
63 }
64  
65 sub _is_tre_agrep {
66 my ($self) = @_;
67 my $command
68 = $self->_cat_path_filename( $self->settings->execpath, 'agrep' )
69 . ' -V';
70 my $version_info = $self->_execute_command_and_return_output($command);
71 if ( $version_info =~ m{TRE}xms ) {
72 return 1;
73 }
74 else {
75 return 0;
76 }
77 }
78  
79 sub is_tre_agrep {
80 my ($self) = @_;
81 return $self->{_tre_agrep};
82 }
83  
84 sub search {
85 my ( $self, $arg_ref ) = @_;
86 my $s = $self->settings;
87 $self->_check_search_settings($arg_ref);
88  
89 $self->{_tre_agrep} = $self->_is_tre_agrep;
90 if ( $self->is_tre_agrep ) {
91 $self->{_line_regex} = qr{\A(\d+)\-(\d+):(\d+):(.*)\z}xmso;
92 }
93 else {
94 $self->{_line_regex} = qr{\A(.*):(.*)\z}xmso;
95 }
96  
97 my $query = $self->_prepare_query();
98 $self->{_real_query} = $query;
99  
100 # now generate the command string
101 my $mm = 0;
102 if ( $s->mismatches_isset && $s->mismatches > 0 ) {
103 $mm = $s->mismatches;
104 }
105  
106 # make insertions and deletions too expensive
107 my $fuzzy = "-$mm -D" . ( $mm + 1 ) . ' -I' . ( $mm + 1 );
108  
109 if ( $s->editdistance_isset ) {
110 $fuzzy = q{-} . $s->editdistance;
111 }
112  
113 my $show_position = q{};
114  
115 if ( $self->is_tre_agrep ) {
116 $show_position = ' --show-position ';
117 }
118  
119 my $command
120 = $self->_cat_path_filename( $s->execpath, 'agrep' )
121 . $show_position . ' -i '
122 . $fuzzy . q{ }
123 . $query . q{ }
124 . $self->_cat_path_filename( $s->datapath, $s->database . '.dat' );
125  
126 if ( $ENV{BIOGREPDEBUG} ) {
127 warn $command . "\n";
128 }
129  
130 my $cmd_ok = $self->_execute_command($command);
131  
132 # delete temporary files
133 #unlink($tmp_query_file) if !$query_file;
134  
135 if ( !$cmd_ok ) {
136 $self->throw(
137 -class => 'Bio::Root::SystemException',
138 -text => "Agrep call failed. Command was:\n\t$command"
139 );
140 }
141  
142 my $indexfile = $s->datapath . q{/} . $s->database . '.idx';
143 $self->{'_idx'} = Bio::Index::Fasta->new($indexfile);
144  
145 $self->_load_mapping();
146 $self->_prepare_results;
147 return 1;
148 }
149  
150 sub get_databases {
151 my $self = shift;
152 return $self->_get_databases('.map');
153 }
154  
155 sub generate_database {
156 my ( $self, @args ) = @_;
157  
158 my %args = $self->_prepare_generate_database(@args);
159  
160 if ( defined $args{skip} ) {
161 return 0;
162 }
163  
164 my $filename = $args{filename};
165  
166 open my $DATFILE, '>', "$filename.dat";
167 open my $MAPFILE, '>', "$filename.map";
168 my $in = Bio::SeqIO->new( -file => $filename, -format => $args{format} );
169 my $id = 1;
170 while ( my $seq = $in->next_seq() ) {
171 print ${MAPFILE} $seq->id . "\n"
172 or $self->_cannot_print("$filename.dat");
173 print ${DATFILE} $id . q{:} . $seq->seq . "\n"
174 or $self->_cannot_print("$filename.map");
175 $id++;
176 }
177 close $DATFILE;
178 close $MAPFILE;
179 $self->_create_index_and_alphabet_file($filename);
180 return 1;
181 }
182  
183 sub _load_mapping {
184 my ($self) = @_;
185 my $s = $self->settings;
186 my $mapfile = $s->datapath . q{/} . $s->database . '.map';
187  
188 my %mapping = ();
189  
190 open my $MAPFILE, '<', $mapfile;
191  
192 my $i = 1;
193 while ( my $line = <$MAPFILE> ) {
194 chomp $line;
195 $mapping{ $i++ } = $line;
196 }
197 close $MAPFILE;
198  
199 $self->{_mapping} = \%mapping;
200 return;
201 }
202  
203 sub _parse_next_res {
204 my $self = shift;
205 my $s = $self->settings;
206 my $query = $self->{_real_query};
207  
208 my $FH = $self->_output_fh;
209 LINE:
210 while ( my $line = <$FH> ) {
211 chomp $line;
212  
213 #warn $line;
214 my ($sequence_id, $sequence, $subject_begin, $subject_end,
215 $subject_seq, $upstream_seq, $downstream_seq
216 );
217 my (@ret) = $line =~ $self->{_line_regex};
218  
219 if ( $self->is_tre_agrep ) {
220 ( $subject_begin, $subject_end, $sequence_id, $sequence ) = @ret;
221 my $pl = 1 + length $sequence_id;
222 $subject_begin -= $pl;
223 $subject_end -= $pl;
224 }
225 else {
226 ( $sequence_id, $sequence ) = @ret;
227 $subject_begin = 0;
228 if ( !defined $sequence ) {
229 $self->warn(
230 "Truncated record. Record is:\n$line\n\nSkipping hit.");
231 next LINE;
232 }
233 $subject_end = length $sequence;
234 $subject_seq = $sequence;
235 }
236 my $id = $self->{_mapping}->{$sequence_id};
237  
238 my $seq_hit = $self->{_idx}->fetch($id);
239  
240 assert( defined $seq_hit, "Found $sequence_id:$id in Bio::Index" )
241 if DEBUG;
242  
243 # take complete sequence as subject sequence for standard agrep
244 $subject_seq = $seq_hit->seq;
245  
246 my $seq_query = Bio::LocatableSeq->new(
247 -id => $self->{_query_obj}->id,
248 -desc => $self->{_query_obj}->desc,
249 -seq => $self->{_query_obj}->seq,
250 -start => 1,
251 -end => length $query,
252 );
253  
254 my %begin_end;
255  
256 if ( $self->is_tre_agrep ) {
257  
258 # for TRE agrep, get regions
259 ( $upstream_seq, $subject_seq, $downstream_seq )
260 = $self->_parse_regions(
261 { complete_seq => $subject_seq,
262 subject_begin => $subject_begin,
263 subject_end => $subject_end,
264 }
265 );
266 $seq_hit->seq( $upstream_seq . $subject_seq . $downstream_seq );
267 %begin_end = (
268 begin => length($upstream_seq),
269 end => length( $upstream_seq . $subject_seq ),
270 );
271 }
272  
273 my $seq_a = Bio::LocatableSeq->new(
274 -id => 'Subject',
275 -seq => $subject_seq,
276 -start => $subject_begin + 1, # starts at 1, not 0
277 -end => $subject_end,
278 );
279  
280 my $alignment;
281  
282 if ( !$s->no_alignments ) {
283 $alignment = $self->_get_alignment( $seq_a, $seq_query, );
284 }
285  
286 my $res = Bio::Grep::SearchResult->new(
287 { sequence => $seq_hit,
288 alignment => $alignment,
289 sequence_id => $seq_hit->id,
290 remark => q{},
291 query => $self->{_query_obj},
292 %begin_end,
293 }
294 );
295  
296 # agrep does not support multiple queries yet
297 return $res;
298 }
299 $self->_delete_output();
300 return 0;
301 }
302  
303 sub get_sequences {
304 my ( $self, $seqid ) = @_;
305 return $self->_get_sequences_from_bio_index($seqid);
306 }
307  
308 sub available_sort_modes {
309 return ();
310 }
311 1; # Magic true value required at end of module
312 __END__
« Grep/Filter/FilterRemoveDuplicates.pm ↑ Summary Grep/Backend/GUUGle.pm »