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