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<String> description = Optional.empty();
+ private Optional<List<SequenceFeature>> features = Optional.empty();
+ private Optional<List<PDBEntry>> pdbId = Optional.empty();
+ private Optional<SequenceI> dataset = Optional.empty();
+ private Optional<HiddenMarkovModel> hmm = Optional.empty();
+ private Optional<AlignmentAnnotation[]> 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
* 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<SequenceFeature> sfeat = new Vector<>();
- List<SequenceFeature> 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<PDBEntry>());
- }
- 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<SequenceFeature> 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;
}
* 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<SequenceFeature> sfeatures = (Vector<SequenceFeature>) sqinfo
- .get("SeqFeatures");
- Vector<PDBEntry> pdbid = (Vector<PDBEntry>) 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;
}
/**
*/
public static String unique_name(int i)
{
- return new String("Sequence" + i);
+ return String.format("Sequence%d", i);
}
/**
* @see deuniquify to recover original names (and properties) for renamed
* sequences
*/
- public static Hashtable uniquify(SequenceI[] sequences,
+ public static Map<String, SequenceInfo> 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<String, SequenceInfo> map = new HashMap<>();
// String[] un_names = new String[sequences.length];
for (int i = 0; i < sequences.length; i++)
* SequenceI[]
* @return boolean
*/
- public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
+ public static boolean deuniquify(Map<String, SequenceInfo> map,
+ SequenceI[] sequences)
{
return deuniquify(map, sequences, true);
}
* map.
* @return boolean
*/
- public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
- boolean quiet)
+ public static boolean deuniquify(Map<String, SequenceInfo> map,
+ SequenceI[] sequences, boolean quiet)
{
jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
sequences);
SequenceI msq = null;
- Enumeration keys = map.keys();
- Vector unmatched = new Vector();
+ Iterator<String> keys = map.keySet().iterator();
+ Vector<SequenceI> 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);
}
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<SequenceI> i = unmatched.elements();
+ i.hasMoreElements();)
{
- ;
+ System.out.println(((SequenceI) i.nextElement()).getName());
}
return false;
}