applied copyright 2008
[jalview.git] / src / jalview / ws / dbsources / EmblXmlSource.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  * \r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  * \r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  * \r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.ws.dbsources;\r
20 \r
21 import jalview.datamodel.Alignment;\r
22 import jalview.datamodel.AlignmentI;\r
23 import jalview.datamodel.SequenceI;\r
24 import jalview.datamodel.xdb.embl.EmblEntry;\r
25 import jalview.ws.ebi.EBIFetchClient;\r
26 \r
27 import java.io.File;\r
28 import java.util.Iterator;\r
29 import java.util.Vector;\r
30 \r
31 public abstract class EmblXmlSource extends EbiFileRetrievedProxy\r
32 {\r
33 \r
34   /**\r
35    * Last properly parsed embl file.\r
36    */\r
37   public jalview.datamodel.xdb.embl.EmblFile efile = null;\r
38 \r
39   public EmblXmlSource()\r
40   {\r
41     super();\r
42   }\r
43   /**\r
44    * retrieve and parse an emblxml file\r
45    * @param emprefx either EMBL or EMBLCDS strings are allowed - anything else will not retrieve emblxml\r
46    * @param query\r
47    * @return\r
48    * @throws Exception\r
49    */\r
50   public AlignmentI getEmblSequenceRecords(String emprefx, String query) throws Exception\r
51   {\r
52     startQuery();\r
53     EBIFetchClient dbFetch = new EBIFetchClient();\r
54     File reply; \r
55     try {\r
56       reply = dbFetch.fetchDataAsFile(\r
57             emprefx.toLowerCase() + ":" + query.trim(),\r
58           "emblxml",null);\r
59     }\r
60     catch (Exception e)\r
61     {\r
62       stopQuery();\r
63       throw new Exception("EBI EMBL XML retrieval failed on "+emprefx.toLowerCase()+":"+query.trim(),e);\r
64     }\r
65     return getEmblSequenceRecords(emprefx, query, reply);\r
66   }\r
67   /**\r
68    * parse an emblxml file stored locally\r
69    * @param emprefx either EMBL or EMBLCDS strings are allowed - anything else will not retrieve emblxml\r
70    * @param query\r
71    * @param file the EMBL XML file containing the results of a query\r
72    * @return\r
73    * @throws Exception\r
74    */\r
75   public AlignmentI getEmblSequenceRecords(String emprefx, String query, File reply) throws Exception\r
76   {\r
77     SequenceI seqs[] = null;\r
78     StringBuffer result = new StringBuffer();\r
79     if (reply != null && reply.exists())\r
80       {\r
81         efile=null;\r
82         file = reply.getAbsolutePath();\r
83         if (reply.length()>25)\r
84         {\r
85             efile = jalview.datamodel.xdb.embl.EmblFile.getEmblFile(reply);\r
86         } else {\r
87           result.append("# No EMBL record retrieved for "+emprefx.toLowerCase()+":"+query.trim());\r
88         }\r
89       }\r
90       if (efile!=null) {\r
91         for (Iterator i=efile.getEntries().iterator(); i.hasNext(); ) {\r
92           EmblEntry entry = (EmblEntry) i.next();\r
93           SequenceI[] seqparts = entry.getSequences(false, true, emprefx); // TODO: use !fetchNa,!fetchPeptide here instead - see todo in emblEntry\r
94           if (seqparts!=null) {\r
95             SequenceI[] newseqs = null;\r
96             int si=0;\r
97             if (seqs==null) {\r
98               newseqs = new SequenceI[seqparts.length];\r
99             } else {\r
100               newseqs  = new SequenceI[seqs.length+seqparts.length];\r
101   \r
102               for (;si<seqs.length; si++) {\r
103                 newseqs[si] = seqs[si];\r
104                 seqs[si] = null;\r
105               }\r
106             }\r
107             for (int j=0;j<seqparts.length; si++, j++) {\r
108               newseqs[si] = seqparts[j].deriveSequence(); // place DBReferences on dataset and refer\r
109             }\r
110             seqs=newseqs;\r
111   \r
112           }\r
113         }\r
114       } else {\r
115         result=null;\r
116       }\r
117     AlignmentI al =null;\r
118     if (seqs!=null && seqs.length>0)\r
119     {\r
120       al = new Alignment(seqs);\r
121       result.append("# Successfully parsed the "+emprefx+" queries into an Alignment");\r
122       results = result;\r
123     }\r
124     stopQuery();\r
125     return al;\r
126   }\r
127 \r
128 }\r