Debian Codegraph @

GO/Handlers/godb_prestore.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # $Id: godb_prestore.pm,v 1.9 2005/03/22 22:38:11 cmungall Exp $
2 #
3 # This GO module is maintained by Chris Mungall <[email protected]>
4 #
5 # see also - http://www.geneontology.org
6 # - http://www.godatabase.org/dev
7 #
8 # You may distribute this module under the same terms as perl itself
9
10 =head1 NAME
11
12 GO::Handlers::godb_prestore - transforms OBO XML to GODB XML
13
14 =head1 SYNOPSIS
15
16
17
18 =head1 DESCRIPTION
19
20 This is a transform for turning OBO XML events into XML events that
21 are isomorphic to the GO Database (ie XML element names match GO DB
22 table and column names).
23
24 This transformation is suitable for direct loading into a db using the
25 generic DBIx::DBStag loader (see http://stag.sourceforge.net)
26
27 This perl transform may later be replaced by an XSL transform (for
28 speed)
29
30 =head1 PUBLIC METHODS -
31
32 =cut
33
34 # makes objects from parser events
35
36 package GO::Handlers::godb_prestore;
37 use Data::Stag qw(:all);
38 use base qw(GO::Handlers::base);
39
40 use strict;
41 use Carp;
42 use Data::Dumper;
43 use Data::Stag qw(:all);
44 use GO::Parsers::ParserEventNames; # XML constants for elements
45
46 sub EMITS {
47 qw(
48 dbstag_metadata
49 source_audit
50 term
51 gene_product
52 )
53 }
54
55 sub is_transform { 1 }
56
57 sub dbstag_mapping {
58 return([
59 "type/term_synonym.synonym_type_id=term.id",
60 "term1/term2term.term1_id=term.id",
61 "term2/term2term.term2_id=term.id",
62 "type/term2term.relationship_type_id=term.id",
63 "type/gene_product.type_id=term.id",
64 "source_db/association.source_db_id=db.id",
65 ]
66 );
67 }
68
69 sub e_source {
70 my $self = shift;
71 my $source = shift;
72 stag_name($source, 'source_audit');
73 return $source;
74 }
75
76 sub e_header {
77 my ($self, $hdr) = @_;
78 [dbstag_metadata=>[
79 map {[map=>$_]} @{$self->dbstag_mapping}
80 ]
81 ];
82 }
83
84 sub e_term {
85 my ($self, $term) = @_;
86 my $id = stag_get($term, ID) || $self->throw($term->sxpr);
87 warn("DEPRECATED - use XSL instead");
88 stag_set($term, ACC, $id);
89 my $name_h = $self->{name_h};
90 my $name = stag_get($term, NAME);
91 my $ont = stag_get($term, NAMESPACE);
92 my $is_obs = stag_get($term, IS_OBSOLETE);
93
94 my @extra_events = ();
95 my @is_as = stag_get($term, IS_A);
96 my @rels = (stag_get($term, RELATIONSHIP),
97 map {[relationship=>[[to=>$_],[type=>'is_a']]]} @is_as);
98 if ($is_obs) {
99 my $obs = "obsolete_$ont";
100 @rels = ([relationship=>[[to=>$obs],
101 [type=>'is_a']]]);
102 if ($obs eq $id) {
103 # place root obs under root
104 @rels = ([relationship=>[[to=>$self->{__added_root} || 'all'],
105 [type=>'is_a']]]);
106 }
107 if (!$self->{"__added_obsnode_$ont"}) {
108 $self->{"__added_obsnode_$ont"} = $obs;
109 push(@extra_events,
110 $self->e_term(Data::Stag->new(term=>[[id=>$obs],
111 [name=>$obs],
112 [namespace=>$ont],
113 [is_obsolete=>1],
114 ]
115 )
116 )
117 );
118 }
119 # in go format, obsolete nodes may have 'fake' parentage;
120 # either to root (for root obsolete term) or to root
121 # obsolete term;
122 # here we replace this with $obs
123 }
124 my @cvrels =
125 map {
126 my $to = stag_get($_=>TO);
127 my $type = stag_get($_=>TYPE);
128 Data::Stag->new(term2term=>[
129 [term1=>[[term=>[[acc=>$to]]]]],
130 [term2=>[[term=>[[acc=>$id]]]]],
131 _type($type, 'relationship'),
132 ]
133 );
134 } @rels;
135
136 if ($name && !@cvrels) {
137 # add 'all' node;
138 # only for named node (to avoid adding root when parsing defs)
139 # and when there are no parents
140 # GO DB relies on having one single root node
141 my $all = $self->{__added_root};
142 if ($all) {
143 }
144 else {
145 my $alldef = 'This term is the most general term possible.';
146 $all = 'all';
147 $self->{__added_root} = $all;
148 @cvrels =
149 (Data::Stag->new(term=>[
150 [acc=>'all'],
151 [name=>'all'],
152 [term_definition=>[
153 [term_definition=>$alldef],
154 ]],
155 [is_root=>1],
156 [term_type=>'universal'],
157 ])
158 );
159 }
160 push(@cvrels,
161 Data::Stag->new(term2term=>[
162 [term1=>[[term=>[[acc=>$all]]]]],
163 [term2=>[[term=>[[acc=>$id]]]]],
164 _type('is_a', 'relationship'),
165 ]
166 )
167 );
168 }
169
170 my $def = stag_get($term, DEF);
171 my @alt_ids = stag_get($term, ALT_ID);
172 my @syns = stag_get($term, SYNONYM);
173
174 my $comment = stag_find($term, COMMENT); # comment can be under term or def
175 my $t = time;
176 my $nuterm =
177 Data::Stag->new(term=>
178 [
179 [acc=>$id],
180 $name ? [NAME=>$name] : (),,
181 $ont ? [term_type=>$ont] : (),
182 defined $is_obs ? [is_obsolete=>$is_obs && $is_obs ne 'false' ? 1:0] : (),
183 $name ? [term_audit=>[
184 [term_loadtime=>$t]
185 ]] : (),
186 ]
187 );
188 my @term_dbxrefs =
189 map {
190 Data::Stag->new(term_dbxref=>[[dbxref=>[_xref_stags($_)]]])
191 } stag_get($term, XREF_ANALOG);
192 if ($def || $comment) {
193 stag_set($nuterm, term_definition=>
194 [
195 ($def ? [term_definition=>stag_sget($def, DEFSTR)]:()),
196 ($comment ? [term_comment=>$comment] : ()),
197 ]);
198 if ($def) {
199 push(@term_dbxrefs,
200 Data::Stag->new(term_dbxref=>[[is_for_definition=>1],
201 [dbxref=>[_xref_stags($_)]]]))
202 foreach stag_get($def, DBXREF);
203 }
204 }
205 stag_add($nuterm, TERM_DBXREF, $_) foreach @term_dbxrefs;
206 stag_add($nuterm, term_synonym=>
207 [
208 [acc_synonym=>$_],
209 [term_synonym=>$_],
210 _type('acc','synonym'),
211 ])
212 foreach @alt_ids;
213
214 foreach (@syns) {
215 my $type = stag_sget($_, TYPE);
216 stag_add($nuterm, term_synonym=>
217 [
218 $type ? _type($type,'synonym') : (),
219 [term_synonym=>stag_sget($_, SYNONYM_TEXT)]
220 ]);
221 }
222 return (@extra_events,$nuterm,@cvrels);
223 }
224
225 sub e_typedef {
226 my ($self, $t) = @_;
227 my $id = stag_get($t, ID);
228 return
229 Data::Stag->new(term=>[
230 [acc=>$id],
231 [name=>$id]
232 ]);
233 }
234
235 sub e_prod {
236 my ($self, $prod) = @_;
237 my $proddb = stag_sget($self->up(1), PRODDB);
238 my $full_name = stag_sget($prod, PRODNAME);
239 my @gpstags =
240 (
241 [symbol=>stag_sget($prod, PRODSYMBOL)],
242 ($full_name ? [full_name=>$full_name] : ()),
243 [dbxref=>[[xref_dbname=>$proddb],
244 [xref_key=>stag_sget($prod, PRODACC)]]],
245 [species=>[[ncbi_taxa_id=>stag_sget($prod, PRODTAXA)]]],
246 _type(stag_sget($prod,TYPE) || 'gene', 'gene_product'),
247 (map {
248 [gene_product_synonym=>[[product_synonym=>$_]]]
249 } stag_get($prod,PRODSYN)),
250 );
251 my @assocs = stag_get($prod,ASSOC);
252 foreach my $assoc (@assocs) {
253 my @quals = stag_sget($assoc,QUALIFIER);
254 my $source_db = stag_sget($assoc, SOURCE_DB);
255 push(@gpstags,
256 [association=>[
257 [term=>[[acc=>stag_sget($assoc,TERMACC)]]],
258 [is_not=>stag_sget($assoc,IS_NOT)],
259 (map {
260 [association_qualifier=>[[term=>[[acc=>$_]]]]]
261 } @quals),
262 # ($qual ? [qualifier=>[[term=>[[acc=>$qual]]]]]: ()),
263 ($source_db ? [source_db=>[[db=>[[name=>$source_db]]]]] : ()),
264 [assocdate=>stag_sget($assoc,ASSOCDATE)],
265 (map {
266 my @withs = stag_get($_,WITH);
267 [evidence=>[
268 [code=>stag_sget($_,EVCODE)],
269 (scalar(@withs) ? [seq_acc=>join('|',@withs)] : ()),
270 _xref(stag_sget($_,REF)),
271 scalar(@withs) ?
272 [evidence_dbxref=>[
273 (map {
274 _xref($_)
275 } @withs)
276 ]] : (),
277 ]]
278 } stag_get($assoc,EVIDENCE))
279 ]
280 ]
281 );
282
283 }
284 return Data::Stag->new(gene_product=>[@gpstags]);
285 }
286
287 sub _xref_stags {
288 my $x = shift;
289 ([xref_key=>stag_sget($x,ACC)],
290 [xref_dbname=>stag_sget($x,DBNAME)]);
291 }
292
293 sub _type {
294 my $type = shift;
295 my $ont = shift;
296 [type=>[[term=>[
297 [acc=>$type],
298 [name=>$type],
299 [term_type=>$ont],
300 ]
301 ]]
302 ];
303
304 }
305
306 sub _xref {
307 my $id = shift;
308 my ($dbname,$acc);
309 if ($id =~ /(\w+):(\S+)/) {
310 $dbname = $1;
311 $acc = $2;
312 }
313 else {
314 $dbname = '';
315 $acc = $id;
316 }
317 return
318 [dbxref=>[
319 [xref_dbname=>$dbname],
320 [xref_key=>$acc]
321 ]
322 ];
323 }
324
325
326 1;