Merge branch 'alpha/origin_2022_JAL-3066_Jalview_212_slivka-integration' into spike...
[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.Cache;
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 = null;
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     if (ACCESSION_REGEX == null)
45     {
46       ACCESSION_REGEX = Platform.newRegex("^[A-Z]+[0-9]+");
47     }
48     return ACCESSION_REGEX;
49   }
50
51   @Override
52   public boolean isValidReference(String accession)
53   {
54     if (accession == null || accession.length() < 2)
55     {
56       return false;
57     }
58     return getAccessionValidator().search(accession);
59   }
60
61   @Override
62   public AlignmentI getSequenceRecords(String queries) throws Exception
63   {
64     return null;
65   }
66
67   @Override
68   public int getTier()
69   {
70     return 0;
71   }
72
73   protected AlignmentI getEmblSequenceRecords(String dbName, String query)
74           throws Exception
75   {
76     startQuery();
77     EBIFetchClient dbFetch = new EBIFetchClient();
78     File reply;
79     try
80     {
81       reply = dbFetch.fetchDataAsFile(
82               dbName.toLowerCase(Locale.ROOT) + ":" + query.trim(), null, "gz");
83     } catch (Exception e)
84     {
85       stopQuery();
86       throw new Exception(
87               String.format("EBI EMBL retrieval failed for %s:%s",
88                       dbName.toLowerCase(Locale.ROOT), query.trim()),
89               e);
90     }
91     return getEmblSequenceRecords(dbName, query, reply);
92   }
93
94   private AlignmentI getEmblSequenceRecords(String dbName, String query,
95           File reply) throws IOException
96   {
97     AlignmentI al = null;
98
99     if (reply != null && reply.exists())
100     {
101       file = reply.getAbsolutePath();
102       FileParse fp = new FileParse(file, DataSourceType.FILE);
103       EmblFlatFile emblParser = new EmblFlatFile(fp, getDbSource());
104       SequenceI[] seqs = emblParser.getSeqsAsArray();
105       if (seqs.length > 0)
106       {
107         al = new Alignment(seqs);
108       }
109
110       if (al == null)
111       {
112         Cache.log.error(
113                 "No record found for '" + dbName + ":" + query + "'");
114       }
115     }
116
117     stopQuery();
118     return al;
119   }
120
121   @Override
122   public boolean isDnaCoding()
123   {
124     return true;
125   }
126 }