Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[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 Regex ACCESSION_REGEX = null;
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     if (ACCESSION_REGEX == null)
66     {
67       ACCESSION_REGEX = Platform.newRegex("^[A-Z]+[0-9]+");
68     }
69     return ACCESSION_REGEX;
70   }
71
72   @Override
73   public boolean isValidReference(String accession)
74   {
75     if (accession == null || accession.length() < 2)
76     {
77       return false;
78     }
79     return getAccessionValidator().search(accession);
80   }
81
82   @Override
83   public AlignmentI getSequenceRecords(String queries) throws Exception
84   {
85     return null;
86   }
87
88   @Override
89   public int getTier()
90   {
91     return 0;
92   }
93
94   protected AlignmentI getEmblSequenceRecords(String dbName, String query)
95           throws Exception
96   {
97     startQuery();
98     EBIFetchClient dbFetch = new EBIFetchClient();
99     File reply;
100     try
101     {
102       reply = dbFetch.fetchDataAsFile(
103               dbName.toLowerCase(Locale.ROOT) + ":" + query.trim(), null,
104               "gz");
105     } catch (Exception e)
106     {
107       stopQuery();
108       throw new Exception(
109               String.format("EBI EMBL retrieval failed for %s:%s",
110                       dbName.toLowerCase(Locale.ROOT), query.trim()),
111               e);
112     }
113     return getEmblSequenceRecords(dbName, query, reply);
114   }
115
116   private AlignmentI getEmblSequenceRecords(String dbName, String query,
117           File reply) throws IOException
118   {
119     AlignmentI al = null;
120
121     if (reply != null && reply.exists())
122     {
123       file = reply.getAbsolutePath();
124       FileParse fp = new FileParse(file, DataSourceType.FILE);
125       EmblFlatFile emblParser = new EmblFlatFile(fp, getDbSource());
126       SequenceI[] seqs = emblParser.getSeqsAsArray();
127       if (seqs.length > 0)
128       {
129         al = new Alignment(seqs);
130       }
131
132       if (al == null)
133       {
134         Console.error("No record found for '" + dbName + ":" + query + "'");
135       }
136     }
137
138     stopQuery();
139     return al;
140   }
141
142   @Override
143   public boolean isDnaCoding()
144   {
145     return true;
146   }
147 }