From: Mateusz Warowny Date: Wed, 6 Oct 2021 14:23:13 +0000 (+0200) Subject: JAL-3899 Refactor sequence de/uniquification. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=5d28c907a598716ca90eaa8a625420e2c0c694f0;p=jalview.git JAL-3899 Refactor sequence de/uniquification. --- diff --git a/src/jalview/analysis/SeqsetUtils.java b/src/jalview/analysis/SeqsetUtils.java index 4118124..6683e3b 100755 --- a/src/jalview/analysis/SeqsetUtils.java +++ b/src/jalview/analysis/SeqsetUtils.java @@ -27,13 +27,36 @@ 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; 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 @@ -43,44 +66,24 @@ public class SeqsetUtils * 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) - { - sqinfo.put("SeqFeatures", sfeat); - sqinfo.put("PdbId", - (seq.getAllPDBEntries() != null) ? seq.getAllPDBEntries() - : new Vector()); - } - else + 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("datasetSequence", - (seq.getDatasetSequence() != null) ? seq.getDatasetSequence() - : new Sequence("THISISAPLACEHOLDER", "")); + ArrayList feats = new ArrayList<>( + seq.getFeatures().getAllFeatures()); + sqinfo.features = Optional.of(feats); + sqinfo.pdbId = Optional.of(Objects.requireNonNullElse( + seq.getAllPDBEntries(), new ArrayList<>())); } if (seq.hasHMMProfile()) { - sqinfo.put("HMM", seq.getHMM()); - } - - if (seq.getAnnotation("Search Scores") != null) - { - sqinfo.put("Score", seq.getAnnotation("Search Scores")); + sqinfo.hmm = Optional.of(seq.getHMM()); } - + sqinfo.searchScores = Optional.ofNullable(seq.getAnnotation("Search Scores")); return sqinfo; } @@ -94,76 +97,41 @@ 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"); - HiddenMarkovModel hmm = (HiddenMarkovModel) sqinfo.get("HMM"); - AlignmentAnnotation[] scores = (AlignmentAnnotation[]) sqinfo - .get("Score"); - - if (oldname == null) - { - namePresent = false; - } - else + if (sqinfo.name != null) { - sq.setName(oldname); + sq.setName(sqinfo.name); } - if (pdbid != null && pdbid.size() > 0) + 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.setPDBId(pdbid); + assert sqinfo.features.isEmpty() : + "Setting dataset sequence for a sequence which has sequence features. " + + "Dataset sequence features will not be visible."; + sq.setDatasetSequence(sqinfo.dataset.get()); } - - if ((start != null) && (end != null)) - { - sq.setStart(start.intValue()); - sq.setEnd(end.intValue()); - } - - if (sfeatures != null && !sfeatures.isEmpty()) - { - sq.setSequenceFeatures(sfeatures); - } - if (description != null) - { - sq.setDescription(description); - } - if ((seqds != null) && !(seqds.getName().equals("THISISAPLACEHOLDER") - && seqds.getLength() == 0)) - { - if (sfeatures != null) - { - System.err.println( - "Implementation error: setting dataset sequence for a sequence which has sequence features.\n\tDataset sequence features will not be visible."); - } - sq.setDatasetSequence(seqds); - } - - if (hmm != null) - { - sq.setHMM(new HiddenMarkovModel(hmm, sq)); - } - - if (scores != null) + if (sqinfo.hmm.isPresent()) + sq.setHMM(new HiddenMarkovModel(sqinfo.hmm.get(), sq)); + if (sqinfo.searchScores.isPresent()) { - for (AlignmentAnnotation score : scores) + for (AlignmentAnnotation score : sqinfo.searchScores.get()) { sq.addAlignmentAnnotation(score); } } - return namePresent; + return sqinfo.name != null; } /** @@ -176,7 +144,7 @@ public class SeqsetUtils */ public static String unique_name(int i) { - return new String("Sequence" + i); + return String.format("Sequence%d", i); } /** @@ -193,12 +161,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++) @@ -226,7 +194,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); } @@ -245,25 +214,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(); + 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); } @@ -285,11 +254,10 @@ public class SeqsetUtils 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())) + for (Enumeration i = unmatched.elements(); + i.hasMoreElements();) { - ; + System.out.println(((SequenceI) i.nextElement()).getName()); } return false; } diff --git a/test/jalview/analysis/SeqsetUtilsTest.java b/test/jalview/analysis/SeqsetUtilsTest.java index 9839ba0..ddb3d2a 100644 --- a/test/jalview/analysis/SeqsetUtilsTest.java +++ b/test/jalview/analysis/SeqsetUtilsTest.java @@ -28,6 +28,7 @@ import jalview.datamodel.SequenceI; import jalview.gui.JvOptionPane; import java.util.Hashtable; +import java.util.Map; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -65,7 +66,7 @@ public class SeqsetUtilsTest SequenceFeature sf1 = new SequenceFeature("f1", "foo", 2, 3, "far"); SequenceFeature sf2 = new SequenceFeature("f2", "foo", 2, 3, "far"); ds.getSequenceAt(0).addSequenceFeature(sf1); - Hashtable unq = SeqsetUtils.uniquify(sqset, true); + Map unq = SeqsetUtils.uniquify(sqset, true); SequenceI[] sqset2 = new SequenceI[] { new Sequence(sqset[0].getName(), sqset[0].getSequenceAsString()), new Sequence(sqset[1].getName(), sqset[1].getSequenceAsString()) };