lib/EBI/FGPT/FuzzyRecogniser.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/EBI/FGPT/FuzzyRecogniser/OntologyTerm/Synonym.pm ↑ Summary
1
2 =head1 NAME
3
4 EBI::FGPT::FuzzyRecogniser
5
6 =head1 DESCRIPTION
7
8 The module EBI::FGPT::FuzzyRecogniser takes in the constructor
9 an ontology file (OWL/OBO/OMIM/MeSH)
10 and parses it into an internal table of
11 ontology terms (type of EBI::FGPT::FuzzyRecogniser::OntologyTerm).
12 The module contains the find_match method which finds the best match
13 for the supplied term in the given ontology. This can be then queried for ->match_similarity(),
14 ->matched_value(), ->matched_label(), and ->matched_accession(). The best match is
15 based on the n-grams similarity metric.
16
17 =head1 SYNOPSIS
18
19 use EBI::FGPT::FuzzyRecogniser;
20
21 # instantiate and pass ontology file
22
23 my $fuzzy = EBI::FGPT::FuzzyRecogniser->new( obofile => 'obo.txt' );
24
25 # call find_match on a supplied term
26 # finds the best match for the supplied term in the ontology.
27
28 my $x = $fuzzy->find_match('submitter');
29
30
31
32 =head1 AUTHOR
33
34 Emma Hastings , <ehastings@cpan.org>
35
36 =head1 ACKNOWLEDGEMENTS
37
38 Tomasz Adamusiak <tomasz@cpan.org>
39
40 =head1 COPYRIGHT AND LICENSE
41
42 Copyright [2011] EMBL - European Bioinformatics Institute
43
44 Licensed under the Apache License, Version 2.0 (the
45 "License"); you may not use this file except in compliance
46 with the License. You may obtain a copy of the License at
47 http://www.apache.org/licenses/LICENSE-2.0
48 Unless required by applicable law or agreed to in writing,
49 software distributed under the License is distributed on an
50 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
51 either express or implied. See the License for the specific
52 language governing permissions and limitations under the
53 License.
54
55 =cut
56
57 package EBI::FGPT::FuzzyRecogniser;
58
59 6 ▶use lib 'C:/Users/emma.EBI/Fuzzy/cpan-distribution/FuzzyRecogniser/lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
60
61 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
62
63 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 6 ▶use GO::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 0 ▶use OWL::Simple::Parser 1.00;
Subroutine BEGIN: called CC=1 SCAR=2.6
68 use MeSH::Parser::ASCII 0.02;
69 use Bio::Phenotype::OMIM::OMIMparser;
70 use EBI::FGPT::FuzzyRecogniser::OntologyTerm;
71 use EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation;
72 use EBI::FGPT::FuzzyRecogniser::OntologyTerm::Label;
73 use EBI::FGPT::FuzzyRecogniser::OntologyTerm::Synonym;
74 use Log::Log4perl qw(:easy);
75 use IO::Handle;
76 use Benchmark ':hireswallclock';
77
78 use List::Util qw{min max};
79
80 use Data::Dumper;
81
82 our $VERSION = 0.09;
83
84 Log::Log4perl->easy_init( { level => $INFO, layout => '%-5p - %m%n' } );
85
86 # module attributes
87
88 has 'meshfile' => ( is => 'rw', isa => 'Str' );
89 has 'obofile' => ( is => 'rw', isa => 'Str' );
90 has 'owlfile' => ( is => 'rw', isa => 'Str' );
91 has 'omimfile' => ( is => 'rw', isa => 'Str' );
92 has 'ontology_terms' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] });
93
94 sub BUILD {
95
96 my $self = shift;
97
98 # ensure ontology files have been supplied
99
100 unless ( $self->obofile()
101 || $self->owlfile()
102 || $self->meshfile()
103 || $self->omimfile() )
104 {
105 LOGDIE "No ontology file supplied";
106 }
107
108 INFO "Parsing file ";
109
110 #establishes ontology object and passes it to
111 #relevantparser
112
113 $self->parseOWL( $self->owlfile ) if $self->owlfile;
114 $self->parseOBO( $self->obofile ) if $self->obofile;
115 $self->parseMeSH( $self->meshfile ) if $self->meshfile;
116 $self->parseOMIM( $self->omimfile ) if $self->omimfile;
117 }
118
119 =over
120
121 =item find_match()
122
123 Finds the best match for the supplied term in the ontology.
124
125 =cut
126
127 sub find_match($) {
128 my ( $self, $string_to_match ) = @_;
129
130 # terms array was already prenormalised earlier
131 my $annotationToMatch =
132 EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation->new( value => $string_to_match );
133
134 my $matched_term;
135 my $matched_value;
136 my $type;
137 my $max_similarity = undef;
138
139 #loop through all ontology terms
140 for my $term ( @{ $self->ontology_terms } ) {
141
142 #loop through all the annotations on the ontology term
143 for my $annotation ( @{ $term->annotations } ) {
144
145 #compare this annotation against passed string
146 my $similarity = $annotation->compare($annotationToMatch);
147 if ( !( defined $max_similarity ) || $similarity > $max_similarity ) {
148 $max_similarity = $similarity;
149 $matched_term = $term;
150 $matched_value = $annotation->value();
151 $type = ref($annotation);
152 }
153 }
154
155 }
156
157 return {
158 term => $matched_term,
159 value => $matched_value,
160 similarity => $max_similarity,
161 type => $type,
162 };
163 }
164
165 =item createOntologyTerm()
166
167 Creates an OntologyTerm object given its accession and annotations
168
169 =cut
170
171 sub createOntologyTerm($$@) {
172 my ( $accession, $label, @synonyms ) = @_;
173
174 # Create annotation array
175 my @annotations;
176
177 # Create label
178 my $label_annot = EBI::FGPT::FuzzyRecogniser::OntologyTerm::Label->new( value => $label );
179
180 # Add label to annotations
181 # NOTE stored twice for convenience, see label field
182 push @annotations, $label_annot;
183
184 # Create synonym annotations
185 for my $value (@synonyms) {
186
187 # Create synonym
188 my $synonym = EBI::FGPT::FuzzyRecogniser::OntologyTerm::Synonym->new( value => $value );
189
190 # Add synonym to term's annotations
191 push @annotations, $synonym;
192 }
193
194 return
195 EBI::FGPT::FuzzyRecogniser::OntologyTerm->new(
196 accession => $accession,
197 label => $label,
198 annotations => \@annotations,
199 );
200 }
201
202 =item parseMeSH()
203
204 Custom MeSH parser for the MeSH ASCII format.
205
206 =cut
207
208 sub parseMeSH($) {
209 my ( $self, $file ) = @_;
210 my $term;
211 INFO "Parsing MeSH file $file ...";
212
213 my $parser = MeSH::Parser::ASCII->new( meshfile => $file );
214
215 # parse the file
216 $parser->parse();
217
218 # loop through all the headings
219 while ( my ( $id, $heading ) = each %{ $parser->heading } ) {
220 my $accession = $id;
221 my $label = $heading->{label};
222 my @synonyms = @{ $heading->{synonyms} };
223 my $term = createOntologyTerm( $accession, $label, @synonyms );
224
225 # Add term to ontology_terms array
226 push @{ $self->ontology_terms }, $term;
227 }
228 }
229
230 =item parseMeSH()
231
232 Custom OMIM parser.
233
234 =cut
235
236 sub parseOMIM($) {
237 my ( $self, $file ) = @_;
238 INFO "Parsing OMIM file $file ...";
239
240 my $synonym_count;
241
242 # FIXME: The external parser is suboptimal in many ways
243 # if this becomes more often used consider creating
244 # a custom one from sratch
245 my $parser = Bio::Phenotype::OMIM::OMIMparser->new( -omimtext => $file );
246
247 # loop through all the records
248 while ( my $omim_entry = $parser->next_phenotype() ) {
249
250 # *FIELD* NO
251 my $id = $omim_entry->MIM_number();
252 $id = 'OMIM:' . $id;
253
254 # *FIELD* TI - first line
255 my $title = $omim_entry->title();
256 $title =~ s/^.\d+ //; # remove id from title
257 $title =~ s/INCLUDED//g; # remove INCLUDED as it screws up scoring
258
259 # *FIELD* TI - additional lines
260 my $alt = $omim_entry->alternative_titles_and_symbols();
261
262 # OMIM uses this weird delimiter ;;
263 # to signal sections irrespective of actual line endings
264 # this is a major headache to resolve, the parser doesn't
265 # do this and we're not going to bother with it either
266 $alt =~ s/;;//g;
267 $alt =~ s/INCLUDED//g; # remove INCLUDED as it screws up scoring
268 my @synonyms = split m!\n!, $alt;
269
270 # if alt doesn't start with ;; it's an overspill from the
271 # title (go figure!)
272 if ( $alt ne ''
273 && $omim_entry->alternative_titles_and_symbols() !~ /^;;/ )
274 {
275 $title .= shift @synonyms;
276 }
277
278 # Instantiate new ontology term
279
280 my $term = createOntologyTerm( $id, $title, @synonyms );
281
282 # Add term to ontology_terms array
283 push @{ $self->ontology_terms }, $term;
284
285 $synonym_count += scalar @synonyms;
286
287 }
288
289 }
290
291 =item parseOBO()
292
293 Custom OBO parser.
294
295 =cut
296
297 sub parseOBO($) {
298 my ( $self, $file ) = @_;
299 INFO "Parsing obo file $file ...";
300 my $parser = new GO::Parser( { handler => 'obj' } );
301 $parser->parse($file);
302 my $graph = $parser->handler->graph();
303
304 # load terms into hash
305 my $class_count;
306 my $synonym_count;
307
308 for my $OBOclass ( @{ $graph->get_all_terms() } ) {
309 if ( $OBOclass->is_obsolete ) {
310 INFO $OBOclass->public_acc() . ' obsoleted';
311 next;
312 }
313 $class_count++;
314 $synonym_count += scalar( @{ $OBOclass->synonym_list() } );
315
316 # Instantiate new ontology term
317 my $accession = $OBOclass->public_acc();
318 my $label = $OBOclass->name();
319 my @synonyms = @{ $OBOclass->synonym_list() };
320 my $term = createOntologyTerm( $accession, $label, @synonyms );
321
322 # Add term to array
323 push @{ $self->ontology_terms }, $term;
324 }
325
326 INFO "Loaded " . $class_count . " classes and " . $synonym_count . " synonyms";
327
328 }
329
330 =item parseOWL()
331
332 Custom OWL parser.
333
334 =cut
335
336 sub parseOWL($) {
337 my ( $self, $file ) = @_;
338 INFO "Parsing owl file $file ...";
339 my $parser;
340
341 # invoke parser
342 $parser = OWL::Simple::Parser->new( owlfile => $file );
343
344 # parse file
345 $parser->parse();
346
347 while ( my ( $id, $OWLClass ) = each %{ $parser->class } ) {
348 unless ( defined $OWLClass->label ) {
349 WARN "Undefined label in $id";
350 }
351 elsif ( $OWLClass->label =~ /obsolete/ ) {
352 next;
353 }
354
355 # Instantiate new ontology term
356 my $accession = $OWLClass->id();
357 my $label = $OWLClass->label();
358 my @synonyms = @{ $OWLClass->synonyms() };
359
360 my $term = createOntologyTerm( $accession, $label, @synonyms );
361
362 # Add term to array
363 push @{ $self->ontology_terms }, $term;
364 }
365
366 }
367 1; # End of EBI::FGPT::FuzzyRecogniser
« lib/EBI/FGPT/FuzzyRecogniser/OntologyTerm/Synonym.pm ↑ Summary