blib/script/gonz_gorel2dict.pl untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 166 CC 4 SCAR 18.3
CC
4
Cov
0%
get_parser
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.2
1 #!/usr/bin/env perl
2  
3 use warnings;
4 use strict;
5  
6 use Data::Dumper;
7 use Carp;
8  
9 use 5.010;
10  
11 use Bio::Gonzales::Util::Cerial;
12 use List::MoreUtils qw/uniq/;
13 use Bio::Gonzales::GO::Util qw/get_recursive_related_terms_by_types/;
14 use Data::Stag;
15  
16 use GO::Parser;
17 use GO::Model::Graph;
18 use Try::Tiny;
19  
20 use Getopt::Long::Descriptive;
21  
22 my ( $opt, $usage ) = describe_options(
23 '%c %o <obo file>',
24 [
25 "format" => hidden => {
26 one_of => [ [ "json|j" => "output in json format" ], [ "yaml|y" => "output in yaml format" ], ],
27 default => 'json'
28 }
29 ],
30 [
31 "relation" => hidden => {
32 one_of => [ [ "parent|p" => "get parents recursively" ], [ "child|c" => "get childs recursively" ], ],
33 default => 'parent'
34 }
35 ],
36 [ 'alt_id', 'also include alternative ids' ],
37 [ 'all_types', 'also include (negatively/positively) regulates parent/child relations' ],
38 [ 'syn', 'also include synonyms' ],
39 [ 'namespace|n=s', 'restrict to certain namespace' ],
40 [ 'verbose|v', "print extra stuff" ],
41 [ 'go_prefix=s', 'set the go prefix (default GO:)', { default => 'GO:' } ],
42 [ 'help', "print usage message and exit" ],
43 );
44  
45 print( $usage->text ), exit if $opt->help;
46  
47 my $go_prefix = $opt->go_prefix;
48 $go_prefix= qr/^$go_prefix/;
49 my $file = shift;
50 die "$file is no file" unless ( -f $file );
51  
52 my $errhandler = Data::Stag->getformathandler('xml');
53 $errhandler->fh( \*STDERR );
54  
55 my $parser = get_parser($file);
56 $parser->errhandler($errhandler);
57  
58 my %go;
59  
60 $parser->parse($file); # parse file -> objects
61 my $graph = $parser->handler->graph; # get L<GO::Model::Graph> object
62  
63 my @terms;
64 if ( $opt->namespace ) {
65 @terms = grep { $_->namespace eq $opt->namespace } @{ $graph->get_all_terms };
66 } else {
67 @terms = @{ $graph->get_all_terms };
68 }
69  
70 my $types = [qw/is_a part_of/];
71  
72 if ( $opt->all_types ) {
73 undef $types;
74 }
75  
76 my $relation = $opt->relation;
77 for my $t (@terms) {
78 if ( $t->acc !~ /$go_prefix/ ) {
79 say STDERR Dumper $t;
80 next;
81 }
82 my @rel_terms;
83 # GO::Model::Term
84 # GO::Model::Relationship
85 # GO::Model::Graph
86  
87 for my $rt ( @{ get_recursive_related_terms_by_types( $graph, $relation, $t->acc, $types ) } ) {
88 push @rel_terms, $rt->acc;
89 push @rel_terms, grep {/$go_prefix/} @{ $rt->alt_id_list } if ( $opt->alt_id );
90 push @rel_terms, grep {/$go_prefix/} @{ $rt->synonym_list } if ( $opt->syn );
91 }
92  
93 @rel_terms = uniq @rel_terms;
94  
95 $go{ $t->acc } = \@rel_terms;
96 for my $alt_id ( grep {/$go_prefix/} @{ $t->alt_id_list } ) {
97 # no alternative ids wanted
98 last unless ( $opt->alt_id );
99 $go{$alt_id} = \@rel_terms;
100 }
101  
102 for my $syn ( grep {/$go_prefix/} @{ $t->synonym_list } ) {
103 # no alternative ids wanted
104 last unless ( $opt->syn );
105 $go{$syn} = \@rel_terms;
106 }
107 }
108 $errhandler->finish;
109  
110 if ( $opt->format eq 'json' ) {
111 jspew( \*STDOUT, \%go );
112 } elsif ( $opt->format eq 'yaml' ) {
113 yspew( \*STDOUT, \%go );
114 }
115  
116 sub get_parser {
117 my $f = shift;
118  
119 my $fmt;
120 my $handler;
121 if ( $f =~ /\.obo$/ ) {
122 $fmt = 'obo_text';
123 $handler = 'obj';
124 } elsif ( $f =~ /\.xml$/ ) {
125 $fmt = 'obo_xml';
126 $handler = 'xml';
127 } elsif ( $f =~ /\.obo-xml$/ ) {
128 $fmt = 'obo_xml';
129 $handler = 'obj';
130 } else {
131 die "could not figure out format of obo file";
132 }
133 my $parser = GO::Parser->new( { handler => $handler, format => $fmt } );
134 #$parser->litemode(1);
135  
136 return $parser;
137 }