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
73 * @return position in sequence for column (or -1 if no match state exists)
75 public int findPosition(int column)
77 int w = 0, ew, p = refseq.findPosition(start);
84 for (int i = 0; i < length; i++)
86 if (operation[i] == M || operation[i] == D)
90 if (operation[i] == M || operation[i] == I)
95 if (operation[i] == I)
99 return p - (ew - column);
109 * Returns sequence as a string with cigar operations applied to it
114 public String getSequenceString(char GapChar)
116 return (length == 0) ? ""
117 : (String) getSequenceAndDeletions(
118 refseq.getSequenceAsString(start, end), GapChar)[0];
122 * recreates a gapped and edited version of RefSeq or null for an empty cigar
127 public SequenceI getSeq(char GapChar)
130 if (refseq == null || length == 0)
134 Object[] edit_result = getSequenceAndDeletions(
135 refseq.getSequenceAsString(start, end), GapChar);
136 if (edit_result == null)
138 throw new Error(MessageManager.getString(
139 "error.implementation_error_unexpected_null_from_get_sequence_and_deletions"));
141 int bounds[] = (int[]) edit_result[1];
142 seq = new Sequence(refseq.getName(), (String) edit_result[0],
143 refseq.getStart() + start + bounds[0], refseq.getStart() + start
144 + ((bounds[2] == 0) ? -1 : bounds[2]));
145 seq.setDescription(refseq.getDescription());
146 int sstart = seq.getStart(), send = seq.getEnd();
147 // seq.checkValidRange(); probably not needed
148 // recover local properties if present
149 if (seqProps != null)
151 // this recovers dataset sequence reference as well as local features,
152 // names, start/end settings.
153 SeqsetUtils.SeqCharacterUnhash(seq, seqProps);
155 // ensure dataset sequence is up to date from local reference
156 seq.setDatasetSequence(refseq);
157 seq.setStart(sstart);
163 * We don't allow this - refseq is given at construction time only public void
164 * setSeq(SequenceI seq) { this.seq = seq; }
167 * internal constructor - sets seq to a gapless sequence derived from seq and
168 * prepends any 'D' operations needed to get to the first residue of seq.
172 * @param initialDeletion
173 * true to mark initial dataset sequence residues as deleted in
176 * index of first position in seq
178 * index after last position in (possibly gapped) seq
179 * @return true if gaps are present in seq
181 private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
184 boolean hasgaps = false;
187 throw new Error(MessageManager
188 .getString("error.implementation_error_set_seq_null"));
192 throw new Error(MessageManager
193 .formatMessage("error.implementation_error_s", new String[]
194 { Integer.valueOf(_s).toString() }));
196 String seq_string = seq.getSequenceAsString();
197 if (_e == 0 || _e < _s || _e > seq_string.length())
199 _e = seq_string.length();
201 // resolve start and end positions relative to ungapped reference sequence
202 start = seq.findPosition(_s) - seq.getStart();
203 end = seq.findPosition(_e) - seq.getStart();
204 int l_ungapped = end - start;
205 // Find correct sequence to reference and correct start and end - if
207 SequenceI ds = seq.getDatasetSequence();
210 // make a new dataset sequence
211 String ungapped = AlignSeq.extractGaps(
212 jalview.util.Comparison.GapChars, new String(seq_string));
213 l_ungapped = ungapped.length();
214 // check that we haven't just duplicated an ungapped sequence.
215 if (l_ungapped == seq.getLength())
221 ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
222 seq.getStart() + ungapped.length() - 1);
223 // JBPNote: this would be consistent but may not be useful
224 // seq.setDatasetSequence(ds);
227 // add in offset between seq and the dataset sequence
228 if (ds.getStart() < seq.getStart())
230 int offset = seq.getStart() - ds.getStart();
233 // absolute cigar string
234 addDeleted(_s + offset);
240 // normal behaviour - just mark start and end subsequence
248 // any gaps to process ?
249 if (l_ungapped != (_e - _s))
255 // copy over local properties for the sequence instance of the refseq
256 seqProps = SeqsetUtils.SeqCharacterHash(seq);
258 if (end > ds.getLength())
260 throw new Error(MessageManager
261 .getString("error.implementation_error_seqcigar_possible"));
262 // end = ds.getLength();
269 * directly initialise a cigar object with a sequence of range, operation
270 * pairs and a sequence to apply it to. operation and range should be relative
271 * to the seq.getStart()'th residue of the dataset seq resolved from seq.
280 public SeqCigar(SequenceI seq, char operation[], int range[])
286 MessageManager.getString("error.implmentation_bug_seq_null"));
288 if (operation.length != range.length)
290 throw new Error(MessageManager.getString(
291 "error.implementation_bug_cigar_operation_list_range_list"));
294 if (operation != null)
296 this.operation = new char[operation.length + _inc_length];
297 this.range = new int[operation.length + _inc_length];
299 if (_setSeq(seq, false, 0, 0))
301 throw new Error(MessageManager.getString(
302 "error.not_yet_implemented_cigar_object_from_cigar_string"));
304 for (int i = this.length, j = 0; j < operation.length; i++, j++)
306 char op = operation[j];
307 if (op != M && op != I && op != D)
309 throw new Error(MessageManager.formatMessage(
310 "error.implementation_bug_cigar_operation", new String[]
311 { Integer.valueOf(j).toString(),
312 Integer.valueOf(op).toString(),
313 Integer.valueOf(M).toString(),
314 Integer.valueOf(I).toString(),
315 Integer.valueOf(D).toString() }));
317 this.operation[i] = op;
318 this.range[i] = range[j];
320 this.length += operation.length;
324 this.operation = null;
327 if (_setSeq(seq, false, 0, 0))
329 throw new Error(MessageManager.getString(
330 "error.not_yet_implemented_cigar_object_from_cigar_string"));
336 * add range matched residues to cigar string
341 public void addMatch(int range)
343 this.addOperation(M, range);
347 * Adds insertion and match operations based on seq to the cigar up to the
348 * endpos column of seq.
358 * @param initialDeletions
359 * if true then initial deletions will be added from start of seq to
362 protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
363 int startpos, int endpos, boolean initialDeletions)
367 int p = 0, res = seq.getLength();
369 if (!initialDeletions)
376 boolean isGap = (p < res)
377 ? jalview.util.Comparison.isGap(seq.getCharAt(p))
379 if ((startpos <= p) && (p <= endpos))
383 if (range > 0 && op != I)
385 cigar.addOperation(op, range);
393 if (range > 0 && op != M)
395 cigar.addOperation(op, range);
406 if (range > 0 && op != D)
408 cigar.addOperation(op, range);
416 // do nothing - insertions are not made in flanking regions
423 cigar.addOperation(op, range);
428 * create a cigar string for given sequence
433 public SeqCigar(SequenceI seq)
438 throw new Error(MessageManager
439 .getString("error.implementation_error_for_new_cigar"));
441 _setSeq(seq, false, 0, 0);
442 // there is still work to do
443 addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
447 * Create Cigar from a range of gaps and residues on a sequence object
452 * int - first column in range
454 * int - last column in range
456 public SeqCigar(SequenceI seq, int start, int end)
461 throw new Error(MessageManager
462 .getString("error.implementation_error_for_new_cigar"));
464 _setSeq(seq, false, start, end + 1);
465 // there is still work to do
466 addSequenceOps(this, seq, start, end, false);
470 * Create a cigar object from a cigar string like '[<I|D|M><range>]+' Will
471 * fail if the given seq already contains gaps (JBPNote: future implementation
475 * SequenceI object resolvable to a dataset sequence
480 public static SeqCigar parseCigar(SequenceI seq, String cigarString)
483 Object[] opsandrange = parseCigarString(cigarString);
484 return new SeqCigar(seq, (char[]) opsandrange[0],
485 (int[]) opsandrange[1]);
489 * create an alignment from the given array of cigar sequences and gap
490 * character, and marking the given segments as visible in the given
494 * @param gapCharacter
496 * - hiddenColumns where hidden regions are marked
498 * - visible regions of alignment
499 * @return SequenceI[]
501 public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
502 char gapCharacter, HiddenColumns hidden, int[] segments)
504 SequenceI[] seqs = new SequenceI[alseqs.length];
505 StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
506 String[] alseqs_string = new String[alseqs.length];
507 Object[] gs_regions = new Object[alseqs.length];
508 for (int i = 0; i < alseqs.length; i++)
510 alseqs_string[i] = alseqs[i].getRefSeq()
511 .getSequenceAsString(alseqs[i].start, alseqs[i].end);
512 gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
513 gapCharacter); // gapped sequence, {start, start col, end.
514 // endcol}, hidden regions {{start, end, col}})
515 if (gs_regions[i] == null)
517 throw new Error(MessageManager.formatMessage(
518 "error.implementation_error_cigar_seq_no_operations",
520 { Integer.valueOf(i).toString() }));
522 g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
527 // Now account for insertions. (well - deletions)
528 // this is complicated because we must keep track of shifted positions in
530 ShiftList shifts = new ShiftList();
531 for (int i = 0; i < alseqs.length; i++)
533 Object[] gs_region = ((Object[]) ((Object[]) gs_regions[i])[2]);
534 if (gs_region != null)
537 for (int hr = 0; hr < gs_region.length; hr++)
539 int[] region = (int[]) gs_region[hr];
540 char[] insert = new char[region[1] - region[0] + 1];
541 for (int s = 0; s < insert.length; s++)
543 insert[s] = gapCharacter;
545 int inspos = shifts.shift(region[2]); // resolve insertion position in
546 // current alignment frame of
548 for (int s = 0; s < alseqs.length; s++)
552 if (g_seqs[s].length() <= inspos)
554 // prefix insertion with more gaps.
555 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
557 g_seqs[s].append(gapCharacter); // to debug - use a diffferent
558 // gap character here
561 g_seqs[s].insert(inspos, insert);
565 g_seqs[s].insert(inspos,
566 alseqs_string[i].substring(region[0], region[1] + 1));
569 shifts.addShift(region[2], insert.length); // update shift in
570 // alignment frame of
572 if (segments == null)
574 // add a hidden column for this deletion
575 hidden.hideColumns(inspos, inspos + insert.length - 1);
580 for (int i = 0; i < alseqs.length; i++)
582 int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
583 SequenceI ref = alseqs[i].getRefSeq();
584 seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
585 ref.getStart() + alseqs[i].start + bounds[0],
586 ref.getStart() + alseqs[i].start
587 + (bounds[2] == 0 ? -1 : bounds[2]));
588 seqs[i].setDatasetSequence(ref);
589 seqs[i].setDescription(ref.getDescription());
591 if (segments != null)
593 for (int i = 0; i < segments.length; i += 3)
595 // int start=shifts.shift(segments[i]-1)+1;
596 // int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
597 hidden.hideColumns(segments[i + 1],
598 segments[i + 1] + segments[i + 2] - 1);
605 * references to entities that this sequence cigar is associated with.
607 private Hashtable selGroups = null;
609 public void setGroupMembership(Object group)
611 if (selGroups == null)
613 selGroups = new Hashtable();
615 selGroups.put(group, new int[0]);
619 * Test for and if present remove association to group.
622 * @return true if group was associated and it was removed
624 public boolean removeGroupMembership(Object group)
626 if (selGroups != null && selGroups.containsKey(group))
628 selGroups.remove(group);
635 * forget all associations for this sequence.
637 public void clearMemberships()
639 if (selGroups != null)
648 * @return null or array of all associated entities
650 public Object[] getAllMemberships()
652 if (selGroups == null)
656 Object[] mmbs = new Object[selGroups.size()];
657 Enumeration en = selGroups.keys();
658 for (int i = 0; en.hasMoreElements(); i++)
660 mmbs[i] = en.nextElement();
666 * Test for group membership
669 * - a selection group or some other object that may be associated
671 * @return true if sgr is associated with this seqCigar
673 public boolean isMemberOf(Object sgr)
675 return (selGroups != null) && selGroups.get(sgr) != null;