blib/lib/Bio/Gonzales/Domain/Group.pm

filter: Statementstmt 88.8% 111 / 125 Branchbran 76.9% 20 / 26 Conditioncond 25.0% 2 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 85.0% 17 / 20 Podpod 14.2% 1 / 7 totaltotal 79.5% 152 / 191 CC 28 SCAR 21.6
CC
28
Cov
84%
_pick_best_domain_hits
42.0
filter_hits
8.0
filter_ids
5.0
CRAP 8.7
« blib/lib/Bio/Gonzales/Project/Functions.pm ↑ Summary blib/lib/Bio/Gonzales/Phylo/Util.pm »
1 package Bio::Gonzales::Domain::Group;
2  
3 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::Gonzales::Feat::IO::GFF3;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::MoreUtils qw/uniq first_value any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.058'; # VERSION
15  
16 #my $q = Bio::Gonzales::Search::IO::HMMER3->new( file => $out )->parse;
17 1 has search_result => ( is => 'rw', required => 1 );
18 1 has required_domains => ( is => 'rw', required => 1 );
19 3 ▶has forbidden_domains => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 has can_have_additional_domains => ( is => 'rw', default => 1 );
21 1 has group_idcs => ( is => 'rw', lazy_build => 1 );
22 1 has only_best_hit => ( is => 'rw' );
23  
24 ▶sub add_required {
Subroutine add_required: not called CC=1 SCAR=0.0
Pod undocumented
25  
26 }
27  
28 ▶sub add_forbidden {
Subroutine add_forbidden: not called CC=1 SCAR=0.0
Pod undocumented
29  
30 }
31  
32 sub BUILD {
33 4 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
34 4 my $r = $self->domain_list( $self->search_result );
35  
36 $self->required_domains( _match_domain_names( $r, $self->required_domains ) )
37 4 ▶ if ( @{ $self->required_domains } > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->required_domains;} > 0 4 0 4
38 $self->forbidden_domains( _match_domain_names( $r, $self->forbidden_domains ) )
39 4 ▶ if ( @{ $self->forbidden_domains } > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->forbidden_domains;} > 0 1 3 4
40  
41 4 return $self;
42 }
43  
44 sub domain_list {
45 6 ▶ my $r = pop;
Subroutine domain_list: called CC=1 SCAR=0.0
Pod undocumented
46  
47 6 return [ keys %$r ];
48 }
49  
50 sub _match_domain_names {
51 5 ▶ my ( $reference, $query ) = @_;
Subroutine _match_domain_names: called CC=4 SCAR=13.9
52  
53 5 my @gresult;
54 5 for my $q (@$query) {
55 6 my @result;
56 6 for my $e (@$q) {
57 8 push @result, grep {/$e/} @$reference;
58 }
59 6 ▶ push @gresult, \@result if (@result);
Branch: 1 branch
BranchTrueFalseTotal
if @result 6 0 6
60 }
61 5 return \@gresult;
62 }
63  
64 sub _build_group_idcs {
65 1 ▶ my ($self) = @_;
Subroutine _build_group_idcs: called CC=2 SCAR=6.9
66  
67 1 my $domain_groups = $self->required_domains;
68 1 my %group;
69 1 my $i = 0;
70 1 for my $g (@$domain_groups) {
71 2 map { $group{$_} = $i } @$g;
72 2 $i++;
73 }
74 1 return \%group;
75 }
76  
77 sub filter_hits {
78 1 ▶ my ($self) = @_;
Subroutine filter_hits: called CC=8 SCAR=20.8
Pod undocumented
79  
80 1 my $q = $self->search_result;
81 1 my $id_lookup = { map { $_ => 1 } @{ $self->filter_ids } };
82 1 my $group_idcs = $self->group_idcs;
83  
84 1 my @hit_col;
85 1 ▶ while ( my ( $domain_id, $result ) = each %$q ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$q) 4 1 5
86 4 ▶ next unless ( exists( $group_idcs->{$domain_id} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $group_idcs->{$domain_id} 1 3 4
87 3 ▶ while ( my ( $seq_id, $hits ) = each %$result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$result) 300 3 303
88 300 ▶ next if ( $id_lookup && !exists( $id_lookup->{$seq_id} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $id_lookup and not exists $id_lookup->{$seq_id} 190 110 300
Condition66%2/3
$id_lookup and not exists $id_lookup->{$seq_id}
!ll&&!rl&&r
0110190
MC/DC50%1/2
$id_lookup and not exists $id_lookup->{$seq_id}
$id_lookup not exists $id_lookup->{$seq_id}
Truth table66%2/3
A and not(B)
A= $id_lookupB= not exists $id_lookup->{$seq_id}
ABresult
0X0
100
111
89 110 for my $hit (@$hits) {
90 141 $hit->{domain_id} = $domain_id;
91 141 $hit->{seq_id} = $seq_id;
92 141 $hit->{group_id} = $group_idcs->{$domain_id};
93 141 push @hit_col, $hit;
94 }
95 }
96 }
97 1 ▶ return $self->_pick_best_domain_hits( \@hit_col )
Branch: 1 branch
BranchTrueFalseTotal
if $self->only_best_hit 0 1 1
98 if ( $self->only_best_hit );
99  
100 1 return \@hit_col;
101 }
102  
103 sub _pick_best_domain_hits {
104 0 ▶ my ( $self, $hit_list ) = @_;
Subroutine _pick_best_domain_hits: not called CC=6 SCAR=37.4
105  
106 0 my %group_collection;
107 0 for my $h (@$hit_list) {
108 0 my $id = $h->{seq_id} . "__//__" . $h->{group_id};
109 0 ▶ $group_collection{$id} //= [];
Condition0%0/2
$group_collection{$id} //= []
l!l
00
MC/DC0%0/1
$group_collection{$id} //= []
$group_collection{$id}
Truth table0%0/2
A
A= $group_collection{$id}
Aresult
11
00
110  
111 0 push @{ $group_collection{$id} }, $h;
112 }
113  
114 0 my @best_ones;
115 0 for my $h ( values %group_collection ) {
116 0 my $max;
117  
118 #find the domain with the maximal score
119 0 ▶ map { $max = $_ if ( !$max || $_->{score} > $max->{score} ) } @$h;
Branch: 1 branch
BranchTrueFalseTotal
if not $max or $_->{"score"} > $max->{"score"} 0 0 0
Condition0%0/3
not $max or $_->{"score"} > $max->{"score"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $max or $_->{"score"} > $max->{"score"}
not $max $_->{"score"} > $max->{"score"}
Truth table0%0/3
not(A) or B
A= not $maxB= $_->{"score"} > $max->{"score"}
ABresult
1X1
011
000
120 0 push @best_ones, $max;
121 }
122  
123 0 return \@best_ones;
124 }
125  
126 sub to_gff {
127 1 ▶ my ( $self, $dest ) = @_;
Subroutine to_gff: called CC=3 SCAR=11.0
Pod undocumented
128 1 my $hits = $self->filter_hits;
129  
130 1 my $gffout = Bio::Gonzales::Feat::IO::GFF3->new( file_or_fh => $dest, mode => '>' );
131  
132 1 my $gidcs = $self->group_idcs;
133 1 ▶ while ( my ( $d, $idx ) = each %$gidcs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$gidcs) 3 1 4
134 3 $gffout->write_comment(" Group $idx contains domain: $d");
135 }
136  
137 1 my $i = 0;
138 1 for my $max (@$hits) {
139  
140 $gffout->write_feat(
141 Bio::Gonzales::Feat->new(
142 seq_id => $max->{seq_id},
143 source => 'hmmer',
144 type => 'region',
145 start => $max->{env_from},
146 end => $max->{env_to},
147 strand => 0,
148 score => $max->{score},
149 attributes => {
150 ID => [ "hit_" . $i++ ],
151 domainID => [ $max->{domain_id} ],
152 141 groupID => [ $max->{group_id} ]
153 }
154 )
155 );
156 }
157  
158 1 $gffout->close;
159 }
160  
161 sub filter_ids {
162 4 ▶ my ($self) = @_;
Subroutine filter_ids: called CC=5 SCAR=16.1
Pod undocumented
163  
164 4 my $q = $self->search_result;
165 4 my $rgroups = $self->required_domains;
166 4 ▶ return unless ($rgroups);
Branch: 1 branch
BranchTrueFalseTotal
unless $rgroups 0 4 4
167  
168 4 my %id_occurrence;
169  
170 # select the ids for every group by...
171 4 for my $domain_synonyms (@$rgroups) {
172 6 my @ids;
173 # iterating through the domains ...
174 6 for my $domain_synonym (@$domain_synonyms) {
175 # and storing all ids in an group-wide id list
176 8 push @ids, grep { @{ $q->{$domain_synonym}{$_} } > 0 } keys %{ $q->{$domain_synonym} };
177 }
178  
179 # we need to eliminate duplicate ids (multiple synonyms per group)
180 # and increment the occurrence of the found ids
181 6 map { $id_occurrence{$_}++ } uniq @ids;
182  
183 }
184  
185 # if an id is in every group it occurred #num of rgroups times and is not forbidden, return it
186 4 ▶ return [ grep { $id_occurrence{$_} == @$rgroups && !$self->_is_forbidden($_) } keys %id_occurrence ];
Branch: 1 branch
BranchTrueFalseTotal
$id_occurrence{$_} == @$rgroups and not $self->_is_forbidden($_) 156 196 352
187 }
188  
189 sub _is_forbidden {
190 156 ▶ my ( $self, $id ) = @_;
Subroutine _is_forbidden: called CC=4 SCAR=13.9
191  
192 156 my $q = $self->search_result;
193 156 my $fgroups = $self->forbidden_domains;
194 156 for my $fg (@$fgroups) {
195 82 for my $fd (@$fg) {
196 90 ▶ if ( exists( $q->{$fd}{$id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $q->{$fd}{$id}) 74 16 90
197 74 return 1;
198 }
199 }
200 }
201 82 return;
202  
203 }
204 1 __PACKAGE__->meta->make_immutable();
« blib/lib/Bio/Gonzales/Project/Functions.pm ↑ Summary blib/lib/Bio/Gonzales/Phylo/Util.pm »