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