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