JAL-1712 fixes/tests for Castor binding and 'show flanking regions'
[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 java.io.File;
24
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.xdb.embl.EmblEntry;
29 import jalview.datamodel.xdb.embl.EmblFile;
30 import jalview.util.MessageManager;
31 import jalview.ws.ebi.EBIFetchClient;
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("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 = 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 (EmblEntry entry : efile.getEntries())
107       {
108         SequenceI[] seqparts = entry.getSequences(false, true, emprefx);
109         // TODO: use !fetchNa,!fetchPeptide here instead - see todo in EmblEntry
110         if (seqparts != null)
111         {
112           SequenceI[] newseqs = null;
113           int si = 0;
114           if (seqs == null)
115           {
116             newseqs = new SequenceI[seqparts.length];
117           }
118           else
119           {
120             newseqs = new SequenceI[seqs.length + seqparts.length];
121
122             for (; si < seqs.length; si++)
123             {
124               newseqs[si] = seqs[si];
125               seqs[si] = null;
126             }
127           }
128           for (int j = 0; j < seqparts.length; si++, j++)
129           {
130             newseqs[si] = seqparts[j].deriveSequence();
131             // place DBReferences on dataset and refer
132           }
133           seqs = newseqs;
134
135         }
136       }
137     }
138     else
139     {
140       result = null;
141     }
142     AlignmentI al = null;
143     if (seqs != null && seqs.length > 0)
144     {
145       al = new Alignment(seqs);
146       result.append(MessageManager.formatMessage("label.embl_successfully_parsed", new String[]{emprefx}));
147       results = result;
148     }
149     stopQuery();
150     return al;
151   }
152
153 }