2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.archaeopteryx.tools;
28 import java.io.IOException;
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;
36 import javax.swing.JApplet;
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 import org.forester.ws.wabi.RestUtil;
46 public final class Blast {
48 final public static void openNcbiBlastWeb( final String query,
49 final boolean is_nucleic_acids,
52 //http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Web&PAGE=Proteins&DATABASE=swissprot&QUERY=gi|163848401
53 final StringBuilder uri_str = new StringBuilder();
54 uri_str.append( "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Web&DATABASE=nr&PAGE=" );
55 if ( is_nucleic_acids ) {
56 uri_str.append( "Nucleotide" );
59 uri_str.append( "Proteins" );
61 uri_str.append( "&QUERY=" );
62 uri_str.append( query );
64 AptxUtil.launchWebBrowser( new URI( uri_str.toString() ), applet != null, applet, "_aptx_blast" );
66 catch ( final IOException e ) {
67 AptxUtil.showErrorMessage( p, e.toString() );
70 catch ( final URISyntaxException e ) {
71 AptxUtil.showErrorMessage( p, e.toString() );
76 final public static String obtainQueryForBlast( final PhylogenyNode node ) {
78 if ( node.getNodeData().isHasSequence() ) {
79 if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
80 query = node.getNodeData().getSequence().getMolecularSequence();
82 if ( ForesterUtil.isEmpty( query ) && ( node.getNodeData().getSequence().getAccession() != null )
83 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) ) {
84 final Accession id = SequenceAccessionTools.parse( node.getNodeData().getSequence().getAccession()
87 query = id.getValue();
90 if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
91 final Accession id = SequenceAccessionTools.parse( node.getNodeData().getSequence().getName() );
93 query = id.getValue();
96 if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
97 final Accession id = SequenceAccessionTools.parse( node.getNodeData().getSequence().getSymbol() );
99 query = id.getValue();
102 if ( ForesterUtil.isEmpty( query )
103 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
104 final Accession id = SequenceAccessionTools.parse( node.getNodeData().getSequence().getGeneName() );
106 query = id.getValue();
110 if ( ForesterUtil.isEmpty( query ) && !ForesterUtil.isEmpty( node.getName() ) ) {
111 final Accession id = SequenceAccessionTools.parse( node.getName() );
113 query = id.getValue();
119 final public static boolean isContainsQueryForBlast( final PhylogenyNode node ) {
120 return !ForesterUtil.isEmpty( obtainQueryForBlast( node ) );
123 final public void ddbjBlast( final String geneName ) {
124 // Retrieve accession number list which has specified gene name from searchByXMLPath of ARSA. Please click here for details of ARSA.
125 /*target: Sequence length is between 300bp and 1000bp.
127 Gene qualifire is same as specified gene name.*/
128 String queryPath = "/ENTRY/DDBJ/division=='HUM' AND (/ENTRY/DDBJ/length>=300 AND "
129 + "/ENTRY/DDBJ/length<=1000) ";
130 queryPath += "AND (/ENTRY/DDBJ/feature-table/feature{/f_key = 'CDS' AND ";
131 queryPath += "/f_quals/qualifier{/q_name = 'gene' AND /q_value=='" + geneName + "'}})";
132 String query = "service=ARSA&method=searchByXMLPath&queryPath=" + queryPath
133 + "&returnPath=/ENTRY/DDBJ/primary-accession&offset=1&count=100";
135 String arsaResult = null;
137 arsaResult = RestUtil.getResult( query );
139 catch ( final IOException e ) {
140 // TODO Auto-generated catch block
143 final String[] arsaResultLines = arsaResult.split( "\n" );
145 final int arsaResultNum = Integer.parseInt( arsaResultLines[ 0 ].replaceAll( "hitscount =", "" ).trim() );
146 //If there is no hit, print a message and exit
147 if ( arsaResultNum == 0 ) {
148 System.out.println( "There is no entry for gene:" + geneName );
151 //Retrieve DNA sequence of top hit entry by using getFASTA_DDBJEntry of GetEntry.
152 //Retrieve DNA sequence of first fit.
153 final String repAccession = arsaResultLines[ 2 ];
154 query = "service=GetEntry&method=getFASTA_DDBJEntry&accession=" + repAccession;
155 String dnaSeq = null;
157 dnaSeq = RestUtil.getResult( query );
159 catch ( final IOException e ) {
160 // TODO Auto-generated catch block
163 System.out.println( "Retrieved DNA sequence is: " + dnaSeq );
164 //Execute blastn by using searchParam of Blast with step2's sequence. Specified option is -e 0.0001 -m 8 -b 50 -v 50. It means "Extract top 50 hit which E-value is more than 0.0001.". The reference databases are specified as follows. ddbjpri(primates) ddbjrod(rodents) ddbjmam(mammals) ddbjvrt(vertebrates ) ddbjinv(invertebrates).
165 //Execute blastn with step3's sequence
166 query = "service=Blast&method=searchParam&program=blastn&database=ddbjpri ddbjrod ddbjmam ddbjvrt "
167 + "ddbjinv&query=" + dnaSeq + "¶m=-m 8 -b 50 -v 50 -e 0.0001";
168 String blastResult = null;
170 blastResult = RestUtil.getResult( query );
172 catch ( final IOException e ) {
173 // TODO Auto-generated catch block
176 // Extract both accession number and similarity score from BLAST result.
177 // This step does not use Web API and extract the part of result or edit the result. Please click here to see the details of each column in the BLAST tab delimited format which is generated by -m 8 option.
178 final String blastResultLines[] = blastResult.split( "\n" );
179 final Vector<String[]> parsedBlastResult = new Vector<String[]>();
180 for( final String blastResultLine : blastResultLines ) {
181 final String cols[] = blastResultLine.split( "\t" );
182 final String accession = cols[ 1 ].substring( 0, cols[ 1 ].indexOf( "|" ) );
183 final String[] result = { accession, cols[ 2 ] };
184 parsedBlastResult.add( result );
186 // Retrieve species name by using searchByXMLPath of ARSA. If the plural subjects whose species
187 // name are same are in the result, the highest similarity score is used.
188 //Retrieve species from accession number.
189 final Hashtable<String, String> organismAccession = new Hashtable<String, String>();
190 for( int i = 0; i < parsedBlastResult.size(); i++ ) {
191 final String[] parsed = parsedBlastResult.elementAt( i );
192 query = "service=ARSA&method=searchByXMLPath&queryPath=/ENTRY/DDBJ/primary-accession=='" + parsed[ 0 ]
193 + "'&returnPath=/ENTRY/DDBJ/organism&offset=1&count=100";
194 String organism = null;
196 organism = RestUtil.getResult( query );
198 catch ( final IOException e ) {
199 // TODO Auto-generated catch block
202 final String[] organismLines = organism.split( "\n" );
203 organism = organismLines[ 2 ];
204 //If same organism name hits, use first hit.
205 if ( !organismAccession.containsKey( organism ) ) {
206 organismAccession.put( organism, parsed[ 0 ] + "\t" + parsed[ 1 ] );
211 System.out.println( "DDBJ entries: " + arsaResultNum );
212 System.out.println( "Representative accession: " + repAccession );
213 System.out.println( "Organism name\tDDBJ accession number\tSequence similarity" );
214 final String[] keys = new String[ organismAccession.size() ];
215 final Enumeration<String> enu = organismAccession.keys();
217 while ( enu.hasMoreElements() ) {
218 keys[ count ] = enu.nextElement();
222 for( final String key : keys ) {
223 System.out.println( key + "\t" + organismAccession.get( key ) );