X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequence.java;h=98b0fb3a4846dae0f1b1ba01fced2039b319b70a;hb=62d2ee96fc3876b3773e786596bc5bf3ed2388df;hp=783b1bc004ca7e5088803e601e010a2a5fb50fc5;hpb=36c0fb5e4293dfa35454d0ab33059a38824aafc8;p=jalview.git diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 783b1bc..98b0fb3 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -89,7 +89,7 @@ public class Sequence extends ASequence implements SequenceI */ int index = -1; - private SequenceFeatures sequenceFeatureStore; + private SequenceFeaturesI sequenceFeatureStore; /* * A cursor holding the approximate current view position to the sequence, @@ -155,8 +155,8 @@ public class Sequence extends ASequence implements SequenceI { if (name == null) { - System.err - .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor."); + System.err.println( + "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor."); name = ""; } // Does sequence have the /start-end signature? @@ -645,10 +645,10 @@ public class Sequence extends ASequence implements SequenceI } /** - * DOCUMENT ME! + * Sets the sequence description, and also parses out any special formats of + * interest * * @param desc - * DOCUMENT ME! */ @Override public void setDescription(String desc) @@ -656,10 +656,67 @@ public class Sequence extends ASequence implements SequenceI this.description = desc; } + @Override + public void setGeneLoci(String speciesId, String assemblyId, + String chromosomeId, MapList map) + { + addDBRef(new DBRefEntry(speciesId, assemblyId, DBRefEntry.CHROMOSOME + + ":" + chromosomeId, new Mapping(map))); + } + /** - * DOCUMENT ME! + * Returns the gene loci mapping for the sequence (may be null) * - * @return DOCUMENT ME! + * @return + */ + @Override + public GeneLociI getGeneLoci() + { + DBRefEntry[] refs = getDBRefs(); + if (refs != null) + { + for (final DBRefEntry ref : refs) + { + if (ref.isChromosome()) + { + return new GeneLociI() + { + @Override + public String getSpeciesId() + { + return ref.getSource(); + } + + @Override + public String getAssemblyId() + { + return ref.getVersion(); + } + + @Override + public String getChromosomeId() + { + // strip of "chromosome:" prefix to chrId + return ref.getAccessionId().substring( + DBRefEntry.CHROMOSOME.length() + 1); + } + + @Override + public MapList getMap() + { + return ref.getMap().getMap(); + } + }; + } + } + } + return null; + } + + /** + * Answers the description + * + * @return */ @Override public String getDescription() @@ -984,6 +1041,53 @@ public class Sequence extends ASequence implements SequenceI } /** + * {@inheritDoc} + */ + @Override + public Range findPositions(int fromColumn, int toColumn) + { + if (toColumn < fromColumn || fromColumn < 1) + { + return null; + } + + /* + * find the first non-gapped position, if any + */ + int firstPosition = 0; + int col = fromColumn - 1; + int length = sequence.length; + while (col < length && col < toColumn) + { + if (!Comparison.isGap(sequence[col])) + { + firstPosition = findPosition(col++); + break; + } + col++; + } + + if (firstPosition == 0) + { + return null; + } + + /* + * find the last non-gapped position + */ + int lastPosition = firstPosition; + while (col < length && col < toColumn) + { + if (!Comparison.isGap(sequence[col++])) + { + lastPosition++; + } + } + + return new Range(firstPosition, lastPosition); + } + + /** * Returns an int array where indices correspond to each residue in the * sequence and the element value gives its position in the alignment * @@ -1317,8 +1421,9 @@ public class Sequence extends ASequence implements SequenceI @Override public AlignmentAnnotation[] getAnnotation() { - return annotation == null ? null : annotation - .toArray(new AlignmentAnnotation[annotation.size()]); + return annotation == null ? null + : annotation + .toArray(new AlignmentAnnotation[annotation.size()]); } @Override @@ -1430,8 +1535,9 @@ public class Sequence extends ASequence implements SequenceI { if (datasetSequence == null) { - Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps( - jalview.util.Comparison.GapChars, getSequenceAsString()), + Sequence dsseq = new Sequence(getName(), + AlignSeq.extractGaps(jalview.util.Comparison.GapChars, + getSequenceAsString()), getStart(), getEnd()); datasetSequence = dsseq; @@ -1573,7 +1679,7 @@ public class Sequence extends ASequence implements SequenceI List sfs = entry.getSequenceFeatures(); for (SequenceFeature feature : sfs) { - SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature) + SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature) : new SequenceFeature[] { new SequenceFeature(feature) }; if (sf != null) { @@ -1730,8 +1836,8 @@ public class Sequence extends ASequence implements SequenceI } } // whilst it looks like it is a primary ref, we also sanity check type - if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals( - DBRefUtils.getCanonicalName(ref.getSource()))) + if (DBRefUtils.getCanonicalName(DBRefSource.PDB) + .equals(DBRefUtils.getCanonicalName(ref.getSource()))) { // PDB dbrefs imply there should be a PDBEntry associated // TODO: tighten PDB dbrefs @@ -1767,52 +1873,45 @@ public class Sequence extends ASequence implements SequenceI String... types) { int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0 - int endPos = findPosition(toColumn - 1); - // to trace / debug behaviour: - // System.out - // .println(String - // .format("%s.findFeatures columns [%d-%d] positions [%d-%d] leaves cursor %s", - // getName(), fromColumn, toColumn, startPos, - // endPos, cursor)); - List result = new ArrayList<>(); - if (datasetSequence != null) - { - result = datasetSequence.getFeatures().findFeatures(startPos, endPos, - types); - } - else - { - result = sequenceFeatureStore.findFeatures(startPos, endPos, types); - } + int endPos = fromColumn == toColumn ? startPos + : findPosition(toColumn - 1); + + List result = getFeatures().findFeatures(startPos, + endPos, types); /* - * if the start or end column is gapped, startPos or endPos may be to the - * left or right, and we may have included adjacent or enclosing features; + * if end column is gapped, endPos may be to the right, + * and we may have included adjacent or enclosing features; * remove any that are not enclosing, non-contact features */ - if (endPos > this.end || Comparison.isGap(sequence[fromColumn - 1]) - || Comparison.isGap(sequence[toColumn - 1])) + if (endPos > this.end || Comparison.isGap(sequence[toColumn - 1])) { ListIterator it = result.listIterator(); while (it.hasNext()) { SequenceFeature sf = it.next(); - int featureStartColumn = findIndex(sf.getBegin()); - int featureEndColumn = findIndex(sf.getEnd()); - boolean noOverlap = featureStartColumn > toColumn - || featureEndColumn < fromColumn; - - /* - * reject an 'enclosing' feature if it is actually a contact feature - */ - if (sf.isContactFeature() && featureStartColumn < fromColumn - && featureEndColumn > toColumn) + int sfBegin = sf.getBegin(); + int sfEnd = sf.getEnd(); + int featureStartColumn = findIndex(sfBegin); + if (featureStartColumn > toColumn) { - noOverlap = true; + it.remove(); } - if (noOverlap) + else if (featureStartColumn < fromColumn) { - it.remove(); + int featureEndColumn = sfEnd == sfBegin ? featureStartColumn + : findIndex(sfEnd); + if (featureEndColumn < fromColumn) + { + it.remove(); + } + else if (featureEndColumn > toColumn && sf.isContactFeature()) + { + /* + * remove an enclosing feature if it is a contact feature + */ + it.remove(); + } } } }