a8518967fa27dd116845f2e2151b531599c9aa13
[jalview.git] / src / jalview / ws / dbsources / EmblXmlSource.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.dbsources;
19
20 import jalview.datamodel.Alignment;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.SequenceI;
23 import jalview.datamodel.xdb.embl.EmblEntry;
24 import jalview.ws.ebi.EBIFetchClient;
25
26 import java.io.File;
27 import java.util.Iterator;
28
29 public abstract class EmblXmlSource extends EbiFileRetrievedProxy
30 {
31
32   /**
33    * Last properly parsed embl file.
34    */
35   public jalview.datamodel.xdb.embl.EmblFile efile = null;
36
37   public EmblXmlSource()
38   {
39     super();
40   }
41
42   /**
43    * retrieve and parse an emblxml file
44    * 
45    * @param emprefx
46    *          either EMBL or EMBLCDS strings are allowed - anything else will
47    *          not retrieve emblxml
48    * @param query
49    * @return
50    * @throws Exception
51    */
52   public AlignmentI getEmblSequenceRecords(String emprefx, String query)
53           throws Exception
54   {
55     startQuery();
56     EBIFetchClient dbFetch = new EBIFetchClient();
57     File reply;
58     try
59     {
60       reply = dbFetch.fetchDataAsFile(
61               emprefx.toLowerCase() + ":" + query.trim(), "emblxml", null);
62     } catch (Exception e)
63     {
64       stopQuery();
65       throw new Exception("EBI EMBL XML retrieval failed on "
66               + emprefx.toLowerCase() + ":" + query.trim(), e);
67     }
68     return getEmblSequenceRecords(emprefx, query, reply);
69   }
70
71   /**
72    * parse an emblxml file stored locally
73    * 
74    * @param emprefx
75    *          either EMBL or EMBLCDS strings are allowed - anything else will
76    *          not retrieve emblxml
77    * @param query
78    * @param file
79    *          the EMBL XML file containing the results of a query
80    * @return
81    * @throws Exception
82    */
83   public AlignmentI getEmblSequenceRecords(String emprefx, String query,
84           File reply) throws Exception
85   {
86     SequenceI seqs[] = null;
87     StringBuffer result = new StringBuffer();
88     if (reply != null && reply.exists())
89     {
90       efile = null;
91       file = reply.getAbsolutePath();
92       if (reply.length() > 25)
93       {
94         efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);
95       }
96       else
97       {
98         result.append("# No EMBL record retrieved for "
99                 + emprefx.toLowerCase() + ":" + query.trim());
100       }
101     }
102     if (efile != null)
103     {
104       for (Iterator i = efile.getEntries().iterator(); i.hasNext();)
105       {
106         EmblEntry entry = (EmblEntry) i.next();
107         SequenceI[] seqparts = entry.getSequences(false, true, emprefx); // TODO:
108         // use
109         // !fetchNa,!fetchPeptide
110         // here
111         // instead
112         // -
113         // see
114         // todo
115         // in
116         // emblEntry
117         if (seqparts != null)
118         {
119           SequenceI[] newseqs = null;
120           int si = 0;
121           if (seqs == null)
122           {
123             newseqs = new SequenceI[seqparts.length];
124           }
125           else
126           {
127             newseqs = new SequenceI[seqs.length + seqparts.length];
128
129             for (; si < seqs.length; si++)
130             {
131               newseqs[si] = seqs[si];
132               seqs[si] = null;
133             }
134           }
135           for (int j = 0; j < seqparts.length; si++, j++)
136           {
137             newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on
138             // dataset and refer
139           }
140           seqs = newseqs;
141
142         }
143       }
144     }
145     else
146     {
147       result = null;
148     }
149     AlignmentI al = null;
150     if (seqs != null && seqs.length > 0)
151     {
152       al = new Alignment(seqs);
153       result.append("# Successfully parsed the " + emprefx
154               + " queries into an Alignment");
155       results = result;
156     }
157     stopQuery();
158     return al;
159   }
160
161 }