forked from cmungall/obo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo-map-annotations.pl
executable file
·228 lines (195 loc) · 5.37 KB
/
obo-map-annotations.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
#!/usr/bin/perl -w
use strict;
my $verbose;
my $silent = 0;
my $kcol = 0;
my $vcol = 0;
my $filter_unmapped = 0;
my $bin_id;
my $bin_go_terms;
while ($ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
elsif ($opt eq '-s' || $opt eq '--silent') {
$silent = 1;
}
elsif ($opt eq '-v' || $opt eq '--verbose') {
$verbose = 1;
}
elsif ($opt eq '-x' || $opt eq '--filter') {
$filter_unmapped = 1;
}
elsif ($opt eq '-b' || $opt eq '--bin') {
$filter_unmapped = 1;
$bin_id = shift @ARGV;
}
elsif ($opt eq '--bin-go-terms') {
$filter_unmapped = 1;
$bin_go_terms = 1;
}
elsif ($opt eq '-k' || $opt eq '--col') {
$kcol = shift @ARGV;
if ($kcol =~ /(\d+),(\d+)/) {
$kcol = $1;
$vcol = $2;
}
else {
$vcol = $kcol;
}
}
}
if (!@ARGV) {
print usage();
exit 0;
}
my @mapfiles = (shift @ARGV);
my @inputfiles = @ARGV;
# build map
my %idmap = ();
foreach my $mapfile (@mapfiles) {
open(F,$mapfile) || die $mapfile;
while (<F>) {
chomp;
next if /^\!/;
my @vals = split(/\t/,$_);
# try and guess columns
if (!$kcol) {
if (scalar(@vals) == 1) {
# GUESS: filter
$kcol = 1;
$vcol = 1; # filter file
$filter_unmapped = 1;
}
elsif (scalar(@vals) == 2) {
if ($filter_unmapped) {
$kcol = 1;
$vcol = 1;
}
else {
# GUESS: 2 column mapping file
$kcol = 1;
$vcol = 2;
}
}
else {
for (my $i=0; $i<scalar(@vals); $i++) {
if ($vals[$i] =~ /^(\S+):(\S+)/) {
if (!$kcol) {
$kcol = $i+1;
}
elsif (!$vcol) {
if (!$filter_unmapped) {
$vcol = $i+1;
}
}
else {
}
}
}
}
} # -- end of guessing
if (!$vcol) {
die "cannot guess";
}
if (!$kcol) {
die "cannot guess";
}
#print STDERR "$kcol => $vcol\n";
my $v = (scalar(@vals) < $vcol) ? $vals[$kcol-1] : $vals[$vcol-1];
push(@{$idmap{$vals[$kcol-1]}}, $v);
}
close(F);
}
# now perform mappings on the main file
my $n = 0;
my @hdr = ();
my @out = ();
foreach my $f (@inputfiles) {
open(F,$f) || die $f;
while (<F>) {
chomp;
my @vals = split(/\t/,$_);
my $v = $vals[4];
if (!$v) {
# probably in header..
print "$_\n";
}
elsif ($idmap{$v}) {
foreach (@{$idmap{$v}}) {
$vals[4] = $_;
print join("\t",@vals)."\n";
}
}
elsif ($filter_unmapped) {
# no mapping
if ($bin_id) {
# put in bin
$vals[4] = $bin_id;
print join("\t",@vals)."\n";
}
elsif ($bin_go_terms) {
my $aspect = $vals[8];
my $bin;
if ($aspect eq 'P') {
$bin = 'GO:0008150';
}
elsif ($aspect eq 'F') {
$bin = 'GO:0003674';
}
elsif ($aspect eq 'C') {
$bin = 'GO:0005575';
}
else {
die "does not appear to be a GO GAF";
}
$vals[4] = $bin;
print join("\t",@vals)."\n";
}
else {
# drop unmapped
}
}
else {
# no mapping, print original
print "$_\n";
}
}
close(F);
}
#printf STDERR "Fixed: $n\n";
exit 0;
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn MAPPING-FILE FILE-TO-MAP [FILE-TO-MAP ...]
maps annotations using a mapping file(s)
This can be used for purposes including mapping to a subset or "slim"
The mapping file is typically two column, tab separated
COL1: Source GO term
COL2: Target GO term
No inference is performed when mapping - if you want to use the
relationships in the ontology, then this should be incorporated into
the mapping file.
If an annotation is to a GO term without a mapping, then the
annotation is dropped, unless --bin-go-terms is specified. If this is
specified, then the annotation is mapped up to the relevant root node
in F/P/C depending on the aspect.
MAPPING FILES:
If your mapping file does not conform to the 2-column format above,
you can use the -k option. E.g.
-k3,4
Uses col3 to read the source GO IDs, and col4 to read the targets
If your mapping file has only one column, it is treated as a filter
file. Any annotation to a term not in this is dropped.
If your file has more than two columns, and the -k opt is not
specified, then the format of the mapping file is guessed.
Lines beginning with ! are ignored
EOM
}