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.janelia.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";
62 public static List<PhylogeniesWebserviceClient> createDefaultClients() {
63 final List<PhylogeniesWebserviceClient> clients = new ArrayList<PhylogeniesWebserviceClient>();
64 clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
65 "Read Tree(s) from TreeBASE Study...",
66 "Use TreeBASE to obtain evolutionary tree(s) from a study",
67 "Please enter a TreeBASE study (\"S\") identifier (without the \"S\")\n(Examples: 14909, 14525, 15613, 15632)",
68 WsPhylogenyFormat.TREEBASE_STUDY,
70 TREEBASE_PHYLOWS_STUDY_URL_BASE
71 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
74 "http://www.treebase.org",
76 clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
77 "Read Tree from TreeBASE...",
78 "Use TreeBASE to obtain a evolutionary tree",
79 "Please enter a TreeBASE tree (\"Tr\") identifier (without the \"Tr\")\n(Examples: 2406, 422, 2654, 825, 4931, 2518, 4934)",
80 WsPhylogenyFormat.TREEBASE_TREE,
82 TREEBASE_PHYLOWS_TREE_URL_BASE
83 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
86 "http://www.treebase.org",
88 clients.add( new BasicPhylogeniesWebserviceClient( PFAM_NAME,
89 "Read Gene Tree from Pfam...",
90 "Use Pfam to obtain gene trees for seed alignments",
91 "Please enter a Pfam (PF) accession number\n(Examples: 01849 for NAC, 00452 for Bcl-2, 00046 for Homeobox)",
92 WsPhylogenyFormat.PFAM,
94 PFAM_SERVER + "/family/PF"
95 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
100 clients.add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
101 "Read Gene Tree from TreeFam...",
102 "Use TreeFam to obtain a gene tree",
103 "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
104 WsPhylogenyFormat.NHX,
107 + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
110 "http://www.treefam.org",
112 clients.add( new BasicPhylogeniesWebserviceClient( TOL_NAME,
113 "Read Tree from Tree of Life (ToL)...",
114 "Use ToL webservice to obtain a evolutionary tree",
115 "Please enter a Tree of Life node identifier\n(Examples: "
116 + "14923 for ray-finned fishes, 19386 for Cephalopoda, 2461 for Cnidaria)",
117 WsPhylogenyFormat.TOL_XML_RESPONSE,
118 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
119 WebserviceUtil.TOL_WEBSERVER,
126 public static void processInstructions( final PhylogeniesWebserviceClient client, final Phylogeny phylogeny )
127 throws PhyloXmlDataFormatException {
128 if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_FAM_INST ) ) {
129 WebserviceUtil.processTreeFamTrees( phylogeny );
131 else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
132 WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
133 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "bootstrap" );
135 else if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_BASE_INST ) ) {
136 if ( PhylogenyMethods.isInternalNamesLookLikeConfidences( phylogeny ) ) {
137 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "" );
139 WebserviceUtil.processTreeBaseTrees( phylogeny );
143 static void extractSpTremblAccFromNodeName( final Phylogeny phy, final String source ) {
144 final PreorderTreeIterator it = new PreorderTreeIterator( phy );
145 while ( it.hasNext() ) {
146 final PhylogenyNode n = it.next();
147 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
148 final String name = n.getName();
149 final int i = name.lastIndexOf( "/" );
151 final String acc_str = name.substring( 0, i );
152 if ( !ForesterUtil.isEmpty( acc_str ) ) {
153 final Sequence seq = new Sequence();
154 final Accession acc = new Accession( acc_str, source );
155 seq.setAccession( acc );
156 n.getNodeData().setSequence( seq );
163 static void processTreeBaseTrees( final Phylogeny phy ) {
164 phy.setDescription( TREE_BASE_DESC );
165 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
166 while ( it.hasNext() ) {
167 final PhylogenyNode n = it.next();
168 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
169 final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
171 if ( !n.getNodeData().isHasSequence() ) {
172 n.getNodeData().addSequence( new Sequence() );
174 final Sequence s = n.getNodeData().getSequence();
175 if ( s.getAccession() == null ) {
176 s.setAccession( acc );
183 static void processTreeFamTrees( final Phylogeny phy ) {
184 final PhylogenyNodeIterator it = phy.iteratorPostorder();
185 while ( it.hasNext() ) {
186 final PhylogenyNode n = it.next();
187 if ( n.isExternal() ) {
188 n.getNodeData().setEvent( null );
189 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
190 final Accession acc = SequenceAccessionTools.parseAccessorFromString( n.getName() );
192 if ( !n.getNodeData().isHasSequence() ) {
193 n.getNodeData().addSequence( new Sequence() );
195 final Sequence s = n.getNodeData().getSequence();
196 if ( s.getAccession() == null ) {
197 s.setAccession( acc );
203 if ( ( n.getBranchData() != null ) && n.getBranchData().isHasConfidences()
204 && ( n.getBranchData().getConfidence( 0 ) != null ) ) {
205 n.getBranchData().getConfidence( 0 ).setType( "bootstrap" );
207 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
208 if ( !n.getNodeData().isHasTaxonomy() ) {
209 n.getNodeData().addTaxonomy( new Taxonomy() );
211 final Taxonomy t = n.getNodeData().getTaxonomy();
212 if ( ForesterUtil.isEmpty( t.getScientificName() ) ) {
213 t.setScientificName( n.getName() );
218 if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
221 .setIdentifier( new Identifier( n.getNodeData().getTaxonomy().getIdentifier().getValue(),