JAL-2245 checks for null added (in case EMBL fetch finds nothing)
[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   /**
50    * @return the entries
51    */
52   public Vector<EmblEntry> getEntries()
53   {
54     return entries;
55   }
56
57   /**
58    * @param entries
59    *          the entries to set
60    */
61   public void setEntries(Vector<EmblEntry> entries)
62   {
63     this.entries = entries;
64   }
65
66   /**
67    * @return the errors
68    */
69   public Vector<EmblError> getErrors()
70   {
71     return errors;
72   }
73
74   /**
75    * @param errors
76    *          the errors to set
77    */
78   public void setErrors(Vector<EmblError> errors)
79   {
80     this.errors = errors;
81   }
82
83   /**
84    * Parse an EmblXML file into an EmblFile object
85    * 
86    * @param file
87    * @return parsed EmblXML or null if exceptions were raised
88    */
89   public static EmblFile getEmblFile(File file)
90   {
91     if (file == null)
92     {
93       return null;
94     }
95     try
96     {
97       return EmblFile.getEmblFile(new FileReader(file));
98     } catch (Exception e)
99     {
100       System.err.println("Exception whilst reading EMBLfile from " + file);
101       e.printStackTrace(System.err);
102     }
103     return null;
104   }
105
106   public static EmblFile getEmblFile(Reader file)
107   {
108     EmblFile record = new EmblFile();
109     try
110     {
111       // 1. Load the mapping information from the file
112       Mapping map = new Mapping(record.getClass().getClassLoader());
113
114       java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
115       map.loadMapping(url);
116
117       // 2. Unmarshal the data
118       Unmarshaller unmar = new Unmarshaller(record);
119       try
120       {
121         // uncomment to DEBUG EMBLFile reading
122         if (jalview.bin.Cache.getDefault(jalview.bin.Cache.CASTORLOGLEVEL,
123                 "debug").equalsIgnoreCase("DEBUG"))
124         {
125           unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());
126         }
127       } catch (Exception e)
128       {
129       }
130       unmar.setIgnoreExtraElements(true);
131       unmar.setIgnoreExtraAttributes(true);
132       unmar.setMapping(map);
133       unmar.setLogWriter(new PrintWriter(System.out));
134       record = (EmblFile) unmar.unmarshal(file);
135
136       canonicaliseDbRefs(record);
137     } catch (Exception e)
138     {
139       e.printStackTrace(System.err);
140       record = null;
141     }
142
143     return record;
144   }
145
146   /**
147    * Change blank version to "0" in any DBRefEntry, to ensure consistent
148    * comparison with other DBRefEntry in Jalview
149    * 
150    * @param record
151    * @see Uniprot#getDbVersion
152    */
153   static void canonicaliseDbRefs(EmblFile record)
154   {
155     if (record.getEntries() == null)
156     {
157       return;
158     }
159     for (EmblEntry entry : record.getEntries())
160     {
161       if (entry.getDbRefs() != null)
162       {
163         for (DBRefEntry dbref : entry.getDbRefs())
164         {
165           if ("".equals(dbref.getVersion()))
166           {
167             dbref.setVersion("0");
168           }
169         }
170       }
171
172       if (entry.getFeatures() != null)
173       {
174         for (EmblFeature feature : entry.getFeatures())
175         {
176           if (feature.getDbRefs() != null)
177           {
178             for (DBRefEntry dbref : feature.getDbRefs())
179             {
180               if ("".equals(dbref.getVersion()))
181               {
182                 dbref.setVersion("0");
183               }
184             }
185           }
186         }
187       }
188     }
189   }
190 }