-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphi-base_ontologies.pl
executable file
·325 lines (266 loc) · 9.88 KB
/
phi-base_ontologies.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env perl
# See the NOTICE file distributed with this work for additional information
# regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use autodie;
use Carp;
use DateTime;
use Data::Dumper;
use File::Basename;
use File::Path qw(make_path rmtree);
use Storable;
use Bio::EnsEMBL::Utils::CliHelper;
my $cli_helper = Bio::EnsEMBL::Utils::CliHelper->new();
#### get the basic options for connecting to a database server
#
my $optsd = [
"input_file:s",
"phenotype_dictionary:s",
"conditions_dictionary:s",
"obo_file_destination:s"
];
my $opts = $cli_helper->process_args( $optsd, \&pod2usage );
my $phibase_file = $opts->{input_file};
my $phenotype_dictionary = $opts->{phenotype_dictionary};
my $conditions_dictionary = $opts->{conditions_dictionary};
my $obo_file_required;
if ( $opts->{obo_file_destination} ) {
$obo_file_required = 1;
print " OBO file is required.\n";
}
print "Phi file: $phibase_file \n";
print "Ontologies phenotypes file: $phenotype_dictionary \n";
##################### List accepted terms ######################################
require $phenotype_dictionary;
print "\n------------- Accepted phenotypes : -------------\n\n";
my %mapped_phenotypes = do $phenotype_dictionary;
my %mapped_phenotypes_values ;
foreach (keys %mapped_phenotypes){
if (!$mapped_phenotypes_values{$_}){
$mapped_phenotypes_values{$mapped_phenotypes{$_}} = 1;
}
}
my @phenotypes_keys = sort { $a cmp $b } keys %mapped_phenotypes_values; # let's show them nicely!
# foreach my $key ( @phenotypes_keys ) {
# print "$key \n" ;
# }
print "\n\n------------- Accepted conditions : -------------\n\n";
my %mapped_conditions = do $conditions_dictionary;
my %mapped_conditions_values ;
foreach (keys %mapped_conditions){
if (!$mapped_conditions_values{$_}){
$mapped_conditions_values{$mapped_conditions{$_}} = 1;
}
}
my @conditions_keys = sort { $a cmp $b } keys %mapped_conditions_values;
# foreach my $key ( @conditions_keys ) {
# print "$key \n" ;
# }
# print " \n";
########################################################
# open input file
open( my $INPUT_CSV, "<", $phibase_file );
my ($temp_file, $dirs) = fileparse($phibase_file);
my $temp_output_phi_file = "${dirs}Ontologies_temp_files/temp_$temp_file";
# create temp folder
eval { make_path("${dirs}Ontologies_temp_files/" ) };
if ($@) {
print "Couldn't create ${dirs}Ontologies_temp_files/ : $@";
}
# open and create output temp file
open( my $OUTPUT_CSV, '>' . $temp_output_phi_file);
# my %result_conditions; # list of conditions
# my %result_phenotypes; # list of phenotypes
my %unmapped_phenotypes;
my %unmapped_conditions;
LINE: while (my $line = <$INPUT_CSV> ) {
my @fields = split(/,/, $line);
if (scalar(@fields)>15) {
print " ********** WARNING: COMMA DETECTED ************ \n" .
" A field with an unexpected comma has been found in entry " . $fields[1] .
". This line will be ignored.\n";
next LINE;
}
my $phenotype = lc(remove_spaces($fields[11]));
my $conditions = lc(remove_spaces($fields[12]));
my $phi_number = $fields[0];
my $phi_id = $fields[1];
my $conditions_field = '';
my $pubmed_id = $fields[13] || '';
my $doi = $fields[14] || '';
if ( $phenotype && $conditions) {
# process phenotypes
if ($mapped_phenotypes{$phenotype}) { # already exists in the dictionnary
$phenotype = $mapped_phenotypes{$phenotype};
# $result_phenotypes{$phenotype}; # add Gene phenotype to results
} else {
if (!$unmapped_phenotypes{$phenotype}) { # if not yet in the list of terms to be solved
$unmapped_phenotypes{$phenotype} = $phenotype; # add it
}
}
#process conditions
my @conditions_splitted = split /[;]/, $conditions; # can also try with /[;\/:]/
my $cond_ctr = 1;
foreach my $cond (@conditions_splitted) {
$cond = remove_spaces($cond);
if ($mapped_conditions{$cond}) { # already exists in the dictionnary
if ($cond_ctr == 1) {
$conditions_field = $mapped_conditions{$cond};
} else { # multiple conditions
$conditions_field = "$conditions_field; " . $mapped_conditions{$cond};
}
#$result_conditions{$cond}; # add to results
} else {
if (!$unmapped_conditions{$cond}) { # if not yet in the list of terms to be solved
$unmapped_conditions{$cond} = $cond; # add it
}
}
$cond_ctr++;
}
my $corrected_line = join(',', $phi_number,
$phi_id,
$fields[2],
$fields[3],
$fields[4],
$fields[5],
$fields[6],
$fields[7],
$fields[8],
$fields[9],
$fields[10],
$phenotype,
$conditions_field,
$pubmed_id,
$doi);
print $OUTPUT_CSV $corrected_line;
}
}
close $INPUT_CSV;
close $OUTPUT_CSV;
# print "\n*********** All conditions found in csv file : ************\n";
# for my $cond ( keys %result_conditions ) {
# print $cond, "\n";
# }
# print "\n*********** All phenotypes found in csv file: ************\n";
# for my $phent ( keys %result_phenotypes ) {
# print $phent, "\n";
# }
print "\n++++++++++++ Unmapped phenotypes : ++++++++++++ \n";
if ( !%unmapped_phenotypes ) {
print " No unmapped phenotypes found.\n"
} else {
foreach ( keys %unmapped_phenotypes ) {
my $new_ph = $unmapped_phenotypes{$_};
print "\"$new_ph\"\n";
}
}
print "\n+++++++++++++ Unmapped conditions : +++++++++++++ \n";
if ( !%unmapped_conditions ) {
print " No unmapped conditions found.\n"
} else {
foreach ( keys %unmapped_conditions ) {
my $new_cond = $unmapped_conditions{$_};
print "\"$new_cond\"\n";
}
}
################ OVERWRITE FILE WITH CORRECT TERMS AND GENERATE OBO FILE ###################
if ( !%unmapped_phenotypes && !%unmapped_conditions ) {
print "\n>> All ontologies are correctly mapped.\n";
_overwrite_ontologies();
if ($obo_file_required) {
_create_obo_file();
}
print "\n";
} else {
print "\n>> Some unmapped phenotypes or conditions need to be added to its corresponding dictionary.\n"
}
# replace the uncorrect entries in the csv file with the correct terminology
sub _overwrite_ontologies {
rename $temp_output_phi_file, $phibase_file;
rmtree("${dirs}Ontologies_temp_files/"); # remove the temp folder
print ">> All terms in the original file have been replaced with controlled terms.\n";
};
# create new .obo file with the updated terminology
sub _create_obo_file {
open my $output, ">", $opts->{obo_file_destination} or croak "Could not open " . $opts->{obo_file_destination};
my $login = getlogin || getpwuid($<) || "User unknown";
my $date_string = DateTime->now->dmy;
my $header = "format-version: 5\ndate: $date_string \nsaved-by: $login \n" .
"auto-generated-by: construct_phibase_obo.pl\n" .
"synonymtypedef: systematic_synonym Systematic synonym EXACT\n" .
"default-namespace: phenotype\nontology: PHI\n\n";
my $PHENOTYPE_PARENT = 1000000;
my $CONDITION_PARENT = 2000000;
my $IDENTIFIER_PARENT = 0;
#add header
print $output $header ;
# define parent terms
print $output "[Term]\n" .
"id: PHI:$PHENOTYPE_PARENT\n" .
"name: phenotype\n" .
"namespace: phenotype\n" .
"\n" .
"[Term]\n" .
"id: PHI:$CONDITION_PARENT\n" .
"name: experiment specification\n" .
"namespace: experiment_specification\n" .
"\n" .
"[Term]\n" .
"id: PHI:0 \n" .
"name: phibase identifier\n" .
"namespace: phibase_identifier\n" .
"\n"
;
# add phenotype terms
my $phi_counter = $PHENOTYPE_PARENT;
foreach my $key ( @phenotypes_keys ) {
if ( $key ne 'phenotype') {
print $output "[Term]\n" .
"id: PHI:" . ++$phi_counter . "\n" .
"name: $key\n" .
"namespace: phenotype\n" .
"is_a: PHI:$PHENOTYPE_PARENT\n\n";
}
}
# add condition terms
$phi_counter = $CONDITION_PARENT;
foreach my $key ( @conditions_keys ) {
if ( $key ne 'experiment specification') {
print $output "[Term]\n" .
"id: PHI:" . ++$phi_counter . "\n" .
"name: $key\n" .
"namespace: experiment_specification\n" .
"is_a: PHI:$CONDITION_PARENT\n\n";
}
}
# add identifiers
$phi_counter = $IDENTIFIER_PARENT;
for (1..9999) {
print $output "[Term]\n" .
"id: PHI:" . ++$phi_counter . "\n" .
"name: $phi_counter\n" .
"namespace: phibase_identifier\n" .
"is_a: PHI:$IDENTIFIER_PARENT\n\n";
}
close $output;
print ">> A new OBO file has been created in: " . $opts->{obo_file_destination} . "\n";
}
sub remove_spaces {
my $string = shift;
$string =~ s/^\s*//;
$string =~ s/\s*$//;
return $string;
}