X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FCrossRef.java;h=7e77fc1e0903799660db67205966ddcf768a1897;hb=ded5f5732732b16ce8c87796397a82ceba6fc204;hp=d45750e0b95c993f479c3c034b48e3c09ff2fee4;hpb=ef9282b464dc189faf9ce40a4b7420a204266668;p=jalview.git diff --git a/src/jalview/analysis/CrossRef.java b/src/jalview/analysis/CrossRef.java index d45750e..7e77fc1 100644 --- a/src/jalview/analysis/CrossRef.java +++ b/src/jalview/analysis/CrossRef.java @@ -30,6 +30,7 @@ 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; @@ -219,30 +220,20 @@ 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(); AlignedCodonFrame cf = new AlignedCodonFrame(); for (SequenceI seq : seqs) @@ -301,7 +292,8 @@ public class CrossRef // xrefs on this sequence. if (dataset != null) { - found |= searchDataset(dss, xref, 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. @@ -335,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++) @@ -362,6 +354,10 @@ public class CrossRef 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++) @@ -379,8 +375,25 @@ public class CrossRef { 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 @@ -389,10 +402,12 @@ 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())) { String msg = "Mapping updated from " + ms.getName() @@ -405,7 +420,8 @@ public class CrossRef map.setTo(dss); /* * copy sequence features as well, avoiding - * duplication (e.g. from 2 transcripts) + * duplication (e.g. same variation from 2 + * transcripts) */ SequenceFeature[] sfs = ms .getSequenceFeatures(); @@ -414,8 +430,8 @@ public class CrossRef for (SequenceFeature feat : sfs) { /* - * we override the equality test here (but not - * elsewhere) to ignore Parent attribute + * we override SequenceFeature.equals here (but + * not elsewhere) to ignore Parent attribute * TODO not quite working yet! */ if (!copiedFeatures @@ -430,6 +446,11 @@ public class CrossRef cf.addMap(retrieved[rs].getDatasetSequence(), dss, map.getMap()); } + else + { + cf.addMap(retrieved[rs].getDatasetSequence(), + map.getTo(), map.getMap()); + } } catch (Exception e) { System.err @@ -452,9 +473,7 @@ public class CrossRef Alignment ral = null; if (rseqs.size() > 0) { - SequenceI[] rsqs = new SequenceI[rseqs.size()]; - rseqs.toArray(rsqs); - ral = new Alignment(rsqs); + ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()])); if (cf != null && !cf.isEmpty()) { ral.addCodonFrame(cf); @@ -464,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.