X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequence.java;h=42303660de8a07b5f50bd7454509edaf43460a2d;hb=d34b7142d47cab72ca9e0822b4407f3f497f191a;hp=8c3e538bd6d1601b5e04586eb50675983ff0f9cf;hpb=3da878124135ff033f42d19d8733891b09e953cd;p=jalview.git diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 8c3e538..4230366 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -27,6 +27,7 @@ import jalview.util.Comparison; import jalview.util.DBRefUtils; import jalview.util.MapList; import jalview.util.StringUtils; +import jalview.workers.InformationThread; import java.util.ArrayList; import java.util.Arrays; @@ -47,23 +48,26 @@ import fr.orsay.lri.varna.models.rna.RNA; public class Sequence extends ASequence implements SequenceI { - /** - * A subclass that gives us access to modCount, which tracks - * whether there have been any changes. We use this to update - * @author hansonr - * - * @param - */ + /** + * A subclass that gives us access to modCount, which tracks whether there + * have been any changes. We use this to update + * + * @author hansonr + * + * @param + */ @SuppressWarnings("serial") - public class DBModList extends ArrayList { + public class DBModList extends ArrayList + { + + protected int getModCount() + { + return modCount; + } - protected int getModCount() { - return modCount; - } - } -SequenceI datasetSequence; + SequenceI datasetSequence; private String name; @@ -79,6 +83,9 @@ SequenceI datasetSequence; private String vamsasId; + HiddenMarkovModel hmm; + + boolean isHMMConsensusSequence = false; private DBModList dbrefs; // controlled access /** @@ -89,7 +96,7 @@ SequenceI datasetSequence; private int refModCount = 0; private RNA rna; - + /** * This annotation is displayed below the alignment but the positions are tied * to the residues of this sequence @@ -251,6 +258,21 @@ SequenceI datasetSequence; } /** + * Create a new sequence object from a characters array using default values + * of 1 and -1 for start and end. The array used to create the sequence is + * copied and is not stored internally. + * + * @param name + * sequence name + * @param sequence + * list of residues + */ + public Sequence(String name, char[] sequence) + { + this(name, Arrays.copyOf(sequence, sequence.length), 1, -1); + } + + /** * Creates a new Sequence object with new AlignmentAnnotations but inherits * any existing dataset sequence reference. If non exists, everything is * copied. @@ -301,7 +323,7 @@ SequenceI datasetSequence; { setDatasetSequence(seq.getDatasetSequence()); } - + /* * only copy DBRefs and seqfeatures if we really are a dataset sequence */ @@ -358,6 +380,10 @@ SequenceI datasetSequence; this.addPDBId(new PDBEntry(pdb)); } } + if (seq.getHMM() != null) + { + this.hmm = new HiddenMarkovModel(seq.getHMM(), this); + } } @Override @@ -376,8 +402,8 @@ SequenceI datasetSequence; { if (sf.getType() == null) { - System.err.println("SequenceFeature type may not be null: " - + sf.toString()); + System.err.println( + "SequenceFeature type may not be null: " + sf.toString()); return false; } @@ -469,18 +495,19 @@ SequenceI datasetSequence; } /** - * DOCUMENT ME! + * Answers the sequence name, with '/start-end' appended if jvsuffix is true * - * @return DOCUMENT ME! + * @return */ @Override public String getDisplayId(boolean jvsuffix) { - StringBuffer result = new StringBuffer(name); - if (jvsuffix) + if (!jvsuffix) { - result.append("/" + start + "-" + end); + return name; } + StringBuilder result = new StringBuilder(name); + result.append("/").append(start).append("-").append(end); return result.toString(); } @@ -519,6 +546,7 @@ SequenceI datasetSequence; public void setStart(int start) { this.start = start; + sequenceChanged(); } /** @@ -596,8 +624,8 @@ SequenceI datasetSequence; public char[] getSequence() { // return sequence; - return sequence == null ? null : Arrays.copyOf(sequence, - sequence.length); + return sequence == null ? null + : Arrays.copyOf(sequence, sequence.length); } /* @@ -693,8 +721,8 @@ SequenceI datasetSequence; public void setGeneLoci(String speciesId, String assemblyId, String chromosomeId, MapList map) { - addDBRef(new DBRefEntry(speciesId, assemblyId, DBRefEntry.CHROMOSOME - + ":" + chromosomeId, new Mapping(map))); + addDBRef(new GeneLocus(speciesId, assemblyId, chromosomeId, + new Mapping(map))); } /** @@ -710,39 +738,9 @@ SequenceI datasetSequence; { for (final DBRefEntry ref : refs) { - if (ref.isChromosome()) + if (ref instanceof GeneLociI) { - return new GeneLociI() - { - @Override - public String getSpeciesId() - { - return ref.getSource(); - } - - @Override - public String getAssemblyId() - { - // DEV NOTE: DBRefEntry is reused here to hold chromosomal locus of a gene sequence. - // source=species, version=assemblyId, accession=chromosome, map = positions. - - return ref.getVersion(); - } - - @Override - public String getChromosomeId() - { - // strip off "chromosome:" prefix to chrId - return ref.getAccessionId().substring( - DBRefEntry.CHROMOSOME.length() + 1); - } - - @Override - public MapList getMap() - { - return ref.getMap().getMap(); - } - }; + return (GeneLociI) ref; } } } @@ -905,7 +903,7 @@ SequenceI datasetSequence; { return findPosition(column + 1, cursor); } - + // TODO recode this more naturally i.e. count residues only // as they are found, not 'in anticipation' @@ -1094,7 +1092,8 @@ SequenceI datasetSequence; @Override public ContiguousI findPositions(int fromColumn, int toColumn) { - if (toColumn < fromColumn || fromColumn < 1) + fromColumn = Math.max(fromColumn, 1); + if (toColumn < fromColumn) { return null; } @@ -1210,7 +1209,7 @@ SequenceI datasetSequence; { ArrayList map = new ArrayList<>(); int lastj = -1, j = 0; - int pos = start; + // int pos = start; int seqlen = sequence.length; while ((j < seqlen)) { @@ -1244,7 +1243,7 @@ SequenceI datasetSequence; { BitSet map = new BitSet(); int lastj = -1, j = 0; - int pos = start; + // int pos = start; int seqlen = sequence.length; while ((j < seqlen)) { @@ -1437,10 +1436,10 @@ SequenceI datasetSequence; return dbrefs; } - @Override public void addDBRef(DBRefEntry entry) { + // TODO JAL-3980 maintain as sorted list if (datasetSequence != null) { datasetSequence.addDBRef(entry); @@ -1449,10 +1448,11 @@ SequenceI datasetSequence; if (dbrefs == null) { - dbrefs = new DBModList(); + dbrefs = new DBModList<>(); } + // TODO JAL-3979 LOOK UP RATHER THAN SWEEP FOR EFFICIENCY - for (int ib = 0, nb= dbrefs.size(); ib < nb; ib++) + for (int ib = 0, nb = dbrefs.size(); ib < nb; ib++) { if (dbrefs.get(ib).updateFrom(entry)) { @@ -1464,19 +1464,18 @@ SequenceI datasetSequence; } } + // /// BH OUCH! + // /* + // * extend the array to make room for one more + // */ + // // TODO use an ArrayList instead + // int j = dbrefs.length; + // List temp = new DBRefEntry[j + 1]; + // System.arraycopy(dbrefs, 0, temp, 0, j); + // temp[temp.length - 1] = entry; + // + // dbrefs = temp; -// /// BH OUCH! -// /* -// * extend the array to make room for one more -// */ -// // TODO use an ArrayList instead -// int j = dbrefs.length; -// List temp = new DBRefEntry[j + 1]; -// System.arraycopy(dbrefs, 0, temp, 0, j); -// temp[temp.length - 1] = entry; -// -// dbrefs = temp; - dbrefs.add(entry); } @@ -1590,7 +1589,7 @@ SequenceI datasetSequence; private int _seqhash = 0; -private List primaryRefs; + private List primaryRefs; /** * Answers false if the sequence is more than 85% nucleotide (ACGTU), else @@ -1609,7 +1608,7 @@ private List primaryRefs; _isNa = Comparison.isNucleotide(this); } return !_isNa; - }; + } /* * (non-Javadoc) @@ -1766,8 +1765,9 @@ private List primaryRefs; List sfs = entry.getSequenceFeatures(); for (SequenceFeature feature : sfs) { - SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature) - : new SequenceFeature[] { new SequenceFeature(feature) }; + SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature) + : new SequenceFeature[] + { new SequenceFeature(feature) }; if (sf != null) { for (int sfi = 0; sfi < sf.length; sfi++) @@ -1825,13 +1825,30 @@ private List primaryRefs; public List getAlignmentAnnotations(String calcId, String label) { + return getAlignmentAnnotations(calcId, label, null, true); + } + + @Override + public List getAlignmentAnnotations(String calcId, + String label, String description) + { + return getAlignmentAnnotations(calcId, label, description, false); + } + + private List getAlignmentAnnotations(String calcId, + String label, String description, boolean ignoreDescription) + { List result = new ArrayList<>(); if (this.annotation != null) { for (AlignmentAnnotation ann : annotation) { - if (ann.calcId != null && ann.calcId.equals(calcId) - && ann.label != null && ann.label.equals(label)) + String id = ann.getCalcId(); + if ((id != null && id.equals(calcId)) + && (ann.label != null && ann.label.equals(label)) + && ((ignoreDescription && description == null) + || (ann.description != null + && ann.description.equals(description)))) { result.add(ann); } @@ -1869,7 +1886,7 @@ private List primaryRefs; } private List tmpList; - + @Override public List getPrimaryDBRefs() { @@ -1884,17 +1901,22 @@ private List primaryRefs; synchronized (dbrefs) { if (refModCount == dbrefs.getModCount() && primaryRefs != null) - return primaryRefs; // no changes - refModCount = dbrefs.getModCount(); - List primaries = (primaryRefs == null ? (primaryRefs = new ArrayList<>()) : primaryRefs); + { + return primaryRefs; // no changes + } + refModCount = dbrefs.getModCount(); + List primaries = (primaryRefs == null + ? (primaryRefs = new ArrayList<>()) + : primaryRefs); primaries.clear(); - if (tmpList == null) { - tmpList = new ArrayList<>(); - tmpList.add(null); // for replacement + if (tmpList == null) + { + tmpList = new ArrayList<>(); + tmpList.add(null); // for replacement } for (int i = 0, n = dbrefs.size(); i < n; i++) { - DBRefEntry ref = dbrefs.get(i); + DBRefEntry ref = dbrefs.get(i); if (!ref.isPrimaryCandidate()) { continue; @@ -1909,7 +1931,8 @@ private List primaryRefs; } } // whilst it looks like it is a primary ref, we also sanity check type - if (DBRefSource.PDB_CANONICAL_NAME.equals(ref.getCanonicalSourceName())) + if (DBRefSource.PDB_CANONICAL_NAME + .equals(ref.getCanonicalSourceName())) { // PDB dbrefs imply there should be a PDBEntry associated // TODO: tighten PDB dbrefs @@ -1922,24 +1945,55 @@ private List primaryRefs; { continue; } - } else { - // check standard protein or dna sources - tmpList.set(0, ref); - List res = DBRefUtils.selectDbRefs(!isProtein(), tmpList); - if (res == null || res.get(0) != tmpList.get(0)) - { - continue; - } - } + } + else + { + // check standard protein or dna sources + tmpList.set(0, ref); + List res = DBRefUtils.selectDbRefs(!isProtein(), + tmpList); + if (res == null || res.get(0) != tmpList.get(0)) + { + continue; + } + } primaries.add(ref); } - - // version must be not null, as otherwise it will not be a candidate, above + + // version must be not null, as otherwise it will not be a candidate, + // above DBRefUtils.ensurePrimaries(this, primaries); return primaries; } } + @Override + public HiddenMarkovModel getHMM() + { + return hmm; + } + + @Override + public void setHMM(HiddenMarkovModel hmm) + { + this.hmm = hmm; + } + + @Override + public boolean hasHMMAnnotation() + { + if (this.annotation == null) { + return false; + } + for (AlignmentAnnotation ann : annotation) + { + if (InformationThread.HMM_CALC_ID.equals(ann.getCalcId())) + { + return true; + } + } + return false; + } /** * {@inheritDoc} */ @@ -1953,15 +2007,6 @@ private List primaryRefs; List result = getFeatures().findFeatures(startPos, endPos, types); - if (datasetSequence != null) - { - result = datasetSequence.getFeatures().findFeatures(startPos, endPos, - types); - } - else - { - result = sequenceFeatureStore.findFeatures(startPos, endPos, types); - } /* * if end column is gapped, endPos may be to the right, @@ -2113,4 +2158,10 @@ private List primaryRefs; // otherwise, sequence was completely hidden return 0; } + + @Override + public boolean hasHMMProfile() + { + return hmm != null; + } }