forked from cmungall/obo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo-multi-merge.pl
executable file
·153 lines (131 loc) · 2.48 KB
/
obo-multi-merge.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
#!/usr/bin/perl -w
use strict;
my %tag_h=();
my $noheader;
my $mapf;
my %prec= ();
while ($ARGV[0] =~ /^\-.+/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
if ($opt eq '-m' || $opt eq '--map-file') {
$mapf = shift @ARGV;
}
if ($opt eq '-p' || $opt eq '--prec') {
my $a = shift @ARGV;
my $b = shift @ARGV;
$prec{$a}->{$b} = 1;
}
if ($opt eq '--noheader') {
$noheader = 1;
}
}
if (!$mapf) {
$mapf = shift @ARGV;
}
my %maph = ();
open(F,$mapf) || die $mapf;
while(<F>) {
chomp;
my ($src,$tgt) = split(/\t/,$_);
$src =~ s/\-.*//;
$tgt =~ s/\-.*//;
my ($src_db) = ($src =~ /^(\w+):/);
my ($tgt_db) = ($tgt =~ /^(\w+):/);
if ($prec{$tgt_db}->{$src_db}) {
$maph{$tgt} = $src;
}
else {
$maph{$src} = $tgt;
}
}
my %KEEP =
(is_a=>1,
relationship=>1,
synonym=>1);
my @lines = ();
my $id;
my %tvh = ();
while (<>) {
push(@lines, $_);
chomp;
if (/^id:\s*(\S+)/) {
$id = $1;
}
else {
if (/(\S+):\s*(.*)/) {
my ($t,$v) = ($1,$2);
if ($KEEP{$t}) {
#print STDERR "TV: $t=$v\n";
push(@{$tvh{$id}},[$t,$v]);
}
}
}
}
# inverse map
my %rmaph = ();
foreach my $k (keys %maph) {
#print STDERR "RMAP: '$maph{$k}' << '$k'\n";
push(@{$rmaph{$maph{$k}}}, $k);
}
my $elim;
my @out = ();
my $n = 0;
while ($_ = shift @lines) {
chomp;
if (/^\[/) {
$elim = 0;
}
next if $elim;
push(@out, $_);
if (/^id:\s*(\S+)/) {
$id = $1;
if ($maph{$id}) {
#print STDERR "elim: $id\n";
$n++;
while (@out) {
my $last = pop @out;
if ($last =~ /^\[/) {
last;
}
if ($last =~ /^\s*$/) {
last;
}
}
$elim = 1;
next;
}
print STDERR "ID: '$id'\n";
foreach my $alt_id (@{$rmaph{$id}}) {
print STDERR "merged: $alt_id --> $id\n";
push(@out, "alt_id: $alt_id");
foreach my $tv (@{$tvh{$alt_id}}) {
push(@out, "$tv->[0]: $tv->[1]");
}
}
}
}
foreach (@out) {
print "$_\n";
}
print STDERR "DONE: $n\n";
exit(0);
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [ OBO-FILE [OBO-FILE2...]
Merges multiple classes based on equivalence mapping file
Example:
$sn -p HP MP equiv.txt mammalian_phenotype.obo human_phenotype.obo
equiv.txt is a list of pairs, src-> tgt, where src is merged into tgt
(i.e. tgt has precedence)
-p SRC TGT
merge all SRC into TGT
EOM
}