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