| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | MeSH::Parser::ASCII - parser for the MeSH ASCII format | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use MeSH::Parser::ASCII; | ||||||||||
| 10 | |||||||||||
| 11 | # instantiate the parser | ||||||||||
| 12 | my $parser = MeSH::Parser::ASCII->new( meshfile => 'd2010.bin' ); | ||||||||||
| 13 | |||||||||||
| 14 | # parse the file | ||||||||||
| 15 | $parser->parse(); | ||||||||||
| 16 | |||||||||||
| 17 | # loop through all the headings | ||||||||||
| 18 | while ( my ( $id, $heading ) = each %{ $parser->heading } ) { | ||||||||||
| 19 | print $id . ' - ' . $heading->{label} . "\n"; | ||||||||||
| 20 | |||||||||||
| 21 | # list synonyms | ||||||||||
| 22 | for my $synonym ( @{ $heading->{synonyms} } ) { | ||||||||||
| 23 | print "\t$synonym\n"; | ||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | # list parents | ||||||||||
| 27 | for my $parent ( @{ $heading->{parents} } ) { | ||||||||||
| 28 | print "\t" . $parent->{label} . "\n"; | ||||||||||
| 29 | } | ||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||
| 33 | |||||||||||
| 34 | Parser for the MeSH ASCII format. | ||||||||||
| 35 | |||||||||||
| 36 | =over | ||||||||||
| 37 | |||||||||||
| 38 | =item meshfile | ||||||||||
| 39 | |||||||||||
| 40 | MeSH file in ASCII format | ||||||||||
| 41 | |||||||||||
| 42 | =back | ||||||||||
| 43 | |||||||||||
| 44 | =head2 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =over | ||||||||||
| 47 | |||||||||||
| 48 | =item parse() | ||||||||||
| 49 | |||||||||||
| 50 | Parses the MeSH file and loads it into a hash ref. | ||||||||||
| 51 | |||||||||||
| 52 | =item heading | ||||||||||
| 53 | |||||||||||
| 54 | Returns a hash ref collection of all the parsed headings. Each consists of a label, | ||||||||||
| 55 | and id and synonyms if any were available. | ||||||||||
| 56 | |||||||||||
| 57 | Label is extracted from I<Mesh Heading> field in Descriptor Data Elements, | ||||||||||
| 58 | or I<Name of substance> in Supplementary Concept Records, | ||||||||||
| 59 | or I<Subheading> in Qualifier Data Elements. | ||||||||||
| 60 | |||||||||||
| 61 | Synonyms are only parsed for Descriptor Data Elements (I<PRINT ENTRY> and I<ENTRY> entries) | ||||||||||
| 62 | |||||||||||
| 63 | =back | ||||||||||
| 64 | |||||||||||
| 65 | =head1 AUTHOR | ||||||||||
| 66 | |||||||||||
| 67 | Tomasz Adamusiak <tomasz@cpan.org> | ||||||||||
| 68 | |||||||||||
| 69 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 70 | |||||||||||
| 71 | Copyright (c) 2010 European Bioinformatics Institute. All Rights Reserved. | ||||||||||
| 72 | |||||||||||
| 73 | This module is free software; you can redistribute it and/or modify it | ||||||||||
| 74 | under GPLv3. | ||||||||||
| 75 | |||||||||||
| 76 | This software is provided "as is" without warranty of any kind. | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | package MeSH::Parser::ASCII; | ||||||||||
| 81 | |||||||||||
| 82 | 2 | ▶ | use Moose 0.89; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 2 | ▶ | use Log::Log4perl qw(:easy); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 84 | 2 | Log::Log4perl->easy_init( { level => $INFO, layout => '%-5p - %m%n' } ); | |||||||||
| 85 | |||||||||||
| 86 | 2 | our $VERSION = 0.03; | |||||||||
| 87 | |||||||||||
| 88 | 2 | has 'meshfile' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||
| 89 | 1 | ▶ | has 'heading' => ( is => 'ro', isa => 'HashRef', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 90 | |||||||||||
| 91 | 1 | ▶ | sub parse() { | ||||||||
|
Subroutine
parse: called CC=24 SCAR=32.0
Pod
documented
| |||||||||||
| 92 | 1 | my $self = shift; | |||||||||
| 93 | |||||||||||
| 94 | 1 | INFO 'Parsing file ' . $self->meshfile . ' ...'; | |||||||||
| 95 | |||||||||||
| 96 | # open file | ||||||||||
| 97 | 1 | open my $fh, '<', $self->meshfile; | |||||||||
| 98 | |||||||||||
| 99 | 1 | my ( $label, $id, $synonyms, $treeNos, $count ); | |||||||||
| 100 | 1 | $count->{syns} = 0; | |||||||||
| 101 | |||||||||||
| 102 | 1 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | # multiplatform chomp | ||||||||||
| 105 | # this will also rtrim the line | ||||||||||
| 106 | 74 | s/\s+$//; | |||||||||
| 107 | |||||||||||
| 108 | # initialise | ||||||||||
| 109 | 74 | ▶ | if (/^\*NEWRECORD/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 2 | $synonyms = undef; | |||||||||
| 111 | 2 | $label = undef; | |||||||||
| 112 | 2 | $id = undef; | |||||||||
| 113 | 2 | $treeNos = undef; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 74 | DEBUG '<' . $_ . '>'; | |||||||||
| 117 | |||||||||||
| 118 | # save on new line | ||||||||||
| 119 | 74 | ▶ | if (/^$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 2 | ▶ | LOGDIE 'Could not parse heading\'s label.' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | unless defined $label; | ||||||||||
| 122 | 2 | $count->{headings}++; | |||||||||
| 123 | WARN "Duplicate heading found for $id" | ||||||||||
| 124 | 2 | ▶ | if defined $self->heading->{$id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 2 | $self->heading->{$id}->{label} = $label; | |||||||||
| 126 | 2 | ▶ | $self->heading->{$id}->{synonyms} = $synonyms | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | if defined $synonyms; | ||||||||||
| 128 | 2 | DEBUG $label . ' ' . $id . "\n"; | |||||||||
| 129 | 2 | for my $syn (@$synonyms) { | |||||||||
| 130 | 5 | DEBUG "\t" . $syn; | |||||||||
| 131 | 5 | $count->{syns}++; | |||||||||
| 132 | } | ||||||||||
| 133 | 2 | ▶ | $self->heading->{$id}->{treeNos} = $treeNos | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | if defined $treeNos; | ||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | # Mesh Heading in Descriptor Data Elements | ||||||||||
| 138 | 74 | ▶ | $label = ( split(/ = /) )[1] if /^MH = /; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | |||||||||||
| 140 | # Name of substance in Supplementary Concept Records | ||||||||||
| 141 | 74 | ▶ | $label = ( split(/ = /) )[1] if /^NM = /; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | # Subheading in Qualifier Data Elements | ||||||||||
| 144 | 74 | ▶ | $label = ( split(/ = /) )[1] if /^SH = /; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | |||||||||||
| 146 | 74 | ▶ | $id = ( split(/ = /) )[1] if /^UI = /; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | |||||||||||
| 148 | # PRINT ENTRY and ENTRY are synonyms in Descriptor Data Elements | ||||||||||
| 149 | # splits on ENTRY = , and then disregards anything after pipe | | ||||||||||
| 150 | 74 | ▶ | push @$synonyms, ( split( /\|/, ( split(/ = /) )[1] ) )[0] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | if /^ENTRY = /; | ||||||||||
| 152 | 74 | ▶ | push @$synonyms, ( split( /\|/, ( split(/ = /) )[1] ) )[0] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | if /^PRINT ENTRY = /; | ||||||||||
| 154 | |||||||||||
| 155 | # MeSH Tree Number | ||||||||||
| 156 | 74 | ▶ | push @$treeNos, ( split(/ = /) )[1] if /^MN = /; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | } | ||||||||||
| 158 | 1 | close $fh; | |||||||||
| 159 | |||||||||||
| 160 | LOGDIE 'Could not parse any headings.' | ||||||||||
| 161 | 1 | ▶ | unless defined $count->{headings}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | |||||||||||
| 163 | INFO "Loaded " | ||||||||||
| 164 | . $count->{headings} | ||||||||||
| 165 | . " headings and " | ||||||||||
| 166 | . $count->{syns} | ||||||||||
| 167 | 1 | . " synonyms"; | |||||||||
| 168 | |||||||||||
| 169 | 1 | INFO "Processing hierarchy if available"; | |||||||||
| 170 | |||||||||||
| 171 | # construct tree | ||||||||||
| 172 | 1 | my $tree; | |||||||||
| 173 | 1 | ▶ | while ( my ( $id, $heading ) = each %{ $self->heading } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | DEBUG "No tree number in $id" | ||||||||||
| 175 | 2 | ▶ | unless defined $heading->{treeNos}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 2 | for my $n ( @{ $heading->{treeNos} } ) { | |||||||||
| 177 | 2 | $tree->{$n} = $heading; | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | # feed back results | ||||||||||
| 182 | 1 | ▶ | while ( my ( $id, $heading ) = each %{ $self->heading } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 2 | for my $n ( @{ $heading->{treeNos} } ) { | |||||||||
| 184 | 2 | $n =~ /^(.*)\..*$/; | |||||||||
| 185 | 2 | ▶ | if ( defined $1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 2 | my $parent = $tree->{$1}; | |||||||||
| 187 | 2 | ▶ | if ( defined $parent ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 1 | push @{ $heading->{parents} }, $parent; | |||||||||
| 189 | } | ||||||||||
| 190 | else { | ||||||||||
| 191 | 1 | WARN "No parent with tree number $n"; | |||||||||
| 192 | } | ||||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | 1 | 1; | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | 2 | 1; | |||||||||