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.Enumeration;
34 import java.util.HashMap;
35 import java.util.Hashtable;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
41 import java.util.Vector;
42 import static java.lang.String.format;
44 public class SeqsetUtils
46 public static class SequenceInfo {
50 private Optional<String> description = Optional.empty();
51 private Optional<List<SequenceFeature>> features = Optional.empty();
52 private Optional<List<PDBEntry>> pdbId = Optional.empty();
53 private Optional<SequenceI> dataset = Optional.empty();
54 private Optional<HiddenMarkovModel> hmm = Optional.empty();
55 private Optional<AlignmentAnnotation[]> searchScores = Optional.empty();
57 private SequenceInfo(String name, int start, int end) {
65 * Store essential properties of a sequence in a hashtable for later recovery
66 * Keys are Name, Start, End, SeqFeatures, PdbId, HMM
72 public static SequenceInfo SeqCharacterHash(SequenceI seq)
74 SequenceInfo sqinfo = new SequenceInfo(seq.getName(), seq.getStart(), seq.getEnd());
75 sqinfo.description = Optional.ofNullable(seq.getDescription());
76 sqinfo.dataset = Optional.ofNullable(seq.getDatasetSequence());
77 if (!sqinfo.dataset.isPresent())
79 ArrayList<SequenceFeature> feats = new ArrayList<>(
80 seq.getFeatures().getAllFeatures());
81 sqinfo.features = Optional.of(feats);
82 sqinfo.pdbId = Optional.of(Objects.requireNonNullElse(
83 seq.getAllPDBEntries(), new ArrayList<>()));
85 if (seq.hasHMMProfile())
87 sqinfo.hmm = Optional.of(seq.getHMM());
89 sqinfo.searchScores = Optional.ofNullable(seq.getAnnotation("Search Scores"));
94 * Recover essential properties of a sequence from a hashtable TODO: replace
95 * these methods with something more elegant.
101 * @return boolean true if name was not updated from sqinfo Name entry
103 public static boolean SeqCharacterUnhash(SequenceI sq, SequenceInfo sqinfo)
109 if (sqinfo.name != null)
111 sq.setName(sqinfo.name);
113 sq.setStart(sqinfo.start);
114 sq.setEnd(sqinfo.end);
115 if (sqinfo.pdbId.isPresent() && !sqinfo.pdbId.get().isEmpty())
116 sq.setPDBId(new Vector<>(sqinfo.pdbId.get()));
117 if (sqinfo.features.isPresent() && !sqinfo.features.get().isEmpty())
118 sq.setSequenceFeatures(sqinfo.features.get());
119 if (sqinfo.description.isPresent())
120 sq.setDescription(sqinfo.description.get());
121 if (sqinfo.dataset.isPresent())
123 if (sqinfo.features.isPresent())
125 Console.warn("Setting dataset sequence for a sequence which has " +
126 "sequence features. Dataset sequence features will not be visible.");
129 sq.setDatasetSequence(sqinfo.dataset.get());
131 if (sqinfo.hmm.isPresent())
132 sq.setHMM(new HiddenMarkovModel(sqinfo.hmm.get(), sq));
133 if (sqinfo.searchScores.isPresent())
135 for (AlignmentAnnotation score : sqinfo.searchScores.get())
137 sq.addAlignmentAnnotation(score);
140 return sqinfo.name != null;
144 * Form of the unique name used in uniquify for the i'th sequence in an
145 * ordered vector of sequences.
151 public static String unique_name(int i)
153 return String.format("Sequence%d", i);
157 * Generates a hash of SeqCharacterHash properties for each sequence in a
158 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
164 * boolean set this to rename each of the sequences to its
165 * unique_name(index) name
166 * @return Hashtable to be passed to
167 * @see deuniquify to recover original names (and properties) for renamed
170 public static Map<String, SequenceInfo> uniquify(SequenceI[] sequences,
173 // Generate a safely named sequence set and a hash to recover the sequence
175 HashMap<String, SequenceInfo> map = new HashMap<>();
176 // String[] un_names = new String[sequences.length];
178 for (int i = 0; i < sequences.length; i++)
180 String safename = unique_name(i);
181 map.put(safename, SeqCharacterHash(sequences[i]));
185 sequences[i].setName(safename);
193 * recover unsafe sequence names and original properties for a sequence set
194 * using a map generated by
196 * @see uniquify(sequences,true)
203 public static boolean deuniquify(Map<String, SequenceInfo> map,
204 SequenceI[] sequences)
206 return deuniquify(map, sequences, true);
210 * recover unsafe sequence names and original properties for a sequence set
211 * using a map generated by
213 * @see uniquify(sequences,true)
219 * when false, don't complain about sequences without any data in the
223 public static boolean deuniquify(Map<String, SequenceInfo> map,
224 SequenceI[] sequences, boolean quiet)
226 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
228 SequenceI msq = null;
229 Iterator<String> keys = map.keySet().iterator();
230 Vector<SequenceI> unmatched = new Vector<>();
231 for (int i = 0, j = sequences.length; i < j; i++)
233 unmatched.addElement(sequences[i]);
235 while (keys.hasNext())
237 String key = keys.next();
239 if ((msq = matcher.findIdMatch((String) key)) != null)
241 SequenceInfo sqinfo = map.get(key);
242 unmatched.removeElement(msq);
243 SeqCharacterUnhash(msq, sqinfo);
249 Console.warn(format("Can't find '%s' in uniquified alignment",
253 } catch (ClassCastException ccastex) {
256 Console.error("Unexpected object in SeqSet map : "+ key.getClass());
260 if (unmatched.size() > 0 && !quiet)
262 StringBuilder sb = new StringBuilder("Did not find match for sequences: ");
263 Enumeration<SequenceI> i = unmatched.elements();
264 sb.append(i.nextElement().getName());
265 for (; i.hasMoreElements();)
267 sb.append(", " + i.nextElement().getName());
269 Console.warn(sb.toString());
277 * returns a subset of the sequenceI seuqences, including only those that
278 * contain at least one residue.
282 * @return SequenceI[]
284 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
286 // Identify first row of alignment with residues for prediction
287 boolean ungapped[] = new boolean[sequences.length];
289 for (int i = 0, j = sequences.length; i < j; i++)
291 String tempseq = jalview.analysis.AlignSeq.extractGaps(
292 jalview.util.Comparison.GapChars,
293 sequences[i].getSequenceAsString());
295 if (tempseq.length() == 0)
307 return null; // no minimal set
309 // compose minimal set
310 SequenceI[] mset = new SequenceI[msflen];
311 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
315 mset[k++] = sequences[i];