blib/lib/Bio/Gonzales/Domain/Identification/HMMER.pm untested

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 283 CC 21 SCAR 47.5
CC
21
Cov
0%
identify
306.0
_transform_hmm_hits
6.0
_update_sequence_mark
6.0
CRAP 115.0
1 #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2  
3 package Bio::Gonzales::Domain::Identification::HMMER;
4  
5 use Mouse;
6 use File::stat;
7  
8 use warnings;
9 use strict;
10 use Carp;
11 use Bio::Gonzales::SearchIO::HMMResult;
12 use File::Spec;
13 use Path::Class;
14 use Data::Dumper;
15 use List::MoreUtils qw/uniq any/;
16 use Bio::Gonzales::Domain::Identification::HMMER::SeqMarks;
17 use Bio::SeqIO;
18 use Bio::Gonzales::PrimarySeqIX;
19  
20 use 5.010;
21 our $VERSION = '0.058'; # VERSION
22  
23 has 'profile_db' => ( is => 'rw', required => 1 );
24 has 'result_dir' => ( is => 'rw', required => 1 );
25 has 'options' => ( is => 'rw', default => sub { [] } );
26  
27 has '_intermediate_result_files' => ( is => 'rw', default => sub { {} } );
28 has '_intermediate_result_id' => ( is => 'bare', default => 0 );
29  
30 has 'domain_spanning_region_file' => ( is => 'rw' );
31 has 'domain_spanning_region_masked_file' => ( is => 'rw' );
32 has 'domains_masked_inverted_file' => ( is => 'rw' );
33 has 'domains_masked_file' => ( is => 'rw' );
34 has 'whole_sequence_file' => ( is => 'rw' );
35 has 'domains_notfound_file' => (
36 is => 'rw',
37 lazy => 1,
38 default => sub { $_[0]->_catmyfile('not_found_domains.txt') }
39 );
40 has 'discovered_cache_file' => (
41 is => 'rw',
42 lazy => 1,
43 default => sub { $_[0]->_catmyfile('discovered.cache') }
44 );
45  
46 has 'from_cache' => ( is => 'rw', default => 0 );
47  
48 has 'domain_groups' => ( is => 'rw', default => sub { [] } );
49  
50 =head1 NAME
51  
52 Bio::Gonzales::Util::FunCon::Domains::Identification::HMMER - Identify Protein Domains with HMMER
53  
54 =head1 SYNOPSIS
55  
56 use Bio::Gonzales::Util::FunCon::Domains::Identification::HMMER;
57 my $idfy =Bio::Gonzales::Util::FunCon::Domains::Identification::HMMER->new({ domain_ids => [ 'id1', .., 'idn'], hmm_query_file => 'path/to/hmm/db'});
58  
59 =head1 DESCRIPTION
60  
61 =head1 METHODS
62  
63 =cut
64  
65 =head2 BUILD
66  
67 standard constructor addition
68  
69 =cut
70  
71 #sub BUILD {
72 #}
73  
74 =head2 $i->identify
75  
76 Starts up hmmsearch and identifies putative domains.
77  
78 Returns an array of hashes of the structure:
79  
80 [
81 {
82 file_name => 'file_name',
83 domain_id => 'id',
84 protein_id => 'id',
85 from => x,
86 to => y
87 },
88 ..
89 ]
90  
91 =cut
92  
93 sub _run_hmmsearch {
94 my ( $self, $sequence_file ) = @_;
95  
96 my $im_file
97 = $self->_gen_intermediate_result_name( 'hmmsearch_' . _basename_no_suffix($sequence_file) . '.result' );
98 open my $hmmsearch, '-|', 'hmmsearch', '--noali', @{ $self->options },
99 '-o', $im_file, $self->profile_db, $sequence_file
100 or croak "Can't open filehandle: $!";
101 return $im_file;
102 }
103  
104 sub _basename_no_suffix {
105 my ($file) = @_;
106 ( my $basename_no_suffix = file($file)->basename ) =~ s/\.\w+?$//;
107 return $basename_no_suffix;
108 }
109  
110 sub _gen_intermediate_result_name {
111 my ( $self, $suffix ) = @_;
112  
113 return File::Spec->catfile( $self->result_dir, $self->{_intermediate_result_id}++ . "_" . $suffix );
114 }
115  
116 sub _catmyfile {
117 my ( $self, $filename ) = @_;
118  
119 return File::Spec->catfile( $self->result_dir, $filename );
120 }
121  
122 =head2 $i->unlink_destination_files
123  
124 Deletes all destination files (the ones required in the constructor, except the cache file
125  
126 =cut
127  
128 =head2 _transform_hmm_hits
129  
130 Transforms the best hits result from
131 Bio::Gonzales::Util::SearchIO::HMMResult->get_best_hits to a more accessible structure
132 and determines the maximum possible spanning region. This function also builds
133 up a cache of sequence-id_size, found-domain and position cache for faster access
134 later on
135  
136 =cut
137  
138 sub _transform_hmm_hits {
139 my ( $self, $best_hits, $sequence_file ) = @_;
140  
141 open my $cache, '>>', $self->discovered_cache_file
142 or croak "Can't open filehandle: $!";
143  
144 my %result_for_sequence;
145 for my $k ( @{$best_hits} ) {
146  
147 #write to cache
148 say $cache $self->_create_cache_string( $sequence_file, $k )
149 unless ( $self->from_cache );
150  
151 #find maximum possible spanning region of a group in one sequence
152 $result_for_sequence{ $k->{seq_id} } //= Bio::Gonzales::Util::FunCon::Domains::Identification::HMMER::SeqMarks->new(
153 num_marks => scalar @{ $self->domain_groups } );
154 $self->_update_sequence_mark( $result_for_sequence{ $k->{seq_id} }, $k );
155 }
156 $cache->close;
157  
158 return \%result_for_sequence;
159 }
160  
161 sub _create_cache_string {
162 my ( $self, $sequence_file, $k ) = @_;
163  
164 return join "\t",
165 (
166 _basename_no_suffix($sequence_file) . "_" . stat($sequence_file)->size,
167 $k->{seq_id}, $k->{hmm_acc}, $k->{hmm_score}, $k->{from}, $k->{to}
168 );
169 }
170  
171 sub _update_sequence_mark {
172 my ( $self, $seq_result, $best_hit ) = @_;
173  
174 my @domain_groups = @{ $self->domain_groups };
175  
176 for ( my $i = 0; $i < @domain_groups; $i++ ) {
177 $seq_result->update_mark( $i, $best_hit->{from}, $best_hit->{to} )
178 if (
179 any { $best_hit->{hmm_acc} eq $_ }
180 keys %{ $domain_groups[$i] }
181 );
182 }
183 }
184  
185 sub _get_cached_hits {
186 my ( $self, $sequence_file ) = @_;
187  
188 my @best_hits;
189  
190 my $seq_file_id = _basename_no_suffix($sequence_file) . "_" . stat($sequence_file)->size;
191  
192 open my $cache, '<', $self->discovered_cache_file
193 or croak "Can't open filehandle: $!";
194 while ( my $l = <$cache> ) {
195 my @rows = split /\t/, $l;
196 push @best_hits,
197 {
198 seq_id => $rows[1],
199 hmm_acc => $rows[2],
200 hmm_score => $rows[3],
201 from => $rows[4],
202 to => $rows[5]
203 }
204 if ( $rows[0] eq $seq_file_id );
205 }
206 return \@best_hits;
207 }
208  
209 sub identify {
210 my ( $self, $sequence_file, $tag ) = @_;
211  
212 #run hmmsearch and get the best hits for each domain and seq_id
213  
214 my $best_hits;
215 unless ( $self->from_cache ) {
216 $best_hits
217 = Bio::Gonzales::Util::SearchIO::HMMResult->new( file => $self->_run_hmmsearch($sequence_file) )->get_best_hits();
218  
219 } elsif ( -f $self->discovered_cache_file ) {
220 $best_hits = $self->_get_cached_hits($sequence_file);
221 } else {
222 croak "Cache file " . $self->discovered_cache_file . " not found";
223 }
224  
225 #use standard groups if attribute not set, one group with all domains
226 unless ( @{ $self->domain_groups } > 0 ) {
227 $self->_create_standard_domain_groups($best_hits);
228 }
229  
230 my $result_for_sequence = $self->_transform_hmm_hits( $best_hits, $sequence_file );
231  
232 #open input sequence file
233 my $snf2 = Bio::SeqIO->new(
234 -format => 'fasta',
235 -file => $sequence_file,
236 );
237  
238 #open file for spanning domains, if set
239 my $domain_spanning_region;
240 $domain_spanning_region = Bio::SeqIO->new(
241 -format => 'fasta',
242 -file => ">>" . $self->domain_spanning_region_file,
243 ) if ( $self->domain_spanning_region_file );
244  
245 my $domain_spanning_region_masked;
246 $domain_spanning_region_masked = Bio::SeqIO->new(
247 -format => 'fasta',
248 -file => ">>" . $self->domain_spanning_region_masked_file,
249 ) if ( $self->domain_spanning_region_masked_file );
250  
251 #open file for masked spanning domains, if set
252 my $domains_masked_inverted;
253 $domains_masked_inverted = Bio::SeqIO->new(
254 -format => 'fasta',
255 -file => ">>" . $self->domains_masked_inverted_file,
256 ) if ( $self->domains_masked_inverted_file );
257  
258 #open file for masked spanning domains, if set
259 my $domains_masked;
260 $domains_masked = Bio::SeqIO->new(
261 -format => 'fasta',
262 -file => ">>" . $self->domains_masked_file,
263 ) if ( $self->domains_masked_file );
264  
265 #open file for complete sequence, if set
266 my $whole_sequence;
267 $whole_sequence = Bio::SeqIO->new(
268 -format => 'fasta',
269 -file => ">>" . $self->whole_sequence_file,
270 ) if ( $self->whole_sequence_file );
271  
272 #store some kind of found/notfound/how many not found
273 #information in a file
274 open my $not_found, '>>', $self->domains_notfound_file
275 or croak "Can't open filehandle: $!";
276  
277 open my $seq_ids_fh, '>>', $self->_catmyfile('hmmmer_result_seqids.tsv');
278 #run through sequences file to extract sequences with hits
279 while ( my $so = $snf2->next_seq ) {
280  
281 $so->desc( "(" . $tag . ") " . $so->desc ) if ($tag);
282 #has the sequence has at least in every group/mark
283 if ( exists $result_for_sequence->{ $so->display_id() }
284 && $result_for_sequence->{ $so->display_id() }->hit_in_every_mark )
285 {
286 my $r = $result_for_sequence->{ $so->display_id() };
287 $r->extend(50);
288 $r->boundaries( [ 1, $so->length ] );
289  
290 say $seq_ids_fh join("\t", $so->display_id, @{ $r->spanning_region }{ 'from', 'to' } );
291 #write, if set
292 if ($whole_sequence) {
293 $whole_sequence->write_seq($so);
294 }
295  
296 if ($domain_spanning_region) {
297 #do we have an ambigous id?
298 #FIXME
299 #say "Non-Unique ID: ", $so->display_id(), " ", $so->length
300 #if ( $so->length < $r->[1] );
301 $domain_spanning_region->write_seq( $so->trunc( @{ $r->spanning_region }{ 'from', 'to' } ) );
302 }
303 $r->clear_extend;
304 if ($domain_spanning_region_masked) {
305 #take care, mask changes the $so object
306 $domain_spanning_region_masked->write_seq(
307 $so->clone->mask( @{ $r->spanning_region }{ 'from', 'to' } )->trunc_masked_ends );
308 }
309 if ($domains_masked) {
310 #take care, mask changes the $so object
311  
312 my $tmp_so = $so->clone;
313 for my $m ( @{ $r->marks } ) {
314 $tmp_so->mask( $m->{from}, $m->{to} );
315 }
316 $domains_masked->write_seq( $tmp_so->trunc_masked_ends );
317 }
318 if ($domains_masked_inverted) {
319 #take care, mask changes the $so object
320  
321 for my $m ( @{ $r->inverted_marks } ) {
322 $so->mask( $m->{from}, $m->{to} );
323 }
324 $domains_masked_inverted->write_seq( $so->trunc_masked_ends );
325 }
326 #no domains found in sequence
327 } elsif ( exists $result_for_sequence->{ $so->display_id() }
328 && $result_for_sequence->{ $so->display_id() }->num_marks_hit > 0 )
329 {
330 #print number of found domains to notfound,
331 #if not all domains occurr in sequence
332 say {$not_found} $so->display_id()
333 . "\tfound in "
334 . $result_for_sequence->{ $so->display_id }->num_marks_hit . '/'
335 . scalar( @{ $self->domain_groups } )
336 . ' groups';
337 } else {
338 say {$not_found} $so->display_id() . "\treally nothing found";
339  
340 }
341 }
342 $seq_ids_fh->close;
343 $not_found->close;
344 }
345  
346 # for every group in array, grep for accession
347 sub _is_in_all_domain_groups {
348 my ( $self, $hmm_hits_acc ) = @_;
349  
350 #find the group
351 my $count_groups = 0;
352 for my $g ( @{ $self->domain_groups } ) {
353 $count_groups++
354 if ( any { $g->{$_} } @{$hmm_hits_acc} );
355  
356 }
357 return @{ $self->domain_groups } == $count_groups;
358 }
359  
360 sub _save_to_cache {
361 my ( $self, $cache_fh, $d ) = @_;
362  
363 }
364  
365 sub _create_standard_domain_groups {
366 my ( $self, $best_hits ) = @_;
367  
368 my %domains = map { $_->{hmm_acc} => 1 } @{$best_hits};
369  
370 $self->domain_groups(
371 [
372 map {
373 { $_ => 1 }
374 } keys %domains
375 ]
376 );
377 }
378  
379 1;
380  
381 __END__
382  
383 =head1 SEE ALSO
384 =head1 AUTHOR
385  
386 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
387  
388 =cut