X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequence.java;h=0da30bad43490e56f666f697365b792de4096f7b;hb=46406ae21c62b964714ce136c9d692985c17211d;hp=88f430803a14178d7bec70baedef7b9647d8e813;hpb=134291d04042b3d7252598a9f73753f40da08a18;p=jalview.git diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 88f4308..0da30ba 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -22,10 +22,14 @@ package jalview.datamodel; import jalview.analysis.AlignSeq; import jalview.api.DBRefEntryI; +import jalview.util.Comparison; +import jalview.util.DBRefUtils; +import jalview.util.MapList; import jalview.util.StringUtils; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.List; import java.util.Vector; @@ -47,18 +51,24 @@ public class Sequence extends ASequence implements SequenceI private char[] sequence; + int previousPosition; + String description; int start; int end; + boolean hasInfo; + + HiddenMarkovModel hmm; + + boolean isHMMConsensusSequence = false; + Vector pdbIds; String vamsasId; - DBRefEntryI sourceDBRef; - DBRefEntry[] dbrefs; RNA rna; @@ -231,12 +241,9 @@ public class Sequence extends ASequence implements SequenceI { char[] oseq = seq.getSequence(); initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length), - seq.getStart(), - seq.getEnd()); + seq.getStart(), seq.getEnd()); } description = seq.getDescription(); - sourceDBRef = seq.getSourceDBRef() == null ? null : new DBRefEntry( - seq.getSourceDBRef()); if (seq != datasetSequence) { setDatasetSequence(seq.getDatasetSequence()); @@ -291,9 +298,12 @@ public class Sequence extends ASequence implements SequenceI this.addPDBId(new PDBEntry(pdb)); } } + if (seq.getHMM() != null) + { + this.hmm = new HiddenMarkovModel(seq.getHMM()); + } } - @Override public void setSequenceFeatures(SequenceFeature[] features) { @@ -316,12 +326,11 @@ public class Sequence extends ASequence implements SequenceI } @Override - public synchronized void addSequenceFeature(SequenceFeature sf) + public synchronized boolean addSequenceFeature(SequenceFeature sf) { - if (sequenceFeatures==null && datasetSequence != null) + if (sequenceFeatures == null && datasetSequence != null) { - datasetSequence.addSequenceFeature(sf); - return; + return datasetSequence.addSequenceFeature(sf); } if (sequenceFeatures == null) { @@ -332,7 +341,7 @@ public class Sequence extends ASequence implements SequenceI { if (sequenceFeatures[i].equals(sf)) { - return; + return false; } } @@ -341,6 +350,7 @@ public class Sequence extends ASequence implements SequenceI temp[sequenceFeatures.length] = sf; sequenceFeatures = temp; + return true; } @Override @@ -348,8 +358,9 @@ public class Sequence extends ASequence implements SequenceI { if (sequenceFeatures == null) { - if (datasetSequence!=null) { - datasetSequence.deleteFeature(sf); + if (datasetSequence != null) + { + datasetSequence.deleteFeature(sf); } return; } @@ -413,28 +424,24 @@ public class Sequence extends ASequence implements SequenceI } @Override - public void addPDBId(PDBEntry entry) + public boolean addPDBId(PDBEntry entry) { if (pdbIds == null) { - pdbIds = new Vector(); - } - if (pdbIds.contains(entry)) - { - updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry); - } - else - { - pdbIds.addElement(entry); + pdbIds = new Vector<>(); + pdbIds.add(entry); + return true; } - } - private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry) - { - if (newEntry.getFile() != null) + for (PDBEntry pdbe : pdbIds) { - oldEntry.setFile(newEntry.getFile()); + if (pdbe.updateFrom(entry)) + { + return false; + } } + pdbIds.addElement(entry); + return true; } /** @@ -792,7 +799,7 @@ public class Sequence extends ASequence implements SequenceI @Override public List getInsertions() { - ArrayList map = new ArrayList(); + ArrayList map = new ArrayList<>(); int lastj = -1, j = 0; int pos = start; int seqlen = sequence.length; @@ -949,7 +956,17 @@ public class Sequence extends ASequence implements SequenceI @Override public void setDBRefs(DBRefEntry[] dbref) { + if (dbrefs == null && datasetSequence != null + && this != datasetSequence) + { + datasetSequence.setDBRefs(dbref); + return; + } dbrefs = dbref; + if (dbrefs != null) + { + DBRefUtils.ensurePrimaries(this); + } } @Override @@ -966,7 +983,12 @@ public class Sequence extends ASequence implements SequenceI @Override public void addDBRef(DBRefEntry entry) { - // TODO add to dataset sequence instead if there is one? + if (datasetSequence != null) + { + datasetSequence.addDBRef(entry); + return; + } + if (dbrefs == null) { dbrefs = new DBRefEntry[0]; @@ -994,12 +1016,23 @@ public class Sequence extends ASequence implements SequenceI temp[temp.length - 1] = entry; dbrefs = temp; + + DBRefUtils.ensurePrimaries(this); } @Override public void setDatasetSequence(SequenceI seq) { - // TODO check for circular reference before setting? + if (seq == this) + { + throw new IllegalArgumentException( + "Implementation Error: self reference passed to SequenceI.setDatasetSequence"); + } + if (seq != null && seq.getDatasetSequence() != null) + { + throw new IllegalArgumentException( + "Implementation error: cascading dataset sequences are not allowed."); + } datasetSequence = seq; } @@ -1027,7 +1060,7 @@ public class Sequence extends ASequence implements SequenceI { if (this.annotation == null) { - this.annotation = new Vector(); + this.annotation = new Vector<>(); } if (!this.annotation.contains(annotation)) { @@ -1072,7 +1105,7 @@ public class Sequence extends ASequence implements SequenceI @Override public SequenceI deriveSequence() { - Sequence seq=null; + Sequence seq = null; if (datasetSequence == null) { if (isValidDatasetSequence()) @@ -1086,7 +1119,7 @@ public class Sequence extends ASequence implements SequenceI else { // Create a new, valid dataset sequence - createDatasetSequence(); + createDatasetSequence(); } } return new Sequence(this); @@ -1096,6 +1129,10 @@ public class Sequence extends ASequence implements SequenceI private long _seqhash = 0; + /** + * Answers false if the sequence is more than 85% nucleotide (ACGTU), else + * true + */ @Override public boolean isProtein() { @@ -1106,7 +1143,7 @@ public class Sequence extends ASequence implements SequenceI if (_seqhash != sequence.hashCode()) { _seqhash = sequence.hashCode(); - _isNa=jalview.util.Comparison.isNucleotide(new SequenceI[] { this }); + _isNa = Comparison.isNucleotide(this); } return !_isNa; }; @@ -1130,9 +1167,9 @@ public class Sequence extends ASequence implements SequenceI dsseq.setDescription(description); // move features and database references onto dataset sequence dsseq.sequenceFeatures = sequenceFeatures; - sequenceFeatures=null; + sequenceFeatures = null; dsseq.dbrefs = dbrefs; - dbrefs=null; + dbrefs = null; // TODO: search and replace any references to this sequence with // references to the dataset sequence in Mappings on dbref dsseq.pdbIds = pdbIds; @@ -1226,46 +1263,22 @@ public class Sequence extends ASequence implements SequenceI { return false; } - Vector newpdb = new Vector(); - for (int i = 0; i < dbrefs.length; i++) + boolean added = false; + for (DBRefEntry dbr : dbrefs) { - if (DBRefSource.PDB.equals(dbrefs[i].getSource())) + if (DBRefSource.PDB.equals(dbr.getSource())) { - PDBEntry pdbe = new PDBEntry(); - pdbe.setId(dbrefs[i].getAccessionId()); - if (pdbIds == null || pdbIds.size() == 0) - { - newpdb.addElement(pdbe); - } - else - { - Enumeration en = pdbIds.elements(); - boolean matched = false; - while (!matched && en.hasMoreElements()) - { - PDBEntry anentry = (PDBEntry) en.nextElement(); - if (anentry.getId().equals(pdbe.getId())) - { - matched = true; - } - } - if (!matched) - { - newpdb.addElement(pdbe); - } - } - } - } - if (newpdb.size() > 0) - { - Enumeration en = newpdb.elements(); - while (en.hasMoreElements()) - { - addPDBId((PDBEntry) en.nextElement()); + /* + * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the + * PDB id is not already present in a 'matching' PDBEntry + * Constructor parses out a chain code if appended to the accession id + * (a fudge used to 'store' the chain code in the DBRef) + */ + PDBEntry pdbe = new PDBEntry(dbr); + added |= addPDBId(pdbe); } - return true; } - return false; + return added; } @Override @@ -1371,7 +1384,7 @@ public class Sequence extends ASequence implements SequenceI public List getAlignmentAnnotations(String calcId, String label) { - List result = new ArrayList(); + List result = new ArrayList<>(); if (this.annotation != null) { for (AlignmentAnnotation ann : annotation) @@ -1395,12 +1408,15 @@ public class Sequence extends ASequence implements SequenceI @Override public PDBEntry getPDBEntry(String pdbIdStr) { - if (getDatasetSequence() == null - || getDatasetSequence().getAllPDBEntries() == null) + if (getDatasetSequence() != null) + { + return getDatasetSequence().getPDBEntry(pdbIdStr); + } + if (pdbIds == null) { return null; } - List entries = getDatasetSequence().getAllPDBEntries(); + List entries = getAllPDBEntries(); for (PDBEntry entry : entries) { if (entry.getId().equalsIgnoreCase(pdbIdStr)) @@ -1412,15 +1428,148 @@ public class Sequence extends ASequence implements SequenceI } @Override - public void setSourceDBRef(DBRefEntryI dbRef) + public List getPrimaryDBRefs() + { + if (datasetSequence != null) + { + return datasetSequence.getPrimaryDBRefs(); + } + if (dbrefs == null || dbrefs.length == 0) + { + return Collections.emptyList(); + } + synchronized (dbrefs) + { + List primaries = new ArrayList<>(); + DBRefEntry[] tmp = new DBRefEntry[1]; + for (DBRefEntry ref : dbrefs) + { + if (!ref.isPrimaryCandidate()) + { + continue; + } + if (ref.hasMap()) + { + MapList mp = ref.getMap().getMap(); + if (mp.getFromLowest() > start || mp.getFromHighest() < end) + { + // map only involves a subsequence, so cannot be primary + continue; + } + } + // whilst it looks like it is a primary ref, we also sanity check type + if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals( + DBRefUtils.getCanonicalName(ref.getSource()))) + { + // PDB dbrefs imply there should be a PDBEntry associated + // TODO: tighten PDB dbrefs + // formally imply Jalview has actually downloaded and + // parsed the pdb file. That means there should be a cached file + // handle on the PDBEntry, and a real mapping between sequence and + // extracted sequence from PDB file + PDBEntry pdbentry = getPDBEntry(ref.getAccessionId()); + if (pdbentry != null && pdbentry.getFile() != null) + { + primaries.add(ref); + } + continue; + } + // check standard protein or dna sources + tmp[0] = ref; + DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp); + if (res != null && res[0] == tmp[0]) + { + primaries.add(ref); + continue; + } + } + return primaries; + } + } + + @Override + public HiddenMarkovModel getHMM() + { + return hmm; + } + + @Override + public void setHMM(HiddenMarkovModel hmm) + { + this.hmm = hmm; + } + + @Override + public void updateHMMMapping() + { + int node = 1; + int column = 0; + for (char residue : sequence) + { + if (!Comparison.isGap(residue)) + { + hmm.setAlignmentColumn(node, column); + hmm.getNodeLookup().put(column, node); + node++; + } + else + { + hmm.getNodeLookup().remove(column); + } + column++; + } + + } + + @Override + public boolean isHMMConsensusSequence() + { + return isHMMConsensusSequence; + } + + @Override + public void setIsHMMConsensusSequence(boolean isHMMConsensusSequence) + { + this.isHMMConsensusSequence = isHMMConsensusSequence; + } + + @Override + public boolean hasHMMAnnotation() + { + return hasInfo; + /* + if (annotation == null) + { + return false; + } + + for (AlignmentAnnotation annot : annotation) + { + if (annot.label.contains("_HMM")) + { + return true; + } + } + return false; + */ + } + + @Override + public void setHasInfo(boolean status) + { + hasInfo = true; + } + + @Override + public int getPreviousPosition() { - this.sourceDBRef = dbRef; + return previousPosition; } @Override - public DBRefEntryI getSourceDBRef() + public void setPreviousPosition(int previousPosition) { - return this.sourceDBRef; + this.previousPosition = previousPosition; } }