initial commit
[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: www.phylosoft.org/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.PhyloXmlUtil;
32 import org.forester.phylogeny.Phylogeny;
33 import org.forester.phylogeny.PhylogenyNode;
34 import org.forester.phylogeny.data.Accession;
35 import org.forester.phylogeny.data.Identifier;
36 import org.forester.phylogeny.data.Sequence;
37 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
38 import org.forester.phylogeny.iterators.PreorderTreeIterator;
39 import org.forester.util.ForesterUtil;
40 import org.forester.util.ForesterUtil.PhylogenyNodeField;
41
42 public final class WebserviceUtil {
43
44     public static final String TAX_CODE_TO_SCI_NAME = "tax_code_to_sci_name";
45     public static final String TREE_FAM_INST        = "tree_fam";
46     public static final String PFAM_INST            = "pfam";
47     public static final String TOL_WEBSERVER        = "http://tolweb.org/onlinecontributors/app?service=external&page=xml/TreeStructureService&node_id="
48                                                             + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER;
49     public static final String TOL_NAME             = "Tree of Life";
50     public static final String TREE_BASE_NAME       = "TreeBASE";
51     public static final String TREE_FAM_NAME        = "TreeFam";
52     public static final String PFAM_NAME            = "Pfam";
53     public static final String PFAM_SERVER          = "http://pfam.janelia.org";
54
55     public static List<PhylogeniesWebserviceClient> createDefaultClients() {
56         final List<PhylogeniesWebserviceClient> clients = new ArrayList<PhylogeniesWebserviceClient>();
57         clients
58                 .add( new BasicPhylogeniesWebserviceClient( TOL_NAME,
59                                                             "Read Tree from Tree of Life...",
60                                                             "Use ToL webservice to obtain a phylogeny",
61                                                             "Please enter a Tree of Life node identifier\n(Examples: "
62                                                                     + "19386 for Cephalopoda, 2461 for Cnidaria, 2466 for Deuterostomia)",
63                                                             WsPhylogenyFormat.TOL_XML_RESPONSE,
64                                                             PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
65                                                             WebserviceUtil.TOL_WEBSERVER,
66                                                             true,
67                                                             "http://tolweb.org",
68                                                             null ) );
69         clients
70                 .add( new BasicPhylogeniesWebserviceClient( TREE_BASE_NAME,
71                                                             "Read Tree from TreeBASE...",
72                                                             "Use TreeBASE to obtain a phylogeny",
73                                                             "Please enter a TreeBASE tree identifier\n(Examples: 2654, 825, 4931, 2518, 2406, 4934)",
74                                                             WsPhylogenyFormat.NEXUS,
75                                                             PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
76                                                             "http://purl.org/phylo/treebase/phylows/tree/TB2:Tr"
77                                                                     + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
78                                                                     + "?format=nexus",
79                                                             true,
80                                                             "http://treebase.nescent.org",
81                                                             null ) );
82         clients
83                 .add( new BasicPhylogeniesWebserviceClient( PFAM_NAME,
84                                                             "Read Gene Tree from Pfam...",
85                                                             "Use  Pfam to obtain a (full) gene tree",
86                                                             "Please enter a Pfam (PF) accession number\n(Examples: 01849 for NAC, 00452 for Bcl-2, 00046 for Homeobox)",
87                                                             WsPhylogenyFormat.PFAM,
88                                                             null,
89                                                             PFAM_SERVER + "/family/tree/download?alnType=full&acc=PF"
90                                                                     + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER,
91                                                             false,
92                                                             PFAM_SERVER,
93                                                             PFAM_INST ) );
94         clients
95                 .add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
96                                                             "Read Full Gene Tree from TreeFam...",
97                                                             "Use TreeFam to obtain a (full) gene tree",
98                                                             "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
99                                                             WsPhylogenyFormat.NHX,
100                                                             null,
101                                                             "http://www.treefam.org/cgi-bin/getdata.pl?ac=TF"
102                                                                     + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
103                                                                     + "&f=full.nhx",
104                                                             true,
105                                                             "http://www.treefam.org",
106                                                             TREE_FAM_INST ) );
107         clients
108                 .add( new BasicPhylogeniesWebserviceClient( TREE_FAM_NAME,
109                                                             "Read Clean Gene Tree from TreeFam...",
110                                                             "Use TreeFam to obtain a (\"clean\") gene tree",
111                                                             "Please enter a TreeFam (TF) accession number\n(Examples: 101004 for Cyclin D, 315938 for Hox, 105310 for Wnt)",
112                                                             WsPhylogenyFormat.NHX,
113                                                             null,
114                                                             "http://www.treefam.org/cgi-bin/getdata.pl?ac=TF"
115                                                                     + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
116                                                                     + "&f=clean.nhx",
117                                                             true,
118                                                             "http://www.treefam.org",
119                                                             TREE_FAM_INST ) );
120         return clients;
121     }
122
123     static void extractSpTremblAccFromNodeName( final Phylogeny phy, final String source ) {
124         final PreorderTreeIterator it = new PreorderTreeIterator( phy );
125         while ( it.hasNext() ) {
126             final PhylogenyNode n = it.next();
127             if ( !ForesterUtil.isEmpty( n.getName() ) ) {
128                 final String name = n.getName();
129                 final int i = name.lastIndexOf( "/" );
130                 if ( i > 0 ) {
131                     final String acc_str = name.substring( 0, i );
132                     if ( !ForesterUtil.isEmpty( acc_str ) ) {
133                         final Sequence seq = new Sequence();
134                         final Accession acc = new Accession( acc_str, source );
135                         seq.setAccession( acc );
136                         n.getNodeData().setSequence( seq );
137                     }
138                 }
139             }
140         }
141     }
142
143     public static void processInstructions( final PhylogeniesWebserviceClient client, final Phylogeny phylogeny ) {
144         if ( client.getProcessingInstructions().equals( WebserviceUtil.TAX_CODE_TO_SCI_NAME ) ) {
145             WebserviceUtil.transferTaxonomyCodeToScientificName( phylogeny );
146         }
147         else if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_FAM_INST ) ) {
148             WebserviceUtil.transferInternalTaxonomyCodeToScientificName( phylogeny );
149             WebserviceUtil.transferExternalScientificNameToTaxonomyCode( phylogeny );
150             WebserviceUtil.transferSequenceNameToSequenceAccession( phylogeny, "ensembl" );
151             WebserviceUtil.setTaxonomyIdentifierType( phylogeny, "ncbi" );
152         }
153         else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
154             WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
155         }
156     }
157
158     static void setTaxonomyIdentifierType( final Phylogeny phy, final String type ) {
159         final PhylogenyNodeIterator it = phy.iteratorPostorder();
160         while ( it.hasNext() ) {
161             final PhylogenyNode n = it.next();
162             if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
163                 n.getNodeData().getTaxonomy().setIdentifier( new Identifier( n.getNodeData().getTaxonomy()
164                         .getIdentifier().getValue(), type ) );
165             }
166         }
167     }
168
169     static void transferExternalScientificNameToTaxonomyCode( final Phylogeny phy ) {
170         final PhylogenyNodeIterator it = phy.iteratorPostorder();
171         while ( it.hasNext() ) {
172             final PhylogenyNode n = it.next();
173             if ( n.isExternal() && n.getNodeData().isHasTaxonomy() ) {
174                 final String name = n.getNodeData().getTaxonomy().getScientificName();
175                 if ( !ForesterUtil.isEmpty( name ) && PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( name ).matches() ) {
176                     n.getNodeData().getTaxonomy().setScientificName( "" );
177                     n.getNodeData().getTaxonomy().setTaxonomyCode( name );
178                 }
179             }
180         }
181     }
182
183     static void transferInternalTaxonomyCodeToScientificName( final Phylogeny phy ) {
184         final PhylogenyNodeIterator it = phy.iteratorPostorder();
185         while ( it.hasNext() ) {
186             final PhylogenyNode n = it.next();
187             if ( !n.isExternal() && n.getNodeData().isHasTaxonomy() ) {
188                 final String name = n.getNodeData().getTaxonomy().getTaxonomyCode();
189                 if ( !ForesterUtil.isEmpty( name ) ) {
190                     n.getNodeData().getTaxonomy().setScientificName( name );
191                     n.getNodeData().getTaxonomy().setTaxonomyCode( "" );
192                 }
193             }
194         }
195     }
196
197     static void transferSequenceNameToSequenceAccession( final Phylogeny phy, final String source ) {
198         final PhylogenyNodeIterator it = phy.iteratorPostorder();
199         while ( it.hasNext() ) {
200             final PhylogenyNode n = it.next();
201             if ( n.getNodeData().isHasSequence() ) {
202                 final String name = n.getNodeData().getSequence().getName();
203                 if ( !ForesterUtil.isEmpty( name ) ) {
204                     n.getNodeData().getSequence().setName( "" );
205                     n.getNodeData().getSequence().setAccession( new Accession( name, source ) );
206                 }
207             }
208         }
209     }
210
211     static void transferTaxonomyCodeToScientificName( final Phylogeny phy ) {
212         final PhylogenyNodeIterator it = phy.iteratorPostorder();
213         while ( it.hasNext() ) {
214             final PhylogenyNode n = it.next();
215             if ( n.getNodeData().isHasTaxonomy() ) {
216                 final String name = n.getNodeData().getTaxonomy().getTaxonomyCode();
217                 if ( !ForesterUtil.isEmpty( name ) ) {
218                     n.getNodeData().getTaxonomy().setScientificName( name );
219                     n.getNodeData().getTaxonomy().setTaxonomyCode( "" );
220                 }
221             }
222         }
223     }
224 }