forked from cmungall/obo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo-remove-case-sensitive-synonyms.pl
executable file
·132 lines (113 loc) · 2.2 KB
/
obo-remove-case-sensitive-synonyms.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
#!/usr/bin/perl -w
use strict;
my %tag_h=();
my $negate = 0;
while ($ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
if ($opt eq '--neg') {
$negate = 1;
}
if ($opt eq '-t' || $opt eq '--tag') {
$tag_h{shift @ARGV} = 1;
}
}
if (!%tag_h) {
$tag_h{'xref'} = 1;
}
if (!@ARGV) {
print usage();
exit 0;
}
my $id;
my %sh = ();
my @lines = ();
while (<>) {
push(@lines,$_);
chomp;
if (/^id:\s+(\S+)/) {
$id = $1;
}
elsif (/^name:\s+(.*)/) {
$sh{$id}->{lc($1)} = $1;
}
elsif (/^synonym:\s+\"(.*)\"\s\w+\s+\[/) {
my $s = $1;
my $cs = $sh{$id}->{lc($s)};
if ($cs) {
die if $cs eq "1";
# synonym clash - choose best
my $best = bestsyn($cs,$s);
print STDERR "BEST = $best [from \"$cs\" and \"$s\"]\n";
$s = $best;
#if ($cs gt $s) {
# print STDERR "Deprecating \"$s\" for \"$cs\"\n";
# $s = $cs;
# }
# else {
# print STDERR "Replacing \"$cs\" with \"$s\"\n";
# }
}
$sh{$id}->{lc($s)} = $s;
}
}
foreach (@lines) {
chomp;
my $omit = 0;
if (/^id:\s+(\S+)/) {
$id = $1;
}
elsif (/^synonym:\s+\"(.*)\"\s\w+\s+\[/) {
my $s = $1;
my $cs= $sh{$id}->{lc($s)};
if ($cs) {
if ($cs ne $s) {
# case insensitive takes priority
print STDERR "omitting $s [already have $cs]\n";
$omit = 1;
}
}
}
print "$_\n" unless $omit;
}
exit 0;
sub bestsyn {
my $x = shift;
my $y = shift;
my $lc = $x;
my $uc = $y;
if ($x lt $y) { # uc lt lc
$uc = $x;
$lc = $y;
}
my $is_latin_num = ($uc =~ /[CVIX]{2,}/) || ($uc =~ /\[[CVIX]+\]/);
if ($is_latin_num) {
print STDERR "LATIN: $uc\n";
return $uc;
}
if ($uc =~ /\'/) {
print STDERR "PROPER: $uc\n";
return $uc;
}
if ($uc !~ /[a-z]/) {
print STDERR "ACRONYM: $uc\n";
return $uc;
}
return $lc;
}
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [-t tag]* BASE-FILE FILE-TO-MERGE1 [FILE-TO-MERGE2...]
merges in tags to base file
Example:
$sn referenced_file1.obo [referenced_file2.obo ...] source_file.obo
EOM
}