JAL-2418 source formatting
[jalview.git] / src / jalview / ws / seqfetcher / DbSourceProxyImpl.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.seqfetcher;
22
23 import jalview.api.FeatureSettingsModelI;
24 import jalview.datamodel.AlignmentI;
25 import jalview.io.DataSourceType;
26 import jalview.io.FileFormatI;
27 import jalview.io.FormatAdapter;
28 import jalview.io.IdentifyFile;
29
30 /**
31  * common methods for implementations of the DbSourceProxy interface.
32  * 
33  * @author JimP
34  * 
35  */
36 public abstract class DbSourceProxyImpl implements DbSourceProxy
37 {
38
39   boolean queryInProgress = false;
40
41   protected StringBuffer results = null;
42
43   public DbSourceProxyImpl()
44   {
45   }
46
47   /*
48    * (non-Javadoc)
49    * 
50    * @see jalview.ws.DbSourceProxy#getRawRecords()
51    */
52   @Override
53   public StringBuffer getRawRecords()
54   {
55     return results;
56   }
57
58   /*
59    * (non-Javadoc)
60    * 
61    * @see jalview.ws.DbSourceProxy#queryInProgress()
62    */
63   @Override
64   public boolean queryInProgress()
65   {
66     return queryInProgress;
67   }
68
69   /**
70    * call to set the queryInProgress flag
71    * 
72    */
73   protected void startQuery()
74   {
75     queryInProgress = true;
76   }
77
78   /**
79    * call to clear the queryInProgress flag
80    * 
81    */
82   protected void stopQuery()
83   {
84     queryInProgress = false;
85   }
86
87   /**
88    * create an alignment from raw text file...
89    * 
90    * @param result
91    * @return null or a valid alignment
92    * @throws Exception
93    */
94   protected AlignmentI parseResult(String result) throws Exception
95   {
96     AlignmentI sequences = null;
97     FileFormatI format = new IdentifyFile().identify(result,
98             DataSourceType.PASTE);
99     if (format != null)
100     {
101       sequences = new FormatAdapter().readFile(result.toString(),
102               DataSourceType.PASTE, format);
103     }
104     return sequences;
105   }
106
107   /**
108    * Returns the first accession id in the query (up to the first accession id
109    * separator), or the whole query if there is no separator or it is not found
110    */
111   @Override
112   public String getAccessionIdFromQuery(String query)
113   {
114     String sep = getAccessionSeparator();
115     if (sep == null)
116     {
117       return query;
118     }
119     int sepPos = query.indexOf(sep);
120     return sepPos == -1 ? query : query.substring(0, sepPos);
121   }
122
123   /**
124    * Default is only one accession id per query - override if more are allowed.
125    */
126   @Override
127   public int getMaximumQueryCount()
128   {
129     return 1;
130   }
131
132   /**
133    * Returns false - override to return true for DNA coding data sources
134    */
135   @Override
136   public boolean isDnaCoding()
137   {
138     return false;
139   }
140
141   /**
142    * Answers false - override as required in subclasses
143    */
144   @Override
145   public boolean isAlignmentSource()
146   {
147     return false;
148   }
149
150   @Override
151   public String getDescription()
152   {
153     return "";
154   }
155
156   @Override
157   public FeatureSettingsModelI getFeatureColourScheme()
158   {
159     return null;
160   }
161 }