Merge branch 'develop' into merged_2_11_2_0_to_2_12
[jalview.git] / src / jalview / ws / dbsources / EmblFlatfileSource.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.util.Locale;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import com.stevesoft.pat.Regex;
29
30 import jalview.bin.Console;
31 import jalview.datamodel.Alignment;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.DataSourceType;
35 import jalview.io.EmblFlatFile;
36 import jalview.io.FileParse;
37 import jalview.util.Platform;
38 import jalview.ws.ebi.EBIFetchClient;
39
40 /**
41  * A class that does partial parsing of an EMBL flatfile.
42  * 
43  * @author gmcarstairs
44  *
45  */
46 public abstract class EmblFlatfileSource extends EbiFileRetrievedProxy
47 {
48   private static final Regex ACCESSION_REGEX = Platform.newRegex("^[A-Z]+[0-9]+");
49
50   @Override
51   public String getDbVersion()
52   {
53     return "0";
54   }
55
56   @Override
57   public String getAccessionSeparator()
58   {
59     return null;
60   }
61
62   @Override
63   public Regex getAccessionValidator()
64   {
65     return ACCESSION_REGEX;
66   }
67
68   @Override
69   public boolean isValidReference(String accession)
70   {
71     if (accession == null || accession.length() < 2)
72     {
73       return false;
74     }
75     return getAccessionValidator().search(accession);
76   }
77
78   @Override
79   public AlignmentI getSequenceRecords(String queries) throws Exception
80   {
81     return null;
82   }
83
84   @Override
85   public int getTier()
86   {
87     return 0;
88   }
89
90   protected AlignmentI getEmblSequenceRecords(String dbName, String query)
91           throws Exception
92   {
93     startQuery();
94     EBIFetchClient dbFetch = new EBIFetchClient();
95     File reply;
96     try
97     {
98       reply = dbFetch.fetchDataAsFile(
99               dbName.toLowerCase(Locale.ROOT) + ":" + query.trim(), null,
100               "gz");
101     } catch (Exception e)
102     {
103       stopQuery();
104       throw new Exception(
105               String.format("EBI EMBL retrieval failed for %s:%s",
106                       dbName.toLowerCase(Locale.ROOT), query.trim()),
107               e);
108     }
109     return getEmblSequenceRecords(dbName, query, reply);
110   }
111
112   private AlignmentI getEmblSequenceRecords(String dbName, String query,
113           File reply) throws IOException
114   {
115     AlignmentI al = null;
116
117     if (reply != null && reply.exists())
118     {
119       file = reply.getAbsolutePath();
120       FileParse fp = new FileParse(file, DataSourceType.FILE);
121       EmblFlatFile emblParser = new EmblFlatFile(fp, getDbSource());
122       SequenceI[] seqs = emblParser.getSeqsAsArray();
123       if (seqs.length > 0)
124       {
125         al = new Alignment(seqs);
126       }
127
128       if (al == null)
129       {
130         Console.error("No record found for '" + dbName + ":" + query + "'");
131       }
132     }
133
134     stopQuery();
135     return al;
136   }
137
138   @Override
139   public boolean isDnaCoding()
140   {
141     return true;
142   }
143 }