/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * \r
* This program is free software; you can redistribute it and/or\r
* modify it under the terms of the GNU General Public License\r
* as published by the Free Software Foundation; either version 2\r
* of the License, or (at your option) any later version.\r
- *\r
+ * \r
* This program is distributed in the hope that it will be useful,\r
* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
* GNU General Public License for more details.\r
- *\r
+ * \r
* You should have received a copy of the GNU General Public License\r
* along with this program; if not, write to the Free Software\r
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
import org.exolab.castor.xml.*;\r
import jalview.analysis.*;\r
import jalview.datamodel.*;\r
+import jalview.datamodel.Mapping;\r
import jalview.gui.*;\r
+import jalview.ws.dbsources.Uniprot;\r
import jalview.ws.ebi.EBIFetchClient;\r
\r
/**\r
- * DOCUMENT ME!\r
- *\r
+ * Implements a runnable for validating a sequence against external databases\r
+ * and then propagating references and features onto the sequence(s)\r
+ * \r
* @author $author$\r
* @version $Revision$\r
*/\r
-public class DBRefFetcher\r
- implements Runnable\r
+public class DBRefFetcher implements Runnable\r
{\r
- SequenceI [] dataset;\r
- AlignFrame af;\r
+ SequenceI[] dataset;\r
+\r
+ IProgressIndicator af;\r
+\r
CutAndPasteTransfer output = new CutAndPasteTransfer();\r
+\r
StringBuffer sbuffer = new StringBuffer();\r
+\r
boolean running = false;\r
\r
- ///This will be a collection of Vectors of sequenceI refs.\r
- //The key will be the seq name or accession id of the seq\r
+ // /This will be a collection of Vectors of sequenceI refs.\r
+ // The key will be the seq name or accession id of the seq\r
Hashtable seqRefs;\r
\r
- public DBRefFetcher()\r
- {}\r
+ String[] dbSources;\r
\r
- public Vector getUniprotEntries(File file)\r
- {\r
- UniprotFile uni = new UniprotFile();\r
- try\r
- {\r
- // 1. Load the mapping information from the file\r
- org.exolab.castor.mapping.Mapping map = new org.exolab.castor.mapping.Mapping(uni.getClass().getClassLoader());\r
- java.net.URL url = getClass().getResource("/uniprot_mapping.xml");\r
- map.loadMapping(url);\r
-\r
- // 2. Unmarshal the data\r
- Unmarshaller unmar = new Unmarshaller(uni);\r
- unmar.setIgnoreExtraElements(true);\r
- unmar.setMapping(map);\r
-\r
- uni = (UniprotFile) unmar.unmarshal(new FileReader(file));\r
- }\r
- catch (Exception e)\r
- {\r
- System.out.println("Error getUniprotEntries() " + e);\r
- }\r
+ SequenceFetcher sfetcher;\r
\r
- return uni.getUniprotEntries();\r
+ public DBRefFetcher()\r
+ {\r
}\r
\r
/**\r
* Creates a new SequenceFeatureFetcher object.\r
- *\r
- * @param align DOCUMENT ME!\r
- * @param ap DOCUMENT ME!\r
+ * \r
+ * @param seqs\r
+ * fetch references for these sequences\r
+ * @param af\r
+ * the parent alignframe for progress bar monitoring.\r
*/\r
- public DBRefFetcher(SequenceI [] seqs, AlignFrame af)\r
+ public DBRefFetcher(SequenceI[] seqs, AlignFrame af)\r
{\r
this.af = af;\r
- SequenceI [] ds = new SequenceI[seqs.length];\r
+ SequenceI[] ds = new SequenceI[seqs.length];\r
for (int i = 0; i < seqs.length; i++)\r
{\r
- if(seqs[i].getDatasetSequence()!=null)\r
+ if (seqs[i].getDatasetSequence() != null)\r
ds[i] = seqs[i].getDatasetSequence();\r
else\r
ds[i] = seqs[i];\r
}\r
this.dataset = ds;\r
+ // TODO Jalview 2.5 lots of this code should be in the gui package!\r
+ sfetcher = jalview.gui.SequenceFetcher.getSequenceFetcherSingleton(af);\r
+ // select appropriate databases based on alignFrame context.\r
+ if (af.getViewport().getAlignment().isNucleotide())\r
+ {\r
+ dbSources = DBRefSource.DNACODINGDBS;\r
+ }\r
+ else\r
+ {\r
+ dbSources = DBRefSource.PROTEINDBS;\r
+ }\r
}\r
\r
- public boolean fetchDBRefs(boolean waitTillFinished)\r
+ /**\r
+ * start the fetcher thread\r
+ * \r
+ * @param waitTillFinished\r
+ * true to block until the fetcher has finished\r
+ */\r
+ public void fetchDBRefs(boolean waitTillFinished)\r
{\r
Thread thread = new Thread(this);\r
thread.start();\r
try\r
{\r
Thread.sleep(500);\r
+ } catch (Exception ex)\r
+ {\r
}\r
- catch (Exception ex)\r
- {}\r
}\r
}\r
-\r
- return true;\r
}\r
\r
/**\r
- * The sequence will be added to a vector of sequences\r
- * belonging to key which could be either seq name or dbref id\r
- * @param seq SequenceI\r
- * @param key String\r
+ * The sequence will be added to a vector of sequences belonging to key which\r
+ * could be either seq name or dbref id\r
+ * \r
+ * @param seq\r
+ * SequenceI\r
+ * @param key\r
+ * String\r
*/\r
void addSeqId(SequenceI seq, String key)\r
{\r
*/\r
public void run()\r
{\r
+ if (dbSources == null)\r
+ {\r
+ throw new Error("Implementation error. Must initialise dbSources");\r
+ }\r
long startTime = System.currentTimeMillis();\r
af.setProgressBar("Fetching db refs", startTime);\r
running = true;\r
-\r
- seqRefs = new Hashtable();\r
-\r
- try\r
+ int db = 0;\r
+ Vector sdataset = new Vector();\r
+ for (int s = 0; s < dataset.length; s++)\r
{\r
+ sdataset.addElement(dataset[s]);\r
+ }\r
+ while (sdataset.size() > 0 && db < dbSources.length)\r
+ {\r
+ int maxqlen = 1; // default number of queries made to at one time\r
+ System.err.println("Verifying against " + dbSources[db]);\r
+ jalview.ws.seqfetcher.DbSourceProxy dbsource = sfetcher\r
+ .getSourceProxy(dbSources[db]);\r
+ if (dbsource == null)\r
+ {\r
+ System.err.println("No proxy for " + dbSources[db]);\r
+ db++;\r
+ continue;\r
+ }\r
+ if (dbsource.getDbSourceProperties()\r
+ .containsKey(DBRefSource.MULTIACC))\r
+ {\r
+ maxqlen = ((Integer) dbsource.getDbSourceProperties().get(\r
+ DBRefSource.MULTIACC)).intValue();\r
+ }\r
+ else\r
+ {\r
+ maxqlen = 1;\r
+ }\r
+ // iterate through db for each remaining un-verified sequence\r
+ SequenceI[] currSeqs = new SequenceI[sdataset.size()];\r
+ sdataset.copyInto(currSeqs);// seqs that are to be validated against\r
+ // dbSources[db]\r
+ Vector queries = new Vector(); // generated queries curSeq\r
+ seqRefs = new Hashtable();\r
+\r
int seqIndex = 0;\r
\r
- while (seqIndex < dataset.length)\r
+ while (queries.size() > 0 || seqIndex < currSeqs.length)\r
{\r
- StringBuffer queryString = new StringBuffer("uniprot:");\r
-\r
- for (int i = 0; (seqIndex < dataset.length) && (i < 50);\r
- seqIndex++, i++)\r
+ if (queries.size() > 0)\r
{\r
- SequenceI sequence = dataset[seqIndex];\r
- DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(sequence.\r
- getDBRef(), new String[]\r
- {\r
- jalview.datamodel.DBRefSource.UNIPROT});\r
- if (uprefs != null)\r
+ // Still queries to make for current seqIndex\r
+ StringBuffer queryString = new StringBuffer("");\r
+ int nqSize = (maxqlen > queries.size()) ? queries.size()\r
+ : maxqlen;\r
+ for (int nq = 0, numq = 0; nq < nqSize; nq++)\r
{\r
- if (uprefs.length + i > 50)\r
+ String query = (String) queries.elementAt(nq);\r
+ if (dbsource.isValidReference(query))\r
{\r
- break;\r
- }\r
-\r
- for (int j = 0; j < uprefs.length; j++)\r
- {\r
- addSeqId(sequence, uprefs[j].getAccessionId());\r
- queryString.append(uprefs[j].getAccessionId() + ";");\r
+ queryString.append((nq == 0) ? "" : dbsource\r
+ .getAccessionSeparator());\r
+ queryString.append(query);\r
+ numq++;\r
}\r
}\r
- else\r
+ for (int nq = 0; nq < nqSize; nq++)\r
+ {\r
+ queries.removeElementAt(0);\r
+ }\r
+ // make the queries and process the response\r
+ AlignmentI retrieved = null;\r
+ try\r
{\r
- StringTokenizer st = new StringTokenizer(sequence.getName(), "|");\r
- if (st.countTokens() + i > 50)\r
+ retrieved = dbsource.getSequenceRecords(queryString.toString());\r
+ } catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ } catch (OutOfMemoryError err)\r
+ {\r
+ new OOMWarning("retrieving database references ("\r
+ + queryString.toString() + ")", err);\r
+ }\r
+ if (retrieved != null)\r
+ {\r
+ transferReferences(sdataset, dbSources[db], retrieved);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // make some more strings for use as queries\r
+ for (int i = 0; (seqIndex < dataset.length) && (i < 50); seqIndex++, i++)\r
+ {\r
+ SequenceI sequence = dataset[seqIndex];\r
+ DBRefEntry[] uprefs = jalview.util.DBRefUtils.selectRefs(\r
+ sequence.getDBRef(), new String[]\r
+ { dbSources[db] }); // jalview.datamodel.DBRefSource.UNIPROT\r
+ // });\r
+ // check for existing dbrefs to use\r
+ if (uprefs != null)\r
{\r
- //Dont send more than 50 id strings to dbFetch!!\r
- seqIndex--;\r
+ for (int j = 0; j < uprefs.length; j++)\r
+ {\r
+ addSeqId(sequence, uprefs[j].getAccessionId());\r
+ queries\r
+ .addElement(uprefs[j].getAccessionId()\r
+ .toUpperCase());\r
+ }\r
}\r
else\r
{\r
+ // generate queries from sequence ID string\r
+ StringTokenizer st = new StringTokenizer(sequence.getName(),\r
+ "|");\r
while (st.hasMoreTokens())\r
{\r
String token = st.nextToken();\r
addSeqId(sequence, token);\r
- queryString.append(token + ";");\r
+ queries.addElement(token.toUpperCase());\r
}\r
}\r
}\r
}\r
-\r
- ///////////////////////////////////\r
- ///READ FROM EBI\r
- EBIFetchClient ebi = new EBIFetchClient();\r
- File file = ebi.fetchDataAsFile(queryString.toString(), "xml", "raw");\r
- if (file != null)\r
- {\r
- ReadUniprotFile(file);\r
- }\r
}\r
- }\r
- catch (Exception ex)\r
- {\r
- ex.printStackTrace();\r
- }\r
-\r
+ // advance to next database\r
+ db++;\r
+ } // all databases have been queries.\r
if (sbuffer.length() > 0)\r
{\r
- output.setText(\r
- "Your sequences have been matched to Uniprot. Some of the ids have been\n" +\r
- "altered, most likely the start/end residue will have been updated.\n" +\r
- "Save your alignment to maintain the updated id.\n\n" +\r
- sbuffer.toString());\r
+ output\r
+ .setText("Your sequences have been verified against known sequence databases. Some of the ids have been\n"\r
+ + "altered, most likely the start/end residue will have been updated.\n"\r
+ + "Save your alignment to maintain the updated id.\n\n"\r
+ + sbuffer.toString());\r
Desktop.addInternalFrame(output, "Sequence names updated ", 600, 300);\r
// The above is the dataset, we must now find out the index\r
// of the viewed sequence\r
}\r
\r
/**\r
- * DOCUMENT ME!\r
- *\r
- * @param result DOCUMENT ME!\r
- * @param out DOCUMENT ME!\r
- * @param align DOCUMENT ME!\r
+ * Verify local sequences in seqRefs against the retrieved sequence database\r
+ * records.\r
+ * \r
*/\r
- void ReadUniprotFile(File file)\r
+ void transferReferences(Vector sdataset, String dbSource,\r
+ AlignmentI retrievedAl) // File\r
+ // file)\r
{\r
- if (!file.exists())\r
+\r
+ if (retrievedAl == null || retrievedAl.getHeight() == 0)\r
{\r
return;\r
}\r
-\r
+ SequenceI[] retrieved = retrievedAl.getSequencesArray();\r
SequenceI sequence = null;\r
\r
- Vector entries = getUniprotEntries(file);\r
+ // Vector entries = new Uniprot().getUniprotEntries(file);\r
\r
- int i, iSize = entries == null ? 0 : entries.size();\r
- UniprotEntry entry;\r
+ int i, iSize = retrieved.length; // entries == null ? 0 : entries.size();\r
+ // UniprotEntry entry;\r
for (i = 0; i < iSize; i++)\r
{\r
- entry = (UniprotEntry) entries.elementAt(i);\r
+ SequenceI entry = retrieved[i]; // (UniprotEntry) entries.elementAt(i);\r
\r
- //Work out which sequences this Uniprot file has matches to,\r
- //taking into account all accessionIds and names in the file\r
+ // Work out which sequences this sequence matches,\r
+ // taking into account all accessionIds and names in the file\r
Vector sequenceMatches = new Vector();\r
- for (int j = 0; j < entry.getAccession().size(); j++)\r
+ // look for corresponding accession ids\r
+ DBRefEntry[] entryRefs = jalview.util.DBRefUtils.selectRefs(entry\r
+ .getDBRef(), new String[]\r
+ { dbSource });\r
+ for (int j = 0; j < entryRefs.length; j++)\r
{\r
- String accessionId = entry.getAccession().elementAt(j).toString();\r
- if (seqRefs.containsKey(accessionId))\r
+ String accessionId = entryRefs[j].getAccessionId(); // .getAccession().elementAt(j).toString();\r
+ // match up on accessionId\r
+ if (seqRefs.containsKey(accessionId.toUpperCase()))\r
{\r
Vector seqs = (Vector) seqRefs.get(accessionId);\r
for (int jj = 0; jj < seqs.size(); jj++)\r
}\r
}\r
}\r
- for (int j = 0; j < entry.getName().size(); j++)\r
+ if (sequenceMatches.size() == 0)\r
{\r
- String name = entry.getName().elementAt(j).toString();\r
- if (seqRefs.containsKey(name))\r
+ // failed to match directly on accessionId==query so just compare all\r
+ // sequences to entry\r
+ Enumeration e = seqRefs.keys();\r
+ while (e.hasMoreElements())\r
{\r
- Vector seqs = (Vector) seqRefs.get(name);\r
- for (int jj = 0; jj < seqs.size(); jj++)\r
+ Vector sqs = (Vector) seqRefs.get(e.nextElement());\r
+ if (sqs != null && sqs.size() > 0)\r
{\r
- sequence = (SequenceI) seqs.elementAt(jj);\r
- if (!sequenceMatches.contains(sequence))\r
+ Enumeration sqe = sqs.elements();\r
+ while (sqe.hasMoreElements())\r
{\r
- sequenceMatches.addElement(sequence);\r
+ sequenceMatches.addElement(sqe.nextElement());\r
}\r
}\r
}\r
}\r
-\r
+ // look for corresponding names\r
+ // this is uniprot specific ?\r
+ // could be useful to extend this so we try to find any 'significant'\r
+ // information in common between two sequence objects.\r
+ /*\r
+ * DBRefEntry[] entryRefs =\r
+ * jalview.util.DBRefUtils.selectRefs(entry.getDBRef(), new String[] {\r
+ * dbSource }); for (int j = 0; j < entry.getName().size(); j++) { String\r
+ * name = entry.getName().elementAt(j).toString(); if\r
+ * (seqRefs.containsKey(name)) { Vector seqs = (Vector) seqRefs.get(name);\r
+ * for (int jj = 0; jj < seqs.size(); jj++) { sequence = (SequenceI)\r
+ * seqs.elementAt(jj); if (!sequenceMatches.contains(sequence)) {\r
+ * sequenceMatches.addElement(sequence); } } } }\r
+ */\r
+ // sequenceMatches now contains the set of all sequences associated with\r
+ // the returned db record\r
+ String entrySeq = entry.getSequenceAsString().toUpperCase();\r
for (int m = 0; m < sequenceMatches.size(); m++)\r
{\r
sequence = (SequenceI) sequenceMatches.elementAt(m);\r
- sequence.addDBRef(new DBRefEntry(DBRefSource.UNIPROT,\r
- "0", // TODO: VERSION FROM UNIPROT\r
- entry.getAccession().elementAt(0).\r
- toString()));\r
-\r
- System.out.println("Adding dbref to " + sequence.getName() + " : " +\r
- entry.getAccession().elementAt(0).toString());\r
+ // only update start and end positions and shift features if there are\r
+ // no existing references\r
+ // TODO: test for legacy where uniprot or EMBL refs exist but no\r
+ // mappings are made (but content matches retrieved set)\r
+ boolean updateRefFrame = sequence.getDBRef() == null\r
+ || sequence.getDBRef().length == 0;\r
+ // verify sequence against the entry sequence\r
\r
String nonGapped = AlignSeq.extractGaps("-. ",\r
- sequence.getSequenceAsString()).\r
- toUpperCase();\r
+ sequence.getSequenceAsString()).toUpperCase();\r
\r
- int absStart = entry.getUniprotSequence().getContent().indexOf(\r
- nonGapped.toString());\r
+ int absStart = entrySeq.indexOf(nonGapped);\r
+ int mapStart = entry.getStart();\r
+ jalview.datamodel.Mapping mp;\r
\r
if (absStart == -1)\r
{\r
- // Is UniprotSequence contained in dataset sequence?\r
- absStart = nonGapped.toString().indexOf(entry.getUniprotSequence().\r
- getContent());\r
+ // Is local sequence contained in dataset sequence?\r
+ absStart = nonGapped.indexOf(entrySeq);\r
if (absStart == -1)\r
- {\r
- sbuffer.append(sequence.getName() + " SEQUENCE NOT %100 MATCH \n");\r
+ { // verification failed.\r
+ sbuffer.append(sequence.getName()\r
+ + " SEQUENCE NOT %100 MATCH \n");\r
continue;\r
}\r
\r
- if (entry.getFeature() != null)\r
+ sbuffer.append(sequence.getName() + " HAS " + absStart\r
+ + " PREFIXED RESIDUES COMPARED TO " + dbSource + "\n");\r
+ //\r
+ // + " - ANY SEQUENCE FEATURES"\r
+ // + " HAVE BEEN ADJUSTED ACCORDINGLY \n");\r
+ // absStart = 0;\r
+ // create valid mapping between matching region of local sequence and\r
+ // the mapped sequence\r
+ mp = new Mapping(null, new int[]\r
+ { sequence.getStart() + absStart,\r
+ sequence.getStart() + absStart + entrySeq.length() - 1 },\r
+ new int[]\r
+ { entry.getStart(),\r
+ entry.getStart() + entrySeq.length() - 1 }, 1, 1);\r
+ updateRefFrame = false; // mapping is based on current start/end so\r
+ // don't modify start and end\r
+ }\r
+ else\r
+ {\r
+ // update start and end of local sequence to place it in entry's\r
+ // reference frame.\r
+ // apply identity map map from whole of local sequence to matching\r
+ // region of database\r
+ // sequence\r
+ mp = null; // Mapping.getIdentityMap();\r
+ // new Mapping(null,\r
+ // new int[] { absStart+sequence.getStart(),\r
+ // absStart+sequence.getStart()+entrySeq.length()-1},\r
+ // new int[] { entry.getStart(), entry.getEnd() }, 1, 1);\r
+ // relocate local features for updated start\r
+ if (updateRefFrame && sequence.getSequenceFeatures() != null)\r
{\r
- Enumeration e = entry.getFeature().elements();\r
- while (e.hasMoreElements())\r
+ SequenceFeature[] sf = sequence.getSequenceFeatures();\r
+ int start = sequence.getStart();\r
+ int end = sequence.getEnd();\r
+ int startShift = 1 - absStart - start; // how much the features are\r
+ // to be shifted by\r
+ for (int sfi = 0; sfi < sf.length; sfi++)\r
{\r
- SequenceFeature sf = (SequenceFeature) e.nextElement();\r
- sf.setBegin(sf.getBegin() + absStart + 1);\r
- sf.setEnd(sf.getEnd() + absStart + 1);\r
+ if (sf[sfi].getBegin() >= start && sf[sfi].getEnd() <= end)\r
+ {\r
+ // shift feature along by absstart\r
+ sf[sfi].setBegin(sf[sfi].getBegin() + startShift);\r
+ sf[sfi].setEnd(sf[sfi].getEnd() + startShift);\r
+ }\r
}\r
-\r
- sbuffer.append(sequence.getName() +\r
- " HAS " + absStart +\r
- " PREFIXED RESIDUES COMPARED TO UNIPROT - ANY SEQUENCE FEATURES"\r
- + " HAVE BEEN ADJUSTED ACCORDINGLY \n");\r
- absStart = 0;\r
}\r
-\r
}\r
\r
- //unknownSequences.remove(sequence);\r
-\r
- int absEnd = absStart + nonGapped.toString().length();\r
+ System.out.println("Adding dbrefs to " + sequence.getName()\r
+ + " from " + dbSource + " sequence : " + entry.getName());\r
+ sequence.transferAnnotation(entry, mp);\r
+ // unknownSequences.remove(sequence);\r
+ int absEnd = absStart + nonGapped.length();\r
absStart += 1;\r
-\r
- Enumeration e = entry.getDbReference().elements();\r
- Vector onlyPdbEntries = new Vector();\r
- while (e.hasMoreElements())\r
+ if (updateRefFrame)\r
{\r
- PDBEntry pdb = (PDBEntry) e.nextElement();\r
- if (!pdb.getType().equals(DBRefSource.PDB))\r
- {\r
- DBRefEntry xref = new DBRefEntry(pdb.getType(), DBRefSource.UNIPROT, pdb.getId());\r
- sequence.addDBRef(xref);\r
- continue;\r
- }\r
- \r
- sequence.addDBRef(new DBRefEntry(DBRefSource.PDB,\r
- "0",\r
- pdb.getId()));\r
-\r
- onlyPdbEntries.addElement(pdb);\r
+ // finally, update local sequence reference frame if we're allowed\r
+ sequence.setStart(absStart);\r
+ sequence.setEnd(absEnd);\r
}\r
-\r
- sequence.setPDBId(onlyPdbEntries);\r
-\r
- sequence.setStart(absStart);\r
- sequence.setEnd(absEnd);\r
-\r
+ // and remove it from the rest\r
+ // TODO: decide if we should remove annotated sequence from set\r
+ sdataset.remove(sequence);\r
}\r
}\r
}\r