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 java.util.Enumeration;
24 import java.util.Hashtable;
26 import jalview.analysis.AlignSeq;
27 import jalview.analysis.SeqsetUtils;
28 import jalview.util.MessageManager;
29 import jalview.util.ShiftList;
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
71 * Returns sequence as a string with cigar operations applied to it
75 public String getSequenceString(char GapChar)
77 return (length == 0) ? "" : (String) getSequenceAndDeletions(
78 refseq.getSequenceAsString(start, end), GapChar)[0];
82 * recreates a gapped and edited version of RefSeq or null for an empty cigar
87 public SequenceI getSeq(char GapChar)
90 if (refseq == null || length == 0)
94 Object[] edit_result = getSequenceAndDeletions(
95 refseq.getSequenceAsString(start, end), GapChar);
96 if (edit_result == null)
98 throw new Error(MessageManager.getString("error.implementation_error_unexpected_null_from_get_sequence_and_deletions"));
100 int bounds[] = (int[]) edit_result[1];
101 seq = new Sequence(refseq.getName(), (String) edit_result[0],
102 refseq.getStart() + start + bounds[0], refseq.getStart()
103 + start + ((bounds[2] == 0) ? -1 : bounds[2]));
104 seq.setDescription(refseq.getDescription());
105 int sstart = seq.getStart(), send = seq.getEnd();
106 // seq.checkValidRange(); probably not needed
107 // recover local properties if present
108 if (seqProps != null)
110 // this recovers dataset sequence reference as well as local features,
111 // names, start/end settings.
112 SeqsetUtils.SeqCharacterUnhash(seq, seqProps);
114 // ensure dataset sequence is up to date from local reference
115 seq.setDatasetSequence(refseq);
116 seq.setStart(sstart);
122 * We don't allow this - refseq is given at construction time only public void
123 * setSeq(SequenceI seq) { this.seq = seq; }
126 * internal constructor - sets seq to a gapless sequence derived from seq and
127 * prepends any 'D' operations needed to get to the first residue of seq.
131 * @param initialDeletion
132 * true to mark initial dataset sequence residues as deleted in
135 * index of first position in seq
137 * index after last position in (possibly gapped) seq
138 * @return true if gaps are present in seq
140 private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
143 boolean hasgaps = false;
146 throw new Error(MessageManager.getString("error.implementation_error_set_seq_null"));
150 throw new Error(MessageManager.formatMessage("error.implementation_error_s", new String[]{Integer.valueOf(_s).toString()}));
152 String seq_string = seq.getSequenceAsString();
153 if (_e == 0 || _e < _s || _e > seq_string.length())
155 _e = seq_string.length();
157 // resolve start and end positions relative to ungapped reference sequence
158 start = seq.findPosition(_s) - seq.getStart();
159 end = seq.findPosition(_e) - seq.getStart();
160 int l_ungapped = end - start;
161 // Find correct sequence to reference and correct start and end - if
163 SequenceI ds = seq.getDatasetSequence();
166 // make a new dataset sequence
167 String ungapped = AlignSeq.extractGaps(
168 jalview.util.Comparison.GapChars, new String(seq_string));
169 l_ungapped = ungapped.length();
170 // check that we haven't just duplicated an ungapped sequence.
171 if (l_ungapped == seq.getLength())
177 ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
178 seq.getStart() + ungapped.length() - 1);
179 // JBPNote: this would be consistent but may not be useful
180 // seq.setDatasetSequence(ds);
183 // add in offset between seq and the dataset sequence
184 if (ds.getStart() < seq.getStart())
186 int offset = seq.getStart() - ds.getStart();
189 // absolute cigar string
190 addDeleted(_s + offset);
196 // normal behaviour - just mark start and end subsequence
204 // any gaps to process ?
205 if (l_ungapped != (_e - _s))
211 // copy over local properties for the sequence instance of the refseq
212 seqProps = SeqsetUtils.SeqCharacterHash(seq);
214 if (end > ds.getLength())
216 throw new Error(MessageManager.getString("error.implementation_error_seqcigar_possible"));
217 // end = ds.getLength();
224 * directly initialise a cigar object with a sequence of range, operation
225 * pairs and a sequence to apply it to. operation and range should be relative
226 * to the seq.getStart()'th residue of the dataset seq resolved from seq.
235 public SeqCigar(SequenceI seq, char operation[], int range[])
240 throw new Error(MessageManager.getString("error.implmentation_bug_seq_null"));
242 if (operation.length != range.length)
244 throw new Error(MessageManager.getString("error.implementation_bug_cigar_operation_list_range_list"));
247 if (operation != null)
249 this.operation = new char[operation.length + _inc_length];
250 this.range = new int[operation.length + _inc_length];
252 if (_setSeq(seq, false, 0, 0))
254 throw new Error(MessageManager.getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
256 for (int i = this.length, j = 0; j < operation.length; i++, j++)
258 char op = operation[j];
259 if (op != M && op != I && op != D)
261 throw new Error(MessageManager.formatMessage("error.implementation_bug_cigar_operation", new String[]{Integer.valueOf(j).toString(),Integer.valueOf(op).toString(),Integer.valueOf(M).toString(),Integer.valueOf(I).toString(),Integer.valueOf(D).toString()}));
263 this.operation[i] = op;
264 this.range[i] = range[j];
266 this.length += operation.length;
270 this.operation = null;
273 if (_setSeq(seq, false, 0, 0))
275 throw new Error(MessageManager.getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
281 * add range matched residues to cigar string
286 public void addMatch(int range)
288 this.addOperation(M, range);
292 * Adds insertion and match operations based on seq to the cigar up to the
293 * endpos column of seq.
303 * @param initialDeletions
304 * if true then initial deletions will be added from start of seq to
307 protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
308 int startpos, int endpos, boolean initialDeletions)
312 int p = 0, res = seq.getLength();
314 if (!initialDeletions)
321 boolean isGap = (p < res) ? jalview.util.Comparison.isGap(seq
322 .getCharAt(p)) : true;
323 if ((startpos <= p) && (p <= endpos))
327 if (range > 0 && op != I)
329 cigar.addOperation(op, range);
337 if (range > 0 && op != M)
339 cigar.addOperation(op, range);
350 if (range > 0 && op != D)
352 cigar.addOperation(op, range);
360 // do nothing - insertions are not made in flanking regions
367 cigar.addOperation(op, range);
372 * create a cigar string for given sequence
377 public SeqCigar(SequenceI seq)
382 throw new Error(MessageManager.getString("error.implementation_error_for_new_cigar"));
384 _setSeq(seq, false, 0, 0);
385 // there is still work to do
386 addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
390 * Create Cigar from a range of gaps and residues on a sequence object
395 * int - first column in range
397 * int - last column in range
399 public SeqCigar(SequenceI seq, int start, int end)
404 throw new Error(MessageManager.getString("error.implementation_error_for_new_cigar"));
406 _setSeq(seq, false, start, end + 1);
407 // there is still work to do
408 addSequenceOps(this, seq, start, end, false);
412 * Create a cigar object from a cigar string like '[<I|D|M><range>]+' Will
413 * fail if the given seq already contains gaps (JBPNote: future implementation
417 * SequenceI object resolvable to a dataset sequence
422 public static SeqCigar parseCigar(SequenceI seq, String cigarString)
425 Object[] opsandrange = parseCigarString(cigarString);
426 return new SeqCigar(seq, (char[]) opsandrange[0],
427 (int[]) opsandrange[1]);
431 * create an alignment from the given array of cigar sequences and gap
432 * character, and marking the given segments as visible in the given
436 * @param gapCharacter
438 * - columnSelection where hidden regions are marked
440 * - visible regions of alignment
441 * @return SequenceI[]
443 public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
444 char gapCharacter, ColumnSelection colsel, int[] segments)
446 SequenceI[] seqs = new SequenceI[alseqs.length];
447 StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
448 String[] alseqs_string = new String[alseqs.length];
449 Object[] gs_regions = new Object[alseqs.length];
450 for (int i = 0; i < alseqs.length; i++)
452 alseqs_string[i] = alseqs[i].getRefSeq().getSequenceAsString(
453 alseqs[i].start, alseqs[i].end);
454 gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
455 gapCharacter); // gapped sequence, {start, start col, end.
456 // endcol}, hidden regions {{start, end, col}})
457 if (gs_regions[i] == null)
459 throw new Error(MessageManager.formatMessage("error.implementation_error_cigar_seq_no_operations", new String[]{Integer.valueOf(i).toString()}));
461 g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
466 // Now account for insertions. (well - deletions)
467 // this is complicated because we must keep track of shifted positions in
469 ShiftList shifts = new ShiftList();
470 for (int i = 0; i < alseqs.length; i++)
472 Object[] gs_region = ((Object[]) ((Object[]) gs_regions[i])[2]);
473 if (gs_region != null)
476 for (int hr = 0; hr < gs_region.length; hr++)
478 int[] region = (int[]) gs_region[hr];
479 char[] insert = new char[region[1] - region[0] + 1];
480 for (int s = 0; s < insert.length; s++)
482 insert[s] = gapCharacter;
484 int inspos = shifts.shift(region[2]); // resolve insertion position in
485 // current alignment frame of
487 for (int s = 0; s < alseqs.length; s++)
491 if (g_seqs[s].length() <= inspos)
493 // prefix insertion with more gaps.
494 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
496 g_seqs[s].append(gapCharacter); // to debug - use a diffferent
497 // gap character here
500 g_seqs[s].insert(inspos, insert);
504 g_seqs[s].insert(inspos,
505 alseqs_string[i].substring(region[0], region[1] + 1));
508 shifts.addShift(region[2], insert.length); // update shift in
509 // alignment frame of
511 if (segments == null)
513 // add a hidden column for this deletion
514 colsel.hideColumns(inspos, inspos + insert.length - 1);
519 for (int i = 0; i < alseqs.length; i++)
521 int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
522 SequenceI ref = alseqs[i].getRefSeq();
523 seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
524 ref.getStart() + alseqs[i].start + bounds[0], ref.getStart()
525 + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
526 seqs[i].setDatasetSequence(ref);
527 seqs[i].setDescription(ref.getDescription());
529 if (segments != null)
531 for (int i = 0; i < segments.length; i += 3)
533 // int start=shifts.shift(segments[i]-1)+1;
534 // int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
535 colsel.hideColumns(segments[i + 1], segments[i + 1]
536 + segments[i + 2] - 1);
543 * references to entities that this sequence cigar is associated with.
545 private Hashtable selGroups = null;
547 public void setGroupMembership(Object group)
549 if (selGroups == null)
551 selGroups = new Hashtable();
553 selGroups.put(group, new int[0]);
557 * Test for and if present remove association to group.
560 * @return true if group was associated and it was removed
562 public boolean removeGroupMembership(Object group)
564 if (selGroups != null && selGroups.containsKey(group))
566 selGroups.remove(group);
573 * forget all associations for this sequence.
575 public void clearMemberships()
577 if (selGroups != null)
586 * @return null or array of all associated entities
588 public Object[] getAllMemberships()
590 if (selGroups == null)
594 Object[] mmbs = new Object[selGroups.size()];
595 Enumeration en = selGroups.keys();
596 for (int i = 0; en.hasMoreElements(); i++)
598 mmbs[i] = en.nextElement();
604 * Test for group membership
607 * - a selection group or some other object that may be associated
609 * @return true if sgr is associated with this seqCigar
611 public boolean isMemberOf(Object sgr)
613 return (selGroups != null) && selGroups.get(sgr) != null;