ee8eb7920975c7d6779cf14601b23eb34676e1c3
[jalview.git] / src / jalview / ws / dbsources / EmblXmlSource.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.ws.ebi.EBIFetchClient;
28
29 import java.io.File;
30 import java.util.Iterator;
31
32 public abstract class EmblXmlSource extends EbiFileRetrievedProxy
33 {
34
35   /**
36    * Last properly parsed embl file.
37    */
38   public jalview.datamodel.xdb.embl.EmblFile efile = null;
39
40   public EmblXmlSource()
41   {
42     super();
43   }
44
45   /**
46    * retrieve and parse an emblxml file
47    * 
48    * @param emprefx
49    *          either EMBL or EMBLCDS strings are allowed - anything else will
50    *          not retrieve emblxml
51    * @param query
52    * @return
53    * @throws Exception
54    */
55   public AlignmentI getEmblSequenceRecords(String emprefx, String query)
56           throws Exception
57   {
58     startQuery();
59     EBIFetchClient dbFetch = new EBIFetchClient();
60     File reply;
61     try
62     {
63       reply = dbFetch.fetchDataAsFile(
64               emprefx.toLowerCase() + ":" + query.trim(), "emblxml", null);
65     } catch (Exception e)
66     {
67       stopQuery();
68       throw new Exception("EBI EMBL XML retrieval failed on "
69               + 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("# No EMBL record retrieved for "
102                 + emprefx.toLowerCase() + ":" + query.trim());
103       }
104     }
105     if (efile != null)
106     {
107       for (Iterator i = efile.getEntries().iterator(); i.hasNext();)
108       {
109         EmblEntry entry = (EmblEntry) i.next();
110         SequenceI[] seqparts = entry.getSequences(false, true, emprefx); // TODO:
111         // use
112         // !fetchNa,!fetchPeptide
113         // here
114         // instead
115         // -
116         // see
117         // todo
118         // in
119         // emblEntry
120         if (seqparts != null)
121         {
122           SequenceI[] newseqs = null;
123           int si = 0;
124           if (seqs == null)
125           {
126             newseqs = new SequenceI[seqparts.length];
127           }
128           else
129           {
130             newseqs = new SequenceI[seqs.length + seqparts.length];
131
132             for (; si < seqs.length; si++)
133             {
134               newseqs[si] = seqs[si];
135               seqs[si] = null;
136             }
137           }
138           for (int j = 0; j < seqparts.length; si++, j++)
139           {
140             newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on
141             // dataset and refer
142           }
143           seqs = newseqs;
144
145         }
146       }
147     }
148     else
149     {
150       result = null;
151     }
152     AlignmentI al = null;
153     if (seqs != null && seqs.length > 0)
154     {
155       al = new Alignment(seqs);
156       result.append("# Successfully parsed the " + emprefx
157               + " queries into an Alignment");
158       results = result;
159     }
160     stopQuery();
161     return al;
162   }
163
164 }