in progress
[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.iterators.PhylogenyNodeIterator;
39 import org.forester.phylogeny.iterators.PreorderTreeIterator;
40 import org.forester.util.ForesterUtil;
41
42 public final class WebserviceUtil {
43
44     public static final String TREE_FAM_INST  = "tree_fam";
45     public static final String PFAM_INST      = "pfam";
46     public static final String TOL_WEBSERVER  = "http://tolweb.org/onlinecontributors/app?service=external&page=xml/TreeStructureService&node_id="
47                                                       + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER;
48     public static final String TOL_NAME       = "Tree of Life";
49     public static final String TREE_BASE_NAME = "TreeBASE";
50     public static final String TREE_FAM_NAME  = "TreeFam";
51     public static final String PFAM_NAME      = "Pfam";
52     public static final String PFAM_SERVER    = "http://pfam.janelia.org";
53
54     public static List<PhylogeniesWebserviceClient> createDefaultClients() {
55         final List<PhylogeniesWebserviceClient> clients = new ArrayList<PhylogeniesWebserviceClient>();
56         clients.add( new BasicPhylogeniesWebserviceClient( TOL_NAME,
57                                                            "Read Tree from Tree of Life...",
58                                                            "Use ToL webservice to obtain a evolutionary tree",
59                                                            "Please enter a Tree of Life node identifier\n(Examples: "
60                                                                    + "19386 for Cephalopoda, 2461 for Cnidaria, 2466 for Deuterostomia)",
61                                                            WsPhylogenyFormat.TOL_XML_RESPONSE,
62                                                            PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
63                                                            WebserviceUtil.TOL_WEBSERVER,
64                                                            true,
65                                                            "http://tolweb.org",
66                                                            null ) );
67         clients.add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
68                                                            "Read Tree from TreeBASE...",
69                                                            "Use TreeBASE to obtain a evolutionary tree",
70                                                            "Please enter a TreeBASE tree identifier\n(Examples: 2654, 825, 4931, 2518, 2406, 4934)",
71                                                            WsPhylogenyFormat.TREEBASE,
72                                                            null,
73                                                            "http://purl.org/phylo/treebase/phylows/tree/TB2:Tr"
74                                                                    + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
75                                                                    + "?format=nexus",
76                                                            true,
77                                                            "http://www.treebase.org",
78                                                            null ) );
79         clients.add( new BasicPhylogeniesWebserviceClient( PFAM_NAME,
80                                                            "Read Gene Tree from Pfam...",
81                                                            "Use  Pfam to obtain gene trees for seed alignments",
82                                                            "Please enter a Pfam (PF) accession number\n(Examples: 01849 for NAC, 00452 for Bcl-2, 00046 for Homeobox)",
83                                                            WsPhylogenyFormat.PFAM,
84                                                            null,
85                                                            PFAM_SERVER + "/family/PF"
86                                                                    + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
87                                                                    + "/tree/download",
88                                                            false,
89                                                            PFAM_SERVER,
90                                                            PFAM_INST ) );
91         clients.add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
92                                                            "Read Gene Tree from TreeFam...",
93                                                            "Use TreeFam to obtain a gene tree",
94                                                            "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
95                                                            WsPhylogenyFormat.NHX,
96                                                            null,
97                                                            "http://www.treefam.org/family/TF"
98                                                                    + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
99                                                                    + "/tree/newick",
100                                                            true,
101                                                            "http://www.treefam.org",
102                                                            TREE_FAM_INST ) );
103         return clients;
104     }
105
106     static void extractSpTremblAccFromNodeName( final Phylogeny phy, final String source ) {
107         final PreorderTreeIterator it = new PreorderTreeIterator( phy );
108         while ( it.hasNext() ) {
109             final PhylogenyNode n = it.next();
110             if ( !ForesterUtil.isEmpty( n.getName() ) ) {
111                 final String name = n.getName();
112                 final int i = name.lastIndexOf( "/" );
113                 if ( i > 0 ) {
114                     final String acc_str = name.substring( 0, i );
115                     if ( !ForesterUtil.isEmpty( acc_str ) ) {
116                         final Sequence seq = new Sequence();
117                         final Accession acc = new Accession( acc_str, source );
118                         seq.setAccession( acc );
119                         n.getNodeData().setSequence( seq );
120                     }
121                 }
122             }
123         }
124     }
125
126     public static void processInstructions( final PhylogeniesWebserviceClient client, final Phylogeny phylogeny )
127             throws PhyloXmlDataFormatException {
128         if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_FAM_INST ) ) {
129             
130             WebserviceUtil.processTreeFamTrees( phylogeny );
131         }
132         else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
133             WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
134             PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "bootstrap" );        }
135     }
136
137     static void processTreeFamTrees( final Phylogeny phy ) {
138         final PhylogenyNodeIterator it = phy.iteratorPostorder();
139         while ( it.hasNext() ) {
140             final PhylogenyNode n = it.next();
141             if ( n.isExternal() ) {
142                 n.getNodeData().setEvent( null );
143             }
144             
145             if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
146                 n.getNodeData()
147                         .getTaxonomy()
148                         .setIdentifier( new Identifier( n.getNodeData().getTaxonomy().getIdentifier().getValue(), "ncbi" ) );
149             }
150         }
151     }
152
153     
154 }