fixing the treebase issue with https
[jalview.git] / forester / java / src / org / forester / archaeopteryx / webservices / WebserviceUtil.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2010 Christian M. Zmasek
6 // Copyright (C) 2008-2010 Burnham Institute for Medical Research
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 //
22 // Contact: phylosoft @ gmail . com
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.archaeopteryx.webservices;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.forester.archaeopteryx.webservices.WebservicesManager.WsPhylogenyFormat;
31 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
32 import org.forester.phylogeny.Phylogeny;
33 import org.forester.phylogeny.PhylogenyMethods;
34 import org.forester.phylogeny.PhylogenyNode;
35 import org.forester.phylogeny.data.Accession;
36 import org.forester.phylogeny.data.Identifier;
37 import org.forester.phylogeny.data.Sequence;
38 import org.forester.phylogeny.data.Taxonomy;
39 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
40 import org.forester.phylogeny.iterators.PreorderTreeIterator;
41 import org.forester.util.ForesterUtil;
42 import org.forester.util.SequenceAccessionTools;
43
44 public final class WebserviceUtil {
45
46     public static final String PFAM_INST                       = "pfam";
47     public static final String PFAM_NAME                       = "Pfam";
48     public static final String PFAM_SERVER                     = "http://pfam.xfam.org";
49     public static final String TOL_NAME                        = "Tree of Life (ToL)";
50     public static final String TOL_URL_BASE                    = "http://tolweb.org/onlinecontributors/app?service=external&page=xml/TreeStructureService&node_id=";
51     public static final String TOL_WEBSERVER                   = TOL_URL_BASE
52             + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER;
53     public static final String TREE_BASE_DESC                  = "This data set was downloaded from TreeBASE, a relational database of phylogenetic knowledge. TreeBASE has been supported by the NSF, Harvard University, Yale University, SDSC and UC Davis. Please do not remove this acknowledgment.";
54     public static final String TREE_BASE_INST                  = "treebase";
55     public static final String TREE_BASE_NAME                  = "TreeBASE";
56     public static final String TREE_FAM_INST                   = "tree_fam";
57     public static final String TREE_FAM_NAME                   = "TreeFam";
58     public static final String TREE_FAM_URL_BASE               = "http://www.treefam.org/family/TF";
59     public static final String TREEBASE_PHYLOWS_STUDY_URL_BASE = "http://purl.org/phylo/treebase/phylows/study/TB2:S";
60   //  public static final String TREEBASE_PHYLOWS_TREE_URL_BASE  = "http://purl.org/phylo/treebase/phylows/tree/TB2:Tr";
61     public static final String TREEBASE_PHYLOWS_TREE_URL_BASE  = "https://treebase.org/treebase-web/phylows/tree/TB2:Tr";
62     
63     
64     
65     public static List<PhylogeniesWebserviceClient> createDefaultClients() {
66         final List<PhylogeniesWebserviceClient> clients = new ArrayList<PhylogeniesWebserviceClient>();
67         clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
68                                                            "Read Tree(s) from TreeBASE Study...",
69                                                            "Use TreeBASE to obtain evolutionary tree(s) from a study",
70                                                            "Please enter a TreeBASE study (\"S\") identifier (without the \"S\")\n(Examples: 14909, 14525, 15613, 15632)",
71                                                            WsPhylogenyFormat.TREEBASE_STUDY,
72                                                            null,
73                                                            TREEBASE_PHYLOWS_STUDY_URL_BASE
74                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
75                                                            + "?format=nexus",
76                                                            true,
77                                                            "http://www.treebase.org",
78                                                            TREE_BASE_INST ) );
79         clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
80                                                            "Read Tree from TreeBASE...",
81                                                            "Use TreeBASE to obtain a evolutionary tree",
82                                                            "Please enter a TreeBASE tree (\"Tr\") identifier (without the \"Tr\")\n(Examples: 2406, 422, 2654, 825, 4931, 2518, 4934)",
83                                                            WsPhylogenyFormat.TREEBASE_TREE,
84                                                            null,
85                                                            TREEBASE_PHYLOWS_TREE_URL_BASE
86                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
87                                                            + "?format=nexus",
88                                                            true,
89                                                            "http://www.treebase.org",
90                                                            TREE_BASE_INST ) );
91         clients.add( new BasicPhylogeniesWebserviceClient( PFAM_NAME,
92                                                            "Read Domain Tree from Pfam...",
93                                                            "Use  Pfam to obtain gene trees for seed alignments",
94                                                            "Please enter a Pfam (PF) accession number\n(Examples: 01849 for NAC, 00452 for Bcl-2, 00046 for Homeobox)",
95                                                            WsPhylogenyFormat.PFAM,
96                                                            null,
97                                                            PFAM_SERVER + "/family/PF"
98                                                                    + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
99                                                                    + "/tree/download",
100                                                                    false,
101                                                                    PFAM_SERVER,
102                                                                    PFAM_INST ) );
103         clients.add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
104                                                            "Read Gene Tree from TreeFam...",
105                                                            "Use TreeFam to obtain a gene tree",
106                                                            "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
107                                                            WsPhylogenyFormat.NHX,
108                                                            null,
109                                                            TREE_FAM_URL_BASE
110                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
111                                                            + "/tree/newick",
112                                                            true,
113                                                            "http://www.treefam.org",
114                                                            TREE_FAM_INST ) );
115         clients.add( new BasicPhylogeniesWebserviceClient( TOL_NAME,
116                                                            "Read Tree from Tree of Life (ToL)...",
117                                                            "Use ToL webservice to obtain a evolutionary tree",
118                                                            "Please enter a Tree of Life node identifier\n(Examples: "
119                                                                    + "14923 for ray-finned fishes, 19386 for Cephalopoda, 2461 for Cnidaria)",
120                                                                    WsPhylogenyFormat.TOL_XML_RESPONSE,
121                                                                    PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
122                                                                    WebserviceUtil.TOL_WEBSERVER,
123                                                                    true,
124                                                                    "http://tolweb.org",
125                                                                    null ) );
126         return clients;
127     }
128
129     public static void processInstructions( final PhylogeniesWebserviceClient client, final Phylogeny phylogeny )
130             throws PhyloXmlDataFormatException {
131         if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_FAM_INST ) ) {
132             WebserviceUtil.processTreeFamTrees( phylogeny );
133         }
134         else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
135             WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
136             PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "bootstrap" );
137         }
138         else if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_BASE_INST ) ) {
139             if ( PhylogenyMethods.isInternalNamesLookLikeConfidences( phylogeny ) ) {
140                 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "" );
141             }
142             WebserviceUtil.processTreeBaseTrees( phylogeny );
143         }
144     }
145
146     static void extractSpTremblAccFromNodeName( final Phylogeny phy, final String source ) {
147         final PreorderTreeIterator it = new PreorderTreeIterator( phy );
148         while ( it.hasNext() ) {
149             final PhylogenyNode n = it.next();
150             if ( !ForesterUtil.isEmpty( n.getName() ) ) {
151                 final String name = n.getName();
152                 final int i = name.lastIndexOf( "/" );
153                 if ( i > 0 ) {
154                     final String acc_str = name.substring( 0, i );
155                     if ( !ForesterUtil.isEmpty( acc_str ) ) {
156                         final Sequence seq = new Sequence();
157                         final Accession acc = new Accession( acc_str, source );
158                         seq.setAccession( acc );
159                         n.getNodeData().setSequence( seq );
160                     }
161                 }
162             }
163         }
164     }
165
166     static void processTreeBaseTrees( final Phylogeny phy ) {
167         phy.setDescription( TREE_BASE_DESC );
168         final PhylogenyNodeIterator it = phy.iteratorExternalForward();
169         while ( it.hasNext() ) {
170             final PhylogenyNode n = it.next();
171             if ( !ForesterUtil.isEmpty( n.getName() ) ) {
172                 final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
173                 if ( acc != null ) {
174                     if ( !n.getNodeData().isHasSequence() ) {
175                         n.getNodeData().addSequence( new Sequence() );
176                     }
177                     final Sequence s = n.getNodeData().getSequence();
178                     if ( s.getAccession() == null ) {
179                         s.setAccession( acc );
180                     }
181                 }
182             }
183         }
184     }
185
186     static void processTreeFamTrees( final Phylogeny phy ) {
187         final PhylogenyNodeIterator it = phy.iteratorPostorder();
188         while ( it.hasNext() ) {
189             final PhylogenyNode n = it.next();
190             if ( n.isExternal() ) {
191                 n.getNodeData().setEvent( null );
192                 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
193                     final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
194                     if ( acc != null ) {
195                         if ( !n.getNodeData().isHasSequence() ) {
196                             n.getNodeData().addSequence( new Sequence() );
197                         }
198                         final Sequence s = n.getNodeData().getSequence();
199                         if ( s.getAccession() == null ) {
200                             s.setAccession( acc );
201                         }
202                     }
203                 }
204             }
205             else {
206                 if ( ( n.getBranchData() != null ) && n.getBranchData().isHasConfidences()
207                         && ( n.getBranchData().getConfidence( 0 ) != null ) ) {
208                     n.getBranchData().getConfidence( 0 ).setType( "bootstrap" );
209                 }
210                 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
211                     if ( !n.getNodeData().isHasTaxonomy() ) {
212                         n.getNodeData().addTaxonomy( new Taxonomy() );
213                     }
214                     final Taxonomy t = n.getNodeData().getTaxonomy();
215                     if ( ForesterUtil.isEmpty( t.getScientificName() ) ) {
216                         t.setScientificName( n.getName() );
217                         n.setName( "" );
218                     }
219                 }
220             }
221             if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
222                 n.getNodeData()
223                 .getTaxonomy()
224                 .setIdentifier( new Identifier( n.getNodeData().getTaxonomy().getIdentifier().getValue(),
225                                                 "ncbi" ) );
226             }
227         }
228     }
229 }