2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
25 import jalview.analysis.*;
26 import jalview.schemes.*;
29 * Collects a set contiguous ranges on a set of sequences
34 public class SequenceGroup
40 Conservation conserve;
44 boolean displayBoxes = true;
46 boolean displayText = true;
48 boolean colourText = false;
50 private Vector sequences = new Vector();
55 public ColourSchemeI cs;
61 public Color outlineColour = Color.black;
63 public Color idColour = null;
65 public int thresholdTextColour = 0;
67 public Color textColour = Color.black;
69 public Color textColour2 = Color.white;
72 * Creates a new SequenceGroup object.
74 public SequenceGroup()
76 groupName = "JGroup:" + this.hashCode();
80 * Creates a new SequenceGroup object.
89 * first column of group
91 * last column of group
93 public SequenceGroup(Vector sequences, String groupName,
94 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
95 boolean colourText, int start, int end)
97 this.sequences = sequences;
98 this.groupName = groupName;
99 this.displayBoxes = displayBoxes;
100 this.displayText = displayText;
101 this.colourText = colourText;
105 recalcConservation();
108 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
110 int iSize = sequences.size();
111 SequenceI[] seqs = new SequenceI[iSize];
112 SequenceI[] inorder = getSequencesInOrder(align);
114 for (int i = 0, ipos = 0; i < inorder.length; i++)
116 SequenceI seq = inorder[i];
118 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
119 if (seqs[ipos] != null)
121 seqs[ipos].setDescription(seq.getDescription());
122 seqs[ipos].setDBRef(seq.getDBRef());
123 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
124 if (seq.getDatasetSequence() != null)
126 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
129 if (seq.getAnnotation() != null)
131 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
132 // Only copy annotation that is either a score or referenced by the
133 // alignment's annotation vector
134 for (int a = 0; a < seq.getAnnotation().length; a++)
136 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
139 boolean found = false;
140 for (int pos = 0; pos < alann.length; pos++)
142 if (alann[pos] == tocopy)
151 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
152 .getAnnotation()[a]);
153 newannot.restrict(startRes, endRes);
154 newannot.setSequenceRef(seqs[ipos]);
155 newannot.adjustForAlignment();
156 seqs[ipos].addAlignmentAnnotation(newannot);
166 if (iSize != inorder.length)
168 SequenceI[] nseqs = new SequenceI[iSize];
169 System.arraycopy(seqs, 0, nseqs, 0, iSize);
177 * If sequence ends in gaps, the end residue can be correctly calculated here
183 public int findEndRes(SequenceI seq)
188 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
190 ch = seq.getCharAt(j);
191 if (!jalview.util.Comparison.isGap((ch)))
199 eres += seq.getStart() - 1;
205 public Vector getSequences(Hashtable hiddenReps)
207 if (hiddenReps == null)
213 Vector allSequences = new Vector();
215 for (int i = 0; i < sequences.size(); i++)
217 seq = (SequenceI) sequences.elementAt(i);
218 allSequences.addElement(seq);
219 if (hiddenReps.containsKey(seq))
221 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
222 for (int h = 0; h < hsg.getSize(); h++)
224 seq2 = hsg.getSequenceAt(h);
225 if (seq2 != seq && !allSequences.contains(seq2))
227 allSequences.addElement(seq2);
237 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
239 Vector tmp = getSequences(hiddenReps);
244 SequenceI[] result = new SequenceI[tmp.size()];
245 for (int i = 0; i < result.length; i++)
247 result[i] = (SequenceI) tmp.elementAt(i);
259 * @return DOCUMENT ME!
261 public boolean adjustForRemoveLeft(int col)
263 // return value is true if the group still exists
266 startRes = startRes - col;
271 endRes = endRes - col;
273 if (startRes > endRes)
280 // must delete this group!!
293 * @return DOCUMENT ME!
295 public boolean adjustForRemoveRight(int col)
314 * @return DOCUMENT ME!
316 public String getName()
321 public String getDescription()
332 public void setName(String name)
337 public void setDescription(String desc)
345 * @return DOCUMENT ME!
347 public Conservation getConservation()
358 public void setConservation(Conservation c)
364 * Add s to this sequence group
367 * alignment sequence to be added
369 * true means Group's conservation should be recalculated
371 public void addSequence(SequenceI s, boolean recalc)
373 if (s != null && !sequences.contains(s))
375 sequences.addElement(s);
380 recalcConservation();
385 * calculate residue conservation for group
387 public void recalcConservation()
396 cs.setConsensus(AAFrequency
397 .calculate(sequences, startRes, endRes + 1));
399 if (cs instanceof ClustalxColourScheme)
401 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
404 if (cs.conservationApplied())
406 Conservation c = new Conservation(groupName,
407 ResidueProperties.propHash, 3, sequences, startRes,
410 c.verdict(false, 25);
412 cs.setConservation(c);
414 if (cs instanceof ClustalxColourScheme)
416 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
419 } catch (java.lang.OutOfMemoryError err)
422 System.out.println("Out of memory loading groups: " + err);
435 public void addOrRemove(SequenceI s, boolean recalc)
437 if (sequences.contains(s))
439 deleteSequence(s, recalc);
443 addSequence(s, recalc);
455 public void deleteSequence(SequenceI s, boolean recalc)
457 sequences.removeElement(s);
461 recalcConservation();
468 * @return DOCUMENT ME!
470 public int getStartRes()
478 * @return DOCUMENT ME!
480 public int getEndRes()
491 public void setStartRes(int i)
502 public void setEndRes(int i)
510 * @return DOCUMENT ME!
514 return sequences.size();
523 * @return DOCUMENT ME!
525 public SequenceI getSequenceAt(int i)
527 return (SequenceI) sequences.elementAt(i);
536 public void setColourText(boolean state)
544 * @return DOCUMENT ME!
546 public boolean getColourText()
557 public void setDisplayText(boolean state)
565 * @return DOCUMENT ME!
567 public boolean getDisplayText()
578 public void setDisplayBoxes(boolean state)
580 displayBoxes = state;
586 * @return DOCUMENT ME!
588 public boolean getDisplayBoxes()
596 * @return DOCUMENT ME!
598 public int getWidth()
600 // MC This needs to get reset when characters are inserted and deleted
601 if (sequences.size() > 0)
603 width = ((SequenceI) sequences.elementAt(0)).getLength();
606 for (int i = 1; i < sequences.size(); i++)
608 SequenceI seq = (SequenceI) sequences.elementAt(i);
610 if (seq.getLength() > width)
612 width = seq.getLength();
625 public void setOutlineColour(Color c)
633 * @return DOCUMENT ME!
635 public Color getOutlineColour()
637 return outlineColour;
642 * returns the sequences in the group ordered by the ordering given by al
646 * @return SequenceI[]
648 public SequenceI[] getSequencesInOrder(AlignmentI al)
650 int sSize = sequences.size();
651 int alHeight = al.getHeight();
653 SequenceI[] seqs = new SequenceI[sSize];
656 for (int i = 0; i < alHeight && index < sSize; i++)
658 if (sequences.contains(al.getSequenceAt(i)))
660 seqs[index++] = al.getSequenceAt(i);
668 * @return the idColour
670 public Color getIdColour()
677 * the idColour to set
679 public void setIdColour(Color idColour)
681 this.idColour = idColour;