|
1
|
|
package baseCode.xml;
|
|
2
|
|
|
|
3
|
|
import java.io.IOException;
|
|
4
|
|
import java.io.InputStream;
|
|
5
|
|
import java.util.HashMap;
|
|
6
|
|
import java.util.Iterator;
|
|
7
|
|
import java.util.Map;
|
|
8
|
|
|
|
9
|
|
import org.xml.sax.Attributes;
|
|
10
|
|
import org.xml.sax.InputSource;
|
|
11
|
|
import org.xml.sax.SAXException;
|
|
12
|
|
import org.xml.sax.XMLReader;
|
|
13
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
14
|
|
import org.xml.sax.helpers.XMLReaderFactory;
|
|
15
|
|
|
|
16
|
|
import baseCode.bio.GOEntry;
|
|
17
|
|
import baseCode.dataStructure.graph.DirectedGraph;
|
|
18
|
|
import baseCode.dataStructure.graph.DirectedGraphNode;
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
public class GOParser {
|
|
29
|
|
|
|
30
|
|
private DirectedGraph m;
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
1
|
public DirectedGraph getGraph() {
|
|
38
|
1
|
return m;
|
|
39
|
|
}
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
0
|
public Map getGONameMap() {
|
|
48
|
0
|
Map nodes = m.getItems();
|
|
49
|
0
|
Map result = new HashMap();
|
|
50
|
0
|
for ( Iterator it = nodes.keySet().iterator(); it.hasNext(); ) {
|
|
51
|
0
|
DirectedGraphNode node = ( DirectedGraphNode ) nodes.get( it.next() );
|
|
52
|
0
|
GOEntry e = ( GOEntry ) node.getItem();
|
|
53
|
0
|
result.put( e.getId().intern(), e.getName().intern() );
|
|
54
|
|
}
|
|
55
|
0
|
return result;
|
|
56
|
|
}
|
|
57
|
|
|
|
58
|
1
|
public GOParser( InputStream i ) throws IOException, SAXException {
|
|
59
|
|
|
|
60
|
1
|
if ( i.available() == 0 ) {
|
|
61
|
0
|
throw new IOException( "XML stream contains no data." );
|
|
62
|
|
}
|
|
63
|
|
|
|
64
|
1
|
System.setProperty( "org.xml.sax.driver",
|
|
65
|
|
"org.apache.xerces.parsers.SAXParser" );
|
|
66
|
|
|
|
67
|
1
|
XMLReader xr = XMLReaderFactory.createXMLReader();
|
|
68
|
1
|
GOHandler handler = new GOHandler();
|
|
69
|
1
|
xr.setFeature( "http://xml.org/sax/features/validation", false );
|
|
70
|
1
|
xr.setFeature( "http://xml.org/sax/features/external-general-entities",
|
|
71
|
|
false );
|
|
72
|
1
|
xr.setFeature(
|
|
73
|
|
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
|
|
74
|
|
false );
|
|
75
|
1
|
xr.setContentHandler( handler );
|
|
76
|
1
|
xr.setErrorHandler( handler );
|
|
77
|
1
|
xr.setEntityResolver( handler );
|
|
78
|
1
|
xr.setDTDHandler( handler );
|
|
79
|
1
|
xr.parse( new InputSource( i ) );
|
|
80
|
|
|
|
81
|
1
|
m = handler.getResults();
|
|
82
|
|
}
|
|
83
|
|
|
|
84
|
|
}
|
|
85
|
|
|
|
86
|
|
class GOHandler extends DefaultHandler {
|
|
87
|
|
|
|
88
|
|
private DirectedGraph m;
|
|
89
|
|
|
|
90
|
1
|
public DirectedGraph getResults() {
|
|
91
|
1
|
return m;
|
|
92
|
|
}
|
|
93
|
|
|
|
94
|
1
|
public GOHandler() {
|
|
95
|
1
|
super();
|
|
96
|
1
|
m = new DirectedGraph();
|
|
97
|
|
}
|
|
98
|
|
|
|
99
|
|
private boolean inTerm = false;
|
|
100
|
|
private boolean inDef = false;
|
|
101
|
|
private boolean inAcc = false;
|
|
102
|
|
private boolean inName = false;
|
|
103
|
|
private boolean inPartOf = false;
|
|
104
|
|
private boolean inIsa = false;
|
|
105
|
|
private boolean inSyn = false;
|
|
106
|
|
|
|
107
|
|
private String currentAspect;
|
|
108
|
|
private StringBuffer nameBuf;
|
|
109
|
|
private StringBuffer accBuf;
|
|
110
|
|
private StringBuffer defBuf;
|
|
111
|
|
|
|
112
|
917
|
public void startElement( String uri, String name, String qName,
|
|
113
|
|
Attributes atts ) {
|
|
114
|
|
|
|
115
|
917
|
if ( name.equals( "term" ) ) {
|
|
116
|
68
|
inTerm = true;
|
|
117
|
849
|
} else if ( name.equals( "accession" ) ) {
|
|
118
|
68
|
accBuf = new StringBuffer();
|
|
119
|
68
|
inAcc = true;
|
|
120
|
781
|
} else if ( name.equals( "definition" ) ) {
|
|
121
|
65
|
defBuf = new StringBuffer();
|
|
122
|
65
|
inDef = true;
|
|
123
|
716
|
} else if ( name.equals( "is_a" ) ) {
|
|
124
|
77
|
inIsa = true;
|
|
125
|
77
|
String res = atts.getValue( "rdf:resource" );
|
|
126
|
77
|
String parent = res.substring( res.lastIndexOf( '#' ) + 1, res
|
|
127
|
|
.length() );
|
|
128
|
|
|
|
129
|
77
|
if ( !m.containsKey( parent ) ) {
|
|
130
|
9
|
m.addNode( parent, new GOEntry( parent, "no name yet",
|
|
131
|
|
"no definition yet", "no aspect yet" ) );
|
|
132
|
|
}
|
|
133
|
77
|
String currentTerm = accBuf.toString();
|
|
134
|
77
|
m.addParentTo( currentTerm, parent );
|
|
135
|
|
|
|
136
|
639
|
} else if ( name.equals( "part_of" ) ) {
|
|
137
|
1
|
inPartOf = true;
|
|
138
|
1
|
String res = atts.getValue( "rdf:resource" );
|
|
139
|
1
|
String parent = res.substring( res.lastIndexOf( '#' ) + 1, res
|
|
140
|
|
.length() );
|
|
141
|
|
|
|
142
|
1
|
if ( !m.containsKey( parent ) ) {
|
|
143
|
0
|
m.addNode( parent, new GOEntry( parent, "no name yet",
|
|
144
|
|
"no definition yet", "no aspect yet" ) );
|
|
145
|
|
}
|
|
146
|
1
|
String currentTerm = accBuf.toString();
|
|
147
|
1
|
m.addParentTo( currentTerm, parent );
|
|
148
|
638
|
} else if ( name.equals( "synonym" ) ) {
|
|
149
|
29
|
inSyn = true;
|
|
150
|
609
|
} else if ( name.equals( "name" ) ) {
|
|
151
|
68
|
nameBuf = new StringBuffer();
|
|
152
|
68
|
inName = true;
|
|
153
|
|
}
|
|
154
|
|
}
|
|
155
|
|
|
|
156
|
917
|
public void endElement( String uri, String name, String qName ) {
|
|
157
|
917
|
if ( name.equals( "term" ) ) {
|
|
158
|
68
|
inTerm = false;
|
|
159
|
849
|
} else if ( name.equals( "accession" ) ) {
|
|
160
|
68
|
inAcc = false;
|
|
161
|
68
|
String currentTerm = accBuf.toString();
|
|
162
|
68
|
m.addNode( currentTerm, new GOEntry( currentTerm, "no name yet",
|
|
163
|
|
"no definition yet", "no aspect yet" ) );
|
|
164
|
781
|
} else if ( name.equals( "definition" ) ) {
|
|
165
|
65
|
String currentTerm = accBuf.toString();
|
|
166
|
65
|
( ( GOEntry ) m.getNodeContents( currentTerm ) ).setDefinition( defBuf
|
|
167
|
|
.toString().intern() );
|
|
168
|
65
|
inDef = false;
|
|
169
|
716
|
} else if ( name.equals( "is_a" ) ) {
|
|
170
|
77
|
inIsa = false;
|
|
171
|
639
|
} else if ( name.equals( "part_of" ) ) {
|
|
172
|
1
|
inPartOf = false;
|
|
173
|
638
|
} else if ( name.equals( "synonym" ) ) {
|
|
174
|
29
|
inSyn = false;
|
|
175
|
609
|
} else if ( name.equals( "name" ) ) {
|
|
176
|
68
|
inName = false;
|
|
177
|
68
|
String currentTerm = accBuf.toString();
|
|
178
|
|
|
|
179
|
68
|
String currentName = nameBuf.toString().intern();
|
|
180
|
|
|
|
181
|
68
|
( ( GOEntry ) m.getNodeContents( currentTerm ) ).setName( currentName );
|
|
182
|
|
|
|
183
|
68
|
if ( currentName.equals( "molecular_function" )
|
|
184
|
|
|| currentName.equals( "biological_process" )
|
|
185
|
|
|| currentName.equals( "cellular_component" ) ) {
|
|
186
|
1
|
currentAspect = currentName;
|
|
187
|
|
}
|
|
188
|
|
|
|
189
|
68
|
( ( GOEntry ) m.getNodeContents( currentTerm ) )
|
|
190
|
|
.setAspect( currentAspect );
|
|
191
|
|
|
|
192
|
|
}
|
|
193
|
|
}
|
|
194
|
|
|
|
195
|
1770
|
public void characters( char ch[], int start, int length ) {
|
|
196
|
|
|
|
197
|
1770
|
if ( inTerm ) {
|
|
198
|
1698
|
if ( inAcc ) {
|
|
199
|
68
|
accBuf.append( ch, start, length );
|
|
200
|
1630
|
} else if ( inDef ) {
|
|
201
|
70
|
defBuf.append( ch, start, length );
|
|
202
|
1560
|
} else if ( inName ) {
|
|
203
|
69
|
nameBuf.append( ch, start, length );
|
|
204
|
|
}
|
|
205
|
|
}
|
|
206
|
|
}
|
|
207
|
|
|
|
208
|
|
}
|