8a32c130c9230c6ac7a4e4e5fa52700200fda18a
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel.xdb.embl;
22
23 import jalview.datamodel.DBRefEntry;
24 import jalview.ws.dbsources.Uniprot;
25
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.PrintWriter;
29 import java.io.Reader;
30 import java.util.Vector;
31
32 import org.exolab.castor.mapping.Mapping;
33 import org.exolab.castor.xml.Unmarshaller;
34
35 /**
36  * Data model for entries returned from an EMBL query, as marshalled by a Castor
37  * binding file
38  * 
39  * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
40  * 
41  * @see embl_mapping.xml
42  */
43 public class EmblFile
44 {
45   Vector<EmblEntry> entries;
46
47   Vector<EmblError> errors;
48
49   String text;
50
51   /**
52    * @return the entries
53    */
54   public Vector<EmblEntry> getEntries()
55   {
56     return entries;
57   }
58
59   /**
60    * @param entries
61    *          the entries to set
62    */
63   public void setEntries(Vector<EmblEntry> entries)
64   {
65     this.entries = entries;
66   }
67
68   /**
69    * @return the errors
70    */
71   public Vector<EmblError> getErrors()
72   {
73     return errors;
74   }
75
76   /**
77    * @param errors
78    *          the errors to set
79    */
80   public void setErrors(Vector<EmblError> errors)
81   {
82     this.errors = errors;
83   }
84
85   /**
86    * Parse an EmblXML file into an EmblFile object
87    * 
88    * @param file
89    * @return parsed EmblXML or null if exceptions were raised
90    */
91   public static EmblFile getEmblFile(File file)
92   {
93     if (file == null)
94     {
95       return null;
96     }
97     try
98     {
99       return EmblFile.getEmblFile(new FileReader(file));
100     } catch (Exception e)
101     {
102       System.err.println("Exception whilst reading EMBLfile from " + file);
103       e.printStackTrace(System.err);
104     }
105     return null;
106   }
107
108   public static EmblFile getEmblFile(Reader file)
109   {
110     EmblFile record = new EmblFile();
111     try
112     {
113       // 1. Load the mapping information from the file
114       Mapping map = new Mapping(record.getClass().getClassLoader());
115
116       java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
117       map.loadMapping(url);
118
119       // 2. Unmarshal the data
120       Unmarshaller unmar = new Unmarshaller(record);
121       try
122       {
123         // uncomment to DEBUG EMBLFile reading
124         if (jalview.bin.Cache
125                 .getDefault(jalview.bin.Cache.CASTORLOGLEVEL, "debug")
126                 .equalsIgnoreCase("DEBUG"))
127         {
128           unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());
129         }
130       } catch (Exception e)
131       {
132       }
133       unmar.setIgnoreExtraElements(true);
134       unmar.setIgnoreExtraAttributes(true);
135       unmar.setMapping(map);
136       unmar.setLogWriter(new PrintWriter(System.out));
137       record = (EmblFile) unmar.unmarshal(file);
138
139       canonicaliseDbRefs(record);
140     } catch (Exception e)
141     {
142       e.printStackTrace(System.err);
143       record = null;
144     }
145
146     return record;
147   }
148
149   /**
150    * Change blank version to "0" in any DBRefEntry, to ensure consistent
151    * comparison with other DBRefEntry in Jalview
152    * 
153    * @param record
154    * @see Uniprot#getDbVersion
155    */
156   static void canonicaliseDbRefs(EmblFile record)
157   {
158     if (record.getEntries() == null)
159     {
160       return;
161     }
162     for (EmblEntry entry : record.getEntries())
163     {
164       if (entry.getDbRefs() != null)
165       {
166         for (DBRefEntry dbref : entry.getDbRefs())
167         {
168           if ("".equals(dbref.getVersion()))
169           {
170             dbref.setVersion("0");
171           }
172         }
173       }
174
175       if (entry.getFeatures() != null)
176       {
177         for (EmblFeature feature : entry.getFeatures())
178         {
179           if (feature.getDbRefs() != null)
180           {
181             for (DBRefEntry dbref : feature.getDbRefs())
182             {
183               if ("".equals(dbref.getVersion()))
184               {
185                 dbref.setVersion("0");
186               }
187             }
188           }
189         }
190       }
191     }
192   }
193
194   public String getText()
195   {
196     return text;
197   }
198
199   public void setText(String text)
200   {
201     this.text = text;
202   }
203 }