root/trunk/dbbuilder/src/org/bridgedb/util/hmdb/ParseHmdb.java @ 321

Revision 321, 4.6 KB (checked in by martijn, 6 months ago)

New HMDB maker and Quality Control script

  • Property svn:eol-style set to native
Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.bridgedb.util.hmdb;
18
19import java.io.IOException;
20import java.io.LineNumberReader;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23
24/**
25 * Parse Metabocards from Hmdb
26 */
27public class ParseHmdb
28{
29        /**
30         * represents some information from
31         * a single HMDB metabocard
32         */
33        static class Compound
34        {
35                /** store a key, value field */
36                void storeField (String key, String value)
37                {
38                        // check for "Not Available" lines
39                        if (value.equals ("Not Available"))
40                        {
41                                value = null;
42                        }
43                        if (key.equals("name"))
44                        {
45                                symbol = value;
46                        }
47                        else if (key.equals("chemical_formula"))
48                        {
49                                formula = value;
50                        }
51                        else if (key.equals("kegg_compound_id"))
52                        {
53                                idKegg = value == null ? null : value.split("; ");
54                        }
55                        else if (key.equals("biocyc_id"))
56                        {
57                                idBioc = value == null ? null : value.split("; ");
58                        }
59                        else if (key.equals("pubchem_compound_id"))
60                        {
61                                idPubchem = value == null ? null : value.split("; ");
62                        }
63                        else if (key.equals("chebi_id"))
64                        {
65                                idChebi = value == null ? null : value.split("; ");
66                        }
67                        else if (key.equals("cas_number"))
68                        {
69                                idCas = value == null ? null : value.split("; ");
70                        }
71                        else if (key.equals("synonyms"))
72                        {
73                                synonyms = value == null ? null : value.split("; ");
74                        }
75                        else if (key.equals("wikipedia_link"))
76                        {
77                                idWikipedia = value == null ? null : value.split("; ");
78                        }
79                        else if (key.equals("smiles_canonical"))
80                        {
81                                smiles = value;
82                        }
83                }
84
85                String idHmdb = null;
86                String symbol = null;
87                String formula = null;
88                String[] idKegg = null;
89                String[] idPubchem = null;
90                String[] idChebi = null;
91                String[] idCas = null;
92                String[] idBioc = null;
93                String[] idWikipedia = null;
94                String smiles = null;
95                String[] synonyms = null;
96        }
97
98        /**
99         * Signals error while parsing a Metabocards-formatted file.
100         * This exception means that either the file is corrupt,
101         * not a valid metabocards file, or (possibly)
102         * that the metabocards format has changed.
103         */
104        static class ParseException extends Exception
105        {
106                ParseException (String message)
107                {
108                        super(message);
109                }
110
111                ParseException (String message, int lineNo, String line)
112                {
113                        super("Parse error: " + message + " at " + lineNo + "\n" + line);
114                }
115        }
116
117        /**
118         * Reads a single metabocard from a text file.
119         * returns null if there are no more records to read.
120         */
121        Compound readNext (LineNumberReader reader) throws IOException, ParseException
122        {
123                Compound result = new Compound();;
124
125                int state = 0;
126                String line;
127                String key = null;
128                String value = null;
129
130                Pattern p1 = Pattern.compile ("#BEGIN_METABOCARD (HMDB\\d+)");
131                Pattern p2 = Pattern.compile ("#END_METABOCARD (HMDB\\d+)");
132                Pattern p3 = Pattern.compile ("# ([a-zA-Z0-9_]+):");
133
134                while ((line = reader.readLine()) != null)
135                {
136                        int newState = state;
137
138                        switch (state)
139                        {
140                        // state 0: expect begin
141                        case 0:
142                                Matcher m1 = p1.matcher(line);
143                                if (m1.matches())
144                                {
145                                        newState = 1;
146                                        result.idHmdb = m1.group(1);
147                                }
148                                else if (line.equals (""))
149                                {
150                                        // ignore
151                                }
152                                else
153                                {
154                                        throw new ParseException ("begin expected", reader.getLineNumber(), line);
155                                }
156                                break;
157                        // state 1: expect key, end or empty
158                        case 1:
159                                Matcher m2 = p2.matcher (line);
160                                Matcher m3 = p3.matcher (line);
161                                if (m2.matches())
162                                {
163                                        // end
164                                        return result;
165                                }
166                                else if (m3.matches())
167                                {
168                                        // store unless this is the first key
169                                        if (key != null) result.storeField (key, value);
170
171                                        key = m3.group(1);
172                                        value = null;
173                                        newState = 1;
174                                }
175                                else if (line.equals (""))
176                                {
177                                        // ignore
178                                }
179                                else
180                                {
181                                        if (value == null)
182                                        {
183                                                value = line;
184                                        }
185                                        else
186                                        {
187                                                value += "\n" + line;
188                                        }
189                                }
190                                break;
191                        }
192                        state = newState;
193                }
194                if (state != 0)
195                {
196                        // The record was not properly terminated.
197                        throw new ParseException ("Parse error: unexpected end of file");
198                }
199                else
200                {
201                        // end of file reached, no more records
202                        return null;
203                }
204        }
205
206}
Note: See TracBrowser for help on using the browser.