forked from cmungall/obo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo-merge-for-visualization.pl
executable file
·156 lines (132 loc) · 3.09 KB
/
obo-merge-for-visualization.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
#!/usr/bin/perl -w
use strict;
my $idspace;
my $verbose;
my $filter_dangling = 1; # default
my $rel;
my $diffrel = "other_isa";
while ($ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
elsif ($opt eq '--rel') {
$rel = shift;
}
elsif ($opt eq '--idspace') {
$idspace = shift @ARGV;
}
elsif ($opt eq '--verbose' || $opt eq '-v') {
$verbose = 1;
}
elsif ($opt eq '-') {
}
else {
die "$opt";
}
}
# ----------------------------------------
# load files
# ----------------------------------------
my ($h1,$b1) = readfile(shift @ARGV);
my ($h2,$b2) = readfile(shift @ARGV);
# ----------------------------------------
# headers
# ----------------------------------------
foreach (@$h1) {
print "$_\n" unless /^\s*$/;
}
print "subsetdef: unique_term1 \"unique to first ontology in comparison\"\n";
print "subsetdef: unique_term2 \"unique to first ontology in comparison\"\n";
foreach (@$h2) {
if (/^(synonymtypedef|subsetdef)/) {
print "$_\n";
}
}
print "\n";
# ----------------------------------------
# write merged file
# ----------------------------------------
foreach my $k (%$b1) {
my $v1 = $b1->{$k};
my $v2 = $b2->{$k};
foreach my $line (@$v1) {
print "$line\n";
if ($line =~ /^id:/) {
if ($v2) {
foreach my $line2 (@$v2) {
if ($line2 =~ /^is_a:\s*(\S+)(.*)/) {
print "relationship: $diffrel $1 $2\n";
}
}
delete $b2->{$k};
}
else {
print "subset: unique_term1\n";
}
}
}
}
# print any remaining entries from file 2
foreach my $k (%$b2) {
my $v2 = $b2->{$k};
foreach my $line (@$v2) {
print "$line\n";
if ($line =~ /^id:/) {
print "subset: unique_term2\n";
}
}
}
print "\n[Typedef]\n";
print "id: $diffrel\n";
print "name: $diffrel\n";
exit 0;
# ----------------------------------------
# utils
# ----------------------------------------
sub readfile {
my $fn = shift;
my $hdr = 1;
my $id = 0;
open(F,$fn) || die $fn;
my $block = [];
my %bh = ();
my @headers = ();
while (<F>) {
chomp;
if (/^\[/) {
$hdr = 0;
$id = 0;
$block = [];
}
if ($hdr) {
push(@headers,$_);
}
else {
if (/^id:\s*(\S+)/) {
$id = $1;
$bh{$id} = $block;
}
push(@$block,$_);
}
}
close(F);
return (\@headers,\%bh);
}
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [--rel DIFF-RELATION] FILE
Example:
$sn --rel is_a goche.obo chebi.obo > visualize-goche-chebi-diffs.obo
diffs relationships between two files and produces an obo file that
shows the differences in a way that can be visualized in oboedit
TODO:
currently only isa links are diffed
EOM
}