X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FSeqsetUtils.java;h=5a246f8d84f83fd644cc6a03ebd1d8da250e8f01;hb=d043ce47fc710d3eb2629ba926a8a7417bd67d8c;hp=fdca89dd5cc5acf2fd08a7a075fafa0244058045;hpb=04c8f7bff663aa469127e9eed4164e02933782f1;p=jalview.git diff --git a/src/jalview/analysis/SeqsetUtils.java b/src/jalview/analysis/SeqsetUtils.java index fdca89d..5a246f8 100755 --- a/src/jalview/analysis/SeqsetUtils.java +++ b/src/jalview/analysis/SeqsetUtils.java @@ -20,55 +20,73 @@ */ package jalview.analysis; +import jalview.bin.Cache; +import jalview.bin.Console; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.HiddenMarkovModel; import jalview.datamodel.PDBEntry; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; +import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.Vector; +import static java.lang.String.format; public class SeqsetUtils { + public static class SequenceInfo { + private String name; + private int start; + private int end; + private Optional description = Optional.empty(); + private Optional> features = Optional.empty(); + private Optional> pdbId = Optional.empty(); + private Optional dataset = Optional.empty(); + private Optional hmm = Optional.empty(); + private Optional searchScores = Optional.empty(); + + private SequenceInfo(String name, int start, int end) { + this.name = name; + this.start = start; + this.end = end; + } + } /** * Store essential properties of a sequence in a hashtable for later recovery - * Keys are Name, Start, End, SeqFeatures, PdbId + * Keys are Name, Start, End, SeqFeatures, PdbId, HMM * * @param seq * SequenceI * @return Hashtable */ - public static Hashtable SeqCharacterHash(SequenceI seq) + public static SequenceInfo SeqCharacterHash(SequenceI seq) { - Hashtable sqinfo = new Hashtable(); - sqinfo.put("Name", seq.getName()); - sqinfo.put("Start", Integer.valueOf(seq.getStart())); - sqinfo.put("End", Integer.valueOf(seq.getEnd())); - if (seq.getDescription() != null) - { - sqinfo.put("Description", seq.getDescription()); - } - - Vector sfeat = new Vector(); - List sfs = seq.getFeatures().getAllFeatures(); - sfeat.addAll(sfs); - - if (seq.getDatasetSequence() == null) + SequenceInfo sqinfo = new SequenceInfo(seq.getName(), seq.getStart(), seq.getEnd()); + sqinfo.description = Optional.ofNullable(seq.getDescription()); + sqinfo.dataset = Optional.ofNullable(seq.getDatasetSequence()); + if (!sqinfo.dataset.isPresent()) { - sqinfo.put("SeqFeatures", sfeat); - sqinfo.put("PdbId", - (seq.getAllPDBEntries() != null) ? seq.getAllPDBEntries() - : new Vector()); + ArrayList feats = new ArrayList<>( + seq.getFeatures().getAllFeatures()); + sqinfo.features = Optional.of(feats); + sqinfo.pdbId = Optional.of(Objects.requireNonNullElse( + seq.getAllPDBEntries(), new ArrayList<>())); } - else + if (seq.hasHMMProfile()) { - sqinfo.put("datasetSequence", - (seq.getDatasetSequence() != null) ? seq.getDatasetSequence() - : new Sequence("THISISAPLACEHOLDER", "")); + sqinfo.hmm = Optional.of(seq.getHMM()); } + sqinfo.searchScores = Optional.ofNullable(seq.getAnnotation("Search Scores")); return sqinfo; } @@ -82,60 +100,44 @@ public class SeqsetUtils * Hashtable * @return boolean true if name was not updated from sqinfo Name entry */ - public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo) + public static boolean SeqCharacterUnhash(SequenceI sq, SequenceInfo sqinfo) { - boolean namePresent = true; if (sqinfo == null) { return false; } - String oldname = (String) sqinfo.get("Name"); - Integer start = (Integer) sqinfo.get("Start"); - Integer end = (Integer) sqinfo.get("End"); - Vector sfeatures = (Vector) sqinfo - .get("SeqFeatures"); - Vector pdbid = (Vector) sqinfo.get("PdbId"); - String description = (String) sqinfo.get("Description"); - Sequence seqds = (Sequence) sqinfo.get("datasetSequence"); - if (oldname == null) - { - namePresent = false; - } - else + if (sqinfo.name != null) { - sq.setName(oldname); + sq.setName(sqinfo.name); } - if (pdbid != null && pdbid.size() > 0) - { - sq.setPDBId(pdbid); - } - - if ((start != null) && (end != null)) + sq.setStart(sqinfo.start); + sq.setEnd(sqinfo.end); + if (sqinfo.pdbId.isPresent() && !sqinfo.pdbId.get().isEmpty()) + sq.setPDBId(new Vector<>(sqinfo.pdbId.get())); + if (sqinfo.features.isPresent() && !sqinfo.features.get().isEmpty()) + sq.setSequenceFeatures(sqinfo.features.get()); + if (sqinfo.description.isPresent()) + sq.setDescription(sqinfo.description.get()); + if (sqinfo.dataset.isPresent()) { - sq.setStart(start.intValue()); - sq.setEnd(end.intValue()); - } - - if (sfeatures != null && !sfeatures.isEmpty()) - { - sq.setSequenceFeatures(sfeatures); - } - if (description != null) - { - sq.setDescription(description); + if (sqinfo.features.isPresent()) + { + Console.warn("Setting dataset sequence for a sequence which has " + + "sequence features. Dataset sequence features will not be visible."); + assert false; + } + sq.setDatasetSequence(sqinfo.dataset.get()); } - if ((seqds != null) && !(seqds.getName().equals("THISISAPLACEHOLDER") - && seqds.getLength() == 0)) + if (sqinfo.hmm.isPresent()) + sq.setHMM(new HiddenMarkovModel(sqinfo.hmm.get(), sq)); + if (sqinfo.searchScores.isPresent()) { - if (sfeatures != null) + for (AlignmentAnnotation score : sqinfo.searchScores.get()) { - System.err.println( - "Implementation error: setting dataset sequence for a sequence which has sequence features.\n\tDataset sequence features will not be visible."); + sq.addAlignmentAnnotation(score); } - sq.setDatasetSequence(seqds); } - - return namePresent; + return sqinfo.name != null; } /** @@ -148,7 +150,7 @@ public class SeqsetUtils */ public static String unique_name(int i) { - return new String("Sequence" + i); + return String.format("Sequence%d", i); } /** @@ -165,12 +167,12 @@ public class SeqsetUtils * @see deuniquify to recover original names (and properties) for renamed * sequences */ - public static Hashtable uniquify(SequenceI[] sequences, + public static Map uniquify(SequenceI[] sequences, boolean write_names) { // Generate a safely named sequence set and a hash to recover the sequence // names - Hashtable map = new Hashtable(); + HashMap map = new HashMap<>(); // String[] un_names = new String[sequences.length]; for (int i = 0; i < sequences.length; i++) @@ -198,7 +200,8 @@ public class SeqsetUtils * SequenceI[] * @return boolean */ - public static boolean deuniquify(Hashtable map, SequenceI[] sequences) + public static boolean deuniquify(Map map, + SequenceI[] sequences) { return deuniquify(map, sequences, true); } @@ -217,26 +220,25 @@ public class SeqsetUtils * map. * @return boolean */ - public static boolean deuniquify(Hashtable map, SequenceI[] sequences, - boolean quiet) + public static boolean deuniquify(Map map, + SequenceI[] sequences, boolean quiet) { jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher( sequences); SequenceI msq = null; - Enumeration keys = map.keys(); - Vector unmatched = new Vector(); + Iterator keys = map.keySet().iterator(); + Vector unmatched = new Vector<>(); for (int i = 0, j = sequences.length; i < j; i++) { unmatched.addElement(sequences[i]); } - while (keys.hasMoreElements()) + while (keys.hasNext()) { - Object key = keys.nextElement(); - if (key instanceof String) - { + String key = keys.next(); + try { if ((msq = matcher.findIdMatch((String) key)) != null) { - Hashtable sqinfo = (Hashtable) map.get(key); + SequenceInfo sqinfo = map.get(key); unmatched.removeElement(msq); SeqCharacterUnhash(msq, sqinfo); } @@ -244,21 +246,27 @@ public class SeqsetUtils { if (!quiet) { - System.err.println("Can't find '" + ((String) key) - + "' in uniquified alignment"); + Console.warn(format("Can't find '%s' in uniquified alignment", + key)); } } + } catch (ClassCastException ccastex) { + if (!quiet) + { + Console.error("Unexpected object in SeqSet map : "+ key.getClass()); + } } } if (unmatched.size() > 0 && !quiet) { - System.err.println("Did not find matches for :"); - for (Enumeration i = unmatched.elements(); i - .hasMoreElements(); System.out - .println(((SequenceI) i.nextElement()).getName())) + StringBuilder sb = new StringBuilder("Did not find match for sequences: "); + Enumeration i = unmatched.elements(); + sb.append(i.nextElement().getName()); + for (; i.hasMoreElements();) { - ; + sb.append(", " + i.nextElement().getName()); } + Console.warn(sb.toString()); return false; }