JAL-2029 many-to-many EnsemblCDS-to-Uniprot mappings
[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 import java.util.ArrayList;
33 import java.util.List;
34
35 public abstract class EmblXmlSource extends EbiFileRetrievedProxy
36 {
37   /*
38    * JAL-1856 Embl returns this text for query not found
39    */
40   private static final String EMBL_NOT_FOUND_REPLY = "ERROR 12 No entries found.";
41
42   public EmblXmlSource()
43   {
44     super();
45   }
46
47   /**
48    * retrieve and parse an emblxml file
49    * 
50    * @param emprefx
51    *          either EMBL or EMBLCDS strings are allowed - anything else will
52    *          not retrieve emblxml
53    * @param query
54    * @return
55    * @throws Exception
56    */
57   public AlignmentI getEmblSequenceRecords(String emprefx, String query)
58           throws Exception
59   {
60     startQuery();
61     EBIFetchClient dbFetch = new EBIFetchClient();
62     File reply;
63     try
64     {
65       reply = dbFetch.fetchDataAsFile(
66               emprefx.toLowerCase() + ":" + query.trim(), "emblxml", null);
67     } catch (Exception e)
68     {
69       stopQuery();
70       throw new Exception(MessageManager.formatMessage(
71               "exception.ebiembl_retrieval_failed_on", new String[] {
72                   emprefx.toLowerCase(), query.trim() }), e);
73     }
74     return getEmblSequenceRecords(emprefx, query, reply);
75   }
76
77   /**
78    * parse an emblxml file stored locally
79    * 
80    * @param emprefx
81    *          either EMBL or EMBLCDS strings are allowed - anything else will
82    *          not retrieve emblxml
83    * @param query
84    * @param file
85    *          the EMBL XML file containing the results of a query
86    * @return
87    * @throws Exception
88    */
89   public AlignmentI getEmblSequenceRecords(String emprefx, String query,
90           File reply) throws Exception
91   {
92     EmblFile efile = null;
93     List<SequenceI> seqs = new ArrayList<SequenceI>();
94
95     if (reply != null && reply.exists())
96     {
97       file = reply.getAbsolutePath();
98       if (reply.length() > EMBL_NOT_FOUND_REPLY.length())
99       {
100         efile = EmblFile.getEmblFile(reply);
101       }
102     }
103
104     List<SequenceI> peptides = new ArrayList<SequenceI>();
105     if (efile != null)
106     {
107       for (EmblEntry entry : efile.getEntries())
108       {
109         SequenceI seq = entry.getSequence(emprefx, peptides);
110         if (seq != null)
111         {
112           seqs.add(seq.deriveSequence());
113           // place DBReferences on dataset and refer
114         }
115       }
116     }
117
118     AlignmentI al = null;
119     if (!seqs.isEmpty())
120     {
121       al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
122     }
123     stopQuery();
124     return al;
125   }
126
127   @Override
128   public boolean isDnaCoding()
129   {
130     return true;
131   }
132
133 }