script/similarity_match.pl untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 182 CC 15 SCAR 33.2
CC
15
Cov
0%
main
56.0
matchFlat
42.0
matchOntology
20.0
CRAP 27.6
1 #!/usr/bin/env perl
2
3 =head1 NAME
4
5 similarity_match.pl
6
7 =head1 SYNOPSIS
8
9 Compares a list of annotations to another ontology and suggests the best match
10 based on the EBI::FGPT::FuzzyRecogniser module. It is also possible to
11 align one ontology to another. Accepts ontologies in both OBO and OWL formats
12 as well as MeSH ASCII and OMIM txt.
13
14 The script runs non-interactively and the results have to be manually inspected,
15 although it can be expected that anything with a similarity score higher
16 than ~80-90% will be a valid match.
17
18 =head2 USAGE
19
20 similarity_match.pl (-w owlfile || -o obofile || -m meshfile || -i omimfile)
21 -t targetfile -r resultfile
22 [--obotarget || --owltarget]
23
24 Optional '--obotarget' setting specifies that the target file is an OBO ontology.
25 Optional '--owltarget' setting specifies that the target file is an OWL ontology.
26
27 =head2 INPUT FILES
28
29 =over
30
31 =item ontologies to map the targetfile against
32
33 owlfile, obofile, meshfile, omimfile are ontologies in OWL, OBO, MeSH ASCII and OMIM
34 formats respectively
35 Only a single file needs to be specified.
36
37 =item targetfile
38
39 The script expects a tab-delimited text file with headers. Only the first column will
40 be used for matching. All other columns will be preserved in the output.
41
42 =back
43
44 =head2 OUTPUT
45
46 The script will produce a single tab-delimited file as set with the
47 -r flag. The file will have four additional headers
48
49 =over
50
51 =item SOURCE_ACCESSION
52
53 Accession of the source term if target file was an ontology.
54
55 =item SOURCE_LABEL
56
57 Label of the source term if target file was an ontology.
58
59 =item SOURCE_VALUE
60
61 The annotation (label or synoym if target file was an ontology) that was matched
62 based on the highest similarity against the supplied ontology file
63
64 =item MATCHED_ACCESSION
65
66 Accession of the matched term that provided the best match.
67
68 =item MATCHED_LABEL
69
70 Matched term's label.
71
72 =item MATCHED_VALUE
73
74 The actual term's annotation (label or synoym) that was matched
75 based on the highest similarity from the supplied ontology file.
76
77 =item MATCH_SIMILARITY%
78
79 Similarity score of the two matched terms normalised by lenght of the
80 longer of the two strings and expressed in %. Higher is better.
81
82 =back
83
84 =cut
85
86 use lib 'C:\Users\Tomasz\workspace\cpan-distribution\FuzzyRecogniser\lib',
87 'C:\strawberry\perl\site\lib',
88 '/ebi/microarray/ma-subs/AE/subs/PERL_SCRIPTS/local/lib/perl5/',
89 '/ebi/microarray/ma-subs/AE/subs/PERL_SCRIPTS/local/lib64/perl5/',
90 '/ebi/microarray/ma-subs/AE/subs/PERL_SCRIPTS/local/lib/perl5/site_perl/',
91 '/ebi/microarray/ma-subs/AE/subs/PERL_SCRIPTS/local/lib64/perl5/site_perl/';
92
93 use strict;
94 use warnings;
95
96 use EBI::FGPT::FuzzyRecogniser;
97
98 #use IO::File;
99 use Getopt::Long;
100 use Log::Log4perl qw(:easy);
101
102 #use IO::Handle;
103 use Benchmark ':hireswallclock';
104
105 use Data::Dumper;
106
107 Log::Log4perl->easy_init( { level => $INFO, layout => '%-5p - %m%n' } );
108
109 # script arguments
110 my (
111 $owlfile, $obofile, $targetfile, $resultfile,
112 $obotarget, $owltarget, $meshfile, $omimfile
113 );
114
115 my @flat_header;
116
117 my $fuzzy;
118
119 sub main() {
120
121 # initalize
122
123 GetOptions(
124 "o|obofile=s" => \$obofile,
125 "w|owlfile=s" => \$owlfile,
126 "m|meshfile=s" => \$meshfile,
127 "i|omimfile=s" => \$omimfile,
128 "t|target=s" => \$targetfile,
129 "r|results=s" => \$resultfile,
130 "obotarget" => \$obotarget,
131 "owltarget" => \$owltarget,
132 );
133
134 usage()
135 unless ( $owlfile || $obofile || $meshfile || $omimfile )
136 && $targetfile
137 && $resultfile;
138
139 # load appropriate files
140 $fuzzy = EBI::FGPT::FuzzyRecogniser->new( obofile => $obofile ) if $obofile;
141 $fuzzy = EBI::FGPT::FuzzyRecogniser->new( owlfile => $owlfile ) if $owlfile;
142 $fuzzy = EBI::FGPT::FuzzyRecogniser->new( meshfile => $meshfile )
143 if $meshfile;
144 $fuzzy = EBI::FGPT::FuzzyRecogniser->new( omimfile => $omimfile )
145 if $omimfile;
146
147 unless ( defined $obotarget || defined $owltarget ) {
148 matchFlat($targetfile);
149 }
150 else {
151 matchOntology();
152 }
153
154 }
155
156 sub usage() {
157 print(<<"USAGE");
158
159 similarity_match.pl (-w owlfile || -o obofile || -m meshfile || -i omimfile)
160 -t targetfile -r resultfile
161 [--obotarget || --owltarget]
162
163 Optional '--obotarget' setting specifies that the target file is an OBO ontology
164 Optional '--owltarget' setting specifies that the target file is an OWL ontology
165 USAGE
166 exit 255;
167 }
168
169 =head1 DESCRIPTION
170
171 =head2 Function list
172
173 =over
174
175 =item align()
176
177 Aligns the two data structures targetfile and ontology. Outputs
178 the results into a file.
179
180 =cut
181
182 sub matchOntology() {
183 open my $fh_out, '>', $resultfile;
184 $fh_out->autoflush(1);
185
186 my $terms;
187 $terms =
188 EBI::FGPT::FuzzyRecogniser->new( obofile => $obofile )->ontology_terms()
189 if $obofile;
190 $terms =
191 EBI::FGPT::FuzzyRecogniser->new( owlfile => $owlfile )->ontology_terms()
192 if $owlfile;
193
194 # write header
195 print $fh_out "SOURCE_ACCESSION\tSOURCE_LABEL\tSOURCE_VALUE\t";
196 print $fh_out
197 "MATCHED_ACCESSION\tMATCHED_LABEL\tMATCHED_VALUE\tMATCH_SIMILARITY%";
198 print $fh_out "\n";
199
200 my $c = 0;
201 my $t0 = new Benchmark;
202
203 for my $term (@$terms) {
204 my $label = $term->label();
205 my $accession = $term->accession();
206
207 #loop through all the annotations on the ontology term
208 for my $annotation ( @{ $term->annotations } ) {
209 my $value = $annotation->value();
210 print $fh_out $accession . "\t" . $label . "\t" . $value . "\t";
211
212 # output match info
213 my $match_result = $fuzzy->find_match($value);
214 print $fh_out $match_result->{term}->accession() . "\t"
215 . $match_result->{term}->label() . "\t"
216 . $match_result->{value} . "\t"
217 . $match_result->{similarity};
218
219 # line ending
220 print $fh_out "\n";
221
222 INFO "Processed " . $c
223 if ++$c % 100 == 0;
224 }
225
226 }
227
228 my $t1 = new Benchmark;
229 INFO "Processed $c elements in " . timestr( timediff( $t1, $t0 ) );
230 close $fh_out;
231 }
232
233 =item parseFlat()
234
235 Custom flat file parser.
236
237 =cut
238
239 sub matchFlat($) {
240 my $file = shift;
241 my $value;
242 my %duplicate;
243 INFO "Parsing flat file $file ...";
244
245 open my $fh_in, '<:utf8', $file;
246 open my $fh_out, '>:utf8', $resultfile;
247 $fh_out->autoflush(1);
248
249 # parse input header
250 my $header = <$fh_in>;
251 chomp $header;
252 ( $flat_header[0], $flat_header[1] ) = parseFlatColumns($header);
253
254 INFO "Using first line as header <$header>";
255 INFO "Using first column <$flat_header[0]> to match terms";
256
257 # Write output header
258 print $fh_out "SOURCE_VALUE[" . $flat_header[0] . "]\t";
259 print $fh_out
260 "MATCHED_ACCESSION\tMATCHED_LABEL\tMATCHED_VALUE\tMATCH_SIMILARITY%";
261 print $fh_out "\t$flat_header[1]" if defined $flat_header[1];
262 print $fh_out "\n";
263
264 my $c = 0;
265 my $t0 = new Benchmark;
266
267 # load input
268 while (<$fh_in>) {
269 chomp;
270 next if /^$/; # skip empty line
271
272 # preserve existing columns in the file
273 my ( $value, $ragged_end ) = parseFlatColumns($_);
274
275 # trim
276 $value =~ s/^\s+//;
277 $value =~ s/\s+$//;
278
279 # drop trailing quotation marks (excel artefact?)
280 $value =~ s/^"+//;
281 $value =~ s/"+$//;
282
283 WARN "Duplicated <$value>" if exists $duplicate{$value};
284 $duplicate{$value}++;
285
286 print $fh_out $value . "\t";
287
288 # output match info
289 my $match_result = $fuzzy->find_match($value);
290 print $fh_out $match_result->{term}->accession() . "\t"
291 . $match_result->{term}->label() . "\t"
292 . $match_result->{value} . "\t"
293 . $match_result->{similarity};
294
295 # line ending
296 print $fh_out "\n";
297
298 INFO "Processed " . $c
299 if ++$c % 100 == 0;
300
301 # output unprocessed columns back
302 print $fh_out "\t" . $ragged_end if defined $ragged_end;
303 }
304
305 my $t1 = new Benchmark;
306 INFO "Processed $c elements in " . timestr( timediff( $t1, $t0 ) );
307 close $fh_out;
308 close $fh_in;
309 }
310
311 =item parseFlatColumns()
312
313 Splits and joins the columns of a flat file. The first column is assigned to the first element.
314 Concatenates the ragged end (leftover columns) into the second element or returns undef for
315 a one-column file.
316
317 =cut
318
319 sub parseFlatColumns($) {
320 my $header = shift;
321
322 my @temp = split /\t/, $header;
323 return ( $temp[0], ( join( "\t", @temp[ 1 .. $#temp ] ) || undef ) );
324 }
325
326 =back
327
328 =cut
329
330 =head1 ACKNOWLEDGMENTS
331
332 Emma Hastings <emma@ebi.ac.uk>
333
334 =cut
335
336 =head1 AUTHORS
337
338 Tomasz Adamusiak <tomasz@cpan.org>
339
340 =cut
341
342 main();