X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2FSequenceFetcher.java;h=10a2b282ddaefeefa447880c2f494ede691cf325;hb=6f2c3b8cb30d8ccf7ff9ab949a7b244f0c415bc1;hp=e6fc3856ec3b916d5040c7d098efb5bcc1f27a9b;hpb=5d88ae6bbf1ea113cefcb43b7918b5baf560a76e;p=jalview.git diff --git a/src/jalview/ws/SequenceFetcher.java b/src/jalview/ws/SequenceFetcher.java index e6fc385..10a2b28 100644 --- a/src/jalview/ws/SequenceFetcher.java +++ b/src/jalview/ws/SequenceFetcher.java @@ -1,167 +1,135 @@ -package jalview.ws; - -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Vector; - -import jalview.datamodel.Alignment; -import jalview.datamodel.AlignmentI; -import jalview.datamodel.DBRefSource; -import jalview.datamodel.SequenceI; - -/** - * prototype of abstract sequence retrieval interface - * - */ -public class SequenceFetcher extends ASequenceFetcher -{ - /** - * Thread safe construction of database proxies TODO: extend to a configurable - * database plugin mechanism where classes are instantiated by reflection and - * queried for their DbRefSource and version association. - * - */ - public SequenceFetcher() - { - FETCHABLEDBS = new Hashtable(); - FETCHABLEDBS.put(DBRefSource.EMBL, - new jalview.ws.dbsources.EmblSource()); - FETCHABLEDBS.put(DBRefSource.EMBLCDS, - new jalview.ws.dbsources.EmblCdsSouce()); - FETCHABLEDBS.put(DBRefSource.UNIPROT, - new jalview.ws.dbsources.Uniprot()); - FETCHABLEDBS.put(DBRefSource.UP_NAME, - new jalview.ws.dbsources.Uniprot()); - FETCHABLEDBS.put(DBRefSource.PDB, new jalview.ws.dbsources.Pdb()); - FETCHABLEDBS.put(DBRefSource.PFAM, new jalview.ws.dbsources.Pfam()); - }; - - public static void main(String[] argv) - { - AlignmentI ds = null; - Vector noProds = new Vector(); - if (argv != null && argv.length > 0) - { - System.out - .println("Ignoring arguments. Future Usage = dbname:query1;query2;..."); - } - SequenceFetcher sfetcher = new SequenceFetcher(); - Enumeration e = sfetcher.FETCHABLEDBS.keys(); - while (e.hasMoreElements()) - { - String db = (String) e.nextElement(); - // skip me - if (db.equals(DBRefSource.PDB)) - continue; - DbSourceProxy sp = sfetcher.getSourceProxy(db); - System.out - .println("" + db + ": retrieving test:" + sp.getTestQuery()); - AlignmentI al = null; - try - { - al = sp.getSequenceRecords(sp.getTestQuery()); - if (al != null && al.getHeight() > 0) - { - boolean dna = sp.getDbSourceProperties().containsKey( - DBRefSource.DNACODINGSEQDB) - || sp.getDbSourceProperties().containsKey( - DBRefSource.DNASEQDB) - || sp.getDbSourceProperties().containsKey( - DBRefSource.CODINGSEQDB); - // try and find products - String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes( - dna, al.getSequencesArray()); - if (types != null) - { - System.out.println("Xref Types for: "+(dna ? "dna" : "prot")); - for (int t = 0; t < types.length; t++) - { - System.out.println("Type: " + types[t]); - SequenceI[] prod = jalview.analysis.CrossRef.findXrefSequences(al - .getSequencesArray(), dna, types[t]).getSequencesArray(); - System.out.println("Found " - + ((prod == null) ? "no" : "" + prod.length) - + " products"); - if (prod!=null) - { - for (int p=0; p0) - { - Enumeration ts = noProds.elements(); - while (ts.hasMoreElements()) - - { - Object[] typeSq = (Object[]) ts.nextElement(); - boolean dna = (typeSq.length>1); - AlignmentI al = (AlignmentI) typeSq[0]; - System.out.println("Trying getProducts for "+al.getSequenceAt(0).getDisplayId(true)); - System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot")); - // have a bash at finding the products amongst all the retrieved sequences. - SequenceI[] prod = jalview.analysis.CrossRef.findXrefSequences(al - .getSequencesArray(), dna, null, ds).getSequencesArray(); // note should test rather than throw away codon mapping (if present) - System.out.println("Found " - + ((prod == null) ? "no" : "" + prod.length) - + " products"); - if (prod!=null) - { - for (int p=0; p. + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.ws; + +import jalview.bin.ApplicationSingletonProvider; +import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; +import jalview.datamodel.DBRefSource; +import jalview.ext.ensembl.EnsemblGene; +import jalview.ws.dbsources.Uniprot; +import jalview.ws.seqfetcher.ASequenceFetcher; +import jalview.ws.seqfetcher.DbSourceProxy; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Thread safe construction of database proxies. This implements the run-time + * discovery of sequence database clients. + * + * TODO: extend to a configurable database plugin mechanism where classes are + * instantiated by reflection and queried for their DbRefSource and version + * association. + */ +public class SequenceFetcher extends ASequenceFetcher implements ApplicationSingletonI +{ + /* + * set a mock fetcher here for testing only - reset to null afterwards + */ + private static SequenceFetcher mockFetcher; + + /** + * Returns a new SequenceFetcher singleton, or a mock object if one has been + * set. + * + * @return + */ + public static SequenceFetcher getInstance() + { + return mockFetcher != null ? mockFetcher + : (SequenceFetcher) ApplicationSingletonProvider + .getInstance(SequenceFetcher.class); + } + + /** + * Set the instance object to use (intended for unit testing with mock + * objects). + * + * Be sure to reset to null in the tearDown method of any tests! + * + * @param sf + */ + public static void setSequenceFetcher(SequenceFetcher sf) + { + mockFetcher = sf; + } + + /** + * + * This public constructor is only is for use by testng to anonymously + * subclass SequenceFetcher. + * + * + */ + public SequenceFetcher() + { + addAllDatabases(); + } + + public void addAllDatabases() + { + addDBRefSourceImpl(EnsemblGene.class); // includes EnsemblGenomes.class + addDBRefSourceImpl(Uniprot.class); // includes UniprotName.class + // addDBRefSourceImpl(EmblSource.class); + // addDBRefSourceImpl(EmblCdsSource.class); + // addDBRefSourceImpl(Pdb.class); + // addDBRefSourceImpl(PfamFull.class); + // addDBRefSourceImpl(PfamSeed.class); + // addDBRefSourceImpl(RfamSeed.class); + addDBRefSourceImpl(DBRefSource.EMBL, + "jalview.ws.dbsources.EmblSource"); + addDBRefSourceImpl(DBRefSource.EMBLCDS, + "jalview.ws.dbsources.EmblCdsSource"); + addDBRefSourceImpl(DBRefSource.PDB, "jalview.ws.dbsources.Pdb"); + addDBRefSourceImpl(DBRefSource.PFAM_FULL, + "jalview.ws.dbsources.PfamFull"); + addDBRefSourceImpl(DBRefSource.PFAM_SEED, + "jalview.ws.dbsources.PfamSeed"); + addDBRefSourceImpl(DBRefSource.RFAM_SEED, + "jalview.ws.dbsources.RfamSeed"); + } + + /** + * return an ordered list of database sources excluding alignment only + * databases + */ + public String[] getNonAlignmentSources() + { + String[] srcs = this.getSupportedDb(); + List src = new ArrayList<>(); + outer: for (int i = 0; i < srcs.length; i++) + { + for (DbSourceProxy dbs : getSourceProxy(srcs[i])) + { + // Skip the alignment databases for the moment - they're not useful for + // verifying a single sequence against its reference source + if (dbs.isAlignmentSource()) + { + continue outer; + } + } + src.add(srcs[i]); + } + Collections.sort(src, String.CASE_INSENSITIVE_ORDER); + return src.toArray(new String[src.size()]); + } +}