JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[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     Cache.debug("XFAM URL for retrieval is: " + xfamUrl);
69
70     AlignmentI rcds = new FormatAdapter().readFile(xfamUrl,
71             DataSourceType.URL, FileFormat.Stockholm);
72
73     for (int s = 0, sNum = rcds.getHeight(); s < sNum; s++)
74     {
75       rcds.getSequenceAt(s).addDBRef(new DBRefEntry(getXfamSource(),
76               // getDbSource(),
77               getDbVersion(), queries.trim().toUpperCase(Locale.ROOT)));
78       if (!getDbSource().equals(getXfamSource()))
79       { // add the specific ref too
80         rcds.getSequenceAt(s).addDBRef(new DBRefEntry(getDbSource(),
81                 getDbVersion(), queries.trim().toUpperCase(Locale.ROOT)));
82       }
83     }
84     stopQuery();
85     return rcds;
86   }
87
88   String getURL(String queries)
89   {
90     return getURLPrefix() + "/family/" + queries.trim().toUpperCase(Locale.ROOT)
91             + getURLSuffix();
92   }
93
94   /**
95    * Pfam and Rfam provide alignments
96    */
97   @Override
98   public boolean isAlignmentSource()
99   {
100     return true;
101   }
102
103   /**
104    * default suffix to append the retrieval URL for this source.
105    * 
106    * @return "" for most Xfam sources
107    */
108   public String getURLSuffix()
109   {
110     return "";
111   }
112
113 }