X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FCrossRef.java;h=7e77fc1e0903799660db67205966ddcf768a1897;hb=0ebcd487f08f9873987d9a32edce7df47aa27927;hp=72382393811ab4b8a7e97b6bcae75726958dec0c;hpb=3ef44bef1f825d26977dedd1608469712a87fe15;p=jalview.git diff --git a/src/jalview/analysis/CrossRef.java b/src/jalview/analysis/CrossRef.java index 7238239..7e77fc1 100644 --- a/src/jalview/analysis/CrossRef.java +++ b/src/jalview/analysis/CrossRef.java @@ -20,21 +20,24 @@ */ package jalview.analysis; -import java.util.ArrayList; -import java.util.List; -import java.util.Vector; - import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefEntry; import jalview.datamodel.DBRefSource; +import jalview.datamodel.Mapping; import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.util.DBRefUtils; +import jalview.util.MapList; import jalview.ws.SequenceFetcher; import jalview.ws.seqfetcher.ASequenceFetcher; +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + /** * Functions for cross-referencing sequence databases. user must first specify * if cross-referencing from protein or dna (set dna==true) @@ -44,6 +47,27 @@ import jalview.ws.seqfetcher.ASequenceFetcher; */ public class CrossRef { + /* + * A sub-class that ignores Parent attribute when comparing sequence + * features. This avoids 'duplicate' CDS features that only + * differ in their parent Transcript ids. + */ + class MySequenceFeature extends SequenceFeature + { + private SequenceFeature feat; + + MySequenceFeature(SequenceFeature sf) + { + this.feat = sf; + } + + @Override + public boolean equals(Object o) + { + return feat.equals(o, true); + } + } + /** * Select just the DNA or protein references for a protein or dna sequence * @@ -88,41 +112,54 @@ public class CrossRef { String[] dbrefs = null; List refs = new ArrayList(); - for (int s = 0; s < seqs.length; s++) + for (SequenceI seq : seqs) { - if (seqs[s] != null) + if (seq != null) { - SequenceI dss = seqs[s]; + SequenceI dss = seq; while (dss.getDatasetSequence() != null) { dss = dss.getDatasetSequence(); } - DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRef()); - for (int r = 0; rfs != null && r < rfs.length; r++) + DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRefs()); + if (rfs != null) { - if (!refs.contains(rfs[r].getSource())) + for (DBRefEntry ref : rfs) { - refs.add(rfs[r].getSource()); + if (!refs.contains(ref.getSource())) + { + refs.add(ref.getSource()); + } } } if (dataset != null) { // search for references to this sequence's direct references. - DBRefEntry[] lrfs = CrossRef - .findXDbRefs(!dna, seqs[s].getDBRef()); + DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs()); List rseqs = new ArrayList(); - CrossRef.searchDatasetXrefs(seqs[s], !dna, lrfs, dataset, rseqs, + CrossRef.searchDatasetXrefs(seq, !dna, lrfs, dataset, rseqs, null); // don't need to specify codon frame for mapping here for (SequenceI rs : rseqs) { - DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRef()); // not used?? - for (int r = 0; rfs != null && r < rfs.length; r++) + DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRefs()); + if (xrs != null) { - if (!refs.contains(rfs[r].getSource())) + for (DBRefEntry ref : xrs) { - refs.add(rfs[r].getSource()); + if (!refs.contains(ref.getSource())) + { + refs.add(ref.getSource()); + } } } + // looks like copy and paste - change rfs to xrs? + // for (int r = 0; rfs != null && r < rfs.length; r++) + // { + // if (!refs.contains(rfs[r].getSource())) + // { + // refs.add(rfs[r].getSource()); + // } + // } } } } @@ -135,13 +172,9 @@ public class CrossRef return dbrefs; } - /* - * if (dna) { if (rfs[r].hasMap()) { // most likely this is a protein cross - * reference if (!refs.contains(rfs[r].getSource())) { - * refs.addElement(rfs[r].getSource()); } } } - */ public static boolean hasCdnaMap(SequenceI[] seqs) { + // TODO unused - remove? String[] reftypes = findSequenceXrefTypes(false, seqs); for (int s = 0; s < reftypes.length; s++) { @@ -156,10 +189,11 @@ public class CrossRef public static SequenceI[] getCdnaMap(SequenceI[] seqs) { + // TODO unused - remove? Vector cseqs = new Vector(); for (int s = 0; s < seqs.length; s++) { - DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRef()); + DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRefs()); for (int c = 0; c < cdna.length; c++) { if (cdna[c].getSource().equals(DBRefSource.EMBLCDS)) @@ -186,44 +220,36 @@ public class CrossRef /** * - * @param dna - * @param seqs - * @return - */ - public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna, - String source) - { - return findXrefSequences(seqs, dna, source, null); - } - - /** - * * @param seqs + * sequences whose xrefs are being retrieved * @param dna + * true if sequences are nucleotide * @param source - * @param dataset - * alignment to search for product sequences. + * @param al + * alignment to search for cross-referenced sequences (and possibly + * add to) * @return products (as dataset sequences) */ - public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna, - String source, AlignmentI dataset) + public static Alignment findXrefSequences(SequenceI[] seqs, + final boolean dna, final String source, AlignmentI al) { + AlignmentI dataset = al.getDataset() == null ? al : al.getDataset(); List rseqs = new ArrayList(); - Alignment ral = null; - AlignedCodonFrame cf = new AlignedCodonFrame(); // nominal width - for (int s = 0; s < seqs.length; s++) + AlignedCodonFrame cf = new AlignedCodonFrame(); + for (SequenceI seq : seqs) { - SequenceI dss = seqs[s]; + SequenceI dss = seq; while (dss.getDatasetSequence() != null) { dss = dss.getDatasetSequence(); } boolean found = false; - DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRef()); + DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRefs()); if ((xrfs == null || xrfs.length == 0) && dataset != null) { System.out.println("Attempting to find ds Xrefs refs."); - DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seqs[s].getDBRef()); + // FIXME should be dss not seq here? + DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs()); // less ambiguous would be a 'find primary dbRefEntry' method. // filter for desired source xref here found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset, @@ -231,29 +257,30 @@ public class CrossRef } for (int r = 0; xrfs != null && r < xrfs.length; r++) { - if (source != null && !source.equals(xrfs[r].getSource())) + DBRefEntry xref = xrfs[r]; + if (source != null && !source.equals(xref.getSource())) { continue; } - if (xrfs[r].hasMap()) + if (xref.hasMap()) { - if (xrfs[r].getMap().getTo() != null) + if (xref.getMap().getTo() != null) { - SequenceI rsq = new Sequence(xrfs[r].getMap().getTo()); + SequenceI rsq = new Sequence(xref.getMap().getTo()); rseqs.add(rsq); - if (xrfs[r].getMap().getMap().getFromRatio() != xrfs[r] + if (xref.getMap().getMap().getFromRatio() != xref .getMap().getMap().getToRatio()) { // get sense of map correct for adding to product alignment. if (dna) { // map is from dna seq to a protein product - cf.addMap(dss, rsq, xrfs[r].getMap().getMap()); + cf.addMap(dss, rsq, xref.getMap().getMap()); } else { // map should be from protein seq to its coding dna - cf.addMap(rsq, dss, xrfs[r].getMap().getMap().getInverse()); + cf.addMap(rsq, dss, xref.getMap().getMap().getInverse()); } } found = true; @@ -265,9 +292,10 @@ public class CrossRef // xrefs on this sequence. if (dataset != null) { - found |= searchDataset(dss, xrfs[r], dataset, rseqs, cf); // ,false,!dna); + found |= searchDataset(dss, xref, dataset, rseqs, cf, false, + !dna); if (found) - { + { xrfs[r] = null; // we've recovered seqs for this one. } } @@ -299,8 +327,8 @@ public class CrossRef } if (l > 0) { - System.out - .println("Attempting to retrieve cross referenced sequences."); + // System.out + // .println("Attempting to retrieve cross referenced sequences."); DBRefEntry[] t = new DBRefEntry[l]; l = 0; for (int r = 0; r < xrfs.length; r++) @@ -313,37 +341,59 @@ public class CrossRef xrfs = t; try { - retrieved = sftch.getSequences(xrfs); // problem here is we don't - // know which of xrfs - // resulted in which + retrieved = sftch.getSequences(xrfs, !dna); + // problem here is we don't know which of xrfs resulted in which // retrieved element } catch (Exception e) { System.err .println("Problem whilst retrieving cross references for Sequence : " - + seqs[s].getName()); + + seq.getName()); e.printStackTrace(); } + if (retrieved != null) { + updateDbrefMappings(dna, seq, xrfs, retrieved, cf); + + SequenceIdMatcher matcher = new SequenceIdMatcher( + dataset.getSequences()); + List copiedFeatures = new ArrayList(); + CrossRef me = new CrossRef(); for (int rs = 0; rs < retrieved.length; rs++) { // TODO: examine each sequence for 'redundancy' - jalview.datamodel.DBRefEntry[] dbr = retrieved[rs] - .getDBRef(); + DBRefEntry[] dbr = retrieved[rs].getDBRefs(); if (dbr != null && dbr.length > 0) { for (int di = 0; di < dbr.length; di++) { // find any entry where we should put in the sequence being // cross-referenced into the map - jalview.datamodel.Mapping map = dbr[di].getMap(); + Mapping map = dbr[di].getMap(); if (map != null) { if (map.getTo() != null && map.getMap() != null) { - // should search the local dataset to find any existing - // candidates for To ! + SequenceI matched = matcher + .findIdMatch(map.getTo()); + if (matched != null) + { + /* + * already got an xref to this sequence; update this + * map to point to the same sequence, and add + * any new dbrefs to it + */ + for (DBRefEntry ref : map.getTo().getDBRefs()) + { + matched.addDBRef(ref); // add or update mapping + } + map.setTo(matched); + } + else + { + matcher.add(map.getTo()); + } try { // compare ms with dss and replace with dss in mapping @@ -352,17 +402,54 @@ public class CrossRef int sf = map.getMap().getToLowest(); int st = map.getMap().getToHighest(); SequenceI mappedrg = ms.getSubSequence(sf, st); - SequenceI loc = dss.getSubSequence(sf, st); + // SequenceI loc = dss.getSubSequence(sf, st); if (mappedrg.getLength() > 0 - && mappedrg.getSequenceAsString().equals( - loc.getSequenceAsString())) + && ms.getSequenceAsString().equals( + dss.getSequenceAsString())) + // && mappedrg.getSequenceAsString().equals( + // loc.getSequenceAsString())) { - System.err - .println("Mapping updated for retrieved crossreference"); + String msg = "Mapping updated from " + + ms.getName() + + " to retrieved crossreference " + + dss.getName(); + System.out.println(msg); // method to update all refs of existing To on // retrieved sequence with dss and merge any props // on To onto dss. map.setTo(dss); + /* + * copy sequence features as well, avoiding + * duplication (e.g. same variation from 2 + * transcripts) + */ + SequenceFeature[] sfs = ms + .getSequenceFeatures(); + if (sfs != null) + { + for (SequenceFeature feat : sfs) + { + /* + * we override SequenceFeature.equals here (but + * not elsewhere) to ignore Parent attribute + * TODO not quite working yet! + */ + if (!copiedFeatures + .contains(me.new MySequenceFeature( + feat))) + { + dss.addSequenceFeature(feat); + copiedFeatures.add(feat); + } + } + } + cf.addMap(retrieved[rs].getDatasetSequence(), + dss, map.getMap()); + } + else + { + cf.addMap(retrieved[rs].getDatasetSequence(), + map.getTo(), map.getMap()); } } catch (Exception e) { @@ -382,12 +469,12 @@ public class CrossRef } } } + + Alignment ral = null; if (rseqs.size() > 0) { - SequenceI[] rsqs = new SequenceI[rseqs.size()]; - rseqs.toArray(rsqs); - ral = new Alignment(rsqs); - if (cf != null && cf.getProtMappings() != null) + ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()])); + if (cf != null && !cf.isEmpty()) { ral.addCodonFrame(cf); } @@ -396,6 +483,69 @@ public class CrossRef } /** + * Updates any empty mappings in the cross-references with one to a compatible + * retrieved sequence if found, and adds any new mappings to the + * AlignedCodonFrame + * + * @param dna + * @param mapFrom + * @param xrefs + * @param retrieved + * @param acf + */ + static void updateDbrefMappings(boolean dna, SequenceI mapFrom, + DBRefEntry[] xrefs, SequenceI[] retrieved, AlignedCodonFrame acf) + { + SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved); + for (DBRefEntry xref : xrefs) + { + if (!xref.hasMap()) + { + String targetSeqName = xref.getSource() + "|" + + xref.getAccessionId(); + SequenceI[] matches = matcher.findAllIdMatches(targetSeqName); + if (matches == null) + { + return; + } + for (SequenceI seq : matches) + { + MapList mapping = null; + if (dna) + { + mapping = AlignmentUtils.mapCdnaToProtein(seq, mapFrom); + } + else + { + mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, seq); + if (mapping != null) + { + mapping = mapping.getInverse(); + } + } + if (mapping != null) + { + xref.setMap(new Mapping(seq, mapping)); + if (dna) + { + AlignmentUtils.computeProteinFeatures(mapFrom, seq, mapping); + } + if (dna) + { + acf.addMap(mapFrom, seq, mapping); + } + else + { + acf.addMap(seq, mapFrom, mapping.getInverse()); + } + continue; + } + } + } + } + } + + /** * find references to lrfs in the cross-reference set of each sequence in * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry * based on source and accession string only - Map and Version are nulled. @@ -408,8 +558,7 @@ public class CrossRef */ private static boolean searchDatasetXrefs(SequenceI sequenceI, boolean dna, DBRefEntry[] lrfs, AlignmentI dataset, - List rseqs, - AlignedCodonFrame cf) + List rseqs, AlignedCodonFrame cf) { boolean found = false; if (lrfs == null) @@ -501,7 +650,7 @@ public class CrossRef } // look for direct or indirect references in common - DBRefEntry[] poss = nxt.getDBRef(), cands = null; + DBRefEntry[] poss = nxt.getDBRefs(), cands = null; if (direct) { cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);