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.analysis;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.ext.android.SparseIntArray;
28 import jalview.schemes.ResidueProperties;
30 import java.awt.Color;
31 import java.util.List;
33 import java.util.TreeMap;
34 import java.util.Vector;
37 * Calculates conservation values for a given set of sequences
42 public class Conservation
44 SequenceI[] sequences;
50 Vector<int[]> seqNums; // vector of int vectors where first is sequence
53 int maxLength = 0; // used by quality calcs
55 boolean seqNumsChanged = false; // updated after any change via calcSeqNum;
57 Map<String, Integer>[] total;
59 boolean canonicaliseAa = true; // if true then conservation calculation will
61 // map all symbols to canonical aa numbering
62 // rather than consider conservation of that
65 /** Stores calculated quality values */
66 private Vector<Double> quality;
68 /** Stores maximum and minimum values of quality values */
69 private double[] qualityRange = new double[2];
71 private Sequence consSequence;
73 private int threshold;
75 private String name = "";
77 private int[][] cons2;
79 private String[] consSymbs;
82 * Creates a new Conservation object.
85 * Name of conservation
87 * to count the residues in residueHash(). commonly used value is 3
89 * sequences to be used in calculation
91 * start residue position
93 * end residue position
95 public Conservation(String name, int threshold,
96 List<SequenceI> sequences, int start, int end)
99 this.threshold = threshold;
103 maxLength = end - start + 1; // default width includes bounds of
106 int s, sSize = sequences.size();
107 SequenceI[] sarray = new SequenceI[sSize];
108 this.sequences = sarray;
111 for (s = 0; s < sSize; s++)
113 sarray[s] = sequences.get(s);
114 if (sarray[s].getLength() > maxLength)
116 maxLength = sarray[s].getLength();
119 } catch (ArrayIndexOutOfBoundsException ex)
121 // bail - another thread has modified the sequence array, so the current
122 // calculation is probably invalid.
123 this.sequences = new SequenceI[0];
129 * Translate sequence i into a numerical representation and store it in the
130 * i'th position of the seqNums array.
134 private void calcSeqNum(int i)
136 String sq = null; // for dumb jbuilder not-inited exception warning
139 int sSize = sequences.length;
141 if ((i > -1) && (i < sSize))
143 sq = sequences[i].getSequenceAsString();
145 if (seqNums.size() <= i)
147 seqNums.addElement(new int[sq.length() + 1]);
150 if (sq.hashCode() != seqNums.elementAt(i)[0])
154 seqNumsChanged = true;
162 sqnum = new int[len + 1]; // better to always make a new array -
163 // sequence can change its length
164 sqnum[0] = sq.hashCode();
166 for (j = 1; j <= len; j++)
168 sqnum[j] = jalview.schemes.ResidueProperties.aaIndex[sq
172 seqNums.setElementAt(sqnum, i);
176 System.out.println("SEQUENCE HAS BEEN DELETED!!!");
181 // JBPNote INFO level debug
183 .println("ERROR: calcSeqNum called with out of range sequence index for Alignment\n");
188 * Calculates the conservation values for given set of sequences
190 public void calculate()
192 int jSize = sequences.length;
193 // int[] values; // Replaces residueHash
194 SparseIntArray values = new SparseIntArray();
196 total = new Map[maxLength];
198 for (int i = start; i <= end; i++)
200 // values = new int[255];
203 for (int j = 0; j < jSize; j++)
205 if (sequences[j].getLength() > i)
207 char c = sequences[j].getCharAt(i);
210 { // lookup the base aa code symbol
211 c = (char) ResidueProperties.aaIndex[sequences[j].getCharAt(i)];
218 // recover canonical aa symbol
219 c = ResidueProperties.aa[c].charAt(0);
224 // original behaviour - operate on ascii symbols directly
225 // No need to check if its a '-'
226 if (c == '.' || c == ' ')
243 // What is the count threshold to count the residues in residueHash()
244 int thresh = (threshold * jSize) / 100;
246 // loop over all the found residues
247 // Hashtable<String, Integer> resultHash = new Hashtable<String,
249 Map<String, Integer> resultHash = new TreeMap<String, Integer>();
250 // for (char v = '-'; v < 'Z'; v++)
251 for (int key = 0; key < values.size(); key++)
253 char v = (char) values.keyAt(key);
254 // if (values[v] > thresh)
255 if (values.valueAt(key) > thresh)
257 String res = String.valueOf(v);
259 // Now loop over the properties
260 for (String type : ResidueProperties.propHash.keySet())
262 Map<String, Integer> ht = ResidueProperties.propHash.get(type);
264 // Have we ticked this before?
265 if (!resultHash.containsKey(type))
267 if (ht.containsKey(res))
269 resultHash.put(type, ht.get(res));
273 resultHash.put(type, ht.get("-"));
276 else if (!resultHash.get(type).equals(ht.get(res)))
278 resultHash.put(type, new Integer(-1));
284 if (total.length > 0)
286 total[i - start] = resultHash;
291 /*****************************************************************************
292 * count conservation for the j'th column of the alignment
294 * @return { gap count, conserved residue count}
296 public int[] countConsNGaps(int j)
301 int[] r = new int[2];
303 int i, iSize = sequences.length;
306 for (i = 0; i < iSize; i++)
308 if (j >= sequences[i].getLength())
314 c = sequences[i].getCharAt(j); // gaps do not have upper/lower case
316 if (jalview.util.Comparison.isGap((c)))
337 r[0] = (nres == cons) ? 1 : 0;
344 * Returns the upper-cased character if between 'a' and 'z', else the
350 char toUpperCase(char c)
352 if ('a' <= c && c <= 'z')
354 c -= (32); // 32 = 'a' - 'A'
360 * Calculates the conservation sequence
363 * if true, positive conservation; false calculates negative
365 * @param percentageGaps
366 * commonly used value is 25
368 public void verdict(boolean consflag, float percentageGaps)
370 StringBuilder consString = new StringBuilder(end);
372 // NOTE THIS SHOULD CHECK IF THE CONSEQUENCE ALREADY
373 // EXISTS AND NOT OVERWRITE WITH '-', BUT THIS CASE
374 // DOES NOT EXIST IN JALVIEW 2.1.2
375 for (int i = 0; i < start; i++)
377 consString.append('-');
379 consSymbs = new String[end - start + 1];
380 for (int i = start; i <= end; i++)
382 int[] gapcons = countConsNGaps(i);
383 int totGaps = gapcons[1];
384 float pgaps = ((float) totGaps * 100) / sequences.length;
385 StringBuilder positives = new StringBuilder(64);
386 StringBuilder negatives = new StringBuilder(32);
387 // consSymbs[i - start] = "";
389 if (percentageGaps > pgaps)
391 Map<String, Integer> resultHash = total[i - start];
392 // Now find the verdict
394 for (String type : resultHash.keySet())
396 int result = resultHash.get(type).intValue();
397 // Do we want to count +ve conservation or +ve and -ve cons.?
402 // consSymbs[i - start] = type + " " + consSymbs[i - start];
403 positives.append(positives.length() == 0 ? "" : " ");
404 positives.append(type);
415 * add negatively conserved properties on the end
417 // consSymbs[i - start] = consSymbs[i - start] + " !" + type;
418 negatives.append(negatives.length() == 0 ? "" : " ");
419 negatives.append("!").append(type);
424 * put positively conserved properties on the front
426 // consSymbs[i - start] = type + " " + consSymbs[i - start];
427 positives.append(positives.length() == 0 ? "" : " ");
428 positives.append(type);
434 if (negatives.length() > 0)
436 positives.append(" ").append(negatives);
438 consSymbs[i - start] = positives.toString();
442 consString.append(count); // Conserved props!=Identity
446 consString.append((gapcons[0] == 1) ? "*" : "+");
451 consString.append('-');
455 consSequence = new Sequence(name, consString.toString(), start, end);
461 * @return Conservation sequence
463 public Sequence getConsSequence()
468 // From Alignment.java in jalview118
469 public void findQuality()
471 findQuality(0, maxLength - 1);
477 private void percentIdentity2()
479 seqNums = new Vector<int[]>();
481 int i = 0, iSize = sequences.length;
482 // Do we need to calculate this again?
483 for (i = 0; i < iSize; i++)
488 if ((cons2 == null) || seqNumsChanged)
490 cons2 = new int[maxLength][24];
492 // Initialize the array
493 for (int j = 0; j < 24; j++)
495 for (i = 0; i < maxLength; i++)
504 while (j < sequences.length)
506 sqnum = seqNums.elementAt(j);
508 for (i = 1; i < sqnum.length; i++)
510 cons2[i - 1][sqnum[i]]++;
513 for (i = sqnum.length - 1; i < maxLength; i++)
515 cons2[i][23]++; // gap count
524 * for (int i=start; i <= end; i++) { int max = -1000; int maxi = -1; int
527 * for (int j=0;j<24;j++) { if (cons2[i][j] > max) { max = cons2[i][j];
528 * maxi = i; maxj = j; } } }
534 * Calculates the quality of the set of sequences
541 public void findQuality(int startRes, int endRes)
543 quality = new Vector<Double>();
546 int[][] BLOSUM62 = ResidueProperties.getBLOSUM62();
548 // Loop over columns // JBPNote Profiling info
549 // long ts = System.currentTimeMillis();
550 // long te = System.currentTimeMillis();
553 int size = seqNums.size();
554 int[] lengths = new int[size];
555 double tot, bigtot, sr, tmp;
557 int l, j, i, ii, i2, k, seqNum;
559 for (l = 0; l < size; l++)
561 lengths[l] = seqNums.elementAt(l).length - 1;
564 for (j = startRes; j <= endRes; j++)
568 // First Xr = depends on column only
571 for (ii = 0; ii < 24; ii++)
575 for (i2 = 0; i2 < 24; i2++)
577 x[ii] += (((double) cons2[j][i2] * BLOSUM62[ii][i2]) + 4);
583 // Now calculate D for each position and sum
584 for (k = 0; k < size; k++)
588 seqNum = (j < lengths[k]) ? seqNums.elementAt(k)[j + 1] : 23; // Sequence,
593 // This is a loop over r
594 for (i = 0; i < 23; i++)
598 sr = (double) BLOSUM62[i][seqNum] + 4;
600 // Calculate X with another loop over residues
601 // System.out.println("Xi " + i + " " + x[i] + " " + sr);
604 tot += (xx[i] * xx[i]);
607 bigtot += Math.sqrt(tot);
610 // This is the quality for one column
616 // bigtot = bigtot * (size-cons2[j][23])/size;
617 quality.addElement(new Double(bigtot));
619 // Need to normalize by gaps
622 double newmax = -10000;
624 for (j = startRes; j <= endRes; j++)
626 tmp = quality.elementAt(j).doubleValue();
627 tmp = ((max - tmp) * (size - cons2[j][23])) / size;
629 // System.out.println(tmp+ " " + j);
630 quality.setElementAt(new Double(tmp), j);
638 // System.out.println("Quality " + s);
639 qualityRange[0] = 0D;
640 qualityRange[1] = newmax;
644 * Complete the given consensus and quuality annotation rows. Note: currently
645 * this method will enlarge the given annotation row if it is too small,
646 * otherwise will leave its length unchanged.
648 * @param conservation
649 * conservation annotation row
651 * (optional - may be null)
653 * first column for conservation
655 * extent of conservation
657 public void completeAnnotations(AlignmentAnnotation conservation,
658 AlignmentAnnotation quality2, int istart, int alWidth)
660 char[] sequence = getConsSequence().getSequence();
672 maxB = 0f - minB; // scalable range for colouring both Conservation and
682 if (conservation != null && conservation.annotations != null
683 && conservation.annotations.length < alWidth)
685 conservation.annotations = new Annotation[alWidth];
688 if (quality2 != null)
690 quality2.graphMax = (float) qualityRange[1];
691 if (quality2.annotations != null
692 && quality2.annotations.length < alWidth)
694 quality2.annotations = new Annotation[alWidth];
696 qmin = (float) qualityRange[0];
697 qmax = (float) qualityRange[1];
700 for (int i = istart; i < alWidth; i++)
706 if (Character.isDigit(c))
719 if (conservation != null)
721 float vprop = value - min;
723 int consp = i - start;
724 String conssym = (value > 0 && consp > -1 && consp < consSymbs.length) ? consSymbs[consp]
726 conservation.annotations[i] = new Annotation(String.valueOf(c),
727 conssym, ' ', value, new Color(minR + (maxR * vprop), minG
728 + (maxG * vprop), minB + (maxB * vprop)));
732 if (quality2 != null)
734 value = quality.elementAt(i).floatValue();
735 float vprop = value - qmin;
737 quality2.annotations[i] = new Annotation(" ",
738 String.valueOf(value), ' ', value, new Color(minR
739 + (maxR * vprop), minG + (maxG * vprop), minB
746 * construct and call the calculation methods on a new Conservation object
749 * - name of conservation
751 * - minimum number of conserved residues needed to indicate
752 * conservation (typically 3)
755 * first column in calculation window
757 * last column in calculation window
759 * positive (true) or negative (false) conservation
760 * @param consPercGaps
761 * percentage of gaps tolerated in column
763 * flag indicating if alignment quality should be calculated
764 * @return Conservation object ready for use in visualization
766 public static Conservation calculateConservation(String name,
767 int threshold, List<SequenceI> seqs, int start, int end,
768 boolean posOrNeg, int consPercGaps, boolean calcQuality)
770 Conservation cons = new Conservation(name, threshold, seqs, start, end);
772 cons.verdict(posOrNeg, consPercGaps);