2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.bin.Cache;
24 import jalview.bin.Console;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.HiddenMarkovModel;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
32 import java.util.ArrayList;
33 import java.util.BitSet;
34 import java.util.Enumeration;
35 import java.util.HashMap;
36 import java.util.Hashtable;
37 import java.util.Iterator;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.Vector;
42 import static java.lang.String.format;
44 import java.nio.CharBuffer;
46 public class SeqsetUtils
48 public static class SequenceInfo {
52 private Optional<String> description = Optional.empty();
53 private Optional<List<SequenceFeature>> features = Optional.empty();
54 private Optional<List<PDBEntry>> pdbId = Optional.empty();
55 private Optional<SequenceI> dataset = Optional.empty();
56 private Optional<HiddenMarkovModel> hmm = Optional.empty();
57 private Optional<AlignmentAnnotation[]> searchScores = Optional.empty();
59 private SequenceInfo(String name, int start, int end) {
67 * Store essential properties of a sequence in a hashtable for later recovery
68 * Keys are Name, Start, End, SeqFeatures, PdbId, HMM
74 public static SequenceInfo SeqCharacterHash(SequenceI seq)
76 SequenceInfo sqinfo = new SequenceInfo(seq.getName(), seq.getStart(), seq.getEnd());
77 sqinfo.description = Optional.ofNullable(seq.getDescription());
78 sqinfo.dataset = Optional.ofNullable(seq.getDatasetSequence());
79 if (!sqinfo.dataset.isPresent())
81 ArrayList<SequenceFeature> feats = new ArrayList<>(
82 seq.getFeatures().getAllFeatures());
83 sqinfo.features = Optional.of(feats);
84 var pdbEntries = seq.getAllPDBEntries();
85 sqinfo.pdbId = Optional.of(pdbEntries != null ? pdbEntries : new ArrayList<>());
87 if (seq.hasHMMProfile())
89 sqinfo.hmm = Optional.of(seq.getHMM());
91 sqinfo.searchScores = Optional.ofNullable(seq.getAnnotation("Search Scores"));
96 * Filter the sequence through the mask leaving only characters at positions
97 * where the mask value was true. The length of the resulting array is
98 * the cardinality of the mask from 0 to sequence length.
103 * mask used to filter the sequence characters
104 * @return input array filtered through the mask
106 public static char[] filterSequence(char[] sequence, BitSet mask)
108 mask = mask.get(0, sequence.length);
109 char[] result = new char[mask.cardinality()];
110 for (int i = mask.nextSetBit(0), j = 0; i >= 0;)
112 result[j++] = sequence[i];
113 if (i == Integer.MAX_VALUE)
114 // prevents integer overflow of (i + 1)
116 i = mask.nextSetBit(i + 1);
122 * Recover essential properties of a sequence from a hashtable TODO: replace
123 * these methods with something more elegant.
129 * @return boolean true if name was not updated from sqinfo Name entry
131 public static boolean SeqCharacterUnhash(SequenceI sq, SequenceInfo sqinfo)
137 if (sqinfo.name != null)
139 sq.setName(sqinfo.name);
141 sq.setStart(sqinfo.start);
142 sq.setEnd(sqinfo.end);
143 if (sqinfo.pdbId.isPresent() && !sqinfo.pdbId.get().isEmpty())
144 sq.setPDBId(new Vector<>(sqinfo.pdbId.get()));
145 if (sqinfo.features.isPresent() && !sqinfo.features.get().isEmpty())
146 sq.setSequenceFeatures(sqinfo.features.get());
147 if (sqinfo.description.isPresent())
148 sq.setDescription(sqinfo.description.get());
149 if (sqinfo.dataset.isPresent())
151 if (sqinfo.features.isPresent())
153 Console.warn("Setting dataset sequence for a sequence which has " +
154 "sequence features. Dataset sequence features will not be visible.");
157 sq.setDatasetSequence(sqinfo.dataset.get());
159 if (sqinfo.hmm.isPresent())
160 sq.setHMM(new HiddenMarkovModel(sqinfo.hmm.get(), sq));
161 if (sqinfo.searchScores.isPresent())
163 for (AlignmentAnnotation score : sqinfo.searchScores.get())
165 sq.addAlignmentAnnotation(score);
168 return sqinfo.name != null;
172 * Form of the unique name used in uniquify for the i'th sequence in an
173 * ordered vector of sequences.
179 public static String unique_name(int i)
181 return String.format("Sequence%d", i);
185 * Generates a hash of SeqCharacterHash properties for each sequence in a
186 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
192 * boolean set this to rename each of the sequences to its
193 * unique_name(index) name
194 * @return Hashtable to be passed to
195 * @see deuniquify to recover original names (and properties) for renamed
198 public static Map<String, SequenceInfo> uniquify(SequenceI[] sequences,
201 // Generate a safely named sequence set and a hash to recover the sequence
203 HashMap<String, SequenceInfo> map = new HashMap<>();
204 // String[] un_names = new String[sequences.length];
206 for (int i = 0; i < sequences.length; i++)
208 String safename = unique_name(i);
209 map.put(safename, SeqCharacterHash(sequences[i]));
213 sequences[i].setName(safename);
221 * recover unsafe sequence names and original properties for a sequence set
222 * using a map generated by
224 * @see uniquify(sequences,true)
231 public static boolean deuniquify(Map<String, SequenceInfo> map,
232 SequenceI[] sequences)
234 return deuniquify(map, sequences, true);
238 * recover unsafe sequence names and original properties for a sequence set
239 * using a map generated by
241 * @see uniquify(sequences,true)
247 * when false, don't complain about sequences without any data in the
251 public static boolean deuniquify(Map<String, SequenceInfo> map,
252 SequenceI[] sequences, boolean quiet)
254 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
256 SequenceI msq = null;
257 Iterator<String> keys = map.keySet().iterator();
258 Vector<SequenceI> unmatched = new Vector<>();
259 for (int i = 0, j = sequences.length; i < j; i++)
261 unmatched.addElement(sequences[i]);
263 while (keys.hasNext())
265 String key = keys.next();
267 if ((msq = matcher.findIdMatch((String) key)) != null)
269 SequenceInfo sqinfo = map.get(key);
270 unmatched.removeElement(msq);
271 SeqCharacterUnhash(msq, sqinfo);
277 Console.warn(format("Can't find '%s' in uniquified alignment",
281 } catch (ClassCastException ccastex) {
284 Console.error("Unexpected object in SeqSet map : "+ key.getClass());
288 if (unmatched.size() > 0 && !quiet)
290 StringBuilder sb = new StringBuilder("Did not find match for sequences: ");
291 Enumeration<SequenceI> i = unmatched.elements();
292 sb.append(i.nextElement().getName());
293 for (; i.hasMoreElements();)
295 sb.append(", " + i.nextElement().getName());
297 Console.warn(sb.toString());
305 * returns a subset of the sequenceI seuqences, including only those that
306 * contain at least one residue.
310 * @return SequenceI[]
312 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
314 // Identify first row of alignment with residues for prediction
315 boolean ungapped[] = new boolean[sequences.length];
317 for (int i = 0, j = sequences.length; i < j; i++)
319 String tempseq = jalview.analysis.AlignSeq.extractGaps(
320 jalview.util.Comparison.GapChars,
321 sequences[i].getSequenceAsString());
323 if (tempseq.length() == 0)
335 return null; // no minimal set
337 // compose minimal set
338 SequenceI[] mset = new SequenceI[msflen];
339 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
343 mset[k++] = sequences[i];