20da45c198b2e62fe2d66ce9c4b0ba84d2c0d772
[jalview.git] / src / jalview / ws / dbsources / EmblXmlSource.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.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.datamodel.xdb.embl.EmblFile;
28 import jalview.util.MessageManager;
29 import jalview.ws.ebi.EBIFetchClient;
30
31 import java.io.File;
32
33 public abstract class EmblXmlSource extends EbiFileRetrievedProxy
34 {
35
36   /**
37    * Last properly parsed embl file.
38    */
39   public 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(
70               "exception.ebiembl_retrieval_failed_on", new String[] {
71                   emprefx.toLowerCase(), query.trim() }), e);
72     }
73     return getEmblSequenceRecords(emprefx, query, reply);
74   }
75
76   /**
77    * parse an emblxml file stored locally
78    * 
79    * @param emprefx
80    *          either EMBL or EMBLCDS strings are allowed - anything else will
81    *          not retrieve emblxml
82    * @param query
83    * @param file
84    *          the EMBL XML file containing the results of a query
85    * @return
86    * @throws Exception
87    */
88   public AlignmentI getEmblSequenceRecords(String emprefx, String query,
89           File reply) throws Exception
90   {
91     SequenceI seqs[] = null;
92     StringBuffer result = new StringBuffer();
93     if (reply != null && reply.exists())
94     {
95       efile = null;
96       file = reply.getAbsolutePath();
97       if (reply.length() > 25)
98       {
99         efile = EmblFile.getEmblFile(reply);
100       }
101       else
102       {
103         result.append(MessageManager.formatMessage(
104                 "label.no_embl_record_found",
105                 new String[] { emprefx.toLowerCase(), query.trim() }));
106       }
107     }
108     if (efile != null)
109     {
110       for (EmblEntry entry : efile.getEntries())
111       {
112         SequenceI[] seqparts = entry.getSequences(false, true, emprefx);
113         // TODO: use !fetchNa,!fetchPeptide here instead - see todo in EmblEntry
114         if (seqparts != null)
115         {
116           SequenceI[] newseqs = null;
117           int si = 0;
118           if (seqs == null)
119           {
120             newseqs = new SequenceI[seqparts.length];
121           }
122           else
123           {
124             newseqs = new SequenceI[seqs.length + seqparts.length];
125
126             for (; si < seqs.length; si++)
127             {
128               newseqs[si] = seqs[si];
129               seqs[si] = null;
130             }
131           }
132           for (int j = 0; j < seqparts.length; si++, j++)
133           {
134             newseqs[si] = seqparts[j].deriveSequence();
135             // place DBReferences on dataset and refer
136           }
137           seqs = newseqs;
138
139         }
140       }
141     }
142     else
143     {
144       result = null;
145     }
146     AlignmentI al = null;
147     if (seqs != null && seqs.length > 0)
148     {
149       al = new Alignment(seqs);
150       result.append(MessageManager.formatMessage(
151               "label.embl_successfully_parsed", new String[] { emprefx }));
152       results = result;
153     }
154     stopQuery();
155     return al;
156   }
157
158 }