| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Getopt::Long; | ||
| 7 | use HTTP::Tiny; | ||
| 8 | use JSON qw/decode_json/; | ||
| 9 | use XML::Twig; | ||
| 10 | use Storable qw/nstore/; | ||
| 11 | |||
| 12 | my $twig = XML::Twig->new( | ||
| 13 | twig_roots => { | ||
| 14 | 'umod:elem' => \&parse_simple, | ||
| 15 | 'umod:aa' => \&parse_simple, | ||
| 16 | 'umod:brick' => \&parse_simple, | ||
| 17 | 'umod:mod' => \&parse_mod, | ||
| 18 | }, | ||
| 19 | start_tag_handlers => { | ||
| 20 | 'umod:unimod' => \&parse_meta, | ||
| 21 | }, | ||
| 22 | |||
| 23 | ); | ||
| 24 | |||
| 25 | my $fi_unimod; | ||
| 26 | my $fi_elements; | ||
| 27 | my $fo_storable; | ||
| 28 | my $dump = 0; | ||
| 29 | |||
| 30 | GetOptions( | ||
| 31 | 'unimod=s' => \$fi_unimod, | ||
| 32 | 'elements=s' => \$fi_elements, | ||
| 33 | 'out=s' => \$fo_storable, | ||
| 34 | 'dump' => \$dump, | ||
| 35 | ); | ||
| 36 | |||
| 37 | my ($fn_in, $fn_out) = @ARGV; | ||
| 38 | |||
| 39 | my $unimod = {}; | ||
| 40 | $twig->parsefile($fi_unimod); | ||
| 41 | |||
| 42 | fetch_missing_elements($unimod); | ||
| 43 | |||
| 44 | if ($dump) { | ||
| 45 | use Data::Dumper; | ||
| 46 | local $Data::Dumper::Indent = 1; | ||
| 47 | local $Data::Dumper::Terse = 1; | ||
| 48 | local $Data::Dumper::Sortkeys = 1; | ||
| 49 | print Dumper $unimod; | ||
| 50 | } | ||
| 51 | |||
| 52 | nstore $unimod => $fo_storable or die "Error writing Storable to disk: $@\n"; | ||
| 53 | |||
| 54 | exit; | ||
| 55 | |||
| 56 | |||
| 57 | sub parse_meta { | ||
| 58 | |||
| 59 | my ($twig, $tag) = @_; | ||
| 60 | |||
| 61 | my $attrs = $tag->atts; | ||
| 62 | |||
| 63 | # check for presence of expected meta attributes | ||
| 64 | for (qw/xmlns:umod majorVersion minorVersion/) { | ||
| 65 | die "Missing expected meta $_\n" if (! defined $attrs->{$_}); | ||
| 66 | } | ||
| 67 | |||
| 68 | # check namespace compatibility | ||
| 69 | my $ns = $attrs->{'xmlns:umod'} // ''; | ||
| 70 | if ($ns ne 'http://www.unimod.org/xmlns/schema/unimod_2') { | ||
| 71 | die "There is a mismatch between the namespace declaration in the" | ||
| 72 | . " Unimod XML and that supported by this converter. This is" | ||
| 73 | . " likely because the Unimod schema has been updated. Please" | ||
| 74 | . " report this to the developers.\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | $unimod->{db_version} | ||
| 78 | = $attrs->{majorVersion} . '.' . $attrs->{minorVersion}; | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | sub parse_simple { | ||
| 85 | |||
| 86 | my ($twig, $elt) = @_; | ||
| 87 | |||
| 88 | my $tag = $elt->tag; | ||
| 89 | $tag =~ s/^umod://; | ||
| 90 | |||
| 91 | # check for common attributes | ||
| 92 | my $attrs = $elt->atts; | ||
| 93 | for (qw/title full_name mono_mass avge_mass/) { | ||
| 94 | die "Missing meta $_ for elt $tag\n" if (! defined $attrs->{$_}); | ||
| 95 | } | ||
| 96 | |||
| 97 | # parse attributes | ||
| 98 | my $title = $attrs->{title}; | ||
| 99 | delete $attrs->{title}; | ||
| 100 | $unimod->{$tag}->{$title} = $attrs; | ||
| 101 | |||
| 102 | # parse element composition | ||
| 103 | for my $atom ($elt->children('umod:element')) { | ||
| 104 | my $attrs = $atom->atts; | ||
| 105 | for (qw/symbol number/) { | ||
| 106 | die "Missing meta $_ for elt\n" if (! defined $attrs->{$_}); | ||
| 107 | } | ||
| 108 | $unimod->{$tag}->{$title}->{atoms}->{ $attrs->{symbol} } | ||
| 109 | = $attrs->{number}; | ||
| 110 | } | ||
| 111 | |||
| 112 | $twig->purge; | ||
| 113 | return; | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | sub parse_mod { | ||
| 118 | |||
| 119 | my ($twig, $elt) = @_; | ||
| 120 | |||
| 121 | my $tag = $elt->tag; | ||
| 122 | $tag =~ s/^umod://; | ||
| 123 | |||
| 124 | my $attrs = $elt->atts; | ||
| 125 | for (qw/title full_name record_id/) { | ||
| 126 | die "Missing meta $_ for elt $tag\n" if (! defined $attrs->{$_}); | ||
| 127 | } | ||
| 128 | my $title = $attrs->{title}; | ||
| 129 | |||
| 130 | my $delta = $elt->first_child('umod:delta') | ||
| 131 | or die "failed to find delta elt"; | ||
| 132 | my $mono = $delta->att('mono_mass'); | ||
| 133 | my $avg = $delta->att('avge_mass'); | ||
| 134 | die "Error parsing delta masses for mod $title\n" | ||
| 135 | if (! defined $mono || ! defined $avg); | ||
| 136 | $unimod->{$tag}->{$title}->{mono_mass} = $mono; | ||
| 137 | $unimod->{$tag}->{$title}->{avge_mass} = $avg; | ||
| 138 | $unimod->{$tag}->{$title}->{full_name} = $attrs->{full_name}; | ||
| 139 | $unimod->{$tag}->{$title}->{record_id} = $attrs->{record_id}; | ||
| 140 | |||
| 141 | # store mappings of record_id to title | ||
| 142 | $unimod->{mod_index}->{$attrs->{record_id}} = $title; | ||
| 143 | |||
| 144 | # parse element composition | ||
| 145 | for my $atom ($delta->children('umod:element')) { | ||
| 146 | my $attrs = $atom->atts; | ||
| 147 | for (qw/symbol number/) { | ||
| 148 | die "Missing meta $_ for elt\n" if (! defined $attrs->{$_}); | ||
| 149 | } | ||
| 150 | print STDERR "$attrs->{symbol} to $attrs->{number}\n"; | ||
| 151 | $unimod->{$tag}->{$title}->{atoms}->{ $attrs->{symbol} } | ||
| 152 | = $attrs->{number}; | ||
| 153 | } | ||
| 154 | |||
| 155 | $unimod->{$tag}->{$title}->{hashref} = $elt->simplify( | ||
| 156 | forcearray => [qw/ | ||
| 157 | umod:element | ||
| 158 | umod:specificity | ||
| 159 | umod:Ignore | ||
| 160 | umod:alt_name | ||
| 161 | umod:xref | ||
| 162 | umod:NeutralLoss | ||
| 163 | umod:PepNeutralLoss | ||
| 164 | /], | ||
| 165 | ); | ||
| 166 | |||
| 167 | $twig->purge; | ||
| 168 | return; | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | sub fetch_missing_elements { | ||
| 173 | |||
| 174 | my ($unimod) = @_; | ||
| 175 | |||
| 176 | my %elements; | ||
| 177 | |||
| 178 | open my $in, '<', $fi_elements; | ||
| 179 | while (my $line = <$in>) { | ||
| 180 | |||
| 181 | next if ($line =~ /^\s*#/); | ||
| 182 | chomp $line; | ||
| 183 | my ($num, $sym, $name) = split ',', $line; | ||
| 184 | $elements{$name} = $sym; | ||
| 185 | |||
| 186 | } | ||
| 187 | close $in; | ||
| 188 | |||
| 189 | my $ua = HTTP::Tiny->new(); | ||
| 190 | |||
| 191 | ELEM: | ||
| 192 | for my $el (keys %elements) { | ||
| 193 | |||
| 194 | say STDERR "Fetching $el"; | ||
| 195 | |||
| 196 | my $url = sprintf | ||
| 197 | "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/fastformula/%s/JSON?MaxRecords=1", | ||
| 198 | $elements{$el}, | ||
| 199 | ; | ||
| 200 | |||
| 201 | my $res = $ua->get($url); | ||
| 202 | |||
| 203 | if (! $res->{success}) { | ||
| 204 | warn "Failed to fetch data for $el: $res->{reason}\n"; | ||
| 205 | next ELEM; | ||
| 206 | } | ||
| 207 | |||
| 208 | my $data = decode_json( $res->{content} ); | ||
| 209 | my @mono = grep {$_->{urn}->{label} eq 'Weight' && $_->{urn}->{name} eq 'MonoIsotopic'} @{ $data->{PC_Compounds}->[0]->{props} }; | ||
| 210 | die "Missing or too many mono masses for $el\n" | ||
| 211 | if (scalar @mono != 1); | ||
| 212 | my @avg = grep {$_->{urn}->{label} eq 'Molecular Weight' } @{ $data->{PC_Compounds}->[0]->{props} }; | ||
| 213 | die "Missing or too many avg masses for $elements{$el}\n" | ||
| 214 | if (scalar @avg != 1); | ||
| 215 | |||
| 216 | my $mass_avg = $avg[0]->{value}->{sval} | ||
| 217 | // die "Missing avg mass for $el"; | ||
| 218 | my $mass_mono = $mono[0]->{value}->{sval} | ||
| 219 | // die "Missing mono mass for $el"; | ||
| 220 | |||
| 221 | my $existing = $unimod->{elem}->{ $elements{$el} }; | ||
| 222 | if (defined $existing) { | ||
| 223 | my $prev = $existing->{mono_mass}; | ||
| 224 | my $delta = abs($prev - $mass_mono); | ||
| 225 | if ($delta > 0.01) { | ||
| 226 | die "Disageement in mono mass: prev $prev, curr $mass_mono\n"; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | $unimod->{elem}->{ $elements{$el} } = { | ||
| 231 | full_name => $el, | ||
| 232 | avge_mass => $mass_avg, | ||
| 233 | mono_mass => $mass_mono, | ||
| 234 | }; | ||
| 235 | say STDERR "\tAdded $el"; | ||
| 236 | } | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | } | ||
| 241 |