X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fdatamodel%2FSequence.java;h=88f430803a14178d7bec70baedef7b9647d8e813;hb=134291d04042b3d7252598a9f73753f40da08a18;hp=d73b28386f69782fac4cc3fca2dae0abd81ac8a7;hpb=e77a693cf000b4ff8a863411acf8c90c6390a061;p=jalview.git diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index d73b283..88f4308 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -25,6 +25,7 @@ import jalview.api.DBRefEntryI; import jalview.util.StringUtils; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; import java.util.List; import java.util.Vector; @@ -185,12 +186,13 @@ public class Sequence extends ASequence implements SequenceI } /** - * Creates a new Sequence object with new features, DBRefEntries, - * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence - * reference. + * Creates a new Sequence object with new AlignmentAnnotations but inherits + * any existing dataset sequence reference. If non exists, everything is + * copied. * * @param seq - * DOCUMENT ME! + * if seq is a dataset sequence, behaves like a plain old copy + * constructor */ public Sequence(SequenceI seq) { @@ -213,30 +215,48 @@ public class Sequence extends ASequence implements SequenceI } + /** + * does the heavy lifting when cloning a dataset sequence, or coping data from + * dataset to a new derived sequence. + * + * @param seq + * - source of attributes. + * @param alAnnotation + * - alignment annotation present on seq that should be copied onto + * this sequence + */ protected void initSeqFrom(SequenceI seq, AlignmentAnnotation[] alAnnotation) { - initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(), + { + char[] oseq = seq.getSequence(); + initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length), + seq.getStart(), seq.getEnd()); + } description = seq.getDescription(); - sourceDBRef = seq.getSourceDBRef(); - if (seq.getSequenceFeatures() != null) + sourceDBRef = seq.getSourceDBRef() == null ? null : new DBRefEntry( + seq.getSourceDBRef()); + if (seq != datasetSequence) { - SequenceFeature[] sf = seq.getSequenceFeatures(); - for (int i = 0; i < sf.length; i++) - { - addSequenceFeature(new SequenceFeature(sf[i])); - } + setDatasetSequence(seq.getDatasetSequence()); } - setDatasetSequence(seq.getDatasetSequence()); if (datasetSequence == null && seq.getDBRefs() != null) { - // only copy DBRefs if we really are a dataset sequence + // only copy DBRefs and seqfeatures if we really are a dataset sequence DBRefEntry[] dbr = seq.getDBRefs(); for (int i = 0; i < dbr.length; i++) { addDBRef(new DBRefEntry(dbr[i])); } + if (seq.getSequenceFeatures() != null) + { + SequenceFeature[] sf = seq.getSequenceFeatures(); + for (int i = 0; i < sf.length; i++) + { + addSequenceFeature(new SequenceFeature(sf[i])); + } + } } if (seq.getAnnotation() != null) { @@ -265,30 +285,44 @@ public class Sequence extends ASequence implements SequenceI } if (seq.getAllPDBEntries() != null) { - Vector ids = seq.getAllPDBEntries(); - Enumeration e = ids.elements(); - while (e.hasMoreElements()) + Vector ids = seq.getAllPDBEntries(); + for (PDBEntry pdb : ids) { - this.addPDBId(new PDBEntry((PDBEntry) e.nextElement())); + this.addPDBId(new PDBEntry(pdb)); } } } - /** - * DOCUMENT ME! - * - * @param v - * DOCUMENT ME! - */ + @Override public void setSequenceFeatures(SequenceFeature[] features) { - sequenceFeatures = features; + if (datasetSequence == null) + { + sequenceFeatures = features; + } + else + { + if (datasetSequence.getSequenceFeatures() != features + && datasetSequence.getSequenceFeatures() != null + && datasetSequence.getSequenceFeatures().length > 0) + { + new Exception( + "Warning: JAL-2046 side effect ? Possible implementation error: overwriting dataset sequence features by setting sequence features on alignment") + .printStackTrace(); + } + datasetSequence.setSequenceFeatures(features); + } } @Override public synchronized void addSequenceFeature(SequenceFeature sf) { + if (sequenceFeatures==null && datasetSequence != null) + { + datasetSequence.addSequenceFeature(sf); + return; + } if (sequenceFeatures == null) { sequenceFeatures = new SequenceFeature[0]; @@ -314,6 +348,9 @@ public class Sequence extends ASequence implements SequenceI { if (sequenceFeatures == null) { + if (datasetSequence!=null) { + datasetSequence.deleteFeature(sf); + } return; } @@ -611,17 +648,15 @@ public class Sequence extends ASequence implements SequenceI } /** - * DOCUMENT ME! - * - * @param i - * DOCUMENT ME! + * Returns the character of the aligned sequence at the given position (base + * zero), or space if the position is not within the sequence's bounds * - * @return DOCUMENT ME! + * @return */ @Override public char getCharAt(int i) { - if (i < sequence.length) + if (i >= 0 && i < sequence.length) { return sequence[i]; } @@ -931,31 +966,31 @@ public class Sequence extends ASequence implements SequenceI @Override public void addDBRef(DBRefEntry entry) { + // TODO add to dataset sequence instead if there is one? if (dbrefs == null) { dbrefs = new DBRefEntry[0]; } - int i, iSize = dbrefs.length; - - for (i = 0; i < iSize; i++) + for (DBRefEntryI dbr : dbrefs) { - if (dbrefs[i].equalRef(entry)) + if (dbr.updateFrom(entry)) { - if (entry.getMap() != null) - { - if (dbrefs[i].getMap() == null) - { - // overwrite with 'superior' entry that contains a mapping. - dbrefs[i] = entry; - } - } + /* + * found a dbref that either matched, or could be + * updated from, the new entry - no need to add it + */ return; } } - DBRefEntry[] temp = new DBRefEntry[iSize + 1]; - System.arraycopy(dbrefs, 0, temp, 0, iSize); + /* + * extend the array to make room for one more + */ + // TODO use an ArrayList instead + int j = dbrefs.length; + DBRefEntry[] temp = new DBRefEntry[j + 1]; + System.arraycopy(dbrefs, 0, temp, 0, j); temp[temp.length - 1] = entry; dbrefs = temp; @@ -964,6 +999,7 @@ public class Sequence extends ASequence implements SequenceI @Override public void setDatasetSequence(SequenceI seq) { + // TODO check for circular reference before setting? datasetSequence = seq; } @@ -1036,33 +1072,45 @@ public class Sequence extends ASequence implements SequenceI @Override public SequenceI deriveSequence() { - SequenceI seq = new Sequence(this); - if (datasetSequence != null) - { - // duplicate current sequence with same dataset - seq.setDatasetSequence(datasetSequence); - } - else + Sequence seq=null; + if (datasetSequence == null) { if (isValidDatasetSequence()) { // Use this as dataset sequence + seq = new Sequence(getName(), "", 1, -1); seq.setDatasetSequence(this); + seq.initSeqFrom(this, getAnnotation()); + return seq; } else { // Create a new, valid dataset sequence - SequenceI ds = seq; - ds.setSequence(AlignSeq.extractGaps( - jalview.util.Comparison.GapChars, new String(sequence))); - setDatasetSequence(ds); - ds.setSequenceFeatures(getSequenceFeatures()); - seq = this; // and return this sequence as the derived sequence. + createDatasetSequence(); } } - return seq; + return new Sequence(this); } + private boolean _isNa; + + private long _seqhash = 0; + + @Override + public boolean isProtein() + { + if (datasetSequence != null) + { + return datasetSequence.isProtein(); + } + if (_seqhash != sequence.hashCode()) + { + _seqhash = sequence.hashCode(); + _isNa=jalview.util.Comparison.isNucleotide(new SequenceI[] { this }); + } + return !_isNa; + }; + /* * (non-Javadoc) * @@ -1073,20 +1121,27 @@ public class Sequence extends ASequence implements SequenceI { if (datasetSequence == null) { - datasetSequence = new Sequence(getName(), AlignSeq.extractGaps( + Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps( jalview.util.Comparison.GapChars, getSequenceAsString()), getStart(), getEnd()); - datasetSequence.setSequenceFeatures(getSequenceFeatures()); - datasetSequence.setDescription(getDescription()); - setSequenceFeatures(null); - // move database references onto dataset sequence - datasetSequence.setDBRefs(getDBRefs()); - setDBRefs(null); - datasetSequence.setPDBId(getAllPDBEntries()); - setPDBId(null); + + datasetSequence = dsseq; + + dsseq.setDescription(description); + // move features and database references onto dataset sequence + dsseq.sequenceFeatures = sequenceFeatures; + sequenceFeatures=null; + dsseq.dbrefs = dbrefs; + dbrefs=null; + // TODO: search and replace any references to this sequence with + // references to the dataset sequence in Mappings on dbref + dsseq.pdbIds = pdbIds; + pdbIds = null; datasetSequence.updatePDBIds(); if (annotation != null) { + // annotation is cloned rather than moved, to preserve what's currently + // on the alignment for (AlignmentAnnotation aa : annotation) { AlignmentAnnotation _aa = new AlignmentAnnotation(aa);