blib/lib/obogaf/parser.pm

filter: Statementstmt 100% 196 / 196 Branchbran 98.0% 100 / 102 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 99.3% 318 / 320 CC 58 SCAR 25.9
CC
58
Cov
99%
map_OBOterm_between_release
24.0
build_edges
12.0
make_stat
7.0
CRAP 13.4
↑ Summary
1 package obogaf::parser;
2  
3 6 require 5.006;
4 6 our $VERSION= '1.272';
5 6 $VERSION= eval $VERSION;
6  
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub build_edges{
14 8 ▶ my ($obofile)= @_;
Subroutine build_edges: called CC=12 SCAR=24.8
Pod documented
15 8 my ($namespace, $idname, $isname, $pofname, $source, $destination, $pof, $res);
16 8 ▶ if($obofile=~/.obo$/){ open FH, "<", "$obofile" or die "cannot open $obofile. $!.\n"; } else { die "cannot open $obofile. The extension must be obo.\n"; }
Branch: 2 branches
BranchTrueFalseTotal
if ($obofile =~ /.obo$/) { } 7 1 8
open FH, "<", "$obofile" or die "cannot open $obofile. $!.\n" 1 6 7
17 6 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 275 6 281
18 275 chomp;
19 275 ▶ next if $_=~/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\s*$/ 12 263 275
20 263 ▶ if($_=~/^namespace:\s+(\D+)/){
Branch: 5 branches
BranchTrueFalseTotal
if ($_ =~ /^namespace:\s+(\D+)/) { } 5 258 263
elsif ($_ =~ /^name:\s+(.+)/) { } 6 252 258
elsif ($_ =~ /^id:\s+(\D+\d+)/) { } 6 246 252
elsif ($_ =~ /^is_a:\s+(\D+\d+)/) { } 6 240 246
elsif ($_ =~ /^relationship: part_of\s+(\D+\d+)/) { } 6 234 240
21 5 $namespace=$1;
22 }elsif($_=~/^name:\s+(.+)/){
23 6 $idname=$1;
24 }elsif($_=~/^id:\s+(\D+\d+)/){
25 6 $destination=$1;
26 }elsif($_=~/^is_a:\s+(\D+\d+)/){
27 6 $source=$1;
28 6 ($isname)= ($_=~/!\s+(.+)/);
29 6 ▶ if(defined $namespace){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) { } 5 1 6
30 5 $res .= "$namespace\t$source\t$destination\t$isname\t$idname\tis-a\n";
31 }else{
32 1 $res .= "$source\t$destination\t$isname\t$idname\tis-a\n";
33 }
34 }elsif($_=~/^relationship: part_of\s+(\D+\d+)/){
35 6 $pof=$1;
36 6 ($pofname)= ($_=~/!\s+(.+)/);
37 6 ▶ if(defined $namespace){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) { } 5 1 6
38 5 $res .= "$namespace\t$pof\t$destination\t$pofname\t$idname\tpart-of\n";
39 }else{
40 1 $res .= "$pof\t$destination\t$pofname\t$idname\tpart-of\n";
41 }
42 }
43 }
44 6 close FH;
45 6 return \$res;
46 }
47  
48 sub build_subonto{
49 6 ▶ my ($edgesfile, $namespace)= @_;
Subroutine build_subonto: called CC=6 SCAR=17.9
Pod documented
50 6 my ($res, %checker);
51 6 ▶ open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n" 1 5 6
52 5 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 11 5 16
53 11 ▶ next if $_=~/^[!,#]|^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^[!,#]|^\s*$/ 1 10 11
54 10 my @vals= split(/\t/, $_);
55 10 $checker{$vals[0]}=1;
56 10 ▶ if($vals[0] eq $namespace){ $res .= join("\t", @vals[1..$#vals]); }
Branch: 1 branch
BranchTrueFalseTotal
if ($vals[0] eq $namespace) 8 2 10
57 }
58 5 close FH;
59 5 ▶ unless(exists($checker{$namespace})){die "$edgesfile does not include $namespace or $namespace is not in the first column of $edgesfile.\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $checker{$namespace}) 1 4 5
60 4 return \$res;
61 }
62  
63 sub make_stat{
64 5 ▶ my ($edgesfile, $parentIndex, $childIndex)= @_;
Subroutine make_stat: called CC=7 SCAR=19.5
Pod documented
65 5 my (%indeg, %outdeg, %deg, $ed, $nd, $mindeg, $maxdeg, $medeg, $avgdeg, $den, $scc, $resdeg, $stat, $res);
66 ## create graph
67 5 my $g= Graph->new(directed => 1);
68 5 ▶ open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n" 1 4 5
69 4 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 7 4 11
70 7 chomp;
71 7 my @vals= split(/\t/,$_);
72 7 $g->add_edge($vals[$parentIndex], $vals[$childIndex]);
73 }
74 4 close FH;
75 ## compute indegree/outdegree/degree
76 4 my @V= $g->vertices;
77 4 foreach my $nd (@V){
78 11 my $i= $g->in_degree($nd);
79 11 my $o= $g->out_degree($nd);
80 11 my $d= $i+$o;
81 11 $indeg{$nd}=$i;
82 11 $outdeg{$nd}=$o;
83 11 $deg{$nd}=$d;
84 }
85 4 ▶ foreach my $node (sort{$deg{$b}<=>$deg{$a} or ($a cmp $b)} keys %deg){ $resdeg .= "$node\t$deg{$node}\t$indeg{$node}\t$outdeg{$node}\n"; }
Branch: 1 branch
BranchTrueFalseTotal
$deg{$b} <=> $deg{$a} or $a cmp $b 4 5 9
86 ## compute: median/max/min degree
87 4 my @sortdeg= sort{$a<=>$b} values (%deg);
88 4 my $len= $#sortdeg+1;
89 4 my $mid = int $len/2;
90 4 ▶ if($len % 2){ $medeg = $sortdeg[$mid]; }else{ $medeg = ( $sortdeg[$mid-1] + $sortdeg[$mid] ) / 2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($len % 2) { } 3 1 4
91 4 $medeg= sprintf("%.4f", $medeg);
92 4 $mindeg= $sortdeg[0];
93 4 $maxdeg= $sortdeg[$#sortdeg];
94 ## compute number of nodes and edges
95 4 $ed= $g->edges;
96 4 $nd= $g->vertices;
97 ## compute average degree and density
98 4 $avgdeg= $ed/$nd;
99 4 $den= $ed / ( $nd * ($nd -1) );
100 4 $avgdeg= sprintf("%.4f", $avgdeg);
101 4 $den= sprintf("%.4e", $den);
102 ## return stat
103 4 $stat .= "nodes: $nd\nedges: $ed\nmax degree: $maxdeg\nmin degree: $mindeg\n";
104 4 $stat .= "median degree: $medeg\naverage degree: $avgdeg\ndensity: $den\n";
105 4 $res= "#oboterm <tab> degree <tab> indegree <tab> outdegree\n".$resdeg."\n"."~summary stat~\n".$stat;
106 4 return $res;
107 }
108  
109 sub get_parents_or_children_list{
110 10 ▶ my ($edgesfile, $parentIndex, $childIndex, $chdORpar)= @_;
Subroutine get_parents_or_children_list: called CC=7 SCAR=19.5
Pod documented
111 10 my (%nodelist);
112 10 ▶ if($chdORpar ne "parents" && $chdORpar ne "children"){ die "$chdORpar can be 'parents' or 'children'.\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($chdORpar ne "parents" and $chdORpar ne "children") 2 8 10
Condition100%3/3
$chdORpar ne "parents" and $chdORpar ne "children"
!ll&&!rl&&r
442
MC/DC100%2/2
$chdORpar ne "parents" and $chdORpar ne "children"
$chdORpar ne "parents" $chdORpar ne "children"
Truth table100%3/3
A and B
A= $chdORpar ne "parents"B= $chdORpar ne "children"
ABresult
0X0
100
111
113 8 ▶ open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", $edgesfile or die "cannot open $edgesfile. $!.\n" 2 6 8
114 6 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 12 6 18
115 12 chomp;
116 12 my @vals= split(/\t/,$_);
117 12 ▶ if($chdORpar eq "parents"){
Branch: 1 branch
BranchTrueFalseTotal
if ($chdORpar eq 'parents') { } 6 6 12
118 6 $nodelist{$vals[$childIndex]} .= $vals[$parentIndex]."|";
119 }else{
120 6 $nodelist{$vals[$parentIndex]} .= $vals[$childIndex]."|";
121 }
122 }
123 6 close FH;
124 6 foreach my $term (keys %nodelist){ chop $nodelist{$term}; }
125 6 return \%nodelist;
126 }
127  
128 sub gene2biofun{
129 6 ▶ my ($annfile, $geneIndex, $classIndex)= @_;
Subroutine gene2biofun: called CC=7 SCAR=19.5
Pod documented
130 6 my (%gene2biofun, @genes, @biofun, $stat)= ();
131 6 my ($sample, $oboterm)= (0)x2;
132 6 ▶ if ($annfile =~ /.gz$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($annfile =~ /.gz$/) { } 2 4 6
133 2 ▶ open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n" 1 1 2
134 }else{
135 4 ▶ open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n" 1 3 4
136 }
137 4 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 27 4 31
138 27 ▶ next if $_=~/^[!,#]|^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^[!,#]|^\s*$/ 3 24 27
139 24 chomp;
140 24 my @vals=split(/\t/,$_);
141 24 push(@genes, $vals[$geneIndex]);
142 24 push(@biofun, $vals[$classIndex]);
143 24 $gene2biofun{$vals[$geneIndex]} .= $vals[$classIndex]."|";
144 }
145 4 close FH;
146 4 foreach my $gene (keys %gene2biofun){ chop $gene2biofun{$gene}; }
147 4 my %seen=();
148 4 my @uniqgenes= grep{!$seen{$_}++} @genes;
149 4 $sample= scalar(@uniqgenes);
150 4 undef %seen;
151 4 my @uniqpbiofun= grep{!$seen{$_}++} @biofun;
152 4 $oboterm= scalar(@uniqpbiofun);
153 4 $stat .= "genes: $sample\nontology terms: $oboterm\n";
154 4 return (\%gene2biofun, \$stat);
155 }
156  
157 sub map_OBOterm_between_release{
158 9 ▶ my ($obofile, $annfile, $classIndex)= @_;
Subroutine map_OBOterm_between_release: called CC=24 SCAR=31.8
Pod documented
159 9 my (%altid, %oldclass, %old2new, $header, $id, $fln, $pair, $stat, $pstat);
160 9 my ($alt, $classes, $seen, $unseen)= (0)x4;
161 ## step 0: pairing altid_2_id (key: alt_id)
162 9 ▶ if($obofile=~/.obo$/){ open FH, "<", "$obofile" or die "cannot open $obofile. $!.\n"; } else { die "cannot open $obofile. The extension must be obo.\n"; }
Branch: 2 branches
BranchTrueFalseTotal
if ($obofile =~ /.obo$/) { } 8 1 9
open FH, "<", "$obofile" or die "cannot open $obofile. $!.\n" 1 7 8
163 7 ▶ while (<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 322 7 329
164 322 chomp;
165 322 ▶ next if $_=~/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\s*$/ 14 308 322
166 308 ▶ if($_=~/^id:\s+(\D+\d+)/){ $id=$1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^id:\s+(\D+\d+)/) 7 301 308
167 308 ▶ if($_=~/^alt_id:\s+(\D+\d+)/){ $altid{$1}=$id; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^alt_id:\s+(\D+\d+)/) 49 259 308
168 }
169 7 close FH;
170 7 $alt= keys(%altid);
171 # step 1: storing old ontology terms in a hash
172 7 ▶ if ($annfile =~ /.gz$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($annfile =~ /.gz$/) { } 2 5 7
173 2 ▶ open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n" 1 1 2
174 }else{
175 5 ▶ open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n" 1 4 5
176 }
177 5 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 28 5 33
178 28 chomp;
179 28 ▶ if($_=~/^[!,#]|^\s*$/){ $header .= "$_\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^[!,#]|^\s*$/) 2 26 28
180 28 ▶ next if $_=~/^[!,#]|^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^[!,#]|^\s*$/ 2 26 28
181 26 my @vals=split(/\t/,$_);
182 26 $oldclass{$vals[$classIndex]}=$vals[$classIndex];
183 }
184 5 close FH;
185 5 $classes= keys(%oldclass);
186 ## step 2: mapping old GO terms to the new one using *alt_id* as key
187 5 my $tmp= "";
188 5 foreach my $k (sort{$a cmp $b} keys(%altid)){
189 35 ▶ if($oldclass{$k}){
Branch: 1 branch
BranchTrueFalseTotal
if ($oldclass{$k}) { } 8 27 35
190 8 $old2new{$k}=$altid{$oldclass{$k}}; ## pairing
191 8 $seen++;
192 8 $tmp= $altid{$oldclass{$k}};
193 }else{
194 27 $tmp= "unseen";
195 27 $unseen++;
196 }
197 35 ▶ if($tmp ne "unseen"){
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp ne "unseen") 8 27 35
198 8 $pair .= "$k\t$altid{$oldclass{$k}}\n";
199 }
200 }
201 ## step 3: substitute ALT-ID with the updated ID, then the annotation file is returned.
202 5 ▶ if ($annfile =~ /.gz$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($annfile =~ /.gz$/) { } 1 4 5
203 1 ▶ open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<:gzip", $annfile or die "cannot open $annfile. $!.\n" 0 1 1
204 }else{
205 4 ▶ open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<", "$annfile" or die "cannot open $annfile. $!.\n" 0 4 4
206 }
207 5 ▶ while(<FH>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 28 5 33
208 28 chomp;
209 28 ▶ next if $_=~/^[!,#]|^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^[!,#]|^\s*$/ 2 26 28
210 26 my @vals= split(/\t/, $_);
211 26 my $oboterm= $vals[$classIndex];
212 26 ▶ if($old2new{$oboterm}){
Branch: 1 branch
BranchTrueFalseTotal
if ($old2new{$oboterm}) { } 16 10 26
213 16 $oboterm= $old2new{$oboterm};
214 16 $_=~ s/$vals[$classIndex]/$oboterm/g;
215 16 $fln .= "$_\n";
216 }else{
217 10 $fln .= "$_\n";
218 }
219 }
220 5 close FH;
221 5 ▶ if(defined $header){$fln = $header.$fln;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 1 4 5
222 ## print mapping stat
223 5 $stat .= "Tot. ontology terms:\t$classes\nTot. altID:\t$alt\nTot. altID seen:\t$seen\nTot. altID unseen:\t$unseen\n";
224 5 ▶ unless(not defined $pair){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pair) 4 1 5
225 4 $pstat .= "#alt-id <tab> id\n$pair\n$stat";
226 4 return (\$fln, \$pstat);
227 }
228 1 return (\$fln, \$stat);
229 }
230  
231 6 1;
232  
233 __END__
↑ Summary