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