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) ? "" : (String) getSequenceAndDeletions(
117 refseq.getSequenceAsString(start, end), GapChar)[0];
121 * recreates a gapped and edited version of RefSeq or null for an empty cigar
126 public SequenceI getSeq(char GapChar)
129 if (refseq == null || length == 0)
133 Object[] edit_result = getSequenceAndDeletions(
134 refseq.getSequenceAsString(start, end), GapChar);
135 if (edit_result == null)
139 .getString("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()
144 + start + ((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;
189 .getString("error.implementation_error_set_seq_null"));
193 throw new Error(MessageManager.formatMessage(
194 "error.implementation_error_s", new String[] { Integer
195 .valueOf(_s).toString() }));
197 String seq_string = seq.getSequenceAsString();
198 if (_e == 0 || _e < _s || _e > seq_string.length())
200 _e = seq_string.length();
202 // resolve start and end positions relative to ungapped reference sequence
203 start = seq.findPosition(_s) - seq.getStart();
204 end = seq.findPosition(_e) - seq.getStart();
205 int l_ungapped = end - start;
206 // Find correct sequence to reference and correct start and end - if
208 SequenceI ds = seq.getDatasetSequence();
211 // make a new dataset sequence
212 String ungapped = AlignSeq.extractGaps(
213 jalview.util.Comparison.GapChars, new String(seq_string));
214 l_ungapped = ungapped.length();
215 // check that we haven't just duplicated an ungapped sequence.
216 if (l_ungapped == seq.getLength())
222 ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
223 seq.getStart() + ungapped.length() - 1);
224 // JBPNote: this would be consistent but may not be useful
225 // seq.setDatasetSequence(ds);
228 // add in offset between seq and the dataset sequence
229 if (ds.getStart() < seq.getStart())
231 int offset = seq.getStart() - ds.getStart();
234 // absolute cigar string
235 addDeleted(_s + offset);
241 // normal behaviour - just mark start and end subsequence
249 // any gaps to process ?
250 if (l_ungapped != (_e - _s))
256 // copy over local properties for the sequence instance of the refseq
257 seqProps = SeqsetUtils.SeqCharacterHash(seq);
259 if (end > ds.getLength())
263 .getString("error.implementation_error_seqcigar_possible"));
264 // end = ds.getLength();
271 * directly initialise a cigar object with a sequence of range, operation
272 * pairs and a sequence to apply it to. operation and range should be relative
273 * to the seq.getStart()'th residue of the dataset seq resolved from seq.
282 public SeqCigar(SequenceI seq, char operation[], int range[])
288 MessageManager.getString("error.implmentation_bug_seq_null"));
290 if (operation.length != range.length)
294 .getString("error.implementation_bug_cigar_operation_list_range_list"));
297 if (operation != null)
299 this.operation = new char[operation.length + _inc_length];
300 this.range = new int[operation.length + _inc_length];
302 if (_setSeq(seq, false, 0, 0))
306 .getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
308 for (int i = this.length, j = 0; j < operation.length; i++, j++)
310 char op = operation[j];
311 if (op != M && op != I && op != D)
313 throw new Error(MessageManager.formatMessage(
314 "error.implementation_bug_cigar_operation", new String[] {
315 Integer.valueOf(j).toString(),
316 Integer.valueOf(op).toString(),
317 Integer.valueOf(M).toString(),
318 Integer.valueOf(I).toString(),
319 Integer.valueOf(D).toString() }));
321 this.operation[i] = op;
322 this.range[i] = range[j];
324 this.length += operation.length;
328 this.operation = null;
331 if (_setSeq(seq, false, 0, 0))
335 .getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
341 * add range matched residues to cigar string
346 public void addMatch(int range)
348 this.addOperation(M, range);
352 * Adds insertion and match operations based on seq to the cigar up to the
353 * endpos column of seq.
363 * @param initialDeletions
364 * if true then initial deletions will be added from start of seq to
367 protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
368 int startpos, int endpos, boolean initialDeletions)
372 int p = 0, res = seq.getLength();
374 if (!initialDeletions)
381 boolean isGap = (p < res) ? jalview.util.Comparison.isGap(seq
382 .getCharAt(p)) : true;
383 if ((startpos <= p) && (p <= endpos))
387 if (range > 0 && op != I)
389 cigar.addOperation(op, range);
397 if (range > 0 && op != M)
399 cigar.addOperation(op, range);
410 if (range > 0 && op != D)
412 cigar.addOperation(op, range);
420 // do nothing - insertions are not made in flanking regions
427 cigar.addOperation(op, range);
432 * create a cigar string for given sequence
437 public SeqCigar(SequenceI seq)
444 .getString("error.implementation_error_for_new_cigar"));
446 _setSeq(seq, false, 0, 0);
447 // there is still work to do
448 addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
452 * Create Cigar from a range of gaps and residues on a sequence object
457 * int - first column in range
459 * int - last column in range
461 public SeqCigar(SequenceI seq, int start, int end)
468 .getString("error.implementation_error_for_new_cigar"));
470 _setSeq(seq, false, start, end + 1);
471 // there is still work to do
472 addSequenceOps(this, seq, start, end, false);
476 * Create a cigar object from a cigar string like '[<I|D|M><range>]+' Will
477 * fail if the given seq already contains gaps (JBPNote: future implementation
481 * SequenceI object resolvable to a dataset sequence
486 public static SeqCigar parseCigar(SequenceI seq, String cigarString)
489 Object[] opsandrange = parseCigarString(cigarString);
490 return new SeqCigar(seq, (char[]) opsandrange[0],
491 (int[]) opsandrange[1]);
495 * create an alignment from the given array of cigar sequences and gap
496 * character, and marking the given segments as visible in the given
500 * @param gapCharacter
502 * - columnSelection where hidden regions are marked
504 * - visible regions of alignment
505 * @return SequenceI[]
507 public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
508 char gapCharacter, ColumnSelection colsel, int[] segments)
510 SequenceI[] seqs = new SequenceI[alseqs.length];
511 StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
512 String[] alseqs_string = new String[alseqs.length];
513 Object[] gs_regions = new Object[alseqs.length];
514 for (int i = 0; i < alseqs.length; i++)
516 alseqs_string[i] = alseqs[i].getRefSeq().getSequenceAsString(
517 alseqs[i].start, alseqs[i].end);
518 gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
519 gapCharacter); // gapped sequence, {start, start col, end.
520 // endcol}, hidden regions {{start, end, col}})
521 if (gs_regions[i] == null)
523 throw new Error(MessageManager.formatMessage(
524 "error.implementation_error_cigar_seq_no_operations",
525 new String[] { Integer.valueOf(i).toString() }));
527 g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
532 // Now account for insertions. (well - deletions)
533 // this is complicated because we must keep track of shifted positions in
535 ShiftList shifts = new ShiftList();
536 for (int i = 0; i < alseqs.length; i++)
538 Object[] gs_region = ((Object[]) ((Object[]) gs_regions[i])[2]);
539 if (gs_region != null)
542 for (int hr = 0; hr < gs_region.length; hr++)
544 int[] region = (int[]) gs_region[hr];
545 char[] insert = new char[region[1] - region[0] + 1];
546 for (int s = 0; s < insert.length; s++)
548 insert[s] = gapCharacter;
550 int inspos = shifts.shift(region[2]); // resolve insertion position in
551 // current alignment frame of
553 for (int s = 0; s < alseqs.length; s++)
557 if (g_seqs[s].length() <= inspos)
559 // prefix insertion with more gaps.
560 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
562 g_seqs[s].append(gapCharacter); // to debug - use a diffferent
563 // gap character here
566 g_seqs[s].insert(inspos, insert);
570 g_seqs[s].insert(inspos,
571 alseqs_string[i].substring(region[0], region[1] + 1));
574 shifts.addShift(region[2], insert.length); // update shift in
575 // alignment frame of
577 if (segments == null)
579 // add a hidden column for this deletion
580 colsel.hideColumns(inspos, inspos + insert.length - 1);
585 for (int i = 0; i < alseqs.length; i++)
587 int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
588 SequenceI ref = alseqs[i].getRefSeq();
589 seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
590 ref.getStart() + alseqs[i].start + bounds[0], ref.getStart()
591 + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
592 seqs[i].setDatasetSequence(ref);
593 seqs[i].setDescription(ref.getDescription());
595 if (segments != null)
597 for (int i = 0; i < segments.length; i += 3)
599 // int start=shifts.shift(segments[i]-1)+1;
600 // int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
601 colsel.hideColumns(segments[i + 1], segments[i + 1]
602 + segments[i + 2] - 1);
609 * references to entities that this sequence cigar is associated with.
611 private Hashtable selGroups = null;
613 public void setGroupMembership(Object group)
615 if (selGroups == null)
617 selGroups = new Hashtable();
619 selGroups.put(group, new int[0]);
623 * Test for and if present remove association to group.
626 * @return true if group was associated and it was removed
628 public boolean removeGroupMembership(Object group)
630 if (selGroups != null && selGroups.containsKey(group))
632 selGroups.remove(group);
639 * forget all associations for this sequence.
641 public void clearMemberships()
643 if (selGroups != null)
652 * @return null or array of all associated entities
654 public Object[] getAllMemberships()
656 if (selGroups == null)
660 Object[] mmbs = new Object[selGroups.size()];
661 Enumeration en = selGroups.keys();
662 for (int i = 0; en.hasMoreElements(); i++)
664 mmbs[i] = en.nextElement();
670 * Test for group membership
673 * - a selection group or some other object that may be associated
675 * @return true if sgr is associated with this seqCigar
677 public boolean isMemberOf(Object sgr)
679 return (selGroups != null) && selGroups.get(sgr) != null;