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.datamodel;
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.util.MessageManager;
26 import jalview.util.ShiftList;
28 import java.util.Enumeration;
29 import java.util.Hashtable;
31 public class SeqCigar extends CigarSimple
34 * start(inclusive) and end(exclusive) of subsequence on refseq
36 private int start, end;
38 private SequenceI refseq = null;
40 private Hashtable seqProps;
43 * Reference dataset sequence for the cigar string
47 public SequenceI getRefSeq()
54 * @return int start index of cigar ops on refSeq
63 * @return int end index (exclusive) of cigar ops on refSeq
72 * Returns sequence as a string with cigar operations applied to it
77 public String getSequenceString(char GapChar)
79 return (length == 0) ? "" : (String) getSequenceAndDeletions(
80 refseq.getSequenceAsString(start, end), GapChar)[0];
84 * recreates a gapped and edited version of RefSeq or null for an empty cigar
89 public SequenceI getSeq(char GapChar)
92 if (refseq == null || length == 0)
96 Object[] edit_result = getSequenceAndDeletions(
97 refseq.getSequenceAsString(start, end), GapChar);
98 if (edit_result == null)
102 .getString("error.implementation_error_unexpected_null_from_get_sequence_and_deletions"));
104 int bounds[] = (int[]) edit_result[1];
105 seq = new Sequence(refseq.getName(), (String) edit_result[0],
106 refseq.getStart() + start + bounds[0], refseq.getStart()
107 + start + ((bounds[2] == 0) ? -1 : bounds[2]));
108 seq.setDescription(refseq.getDescription());
109 int sstart = seq.getStart(), send = seq.getEnd();
110 // seq.checkValidRange(); probably not needed
111 // recover local properties if present
112 if (seqProps != null)
114 // this recovers dataset sequence reference as well as local features,
115 // names, start/end settings.
116 SeqsetUtils.SeqCharacterUnhash(seq, seqProps);
118 // ensure dataset sequence is up to date from local reference
119 seq.setDatasetSequence(refseq);
120 seq.setStart(sstart);
126 * We don't allow this - refseq is given at construction time only public void
127 * setSeq(SequenceI seq) { this.seq = seq; }
130 * internal constructor - sets seq to a gapless sequence derived from seq and
131 * prepends any 'D' operations needed to get to the first residue of seq.
135 * @param initialDeletion
136 * true to mark initial dataset sequence residues as deleted in
139 * index of first position in seq
141 * index after last position in (possibly gapped) seq
142 * @return true if gaps are present in seq
144 private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
147 boolean hasgaps = false;
152 .getString("error.implementation_error_set_seq_null"));
156 throw new Error(MessageManager.formatMessage(
157 "error.implementation_error_s", new String[] { Integer
158 .valueOf(_s).toString() }));
160 String seq_string = seq.getSequenceAsString();
161 if (_e == 0 || _e < _s || _e > seq_string.length())
163 _e = seq_string.length();
165 // resolve start and end positions relative to ungapped reference sequence
166 start = seq.findPosition(_s) - seq.getStart();
167 end = seq.findPosition(_e) - seq.getStart();
168 int l_ungapped = end - start;
169 // Find correct sequence to reference and correct start and end - if
171 SequenceI ds = seq.getDatasetSequence();
174 // make a new dataset sequence
175 String ungapped = AlignSeq.extractGaps(
176 jalview.util.Comparison.GapChars, new String(seq_string));
177 l_ungapped = ungapped.length();
178 // check that we haven't just duplicated an ungapped sequence.
179 if (l_ungapped == seq.getLength())
185 ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
186 seq.getStart() + ungapped.length() - 1);
187 // JBPNote: this would be consistent but may not be useful
188 // seq.setDatasetSequence(ds);
191 // add in offset between seq and the dataset sequence
192 if (ds.getStart() < seq.getStart())
194 int offset = seq.getStart() - ds.getStart();
197 // absolute cigar string
198 addDeleted(_s + offset);
204 // normal behaviour - just mark start and end subsequence
212 // any gaps to process ?
213 if (l_ungapped != (_e - _s))
219 // copy over local properties for the sequence instance of the refseq
220 seqProps = SeqsetUtils.SeqCharacterHash(seq);
222 if (end > ds.getLength())
226 .getString("error.implementation_error_seqcigar_possible"));
227 // end = ds.getLength();
234 * directly initialise a cigar object with a sequence of range, operation
235 * pairs and a sequence to apply it to. operation and range should be relative
236 * to the seq.getStart()'th residue of the dataset seq resolved from seq.
245 public SeqCigar(SequenceI seq, char operation[], int range[])
251 MessageManager.getString("error.implmentation_bug_seq_null"));
253 if (operation.length != range.length)
257 .getString("error.implementation_bug_cigar_operation_list_range_list"));
260 if (operation != null)
262 this.operation = new char[operation.length + _inc_length];
263 this.range = new int[operation.length + _inc_length];
265 if (_setSeq(seq, false, 0, 0))
269 .getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
271 for (int i = this.length, j = 0; j < operation.length; i++, j++)
273 char op = operation[j];
274 if (op != M && op != I && op != D)
276 throw new Error(MessageManager.formatMessage(
277 "error.implementation_bug_cigar_operation", new String[] {
278 Integer.valueOf(j).toString(),
279 Integer.valueOf(op).toString(),
280 Integer.valueOf(M).toString(),
281 Integer.valueOf(I).toString(),
282 Integer.valueOf(D).toString() }));
284 this.operation[i] = op;
285 this.range[i] = range[j];
287 this.length += operation.length;
291 this.operation = null;
294 if (_setSeq(seq, false, 0, 0))
298 .getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
304 * add range matched residues to cigar string
309 public void addMatch(int range)
311 this.addOperation(M, range);
315 * Adds insertion and match operations based on seq to the cigar up to the
316 * endpos column of seq.
326 * @param initialDeletions
327 * if true then initial deletions will be added from start of seq to
330 protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
331 int startpos, int endpos, boolean initialDeletions)
335 int p = 0, res = seq.getLength();
337 if (!initialDeletions)
344 boolean isGap = (p < res) ? jalview.util.Comparison.isGap(seq
345 .getCharAt(p)) : true;
346 if ((startpos <= p) && (p <= endpos))
350 if (range > 0 && op != I)
352 cigar.addOperation(op, range);
360 if (range > 0 && op != M)
362 cigar.addOperation(op, range);
373 if (range > 0 && op != D)
375 cigar.addOperation(op, range);
383 // do nothing - insertions are not made in flanking regions
390 cigar.addOperation(op, range);
395 * create a cigar string for given sequence
400 public SeqCigar(SequenceI seq)
407 .getString("error.implementation_error_for_new_cigar"));
409 _setSeq(seq, false, 0, 0);
410 // there is still work to do
411 addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
415 * Create Cigar from a range of gaps and residues on a sequence object
420 * int - first column in range
422 * int - last column in range
424 public SeqCigar(SequenceI seq, int start, int end)
431 .getString("error.implementation_error_for_new_cigar"));
433 _setSeq(seq, false, start, end + 1);
434 // there is still work to do
435 addSequenceOps(this, seq, start, end, false);
439 * Create a cigar object from a cigar string like '[<I|D|M><range>]+' Will
440 * fail if the given seq already contains gaps (JBPNote: future implementation
444 * SequenceI object resolvable to a dataset sequence
449 public static SeqCigar parseCigar(SequenceI seq, String cigarString)
452 Object[] opsandrange = parseCigarString(cigarString);
453 return new SeqCigar(seq, (char[]) opsandrange[0],
454 (int[]) opsandrange[1]);
458 * create an alignment from the given array of cigar sequences and gap
459 * character, and marking the given segments as visible in the given
463 * @param gapCharacter
465 * - columnSelection where hidden regions are marked
467 * - visible regions of alignment
468 * @return SequenceI[]
470 public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
471 char gapCharacter, ColumnSelection colsel, int[] segments)
473 SequenceI[] seqs = new SequenceI[alseqs.length];
474 StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
475 String[] alseqs_string = new String[alseqs.length];
476 Object[] gs_regions = new Object[alseqs.length];
477 for (int i = 0; i < alseqs.length; i++)
479 alseqs_string[i] = alseqs[i].getRefSeq().getSequenceAsString(
480 alseqs[i].start, alseqs[i].end);
481 gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
482 gapCharacter); // gapped sequence, {start, start col, end.
483 // endcol}, hidden regions {{start, end, col}})
484 if (gs_regions[i] == null)
486 throw new Error(MessageManager.formatMessage(
487 "error.implementation_error_cigar_seq_no_operations",
488 new String[] { Integer.valueOf(i).toString() }));
490 g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
495 // Now account for insertions. (well - deletions)
496 // this is complicated because we must keep track of shifted positions in
498 ShiftList shifts = new ShiftList();
499 for (int i = 0; i < alseqs.length; i++)
501 Object[] gs_region = ((Object[]) ((Object[]) gs_regions[i])[2]);
502 if (gs_region != null)
505 for (int hr = 0; hr < gs_region.length; hr++)
507 int[] region = (int[]) gs_region[hr];
508 char[] insert = new char[region[1] - region[0] + 1];
509 for (int s = 0; s < insert.length; s++)
511 insert[s] = gapCharacter;
513 int inspos = shifts.shift(region[2]); // resolve insertion position in
514 // current alignment frame of
516 for (int s = 0; s < alseqs.length; s++)
520 if (g_seqs[s].length() <= inspos)
522 // prefix insertion with more gaps.
523 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
525 g_seqs[s].append(gapCharacter); // to debug - use a diffferent
526 // gap character here
529 g_seqs[s].insert(inspos, insert);
533 g_seqs[s].insert(inspos,
534 alseqs_string[i].substring(region[0], region[1] + 1));
537 shifts.addShift(region[2], insert.length); // update shift in
538 // alignment frame of
540 if (segments == null)
542 // add a hidden column for this deletion
543 colsel.hideColumns(inspos, inspos + insert.length - 1);
548 for (int i = 0; i < alseqs.length; i++)
550 int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
551 SequenceI ref = alseqs[i].getRefSeq();
552 seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
553 ref.getStart() + alseqs[i].start + bounds[0], ref.getStart()
554 + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
555 seqs[i].setDatasetSequence(ref);
556 seqs[i].setDescription(ref.getDescription());
558 if (segments != null)
560 for (int i = 0; i < segments.length; i += 3)
562 // int start=shifts.shift(segments[i]-1)+1;
563 // int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
564 colsel.hideColumns(segments[i + 1], segments[i + 1]
565 + segments[i + 2] - 1);
572 * references to entities that this sequence cigar is associated with.
574 private Hashtable selGroups = null;
576 public void setGroupMembership(Object group)
578 if (selGroups == null)
580 selGroups = new Hashtable();
582 selGroups.put(group, new int[0]);
586 * Test for and if present remove association to group.
589 * @return true if group was associated and it was removed
591 public boolean removeGroupMembership(Object group)
593 if (selGroups != null && selGroups.containsKey(group))
595 selGroups.remove(group);
602 * forget all associations for this sequence.
604 public void clearMemberships()
606 if (selGroups != null)
615 * @return null or array of all associated entities
617 public Object[] getAllMemberships()
619 if (selGroups == null)
623 Object[] mmbs = new Object[selGroups.size()];
624 Enumeration en = selGroups.keys();
625 for (int i = 0; en.hasMoreElements(); i++)
627 mmbs[i] = en.nextElement();
633 * Test for group membership
636 * - a selection group or some other object that may be associated
638 * @return true if sgr is associated with this seqCigar
640 public boolean isMemberOf(Object sgr)
642 return (selGroups != null) && selGroups.get(sgr) != null;