JAL-2245 defence against NullPointer if no EMBL data unmarshalled
[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.bin.Cache;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.ws.dbsources.Uniprot;
26
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.PrintWriter;
30 import java.io.Reader;
31 import java.net.URL;
32 import java.util.Vector;
33
34 import org.exolab.castor.mapping.Mapping;
35 import org.exolab.castor.xml.Unmarshaller;
36
37 /**
38  * Data model for entries returned from an EMBL query, as marshalled by a Castor
39  * binding file
40  * 
41  * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
42  * 
43  * @see embl_mapping.xml
44  */
45 public class EmblFile
46 {
47   Vector<EmblEntry> entries;
48
49   Vector<EmblError> errors;
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 Embl XML file into an EmblEntry object
87    * 
88    * @param file
89    * @return parsed EmblXML or null if exceptions were raised
90    */
91   public static EmblEntry getEmblEntry(File file)
92   {
93     if (file == null)
94     {
95       return null;
96     }
97     try
98     {
99       return EmblFile.getEntry(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   /**
109    * Reads the XML response from file and unmarshals into a Java object
110    * 
111    * @param fileReader
112    * @return
113    */
114   public static EmblEntry getEntry(Reader fileReader)
115   {
116     EmblEntry record = new EmblEntry();
117     try
118     {
119       // 1. Load the mapping information from the file
120       Mapping map = new Mapping(record.getClass().getClassLoader());
121
122       URL url = record.getClass().getResource("/embl_mapping.xml");
123       map.loadMapping(url);
124
125       // 2. Unmarshal the data
126       Unmarshaller unmar = new Unmarshaller(record);
127       try
128       {
129         if (Cache.getDefault(Cache.CASTORLOGLEVEL,
130                 "debug").equalsIgnoreCase("DEBUG"))
131         {
132           unmar.setDebug(Cache.log.isDebugEnabled());
133           // unmar.setDebug(true);// uncomment to debug unmarshalling
134         }
135       } catch (Exception e)
136       {
137       }
138       unmar.setIgnoreExtraElements(true);
139       unmar.setIgnoreExtraAttributes(true);
140       unmar.setMapping(map);
141       unmar.setLogWriter(new PrintWriter(System.out));
142       record = (EmblEntry) unmar.unmarshal(fileReader);
143
144       canonicaliseDbRefs(record);
145     } catch (Exception e)
146     {
147       e.printStackTrace(System.err);
148       record = null;
149     }
150
151     return record;
152   }
153
154   /**
155    * Change blank version to "0" in any DBRefEntry, to ensure consistent
156    * comparison with other DBRefEntry in Jalview
157    * 
158    * @param entry
159    * @see Uniprot#getDbVersion
160    */
161   static void canonicaliseDbRefs(EmblEntry entry)
162   {
163     if (entry == null)
164     {
165       return;
166     }
167 //    for (EmblEntry entry : record.getEntries())
168 //    {
169       if (entry.getDbRefs() != null)
170       {
171         for (DBRefEntry dbref : entry.getDbRefs())
172         {
173           if ("".equals(dbref.getVersion()))
174           {
175             dbref.setVersion("0");
176           }
177         }
178       }
179
180     if (entry.getFeatures() != null)
181       {
182         for (EmblFeature feature : entry.getFeatures())
183         {
184           if (feature.getDbRefs() != null)
185           {
186             for (DBRefEntry dbref : feature.getDbRefs())
187             {
188               if ("".equals(dbref.getVersion()))
189               {
190                 dbref.setVersion("0");
191               }
192             }
193           }
194         }
195       }
196     // }
197   }
198 }