Fixed issue with reading from TreeBase
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / Blast.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.Arrays;
32 import java.util.Enumeration;
33 import java.util.Hashtable;
34 import java.util.Vector;
35
36 import javax.swing.JApplet;
37
38 import org.forester.archaeopteryx.AptxUtil;
39 import org.forester.archaeopteryx.TreePanel;
40 import org.forester.phylogeny.PhylogenyNode;
41 import org.forester.phylogeny.data.Accession;
42 import org.forester.util.ForesterUtil;
43 import org.forester.util.SequenceAccessionTools;
44
45 public final class Blast {
46
47     final public static void openNcbiBlastWeb( final String query,
48                                                final boolean is_nucleic_acids,
49                                                final JApplet applet,
50                                                final TreePanel p ) {
51         //http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Web&PAGE=Proteins&DATABASE=swissprot&QUERY=gi|163848401
52         final StringBuilder uri_str = new StringBuilder();
53         uri_str.append( "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Web&DATABASE=nr&PAGE=" );
54         if ( is_nucleic_acids ) {
55             uri_str.append( "Nucleotide" );
56         }
57         else {
58             uri_str.append( "Proteins" );
59         }
60         uri_str.append( "&QUERY=" );
61         uri_str.append( query );
62         try {
63             AptxUtil.launchWebBrowser( new URI( uri_str.toString() ), applet != null, applet, "_aptx_blast" );
64         }
65         catch ( final IOException e ) {
66             AptxUtil.showErrorMessage( p, e.toString() );
67             e.printStackTrace();
68         }
69         catch ( final URISyntaxException e ) {
70             AptxUtil.showErrorMessage( p, e.toString() );
71             e.printStackTrace();
72         }
73     }
74
75     final public static String obtainQueryForBlast( final PhylogenyNode node ) {
76         String query = "";
77         if ( node.getNodeData().isHasSequence() ) {
78             if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
79                 query = node.getNodeData().getSequence().getMolecularSequence();
80             }
81             if ( ForesterUtil.isEmpty( query ) && ( node.getNodeData().getSequence().getAccession() != null )
82                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) ) {
83                 final Accession id = SequenceAccessionTools.parseAccessorFromString( node.getNodeData().getSequence()
84                                                                                      .getAccession().getValue() );
85                 if ( id != null ) {
86                     query = id.getValue();
87                 }
88             }
89             if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
90                 final Accession id = SequenceAccessionTools.parseAccessorFromString( node.getNodeData().getSequence()
91                                                                                      .getName() );
92                 if ( id != null ) {
93                     query = id.getValue();
94                 }
95             }
96             if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
97                 final Accession id = SequenceAccessionTools.parseAccessorFromString( node.getNodeData().getSequence()
98                                                                                      .getSymbol() );
99                 if ( id != null ) {
100                     query = id.getValue();
101                 }
102             }
103             if ( ForesterUtil.isEmpty( query )
104                     && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
105                 final Accession id = SequenceAccessionTools.parseAccessorFromString( node.getNodeData().getSequence()
106                                                                                      .getGeneName() );
107                 if ( id != null ) {
108                     query = id.getValue();
109                 }
110             }
111         }
112         if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getName() ) ) {
113             final Accession id = SequenceAccessionTools.parseAccessorFromString( node.getName() );
114             if ( id != null ) {
115                 query = id.getValue();
116             }
117         }
118         return query;
119     }
120
121     final public static boolean isContainsQueryForBlast( final PhylogenyNode node ) {
122         return !ForesterUtil.isEmpty( obtainQueryForBlast( node ) );
123     }
124 }