9340f659ef5e125b01546a2e61b04938ab6a0861
[jalview.git] / src / jalview / ws / dbsources / Xfam.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 jalview.bin.Cache;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.io.DataSourceType;
29 import jalview.io.FileFormat;
30 import jalview.io.FormatAdapter;
31 import jalview.ws.seqfetcher.DbSourceProxyImpl;
32
33 /**
34  * Acts as a superclass for the Rfam and Pfam classes
35  * 
36  * @author Lauren Michelle Lui
37  * 
38  */
39 public abstract class Xfam extends DbSourceProxyImpl
40 {
41   public Xfam()
42   {
43     super();
44   }
45
46   /**
47    * the base URL for this Xfam-like service
48    * 
49    * @return
50    */
51   protected abstract String getURLPrefix();
52
53   @Override
54   public abstract String getDbVersion();
55
56   abstract String getXfamSource();
57
58   @Override
59   public AlignmentI getSequenceRecords(String queries) throws Exception
60   {
61     // TODO: this is not a perfect implementation. We need to be able to add
62     // individual references to each sequence in each family alignment that's
63     // retrieved.
64     startQuery();
65     // TODO: trap HTTP 404 exceptions and return null
66     String xfamUrl = getURL(queries);
67
68     if (Cache.log != null)
69     {
70       Cache.log.debug("XFAM URL for retrieval is: " + xfamUrl);
71     }
72
73     AlignmentI rcds = new FormatAdapter().readFile(xfamUrl,
74             DataSourceType.URL, FileFormat.Stockholm);
75
76     for (int s = 0, sNum = rcds.getHeight(); s < sNum; s++)
77     {
78       rcds.getSequenceAt(s).addDBRef(new DBRefEntry(getXfamSource(),
79               // getDbSource(),
80               getDbVersion(), queries.trim().toUpperCase(Locale.ROOT)));
81       if (!getDbSource().equals(getXfamSource()))
82       { // add the specific ref too
83         rcds.getSequenceAt(s).addDBRef(new DBRefEntry(getDbSource(),
84                 getDbVersion(), queries.trim().toUpperCase(Locale.ROOT)));
85       }
86     }
87     stopQuery();
88     return rcds;
89   }
90
91   String getURL(String queries)
92   {
93     return getURLPrefix() + "/family/" + queries.trim().toUpperCase(Locale.ROOT)
94             + getURLSuffix();
95   }
96
97   /**
98    * Pfam and Rfam provide alignments
99    */
100   @Override
101   public boolean isAlignmentSource()
102   {
103     return true;
104   }
105
106   /**
107    * default suffix to append the retrieval URL for this source.
108    * 
109    * @return "" for most Xfam sources
110    */
111   public String getURLSuffix()
112   {
113     return "";
114   }
115
116 }