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