2 // forester -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2010 Christian M. Zmasek
6 // Copyright (C) 2008-2010 Burnham Institute for Medical Research
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.
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.
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
22 // Contact: phylosoft @ gmail . com
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 package org.forester.archaeopteryx.webservices;
27 import java.util.ArrayList;
28 import java.util.List;
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;
44 public final class WebserviceUtil {
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 = "https://treebase.org/treebase-web/phylows/study/TB2:S";
60 public static final String TREEBASE_PHYLOWS_TREE_URL_BASE = "https://treebase.org/treebase-web/phylows/tree/TB2:Tr";
64 public static List<PhylogeniesWebserviceClient> createDefaultClients() {
65 final List<PhylogeniesWebserviceClient> clients = new ArrayList<PhylogeniesWebserviceClient>();
66 clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
67 "Read Tree(s) from TreeBASE Study...",
68 "Use TreeBASE to obtain evolutionary tree(s) from a study",
69 "Please enter a TreeBASE study (\"S\") identifier (without the \"S\")\n(Examples: 13246, 11662, 562, 16424, 17878, 10190, 14909, 14525, 15613, 15632)",
70 WsPhylogenyFormat.TREEBASE_STUDY,
72 TREEBASE_PHYLOWS_STUDY_URL_BASE
73 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
76 "https://treebase.org",
78 clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
79 "Read Tree from TreeBASE...",
80 "Use TreeBASE to obtain a evolutionary tree",
81 "Please enter a TreeBASE tree (\"Tr\") identifier (without the \"Tr\")\n(Examples: 2406, 422, 2654, 825, 4931, 2518, 4934)",
82 WsPhylogenyFormat.TREEBASE_TREE,
84 TREEBASE_PHYLOWS_TREE_URL_BASE
85 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
88 "https://treebase.org",
90 clients.add( new BasicPhylogeniesWebserviceClient( PFAM_NAME,
91 "Read Domain Tree from Pfam...",
92 "Use Pfam to obtain gene trees for seed alignments",
93 "Please enter a Pfam (PF) accession number\n(Examples: 01849 for NAC, 00452 for Bcl-2, 00046 for Homeobox)",
94 WsPhylogenyFormat.PFAM,
96 PFAM_SERVER + "/family/PF"
97 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
102 clients.add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
103 "Read Gene Tree from TreeFam...",
104 "Use TreeFam to obtain a gene tree",
105 "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
106 WsPhylogenyFormat.NHX,
109 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
112 "http://www.treefam.org",
114 clients.add( new BasicPhylogeniesWebserviceClient( TOL_NAME,
115 "Read Tree from Tree of Life (ToL)...",
116 "Use ToL webservice to obtain a evolutionary tree",
117 "Please enter a Tree of Life node identifier\n(Examples: "
118 + "14923 for ray-finned fishes, 19386 for Cephalopoda, 2461 for Cnidaria)",
119 WsPhylogenyFormat.TOL_XML_RESPONSE,
120 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
121 WebserviceUtil.TOL_WEBSERVER,
128 public static void processInstructions( final PhylogeniesWebserviceClient client, final Phylogeny phylogeny )
129 throws PhyloXmlDataFormatException {
130 if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_FAM_INST ) ) {
131 WebserviceUtil.processTreeFamTrees( phylogeny );
133 else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
134 WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
135 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "bootstrap" );
137 else if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_BASE_INST ) ) {
138 if ( PhylogenyMethods.isInternalNamesLookLikeConfidences( phylogeny ) ) {
139 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "" );
141 WebserviceUtil.processTreeBaseTrees( phylogeny );
145 static void extractSpTremblAccFromNodeName( final Phylogeny phy, final String source ) {
146 final PreorderTreeIterator it = new PreorderTreeIterator( phy );
147 while ( it.hasNext() ) {
148 final PhylogenyNode n = it.next();
149 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
150 final String name = n.getName();
151 final int i = name.lastIndexOf( "/" );
153 final String acc_str = name.substring( 0, i );
154 if ( !ForesterUtil.isEmpty( acc_str ) ) {
155 final Sequence seq = new Sequence();
156 final Accession acc = new Accession( acc_str, source );
157 seq.setAccession( acc );
158 n.getNodeData().setSequence( seq );
165 static void processTreeBaseTrees( final Phylogeny phy ) {
166 phy.setDescription( TREE_BASE_DESC );
167 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
168 while ( it.hasNext() ) {
169 final PhylogenyNode n = it.next();
170 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
171 final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
173 if ( !n.getNodeData().isHasSequence() ) {
174 n.getNodeData().addSequence( new Sequence() );
176 final Sequence s = n.getNodeData().getSequence();
177 if ( s.getAccession() == null ) {
178 s.setAccession( acc );
185 static void processTreeFamTrees( final Phylogeny phy ) {
186 final PhylogenyNodeIterator it = phy.iteratorPostorder();
187 while ( it.hasNext() ) {
188 final PhylogenyNode n = it.next();
189 if ( n.isExternal() ) {
190 n.getNodeData().setEvent( null );
191 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
192 final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
194 if ( !n.getNodeData().isHasSequence() ) {
195 n.getNodeData().addSequence( new Sequence() );
197 final Sequence s = n.getNodeData().getSequence();
198 if ( s.getAccession() == null ) {
199 s.setAccession( acc );
205 if ( ( n.getBranchData() != null ) && n.getBranchData().isHasConfidences()
206 && ( n.getBranchData().getConfidence( 0 ) != null ) ) {
207 n.getBranchData().getConfidence( 0 ).setType( "bootstrap" );
209 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
210 if ( !n.getNodeData().isHasTaxonomy() ) {
211 n.getNodeData().addTaxonomy( new Taxonomy() );
213 final Taxonomy t = n.getNodeData().getTaxonomy();
214 if ( ForesterUtil.isEmpty( t.getScientificName() ) ) {
215 t.setScientificName( n.getName() );
220 if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
223 .setIdentifier( new Identifier( n.getNodeData().getTaxonomy().getIdentifier().getValue(),