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