X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fws%2Fseqdb%2FSequenceDbWsTools.java;h=f5f83e49343878a0f73bd18634cb512a9b4401b4;hb=f8ecfc1d254f7f413ee5c47fbb012b609f7f4fd1;hp=7ebf256af1b87f77320eeaa40ca46cf0f024749a;hpb=a1faf8469197d44197c70d2eeb529f22e8e396b4;p=jalview.git diff --git a/forester/java/src/org/forester/ws/seqdb/SequenceDbWsTools.java b/forester/java/src/org/forester/ws/seqdb/SequenceDbWsTools.java index 7ebf256..f5f83e4 100644 --- a/forester/java/src/org/forester/ws/seqdb/SequenceDbWsTools.java +++ b/forester/java/src/org/forester/ws/seqdb/SequenceDbWsTools.java @@ -21,7 +21,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.ws.seqdb; @@ -34,56 +34,33 @@ import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.SortedSet; +import java.util.TreeSet; +import org.forester.go.GoTerm; +import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException; +import org.forester.phylogeny.Phylogeny; +import org.forester.phylogeny.PhylogenyNode; +import org.forester.phylogeny.data.Accession; +import org.forester.phylogeny.data.Annotation; import org.forester.phylogeny.data.Identifier; +import org.forester.phylogeny.data.Sequence; +import org.forester.phylogeny.data.Taxonomy; +import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.util.ForesterUtil; +import org.forester.util.SequenceIdParser; public final class SequenceDbWsTools { - private static final boolean ALLOW_TAXONOMY_CODE_HACKS = true; //TODO turn off for final realease! + public final static String BASE_UNIPROT_URL = "http://www.uniprot.org/"; + public final static String BASE_EMBL_DB_URL = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"; + public final static String EMBL_DBS_EMBL = "embl"; + public final static String EMBL_DBS_REFSEQ_P = "refseqp"; + public final static String EMBL_DBS_REFSEQ_N = "refseqn"; + private final static String URL_ENC = "UTF-8"; + private final static boolean DEBUG = false; - public enum Db { - UNKNOWN, UNIPROT; - } - public final static String BASE_UNIPROT_URL = "http://www.uniprot.org/"; - public final static String BASE_EMBL_DB_URL = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"; - public final static String EMBL_DBS_EMBL = "embl"; - public final static String EMBL_DBS_REFSEQ_P = "refseqp"; - public final static String EMBL_DBS_REFSEQ_N = "refseqn"; - private final static String URL_ENC = "UTF-8"; - // uniprot/expasy accession number format (6 chars): - // letter digit letter-or-digit letter-or-digit letter-or-digit digit - // ?: => no back-reference - // \A => begin of String - // \Z => end of String - private final static Pattern UNIPROT_AC_PATTERN = Pattern - .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d[A-Z0-9]{3}\\d)(?:[^a-zA-Z0-9]|\\Z)" ); - private final static boolean DEBUG = false; - - private static String encode( final String str ) throws UnsupportedEncodingException { - return URLEncoder.encode( str.trim(), URL_ENC ); - } - - /** - * Returns null if no match. - * - * @param query - * @param db - * @return - */ - static public String parseUniProtAccessor( final String query ) { - final Matcher m = UNIPROT_AC_PATTERN.matcher( query ); - if ( m.lookingAt() ) { - return m.group( 1 ); - } - else { - return null; - } - } - - public static List getTaxonomiesFromCommonName( final String cn, final int max_taxonomies_return ) + private static List getTaxonomiesFromCommonName( final String cn, final int max_taxonomies_return ) throws IOException { final List result = getTaxonomyStringFromCommonName( cn, max_taxonomies_return ); if ( result.size() > 0 ) { @@ -117,19 +94,9 @@ public final class SequenceDbWsTools { return null; } - public static List getTaxonomiesFromScientificName( final String sn, - final int max_taxonomies_return ) + private static List getTaxonomiesFromScientificName( final String sn, + final int max_taxonomies_return ) throws IOException { - // Hack! Craniata? .. - if ( sn.equals( "Drosophila" ) ) { - return uniProtTaxonomyToList( UniProtTaxonomy.DROSOPHILA_GENUS ); - } - else if ( sn.equals( "Xenopus" ) ) { - return uniProtTaxonomyToList( UniProtTaxonomy.XENOPUS_GENUS ); - } - // else if ( sn.equals( "Nucleariidae and Fonticula group" ) ) { - // return hack( UniProtTaxonomy.NUCLEARIIDAE_AND_FONTICULA ); - // } final List result = getTaxonomyStringFromScientificName( sn, max_taxonomies_return ); if ( result.size() > 0 ) { return parseUniProtTaxonomy( result ); @@ -163,12 +130,6 @@ public final class SequenceDbWsTools { final int max_taxonomies_return ) throws IOException { final String my_code = new String( code ); - if ( ALLOW_TAXONOMY_CODE_HACKS ) { - final List l = resolveFakeTaxonomyCodes( max_taxonomies_return, my_code ); - if ( l != null ) { - return l; - } - } final List result = getTaxonomyStringFromTaxonomyCode( my_code, max_taxonomies_return ); if ( result.size() > 0 ) { return parseUniProtTaxonomy( result ); @@ -176,178 +137,219 @@ public final class SequenceDbWsTools { return null; } - private static List resolveFakeTaxonomyCodes( final int max_taxonomies_return, final String code ) - throws IOException { - if ( code.equals( "CAP" ) ) { - return getTaxonomiesFromId( "283909", max_taxonomies_return ); - } - else if ( code.equals( "FUGRU" ) ) { - return getTaxonomiesFromId( "31033", max_taxonomies_return ); - } - else if ( code.equals( "GIALA" ) ) { - return getTaxonomiesFromId( "5741", max_taxonomies_return ); - } - else if ( code.equals( "TRIVE" ) ) { - return getTaxonomiesFromId( "413071", max_taxonomies_return ); - } - else if ( code.equals( "CAPOWC" ) ) { - return getTaxonomiesFromId( "192875", max_taxonomies_return ); - } - else if ( code.equals( "SPHARC" ) ) { - return getTaxonomiesFromId( "667725", max_taxonomies_return ); - } - else if ( code.equals( "THETRA" ) ) { - return getTaxonomiesFromId( "529818", max_taxonomies_return ); - } - else if ( code.equals( "CHLVUL" ) ) { - return getTaxonomiesFromId( "574566", max_taxonomies_return ); - } - else if ( code.equals( "CITCLE" ) ) { - return getTaxonomiesFromId( "85681", max_taxonomies_return ); - } - else if ( code.equals( "MYCPOP" ) ) { - return getTaxonomiesFromId( "85929", max_taxonomies_return ); - } - else if ( code.equals( "AGABB" ) ) { - return getTaxonomiesFromId( "597362", max_taxonomies_return ); - } - else if ( code.equals( "BAUCOM" ) ) { - return getTaxonomiesFromId( "430998", max_taxonomies_return ); - } - else if ( code.equals( "DICSQU" ) ) { - return getTaxonomiesFromId( "114155", max_taxonomies_return ); - } - else if ( code.equals( "FOMPIN" ) ) { - return getTaxonomiesFromId( "40483", max_taxonomies_return ); - } - else if ( code.equals( "HYDMA" ) ) { - return getTaxonomiesFromId( "6085", max_taxonomies_return ); - } - else if ( code.equals( "MYCFI" ) ) { - return getTaxonomiesFromId( "83344", max_taxonomies_return ); - } - else if ( code.equals( "OIDMAI" ) ) { - return getTaxonomiesFromId( "78148", max_taxonomies_return ); - } - else if ( code.equals( "OSTRC" ) ) { - return getTaxonomiesFromId( "385169", max_taxonomies_return ); - } - else if ( code.equals( "POSPL" ) ) { - return getTaxonomiesFromId( "104341", max_taxonomies_return ); - } - else if ( code.equals( "SAICOM" ) ) { - return getTaxonomiesFromId( "5606", max_taxonomies_return ); - } - else if ( code.equals( "SERLA" ) ) { - return getTaxonomiesFromId( "85982", max_taxonomies_return ); - } - else if ( code.equals( "SPORO" ) ) { - return getTaxonomiesFromId( "40563", max_taxonomies_return ); - } - else if ( code.equals( "ACRALC" ) ) { - return getTaxonomiesFromId( "398408", max_taxonomies_return ); - } - else if ( code.equals( "THITER" ) ) { - return getTaxonomiesFromId( "35720", max_taxonomies_return ); - } - else if ( code.equals( "MYCTHE" ) ) { - return getTaxonomiesFromId( "78579", max_taxonomies_return ); - } - else if ( code.equals( "CONPUT" ) ) { - return getTaxonomiesFromId( "80637", max_taxonomies_return ); - } - else if ( code.equals( "WOLCOC" ) ) { - return getTaxonomiesFromId( "81056", max_taxonomies_return ); - } - else if ( code.equals( "CLAGRA" ) ) { - return getTaxonomiesFromId( "27339", max_taxonomies_return ); - } - else if ( code.equals( "XANPAR" ) ) { - return getTaxonomiesFromId( "107463", max_taxonomies_return ); - } - else if ( code.equals( "HYDPIN" ) ) { - return getTaxonomiesFromId( "388859", max_taxonomies_return ); - } - else if ( code.equals( "SERLAC" ) ) { - return getTaxonomiesFromId( "85982", max_taxonomies_return ); - } - else { - return null; - } - } - - private static List getTaxonomyStringFromCommonName( final String cn, final int max_lines_to_return ) - throws IOException { - return queryUniprot( "taxonomy/?query=common%3a%22" + encode( cn ) + "%22&format=tab", max_lines_to_return ); - } - - private static List getTaxonomyStringFromId( final String id, final int max_lines_to_return ) - throws IOException { - return queryUniprot( "taxonomy/?query=id%3a%22" + encode( id ) + "%22&format=tab", max_lines_to_return ); - } - - private static List getTaxonomyStringFromScientificName( final String sn, final int max_lines_to_return ) + public static SequenceDatabaseEntry obtainEmblEntry( final Identifier id, final int max_lines_to_return ) throws IOException { - return queryUniprot( "taxonomy/?query=scientific%3a%22" + encode( sn ) + "%22&format=tab", max_lines_to_return ); + final List lines = queryEmblDb( id, max_lines_to_return ); + return EbiDbEntry.createInstanceFromPlainText( lines ); } - private static List getTaxonomyStringFromTaxonomyCode( final String code, final int max_lines_to_return ) + public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Identifier id, final int max_lines_to_return ) throws IOException { - return queryUniprot( "taxonomy/?query=mnemonic%3a%22" + encode( code ) + "%22&format=tab", max_lines_to_return ); - } - - private static List uniProtTaxonomyToList( final UniProtTaxonomy tax ) { - final List l = new ArrayList(); - l.add( tax ); - return l; + final List lines = queryEmblDb( id, max_lines_to_return ); + return EbiDbEntry.createInstanceFromPlainTextForRefSeq( lines ); } - private static List parseUniProtTaxonomy( final List result ) throws IOException { - final List taxonomies = new ArrayList(); - for( final String line : result ) { - if ( ForesterUtil.isEmpty( line ) ) { - // Ignore empty lines. + public static SortedSet obtainSeqInformation( final Phylogeny phy, + final boolean ext_nodes_only, + final boolean allow_to_set_taxonomic_data, + final int lines_to_return ) throws IOException { + final SortedSet not_found = new TreeSet(); + for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) { + final PhylogenyNode node = iter.next(); + if ( ext_nodes_only && node.isInternal() ) { + continue; } - else if ( line.startsWith( "Taxon" ) ) { - final String[] items = line.split( "\t" ); - if ( !( items[ 1 ].equalsIgnoreCase( "Mnemonic" ) && items[ 2 ].equalsIgnoreCase( "Scientific name" ) - && items[ 3 ].equalsIgnoreCase( "Common name" ) && items[ 4 ].equalsIgnoreCase( "Synonym" ) - && items[ 5 ].equalsIgnoreCase( "Other Names" ) && items[ 6 ].equalsIgnoreCase( "Reviewed" ) - && items[ 7 ].equalsIgnoreCase( "Rank" ) && items[ 8 ].equalsIgnoreCase( "Lineage" ) ) ) { - throw new IOException( "Unreconized UniProt Taxonomy format: " + line ); - } + String query = null; + Identifier id = null; + Db db = Db.NONE; + if ( node.getNodeData().isHasSequence() + && ( node.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) + && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase() + .startsWith( "uniprot" ) + || node.getNodeData().getSequence().getAccession().getValue().toLowerCase() + .startsWith( "swissprot" ) + || node.getNodeData().getSequence().getAccession().getValue().toLowerCase() + .startsWith( "trembl" ) + || node.getNodeData().getSequence().getAccession().getValue().toLowerCase() + .startsWith( "sp" ) || node.getNodeData().getSequence().getAccession().getValue() + .toLowerCase().startsWith( "uniprotkb" ) ) ) { + query = node.getNodeData().getSequence().getAccession().getValue(); + db = Db.UNIPROT; + } + else if ( node.getNodeData().isHasSequence() + && ( node.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) + && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "embl" ) || node + .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ebi" ) ) ) { + query = node.getNodeData().getSequence().getAccession().getValue(); + db = Db.EMBL; + } + else if ( node.getNodeData().isHasSequence() + && ( node.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) + && ( node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "ncbi" ) || node + .getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "genbank" ) ) ) { + query = node.getNodeData().getSequence().getAccession().getValue(); + // db = Db.NCBI; + } + else if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() ) + && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) + && node.getNodeData().getSequence().getAccession().getValue().toLowerCase().startsWith( "refseq" ) ) { + query = node.getNodeData().getSequence().getAccession().getValue(); + db = Db.REFSEQ; } else { - if ( line.split( "\t" ).length > 4 ) { - taxonomies.add( new UniProtTaxonomy( line ) ); + if ( ( query = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node ) ) != null ) { + db = Db.UNIPROT; + } + else if ( node.getNodeData().isHasSequence() ) { + if ( ( id = SequenceIdParser.parse( node.getName() ) ) != null ) { + if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { + // db = Db.NCBI; + } + else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { + db = Db.REFSEQ; + } + } + else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getName() ) ) != null ) { + if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { + // = Db.NCBI; + } + else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { + db = Db.REFSEQ; + } + } + else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getGeneName() ) ) != null ) { + if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { + // db = Db.NCBI; + } + else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { + db = Db.REFSEQ; + } + } + else if ( ( id = SequenceIdParser.parse( node.getNodeData().getSequence().getSymbol() ) ) != null ) { + if ( id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { + // db = Db.NCBI; + } + else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { + db = Db.REFSEQ; + } + } } } - } - return taxonomies; - } - - public static List queryEmblDb( final Identifier id, final int max_lines_to_return ) throws IOException { - final StringBuilder url_sb = new StringBuilder(); - url_sb.append( BASE_EMBL_DB_URL ); - if ( ForesterUtil.isEmpty( id.getProvider() ) || id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { - url_sb.append( SequenceDbWsTools.EMBL_DBS_EMBL ); - url_sb.append( '/' ); - } - else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { - if ( id.getValue().toUpperCase().indexOf( 'P' ) == 1 ) { - url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_P ); - url_sb.append( '/' ); + if ( db == Db.NONE ) { + not_found.add( node.toString() ); } - else { - url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_N ); - url_sb.append( '/' ); + SequenceDatabaseEntry db_entry = null; + if ( !ForesterUtil.isEmpty( query ) ) { + if ( db == Db.UNIPROT ) { + if ( DEBUG ) { + System.out.println( "uniprot: " + query ); + } + db_entry = obtainUniProtEntry( query, lines_to_return ); + } + else if ( db == Db.EMBL ) { + if ( DEBUG ) { + System.out.println( "embl: " + query ); + } + db_entry = obtainEmblEntry( new Identifier( query ), lines_to_return ); + } + else if ( db == Db.REFSEQ ) { + if ( DEBUG ) { + System.out.println( "refseq: " + query ); + } + db_entry = obtainRefSeqEntryFromEmbl( new Identifier( query ), lines_to_return ); + } + // else if ( db == Db.NCBI ) { + // if ( DEBUG ) { + // System.out.println( "ncbi: " + query ); + // } + // db_entry = obtainNcbiEntry( new Identifier( query ), lines_to_return ); + // } + } + else if ( ( db == Db.REFSEQ ) && ( id != null ) ) { + db_entry = obtainRefSeqEntryFromEmbl( id, lines_to_return ); + } + //else if ( ( db == Db.NCBI ) && ( id != null ) ) { + // db_entry = obtainNcbiEntry( id, lines_to_return ); + //} + if ( ( db_entry != null ) && !db_entry.isEmpty() ) { + final Sequence seq = node.getNodeData().isHasSequence() ? node.getNodeData().getSequence() + : new Sequence(); + if ( !ForesterUtil.isEmpty( db_entry.getAccession() ) ) { + String type = null; + if ( db == Db.EMBL ) { + type = "embl"; + } + else if ( db == Db.UNIPROT ) { + type = "uniprot"; + } + // else if ( db == Db.NCBI ) { + // type = "ncbi"; + // } + else if ( db == Db.REFSEQ ) { + type = "refseq"; + } + seq.setAccession( new Accession( db_entry.getAccession(), type ) ); + } + if ( !ForesterUtil.isEmpty( db_entry.getSequenceName() ) ) { + seq.setName( db_entry.getSequenceName() ); + } + if ( !ForesterUtil.isEmpty( db_entry.getGeneName() ) ) { + seq.setGeneName( db_entry.getGeneName() ); + } + if ( !ForesterUtil.isEmpty( db_entry.getSequenceSymbol() ) ) { + try { + seq.setSymbol( db_entry.getSequenceSymbol() ); + } + catch ( final PhyloXmlDataFormatException e ) { + // Eat this exception. + } + } + if ( ( db_entry.getGoTerms() != null ) && !db_entry.getGoTerms().isEmpty() ) { + for( final GoTerm go : db_entry.getGoTerms() ) { + final Annotation ann = new Annotation( go.getGoId().getId() ); + ann.setDesc( go.getName() ); + seq.addAnnotation( ann ); + } + } + if ( ( db_entry.getCrossReferences() != null ) && !db_entry.getCrossReferences().isEmpty() ) { + for( final Accession x : db_entry.getCrossReferences() ) { + seq.addCrossReference( x ); + } + } + final Taxonomy tax = node.getNodeData().isHasTaxonomy() ? node.getNodeData().getTaxonomy() + : new Taxonomy(); + if ( !ForesterUtil.isEmpty( db_entry.getTaxonomyScientificName() ) ) { + tax.setScientificName( db_entry.getTaxonomyScientificName() ); + } + if ( allow_to_set_taxonomic_data && !ForesterUtil.isEmpty( db_entry.getTaxonomyIdentifier() ) ) { + tax.setIdentifier( new Identifier( db_entry.getTaxonomyIdentifier(), "uniprot" ) ); + } + node.getNodeData().setTaxonomy( tax ); + node.getNodeData().setSequence( seq ); + } + else if ( db != Db.NONE ) { + not_found.add( node.getName() ); + } + try { + Thread.sleep( 10 );// Sleep for 10 ms + } + catch ( final InterruptedException ie ) { } } - return queryDb( id.getValue(), max_lines_to_return, url_sb.toString() ); + return not_found; } - public static List queryUniprot( final String query, final int max_lines_to_return ) throws IOException { - return queryDb( query, max_lines_to_return, BASE_UNIPROT_URL ); + public static SequenceDatabaseEntry obtainUniProtEntry( final String query, final int max_lines_to_return ) + throws IOException { + final List lines = queryUniprot( "uniprot/" + query + ".txt", max_lines_to_return ); + return UniProtEntry.createInstanceFromPlainText( lines ); } public static List queryDb( final String query, int max_lines_to_return, final String base_url ) @@ -380,27 +382,85 @@ public final class SequenceDbWsTools { // To prevent accessing online dbs in too quick succession. Thread.sleep( 20 ); } - catch ( InterruptedException e ) { + catch ( final InterruptedException e ) { e.printStackTrace(); } return result; } - public static SequenceDatabaseEntry obtainUniProtEntry( final String query, final int max_lines_to_return ) + public static List queryEmblDb( final Identifier id, final int max_lines_to_return ) throws IOException { + final StringBuilder url_sb = new StringBuilder(); + url_sb.append( BASE_EMBL_DB_URL ); + if ( ForesterUtil.isEmpty( id.getProvider() ) || id.getProvider().equalsIgnoreCase( Identifier.NCBI ) ) { + url_sb.append( SequenceDbWsTools.EMBL_DBS_EMBL ); + url_sb.append( '/' ); + } + else if ( id.getProvider().equalsIgnoreCase( Identifier.REFSEQ ) ) { + if ( id.getValue().toUpperCase().indexOf( 'P' ) == 1 ) { + url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_P ); + url_sb.append( '/' ); + } + else { + url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_N ); + url_sb.append( '/' ); + } + } + return queryDb( id.getValue(), max_lines_to_return, url_sb.toString() ); + } + + public static List queryUniprot( final String query, final int max_lines_to_return ) throws IOException { + return queryDb( query, max_lines_to_return, BASE_UNIPROT_URL ); + } + + private static String encode( final String str ) throws UnsupportedEncodingException { + return URLEncoder.encode( str.trim(), URL_ENC ); + } + + private static List getTaxonomyStringFromCommonName( final String cn, final int max_lines_to_return ) throws IOException { - final List lines = queryUniprot( "uniprot/" + query + ".txt", max_lines_to_return ); - return UniProtEntry.createInstanceFromPlainText( lines ); + return queryUniprot( "taxonomy/?query=common%3a%22" + encode( cn ) + "%22&format=tab", max_lines_to_return ); } - public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Identifier id, final int max_lines_to_return ) + private static List getTaxonomyStringFromId( final String id, final int max_lines_to_return ) throws IOException { - final List lines = queryEmblDb( id, max_lines_to_return ); - return EbiDbEntry.createInstanceFromPlainTextForRefSeq( lines ); + return queryUniprot( "taxonomy/?query=id%3a%22" + encode( id ) + "%22&format=tab", max_lines_to_return ); } - public static SequenceDatabaseEntry obtainEmblEntry( final Identifier id, final int max_lines_to_return ) + private static List getTaxonomyStringFromScientificName( final String sn, final int max_lines_to_return ) throws IOException { - final List lines = queryEmblDb( id, max_lines_to_return ); - return EbiDbEntry.createInstanceFromPlainText( lines ); + return queryUniprot( "taxonomy/?query=scientific%3a%22" + encode( sn ) + "%22&format=tab", max_lines_to_return ); + } + + private static List getTaxonomyStringFromTaxonomyCode( final String code, final int max_lines_to_return ) + throws IOException { + return queryUniprot( "taxonomy/?query=mnemonic%3a%22" + encode( code ) + "%22&format=tab", max_lines_to_return ); + } + + private static List parseUniProtTaxonomy( final List result ) throws IOException { + final List taxonomies = new ArrayList(); + for( final String line : result ) { + if ( ForesterUtil.isEmpty( line ) ) { + // Ignore empty lines. + } + else if ( line.startsWith( "Taxon" ) ) { + final String[] items = line.split( "\t" ); + if ( !( items[ 1 ].equalsIgnoreCase( "Mnemonic" ) && items[ 2 ].equalsIgnoreCase( "Scientific name" ) + && items[ 3 ].equalsIgnoreCase( "Common name" ) && items[ 4 ].equalsIgnoreCase( "Synonym" ) + && items[ 5 ].equalsIgnoreCase( "Other Names" ) && items[ 6 ].equalsIgnoreCase( "Reviewed" ) + && items[ 7 ].equalsIgnoreCase( "Rank" ) && items[ 8 ].equalsIgnoreCase( "Lineage" ) ) ) { + throw new IOException( "Unreconized UniProt Taxonomy format: " + line ); + } + } + else { + if ( line.split( "\t" ).length > 4 ) { + taxonomies.add( new UniProtTaxonomy( line ) ); + } + } + } + return taxonomies; + } + + public enum Db { + UNIPROT, EMBL, NCBI, NONE, REFSEQ; } }