| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Storable qw/nstore_fd retrieve/; | ||
| 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 | my $fn_existing; | ||
| 12 | my $force_new = 0; | ||
| 13 | my $old_fix = 0; | ||
| 14 | |||
| 15 | GetOptions( | ||
| 16 | 'ms=s' => \$fn_ms, | ||
| 17 | 'mi=s' => \$fn_mi, | ||
| 18 | 'mod=s' => \$fn_mod, | ||
| 19 | 'ims=s' => \$fn_ims, | ||
| 20 | 'uo=s' => \$fn_uo, | ||
| 21 | 'dump' => \$dump, | ||
| 22 | 'existing=s' => \$fn_existing, | ||
| 23 | 'force_new' => \$force_new, | ||
| 24 | 'old_fix' => \$old_fix, | ||
| 25 | ); | ||
| 26 | |||
| 27 | my $existing; | ||
| 28 | if ($force_new) { | ||
| 29 | warn "***You have requested to force a new constant table. This WILL likely break backward compatibility of the CV constants, as the OBOs change rapidly and clashing names may change. I hope you know what you are doing!!!***\n"; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | die "Must specify existing Storable file to verify that no constants have changed. If you REALLY know what you are doing, you may override this with '--force_new', but you may break backward compatibility of CV constants\n" | ||
| 33 | if (! defined $fn_existing); | ||
| 34 | $existing = retrieve($fn_existing); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $terms; | ||
| 38 | my %used; | ||
| 39 | |||
| 40 | # NOTE: if you add to or change these overrides, be sure to update the | ||
| 41 | # corresponding documentation in the MS::CV POD | ||
| 42 | my $overrides = { | ||
| 43 | 'MS_M+H ION' => 'M_PLUS_H_ION', | ||
| 44 | 'MS_M-H ION' => 'M_MINUS_H_ION', | ||
| 45 | }; | ||
| 46 | |||
| 47 | parse_obo($_) for ( | ||
| 48 | ['MS' => $fn_ms ], | ||
| 49 | ['MI' => $fn_mi ], | ||
| 50 | ['MOD' => $fn_mod ], | ||
| 51 | ['IMS' => $fn_ims ], | ||
| 52 | ['UO' => $fn_uo ], | ||
| 53 | ); | ||
| 54 | |||
| 55 | #transfer regular expressions | ||
| 56 | for my $v (values %{ $terms }) { | ||
| 57 | next if (! defined $v->{has_regexp}); | ||
| 58 | die "Multiple regular expressions not supported for $v->{name}\n" | ||
| 59 | if (defined $v->{has_regexp}->[1]); | ||
| 60 | my $re = $terms->{ $v->{has_regexp}->[0] } | ||
| 61 | // die "Missing linked RE term for $v->{name}\n"; | ||
| 62 | die if (! length $re->{name}); | ||
| 63 | $v->{regexp} = $re->{name}; | ||
| 64 | delete $v->{has_regexp}; | ||
| 65 | } | ||
| 66 | |||
| 67 | #remove partials and regexps | ||
| 68 | for my $k (keys %{ $terms }) { | ||
| 69 | delete $terms->{$k} | ||
| 70 | if (! defined $terms->{$k}->{constant}); | ||
| 71 | } | ||
| 72 | |||
| 73 | if ($dump) { | ||
| 74 | |||
| 75 | use Data::Dumper; | ||
| 76 | local $Data::Dumper::Indent = 1; | ||
| 77 | local $Data::Dumper::Terse = 1; | ||
| 78 | local $Data::Dumper::Sortkeys = 1; | ||
| 79 | print Dumper $terms; | ||
| 80 | exit; | ||
| 81 | |||
| 82 | } | ||
| 83 | |||
| 84 | # check that nothing has changed from current constant table | ||
| 85 | my $old_constants = extract_constants($existing); | ||
| 86 | my $new_constants = extract_constants($terms); | ||
| 87 | for my $const (keys %{ $old_constants }) { | ||
| 88 | # this block deals with a bug in previous versions that contained carriage | ||
| 89 | # returns in the PSI-MOD file | ||
| 90 | if ($old_fix && $const =~ /^MOD_/) { | ||
| 91 | my $cn = $const; | ||
| 92 | $cn =~ s/_$//; | ||
| 93 | if (! defined $new_constants->{$cn}) { | ||
| 94 | die "Missing $cn ($old_constants->{$const}) in new table\n" | ||
| 95 | } | ||
| 96 | elsif ($new_constants->{$cn} ne $old_constants->{$const}) { | ||
| 97 | die "Mismatched $const (old: $old_constants->{$const}," | ||
| 98 | . " new: $new_constants->{$cn})\n"; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | if (! defined $new_constants->{$const}) { | ||
| 103 | warn "Missing $const ($old_constants->{$const}) in new table\n"; | ||
| 104 | my $id = $old_constants->{$const}; | ||
| 105 | if (defined $terms->{$id}) { | ||
| 106 | warn "Adding $const as alternative for $id\n"; | ||
| 107 | push @{ $terms->{$id}->{alt_constants} }, $const; | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | die "No place found to put constant $const\n"; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | elsif ($new_constants->{$const} ne $old_constants->{$const}) { | ||
| 114 | die "Mismatched $const (old: $old_constants->{$const}," | ||
| 115 | . " new: $new_constants->{$const})\n"; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | # final check for clashing constants | ||
| 121 | my %used_final; | ||
| 122 | for my $id (keys %{ $terms }) { | ||
| 123 | my $constant = $terms->{$id}->{constant} | ||
| 124 | // die "Missing constant in final check for $id\n"; | ||
| 125 | die "Duplicate constant $constant ($id, $used_final{$constant})\n" | ||
| 126 | if (defined $used_final{$constant}); | ||
| 127 | $used_final{$constant} = $id; | ||
| 128 | my $alt = $terms->{$id}->{alt_constants}; | ||
| 129 | if (defined $alt) { | ||
| 130 | for my $constant (@{ $alt }) { | ||
| 131 | die "Duplicate constant $constant ($id, $used_final{$constant})\n" | ||
| 132 | if (defined $used_final{$constant}); | ||
| 133 | $used_final{$constant} = $id; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | binmode STDOUT; | ||
| 139 | nstore_fd $terms => \*STDOUT or die "Error writing Storable to disk: $@\n"; | ||
| 140 | |||
| 141 | exit; | ||
| 142 | |||
| 143 | sub parse_obo { | ||
| 144 | |||
| 145 | my ($prefix, $fn) = @{ $_[0] }; | ||
| 146 | |||
| 147 | open my $in, '<', $fn or die "Failed to open OBO file $fn for reading"; | ||
| 148 | |||
| 149 | my $is_term = 0; | ||
| 150 | my $curr_term; | ||
| 151 | |||
| 152 | LINE: | ||
| 153 | while (my $line = <$in>) { | ||
| 154 | |||
| 155 | chomp $line; | ||
| 156 | next if ($line !~ /\S/); | ||
| 157 | |||
| 158 | if ($line =~ /^\[([^\]]+)\]/) { | ||
| 159 | $is_term = $1 eq 'Term'; | ||
| 160 | |||
| 161 | next LINE if (! defined $curr_term->{id}); | ||
| 162 | my $id = $curr_term->{id}; | ||
| 163 | delete $curr_term->{id}; | ||
| 164 | my $term_prefix = $prefix; | ||
| 165 | if ($id =~ /^([^:]+):([^:]+)$/) { | ||
| 166 | $term_prefix = $1; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | die "Unable to parse term ID: $id\n"; | ||
| 170 | } | ||
| 171 | |||
| 172 | my $tmp_term = $curr_term; | ||
| 173 | $curr_term = {}; | ||
| 174 | $tmp_term->{cv} = $term_prefix; | ||
| 175 | |||
| 176 | if (defined $terms->{$id}->{constant}) { | ||
| 177 | warn "Found duplicate: $id (skipping)\n"; | ||
| 178 | next LINE; | ||
| 179 | } | ||
| 180 | #next LINE if ($tmp_term->{is_obsolete}); | ||
| 181 | |||
| 182 | |||
| 183 | # special case - a special class of terms in the MS CV represent | ||
| 184 | # regular expressions associated with cleavage reagents. It | ||
| 185 | # doesn't make sense to autogenerate constants for these as their | ||
| 186 | # names are in RE syntax. Track this and skip constant generation | ||
| 187 | # for these. | ||
| 188 | my $is_regex = 0; | ||
| 189 | |||
| 190 | if (defined $tmp_term->{is_a}) { | ||
| 191 | for my $parent (@{ $tmp_term->{is_a}}) { | ||
| 192 | $terms->{$parent}->{children}->{$id} = 1; | ||
| 193 | |||
| 194 | # special case for cleavage agent regular expressions | ||
| 195 | $is_regex = 1 if ($parent eq 'MS:1001180'); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | my $const_name = uc($term_prefix . '_' . $tmp_term->{name} ); | ||
| 200 | |||
| 201 | if (! $is_regex && defined $const_name) { | ||
| 202 | |||
| 203 | # handle special cases | ||
| 204 | $const_name = $overrides->{$const_name} | ||
| 205 | if (defined $overrides->{$const_name}); | ||
| 206 | $const_name =~ s/\W/_/g; | ||
| 207 | |||
| 208 | |||
| 209 | my $tmp = $const_name; | ||
| 210 | if (defined $used{$const_name}) { | ||
| 211 | #next LINE if ($tmp_term->{is_obsolete}); | ||
| 212 | warn "$tmp already used!\n"; | ||
| 213 | my $id = $used{$const_name}; | ||
| 214 | if (length($id)) { | ||
| 215 | $terms->{$id}->{constant} = $tmp . '_1'; | ||
| 216 | if ($existing->{$id}->{constant} && $existing->{$id}->{constant} eq $tmp) { | ||
| 217 | push @{$terms->{$id}->{alt_constants}}, $tmp; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | $used{$const_name} = ''; | ||
| 221 | $const_name .= '_2'; | ||
| 222 | } | ||
| 223 | my $i = 3; | ||
| 224 | while (defined $used{$const_name}) { | ||
| 225 | warn "$tmp already used!\n"; | ||
| 226 | $const_name = $tmp . '_' . $i++; | ||
| 227 | } | ||
| 228 | |||
| 229 | # for backward-compatibility | ||
| 230 | if ($prefix ne $term_prefix) { | ||
| 231 | my $alt = $const_name; | ||
| 232 | $alt =~ s/^$term_prefix/$prefix/; | ||
| 233 | #push @{$terms->{$id}->{alt_constants}}, $alt; | ||
| 234 | } | ||
| 235 | |||
| 236 | $used{$const_name} = $id; | ||
| 237 | $tmp_term->{constant} = $const_name; | ||
| 238 | |||
| 239 | } | ||
| 240 | |||
| 241 | # copy values individually in case hash entry already exists | ||
| 242 | $terms->{$id}->{$_} = $tmp_term->{$_} | ||
| 243 | for (keys %{$tmp_term}); | ||
| 244 | |||
| 245 | } | ||
| 246 | elsif ($is_term) { | ||
| 247 | if ( $line =~ /^(\w+):\s*(.+)$/ ) { | ||
| 248 | my $key = $1; | ||
| 249 | next if (! any {$key eq $_} qw/id name def is_a is_obsolete relationship/); | ||
| 250 | my $val = $2; | ||
| 251 | $val =~ s/\s*(?<!\\)\!.*$//; # remove comments | ||
| 252 | $val =~ s/\\\!/\!/g; # remove escaping | ||
| 253 | if ($key eq 'is_a') { | ||
| 254 | push @{$curr_term->{$key}}, $val; | ||
| 255 | } | ||
| 256 | elsif ($key eq 'relationship') { | ||
| 257 | my ($type, $id) = split ' ', $val; | ||
| 258 | push @{$curr_term->{$type}}, $id; | ||
| 259 | } | ||
| 260 | else { | ||
| 261 | $curr_term->{$key} = $val; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | close $fn; | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | sub extract_constants { | ||
| 273 | |||
| 274 | my ($ref) = @_; | ||
| 275 | |||
| 276 | my %map; | ||
| 277 | TERM: | ||
| 278 | for my $term (keys %{$ref}) { | ||
| 279 | my $t = $term; | ||
| 280 | if ($old_fix) { | ||
| 281 | $t =~ s/\s+$//; # handle old bug (for now) | ||
| 282 | }; | ||
| 283 | my $c = $ref->{$term}->{constant}; | ||
| 284 | if (! defined $c) { | ||
| 285 | warn "Missing constant for $term\n"; | ||
| 286 | next TERM; | ||
| 287 | } | ||
| 288 | $map{$c} = $t; | ||
| 289 | if (defined $ref->{$term}->{alt_constants}) { | ||
| 290 | for my $a (@{ $ref->{$term}->{alt_constants} }) { | ||
| 291 | $map{$a} = $t; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | } | ||
| 295 | return \%map; | ||
| 296 | |||
| 297 | } |