JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / dbsources / EmblXmlSource.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.ws.dbsources;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.xdb.embl.EmblEntry;
27 import jalview.util.MessageManager;
28 import jalview.ws.ebi.EBIFetchClient;
29
30 import java.io.File;
31 import java.util.Iterator;
32
33 public abstract class EmblXmlSource extends EbiFileRetrievedProxy
34 {
35
36   /**
37    * Last properly parsed embl file.
38    */
39   public jalview.datamodel.xdb.embl.EmblFile efile = null;
40
41   public EmblXmlSource()
42   {
43     super();
44   }
45
46   /**
47    * retrieve and parse an emblxml file
48    * 
49    * @param emprefx
50    *          either EMBL or EMBLCDS strings are allowed - anything else will
51    *          not retrieve emblxml
52    * @param query
53    * @return
54    * @throws Exception
55    */
56   public AlignmentI getEmblSequenceRecords(String emprefx, String query)
57           throws Exception
58   {
59     startQuery();
60     EBIFetchClient dbFetch = new EBIFetchClient();
61     File reply;
62     try
63     {
64       reply = dbFetch.fetchDataAsFile(
65               emprefx.toLowerCase() + ":" + query.trim(), "emblxml", null);
66     } catch (Exception e)
67     {
68       stopQuery();
69       throw new Exception(MessageManager.formatMessage("exception.ebiembl_retrieval_failed_on", new String[]{emprefx.toLowerCase(),query.trim()}), e);
70     }
71     return getEmblSequenceRecords(emprefx, query, reply);
72   }
73
74   /**
75    * parse an emblxml file stored locally
76    * 
77    * @param emprefx
78    *          either EMBL or EMBLCDS strings are allowed - anything else will
79    *          not retrieve emblxml
80    * @param query
81    * @param file
82    *          the EMBL XML file containing the results of a query
83    * @return
84    * @throws Exception
85    */
86   public AlignmentI getEmblSequenceRecords(String emprefx, String query,
87           File reply) throws Exception
88   {
89     SequenceI seqs[] = null;
90     StringBuffer result = new StringBuffer();
91     if (reply != null && reply.exists())
92     {
93       efile = null;
94       file = reply.getAbsolutePath();
95       if (reply.length() > 25)
96       {
97         efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);
98       }
99       else
100       {
101         result.append(MessageManager.formatMessage("label.no_embl_record_found", new String[]{emprefx.toLowerCase(),query.trim()}));
102       }
103     }
104     if (efile != null)
105     {
106       for (Iterator i = efile.getEntries().iterator(); i.hasNext();)
107       {
108         EmblEntry entry = (EmblEntry) i.next();
109         SequenceI[] seqparts = entry.getSequences(false, true, emprefx); // TODO:
110         // use
111         // !fetchNa,!fetchPeptide
112         // here
113         // instead
114         // -
115         // see
116         // todo
117         // in
118         // emblEntry
119         if (seqparts != null)
120         {
121           SequenceI[] newseqs = null;
122           int si = 0;
123           if (seqs == null)
124           {
125             newseqs = new SequenceI[seqparts.length];
126           }
127           else
128           {
129             newseqs = new SequenceI[seqs.length + seqparts.length];
130
131             for (; si < seqs.length; si++)
132             {
133               newseqs[si] = seqs[si];
134               seqs[si] = null;
135             }
136           }
137           for (int j = 0; j < seqparts.length; si++, j++)
138           {
139             newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on
140             // dataset and refer
141           }
142           seqs = newseqs;
143
144         }
145       }
146     }
147     else
148     {
149       result = null;
150     }
151     AlignmentI al = null;
152     if (seqs != null && seqs.length > 0)
153     {
154       al = new Alignment(seqs);
155       result.append(MessageManager.formatMessage("label.embl_successfully_parsed", new String[]{emprefx}));
156       results = result;
157     }
158     stopQuery();
159     return al;
160   }
161
162 }