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 = false;
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,ipos=0; i < inorder.length; i++)
99 SequenceI seq = inorder[i];
101 seqs[ipos] = seq.getSubSequence(startRes, endRes+1);
102 if (seqs[ipos]!=null)
104 seqs[ipos].setDescription(seq.getDescription());
105 seqs[ipos].setDBRef(seq.getDBRef());
106 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
107 if (seq.getDatasetSequence() != null)
109 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
112 if (seq.getAnnotation() != null)
114 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
115 // Only copy annotation that is either a score or referenced by the alignment's annotation vector
116 for (int a = 0; a < seq.getAnnotation().length; a++)
118 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
122 for (int pos=0;pos<alann.length; pos++)
124 if (alann[pos]==tocopy)
133 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
134 .getAnnotation()[a]);
135 newannot.restrict(startRes, endRes);
136 newannot.setSequenceRef(seqs[ipos]);
137 newannot.adjustForAlignment();
138 seqs[ipos].addAlignmentAnnotation(newannot);
146 if (iSize!=inorder.length)
148 SequenceI[] nseqs = new SequenceI[iSize];
149 System.arraycopy(seqs, 0, nseqs, 0, iSize);
157 * If sequence ends in gaps, the end residue can
158 * be correctly calculated here
159 * @param seq SequenceI
162 public int findEndRes(SequenceI seq)
167 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
169 ch = seq.getCharAt(j);
170 if (!jalview.util.Comparison.isGap( (ch)))
178 eres += seq.getStart() - 1;
184 public Vector getSequences(Hashtable hiddenReps)
186 if (hiddenReps == null)
192 Vector allSequences = new Vector();
194 for (int i = 0; i < sequences.size(); i++)
196 seq = (SequenceI) sequences.elementAt(i);
197 allSequences.addElement(seq);
198 if (hiddenReps.containsKey(seq))
200 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
201 for (int h = 0; h < hsg.getSize(); h++)
203 seq2 = hsg.getSequenceAt(h);
205 && !allSequences.contains(seq2))
207 allSequences.addElement(seq2);
217 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
219 Vector tmp = getSequences(hiddenReps);
224 SequenceI[] result = new SequenceI[tmp.size()];
225 for (int i = 0; i < result.length; i++)
227 result[i] = (SequenceI) tmp.elementAt(i);
236 * @param col DOCUMENT ME!
238 * @return DOCUMENT ME!
240 public boolean adjustForRemoveLeft(int col)
242 // return value is true if the group still exists
245 startRes = startRes - col;
250 endRes = endRes - col;
252 if (startRes > endRes)
259 // must delete this group!!
269 * @param col DOCUMENT ME!
271 * @return DOCUMENT ME!
273 public boolean adjustForRemoveRight(int col)
292 * @return DOCUMENT ME!
294 public String getName()
299 public String getDescription()
307 * @param name DOCUMENT ME!
309 public void setName(String name)
314 public void setDescription(String desc)
322 * @return DOCUMENT ME!
324 public Conservation getConservation()
332 * @param c DOCUMENT ME!
334 public void setConservation(Conservation c)
342 * @param s DOCUMENT ME!
343 * @param recalc DOCUMENT ME!
345 public void addSequence(SequenceI s, boolean recalc)
347 if (s != null && !sequences.contains(s))
349 sequences.addElement(s);
354 recalcConservation();
361 public void recalcConservation()
370 cs.setConsensus(AAFrequency.calculate(sequences, startRes, endRes + 1));
372 if (cs instanceof ClustalxColourScheme)
374 ( (ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
377 if (cs.conservationApplied())
379 Conservation c = new Conservation(groupName,
380 ResidueProperties.propHash, 3,
382 startRes, endRes + 1);
384 c.verdict(false, 25);
386 cs.setConservation(c);
388 if (cs instanceof ClustalxColourScheme)
390 ( (ClustalxColourScheme) cs).resetClustalX(sequences,
395 catch (java.lang.OutOfMemoryError err)
397 System.out.println("Out of memory loading groups: " + err);
405 * @param s DOCUMENT ME!
406 * @param recalc DOCUMENT ME!
408 public void addOrRemove(SequenceI s, boolean recalc)
410 if (sequences.contains(s))
412 deleteSequence(s, recalc);
416 addSequence(s, recalc);
423 * @param s DOCUMENT ME!
424 * @param recalc DOCUMENT ME!
426 public void deleteSequence(SequenceI s, boolean recalc)
428 sequences.removeElement(s);
432 recalcConservation();
439 * @return DOCUMENT ME!
441 public int getStartRes()
449 * @return DOCUMENT ME!
451 public int getEndRes()
459 * @param i DOCUMENT ME!
461 public void setStartRes(int i)
469 * @param i DOCUMENT ME!
471 public void setEndRes(int i)
479 * @return DOCUMENT ME!
483 return sequences.size();
489 * @param i DOCUMENT ME!
491 * @return DOCUMENT ME!
493 public SequenceI getSequenceAt(int i)
495 return (SequenceI) sequences.elementAt(i);
501 * @param state DOCUMENT ME!
503 public void setColourText(boolean state)
511 * @return DOCUMENT ME!
513 public boolean getColourText()
521 * @param state DOCUMENT ME!
523 public void setDisplayText(boolean state)
531 * @return DOCUMENT ME!
533 public boolean getDisplayText()
541 * @param state DOCUMENT ME!
543 public void setDisplayBoxes(boolean state)
545 displayBoxes = state;
551 * @return DOCUMENT ME!
553 public boolean getDisplayBoxes()
561 * @return DOCUMENT ME!
563 public int getWidth()
565 // MC This needs to get reset when characters are inserted and deleted
566 if (sequences.size() > 0)
568 width = ( (SequenceI) sequences.elementAt(0)).getLength();
571 for (int i = 1; i < sequences.size(); i++)
573 SequenceI seq = (SequenceI) sequences.elementAt(i);
575 if (seq.getLength() > width)
577 width = seq.getLength();
587 * @param c DOCUMENT ME!
589 public void setOutlineColour(Color c)
597 * @return DOCUMENT ME!
599 public Color getOutlineColour()
601 return outlineColour;
606 * returns the sequences in the group ordered by the ordering given by al
608 * @param al Alignment
609 * @return SequenceI[]
611 public SequenceI[] getSequencesInOrder(AlignmentI al)
613 int sSize = sequences.size();
614 int alHeight = al.getHeight();
616 SequenceI[] seqs = new SequenceI[sSize];
619 for (int i = 0; i < alHeight && index < sSize; i++)
621 if (sequences.contains(al.getSequenceAt(i)))
623 seqs[index++] = al.getSequenceAt(i);