2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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.*;
34 public class SequenceGroup
38 Conservation conserve;
40 boolean displayBoxes = true;
41 boolean displayText = true;
42 boolean colourText = true;
43 private Vector sequences = new Vector();
47 public ColourSchemeI cs;
50 Color outlineColour = Color.black;
51 public int thresholdTextColour = 0;
52 public Color textColour = Color.black;
53 public Color textColour2 = Color.white;
56 * Creates a new SequenceGroup object.
58 public SequenceGroup()
60 groupName = "JGroup:" + this.hashCode();
64 * Creates a new SequenceGroup object.
66 * @param sequences DOCUMENT ME!
67 * @param groupName DOCUMENT ME!
68 * @param scheme DOCUMENT ME!
69 * @param displayBoxes DOCUMENT ME!
70 * @param displayText DOCUMENT ME!
71 * @param colourText DOCUMENT ME!
72 * @param start DOCUMENT ME!
73 * @param end DOCUMENT ME!
75 public SequenceGroup(Vector sequences, String groupName,
76 ColourSchemeI scheme, boolean displayBoxes,
78 boolean colourText, int start, int end)
80 this.sequences = sequences;
81 this.groupName = groupName;
82 this.displayBoxes = displayBoxes;
83 this.displayText = displayText;
84 this.colourText = colourText;
91 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
93 int iSize = sequences.size();
94 SequenceI[] seqs = new SequenceI[iSize];
95 SequenceI[] inorder = getSequencesInOrder(align);
97 for (int i = 0; i < iSize; i++)
99 SequenceI seq = inorder[i];
101 seqs[i] = new Sequence(seq.getName(),
102 seq.getSequence(startRes, endRes + 1),
103 seq.findPosition(startRes),
106 seqs[i].setDescription(seq.getDescription());
107 seqs[i].setDBRef(seq.getDBRef());
108 seqs[i].setSequenceFeatures(seq.getSequenceFeatures());
109 if (seq.getDatasetSequence() != null)
111 seqs[i].setDatasetSequence(seq.getDatasetSequence());
114 if (seq.getAnnotation() != null)
116 for (int a = 0; a < seq.getAnnotation().length; a++)
118 seqs[i].addAlignmentAnnotation(new AlignmentAnnotation(seq.getAnnotation()[a]));
128 * If sequence ends in gaps, the end residue can
129 * be correctly calculated here
130 * @param seq SequenceI
133 public int findEndRes(SequenceI seq)
138 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
140 ch = seq.getCharAt(j);
141 if (!jalview.util.Comparison.isGap( (ch)))
149 eres += seq.getStart() - 1;
155 public Vector getSequences(Hashtable hiddenReps)
157 if (hiddenReps == null)
163 Vector allSequences = new Vector();
165 for (int i = 0; i < sequences.size(); i++)
167 seq = (SequenceI) sequences.elementAt(i);
168 allSequences.addElement(seq);
169 if (hiddenReps.containsKey(seq))
171 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
172 for (int h = 0; h < hsg.getSize(); h++)
174 seq2 = hsg.getSequenceAt(h);
176 && !allSequences.contains(seq2))
178 allSequences.addElement(seq2);
188 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
190 Vector tmp = getSequences(hiddenReps);
195 SequenceI[] result = new SequenceI[tmp.size()];
196 for (int i = 0; i < result.length; i++)
198 result[i] = (SequenceI) tmp.elementAt(i);
207 * @param col DOCUMENT ME!
209 * @return DOCUMENT ME!
211 public boolean adjustForRemoveLeft(int col)
213 // return value is true if the group still exists
216 startRes = startRes - col;
221 endRes = endRes - col;
223 if (startRes > endRes)
230 // must delete this group!!
240 * @param col DOCUMENT ME!
242 * @return DOCUMENT ME!
244 public boolean adjustForRemoveRight(int col)
263 * @return DOCUMENT ME!
265 public String getName()
270 public String getDescription()
278 * @param name DOCUMENT ME!
280 public void setName(String name)
285 public void setDescription(String desc)
293 * @return DOCUMENT ME!
295 public Conservation getConservation()
303 * @param c DOCUMENT ME!
305 public void setConservation(Conservation c)
313 * @param s DOCUMENT ME!
314 * @param recalc DOCUMENT ME!
316 public void addSequence(SequenceI s, boolean recalc)
318 if (s != null && !sequences.contains(s))
320 sequences.addElement(s);
325 recalcConservation();
332 public void recalcConservation()
341 cs.setConsensus(AAFrequency.calculate(sequences, startRes, endRes + 1));
343 if (cs instanceof ClustalxColourScheme)
345 ( (ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
348 if (cs.conservationApplied())
350 Conservation c = new Conservation(groupName,
351 ResidueProperties.propHash, 3,
353 startRes, endRes + 1);
355 c.verdict(false, 25);
357 cs.setConservation(c);
359 if (cs instanceof ClustalxColourScheme)
361 ( (ClustalxColourScheme) cs).resetClustalX(sequences,
366 catch (java.lang.OutOfMemoryError err)
368 System.out.println("Out of memory loading groups: " + err);
376 * @param s DOCUMENT ME!
377 * @param recalc DOCUMENT ME!
379 public void addOrRemove(SequenceI s, boolean recalc)
381 if (sequences.contains(s))
383 deleteSequence(s, recalc);
387 addSequence(s, recalc);
394 * @param s DOCUMENT ME!
395 * @param recalc DOCUMENT ME!
397 public void deleteSequence(SequenceI s, boolean recalc)
399 sequences.removeElement(s);
403 recalcConservation();
410 * @return DOCUMENT ME!
412 public int getStartRes()
420 * @return DOCUMENT ME!
422 public int getEndRes()
430 * @param i DOCUMENT ME!
432 public void setStartRes(int i)
440 * @param i DOCUMENT ME!
442 public void setEndRes(int i)
450 * @return DOCUMENT ME!
454 return sequences.size();
460 * @param i DOCUMENT ME!
462 * @return DOCUMENT ME!
464 public SequenceI getSequenceAt(int i)
466 return (SequenceI) sequences.elementAt(i);
472 * @param state DOCUMENT ME!
474 public void setColourText(boolean state)
482 * @return DOCUMENT ME!
484 public boolean getColourText()
492 * @param state DOCUMENT ME!
494 public void setDisplayText(boolean state)
502 * @return DOCUMENT ME!
504 public boolean getDisplayText()
512 * @param state DOCUMENT ME!
514 public void setDisplayBoxes(boolean state)
516 displayBoxes = state;
522 * @return DOCUMENT ME!
524 public boolean getDisplayBoxes()
532 * @return DOCUMENT ME!
534 public int getWidth()
536 // MC This needs to get reset when characters are inserted and deleted
537 if (sequences.size() > 0)
539 width = ( (SequenceI) sequences.elementAt(0)).getLength();
542 for (int i = 1; i < sequences.size(); i++)
544 SequenceI seq = (SequenceI) sequences.elementAt(i);
546 if (seq.getLength() > width)
548 width = seq.getLength();
558 * @param c DOCUMENT ME!
560 public void setOutlineColour(Color c)
568 * @return DOCUMENT ME!
570 public Color getOutlineColour()
572 return outlineColour;
577 * returns the sequences in the group ordered by the ordering given by al
579 * @param al Alignment
580 * @return SequenceI[]
582 public SequenceI[] getSequencesInOrder(AlignmentI al)
584 int sSize = sequences.size();
585 int alHeight = al.getHeight();
587 SequenceI[] seqs = new SequenceI[sSize];
590 for (int i = 0; i < alHeight && index < sSize; i++)
592 if (sequences.contains(al.getSequenceAt(i)))
594 seqs[index++] = al.getSequenceAt(i);