blib/script/cv2storable.pl untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 155 CC 16 SCAR 52.0
CC
16
Cov
0%
parse_obo
272.0
BEGIN
2.0
BEGIN
2.0
CRAP 182.0
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 use Storable qw/nstore_fd/;
7 use List::MoreUtils qw/any/;
8 use Getopt::Long;
9  
10 my ($fn_ms, $fn_mod, $fn_mi, $fn_ims, $fn_uo, $dump) = (undef) x 6;
11  
12 GetOptions(
13 'ms=s' => \$fn_ms,
14 'mi=s' => \$fn_mi,
15 'mod=s' => \$fn_mod,
16 'ims=s' => \$fn_ims,
17 'uo=s' => \$fn_uo,
18 'dump' => \$dump,
19 );
20  
21 my $terms;
22 my %used;
23  
24 # NOTE: if you add to or change these overrides, be sure to update the
25 # corresponding documentation in the MS::CV POD
26 my $overrides = {
27 'MS_M+H ION' => 'M_PLUS_H_ION',
28 'MS_M-H ION' => 'M_MINUS_H_ION',
29 };
30  
31 parse_obo($_) for (
32 ['MS' => $fn_ms ],
33 ['MI' => $fn_mi ],
34 ['MOD' => $fn_mod ],
35 ['IMS' => $fn_ims ],
36 ['UO' => $fn_uo ],
37 );
38  
39 if ($dump) {
40  
41 use Data::Dumper;
42 local $Data::Dumper::Indent = 1;
43 local $Data::Dumper::Terse = 1;
44 local $Data::Dumper::Sortkeys = 1;
45 print Dumper $terms;
46 exit;
47  
48 }
49  
50 binmode STDOUT;
51 nstore_fd $terms => \*STDOUT or die "Error writing Storable to disk: $@\n";
52  
53 exit;
54  
55  
56  
57 sub parse_obo {
58  
59 my ($prefix, $fn) = @{ $_[0] };
60  
61 open my $in, '<', $fn or die "Failed to open OBO file $fn for reading";
62  
63 my $is_term = 0;
64 my $curr_term;
65  
66 no strict 'refs';
67  
68 LINE:
69 while (my $line = <$in>) {
70  
71 chomp $line;
72 next if ($line !~ /\S/);
73  
74 if ($line =~ /^\[([^\]]+)\]/) {
75 $is_term = $1 eq 'Term';
76  
77 next LINE if (! defined $curr_term->{id});
78 my $id = $curr_term->{id};
79 delete $curr_term->{id};
80  
81 my $tmp_term = $curr_term;
82 $curr_term = {};
83 $tmp_term->{cv} = $prefix;
84  
85 next LINE if ($tmp_term->{is_obsolete});
86  
87  
88 # special case - a special class of terms in the MS CV represent
89 # regular expressions associated with cleavage reagents. It
90 # doesn't make sense to autogenerate constants for these as their
91 # names are in RE syntax. Track this and skip constant generation
92 # for these.
93 my $is_regex = 0;
94  
95 if (defined $tmp_term->{is_a}) {
96 for my $parent (@{ $tmp_term->{is_a}}) {
97 $terms->{$parent}->{children}->{$id} = 1;
98  
99 # special case for cleavage agent regular expressions
100 $is_regex = 1 if ($parent eq 'MS:1001180');
101 }
102 }
103  
104 my $const_name = uc($prefix . '_' . $tmp_term->{name} );
105  
106 if (! $is_regex && defined $const_name) {
107
108 # handle special cases
109 $const_name = $overrides->{$const_name}
110 if (defined $overrides->{$const_name});
111 $const_name =~ s/\W/_/g;
112  
113 my $tmp = $const_name;
114 if (defined $used{$const_name}) {
115 warn "$tmp already used!\n";
116 my $id = $used{$const_name};
117 if (length($id)) {
118 $terms->{$id}->{constant} = $tmp . '_1';
119 }
120 $used{$const_name} = '';
121 $const_name .= '_2';
122 }
123 my $i = 3;
124 while (defined $used{$const_name}) {
125 warn "$tmp already used!\n";
126 $const_name = $tmp . '_' . $i++;
127 }
128 $used{$const_name} = $id;
129 $tmp_term->{constant} = $const_name;
130  
131 }
132
133 # copy values individually in case hash entry already exists
134 $terms->{$id}->{$_} = $tmp_term->{$_}
135 for (keys %{$tmp_term});
136  
137 }
138 elsif ($is_term) {
139 if ( $line =~ /^(\w+):\s*(.+)$/ ) {
140 my $key = $1;
141 next if (! any {$key eq $_} qw/id name def is_a is_obsolete relationship/);
142 my $val = $2;
143 $val =~ s/\s*(?<!\\)\!.*$//; # remove comments
144 $val =~ s/\\\!/\!/g; # remove escaping
145 if ($key eq 'is_a') {
146 push @{$curr_term->{$key}}, $val;
147 }
148 elsif ($key eq 'relationship') {
149 my ($type, $id) = split ' ', $val;
150 push @{$curr_term->{$type}}, $id;
151 }
152 else {
153 $curr_term->{$key} = $val;
154 }
155 }
156 }
157  
158 }
159  
160 use strict 'refs';
161  
162 close $fn;
163  
164 }