Fixed issue with reading from TreeBase
[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.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;
43
44 public final class WebserviceUtil {
45
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";
61     
62     
63     
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,
71                                                            null,
72                                                            TREEBASE_PHYLOWS_STUDY_URL_BASE
73                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
74                                                            + "?format=nexus",
75                                                            true,
76                                                            "https://treebase.org",
77                                                            TREE_BASE_INST ) );
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,
83                                                            null,
84                                                            TREEBASE_PHYLOWS_TREE_URL_BASE
85                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
86                                                            + "?format=nexus",
87                                                            true,
88                                                            "https://treebase.org",
89                                                            TREE_BASE_INST ) );
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,
95                                                            null,
96                                                            PFAM_SERVER + "/family/PF"
97                                                                    + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
98                                                                    + "/tree/download",
99                                                                    false,
100                                                                    PFAM_SERVER,
101                                                                    PFAM_INST ) );
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,
107                                                            null,
108                                                            TREE_FAM_URL_BASE
109                                                            + PhylogeniesWebserviceClient.QUERY_PLACEHOLDER
110                                                            + "/tree/newick",
111                                                            true,
112                                                            "http://www.treefam.org",
113                                                            TREE_FAM_INST ) );
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,
122                                                                    true,
123                                                                    "http://tolweb.org",
124                                                                    null ) );
125         return clients;
126     }
127
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 );
132         }
133         else if ( client.getProcessingInstructions().equals( WebserviceUtil.PFAM_INST ) ) {
134             WebserviceUtil.extractSpTremblAccFromNodeName( phylogeny, "sptrembl" );
135             PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "bootstrap" );
136         }
137         else if ( client.getProcessingInstructions().equals( WebserviceUtil.TREE_BASE_INST ) ) {
138             if ( PhylogenyMethods.isInternalNamesLookLikeConfidences( phylogeny ) ) {
139                 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny, "" );
140             }
141             WebserviceUtil.processTreeBaseTrees( phylogeny );
142         }
143     }
144
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( "/" );
152                 if ( i > 0 ) {
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 );
159                     }
160                 }
161             }
162         }
163     }
164
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() );
172                 if ( acc != null ) {
173                     if ( !n.getNodeData().isHasSequence() ) {
174                         n.getNodeData().addSequence( new Sequence() );
175                     }
176                     final Sequence s = n.getNodeData().getSequence();
177                     if ( s.getAccession() == null ) {
178                         s.setAccession( acc );
179                     }
180                 }
181             }
182         }
183     }
184
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() );
193                     if ( acc != null ) {
194                         if ( !n.getNodeData().isHasSequence() ) {
195                             n.getNodeData().addSequence( new Sequence() );
196                         }
197                         final Sequence s = n.getNodeData().getSequence();
198                         if ( s.getAccession() == null ) {
199                             s.setAccession( acc );
200                         }
201                     }
202                 }
203             }
204             else {
205                 if ( ( n.getBranchData() != null ) && n.getBranchData().isHasConfidences()
206                         && ( n.getBranchData().getConfidence( 0 ) != null ) ) {
207                     n.getBranchData().getConfidence( 0 ).setType( "bootstrap" );
208                 }
209                 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
210                     if ( !n.getNodeData().isHasTaxonomy() ) {
211                         n.getNodeData().addTaxonomy( new Taxonomy() );
212                     }
213                     final Taxonomy t = n.getNodeData().getTaxonomy();
214                     if ( ForesterUtil.isEmpty( t.getScientificName() ) ) {
215                         t.setScientificName( n.getName() );
216                         n.setName( "" );
217                     }
218                 }
219             }
220             if ( n.getNodeData().isHasTaxonomy() && ( n.getNodeData().getTaxonomy().getIdentifier() != null ) ) {
221                 n.getNodeData()
222                 .getTaxonomy()
223                 .setIdentifier( new Identifier( n.getNodeData().getTaxonomy().getIdentifier().getValue(),
224                                                 "ncbi" ) );
225             }
226         }
227     }
228 }