Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.util

File Comparison.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

62
103
12
1
431
245
54
0.52
8.58
12
4.5

Classes

Class Line # Actions
Comparison 31 103 54 46
0.74011374%
 

Contributing tests

This file is covered by 469 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.util;
22   
23    import jalview.datamodel.SequenceI;
24   
25    import java.util.ArrayList;
26    import java.util.List;
27   
28    /**
29    * Assorted methods for analysing or comparing sequences.
30    */
 
31    public class Comparison
32    {
33    private static final int EIGHTY_FIVE = 85;
34   
35    private static final int TO_UPPER_CASE = 'a' - 'A';
36   
37    public static final char GAP_SPACE = ' ';
38   
39    public static final char GAP_DOT = '.';
40   
41    public static final char GAP_DASH = '-';
42   
43    public static final String GapChars = new String(
44    new char[]
45    { GAP_SPACE, GAP_DOT, GAP_DASH });
46   
47    /**
48    * DOCUMENT ME!
49    *
50    * @param ii
51    * DOCUMENT ME!
52    * @param jj
53    * DOCUMENT ME!
54    *
55    * @return DOCUMENT ME!
56    */
 
57  0 toggle public static final float compare(SequenceI ii, SequenceI jj)
58    {
59  0 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
60    }
61   
62    /**
63    * this was supposed to be an ungapped pid calculation
64    *
65    * @param ii
66    * SequenceI
67    * @param jj
68    * SequenceI
69    * @param start
70    * int
71    * @param end
72    * int
73    * @return float
74    */
 
75  0 toggle public static float compare(SequenceI ii, SequenceI jj, int start,
76    int end)
77    {
78  0 String si = ii.getSequenceAsString();
79  0 String sj = jj.getSequenceAsString();
80   
81  0 int ilen = si.length() - 1;
82  0 int jlen = sj.length() - 1;
83   
84  0 while (Comparison.isGap(si.charAt(start + ilen)))
85    {
86  0 ilen--;
87    }
88   
89  0 while (Comparison.isGap(sj.charAt(start + jlen)))
90    {
91  0 jlen--;
92    }
93   
94  0 int count = 0;
95  0 int match = 0;
96  0 float pid = -1;
97   
98  0 if (ilen > jlen)
99    {
100  0 for (int j = 0; j < jlen; j++)
101    {
102  0 if (si.substring(start + j, start + j + 1)
103    .equals(sj.substring(start + j, start + j + 1)))
104    {
105  0 match++;
106    }
107   
108  0 count++;
109    }
110   
111  0 pid = (float) match / (float) ilen * 100;
112    }
113    else
114    {
115  0 for (int j = 0; j < jlen; j++)
116    {
117  0 if (si.substring(start + j, start + j + 1)
118    .equals(sj.substring(start + j, start + j + 1)))
119    {
120  0 match++;
121    }
122   
123  0 count++;
124    }
125   
126  0 pid = (float) match / (float) jlen * 100;
127    }
128   
129  0 return pid;
130    }
131   
132    /**
133    * this is a gapped PID calculation
134    *
135    * @param s1
136    * SequenceI
137    * @param s2
138    * SequenceI
139    * @return float
140    * @deprecated use PIDModel.computePID()
141    */
 
142  8 toggle @Deprecated
143    public final static float PID(String seq1, String seq2)
144    {
145  8 return PID(seq1, seq2, 0, seq1.length());
146    }
147   
148    static final int caseShift = 'a' - 'A';
149   
150    // Another pid with region specification
151    /**
152    * @deprecated use PIDModel.computePID()
153    */
 
154  8 toggle @Deprecated
155    public final static float PID(String seq1, String seq2, int start,
156    int end)
157    {
158  8 return PID(seq1, seq2, start, end, true, false);
159    }
160   
161    /**
162    * Calculate percent identity for a pair of sequences over a particular range,
163    * with different options for ignoring gaps.
164    *
165    * @param seq1
166    * @param seq2
167    * @param start
168    * - position in seqs
169    * @param end
170    * - position in seqs
171    * @param wcGaps
172    * - if true - gaps match any character, if false, do not match
173    * anything
174    * @param ungappedOnly
175    * - if true - only count PID over ungapped columns
176    * @return
177    * @deprecated use PIDModel.computePID()
178    */
 
179  12 toggle @Deprecated
180    public final static float PID(String seq1, String seq2, int start,
181    int end, boolean wcGaps, boolean ungappedOnly)
182    {
183  12 int s1len = seq1.length();
184  12 int s2len = seq2.length();
185   
186  12 int len = Math.min(s1len, s2len);
187   
188  12 if (end < len)
189    {
190  0 len = end;
191    }
192   
193  12 if (len < start)
194    {
195  0 start = len - 1; // we just use a single residue for the difference
196    }
197   
198  12 int elen = len - start, bad = 0;
199  12 char chr1;
200  12 char chr2;
201  12 boolean agap;
202  109 for (int i = start; i < len; i++)
203    {
204  97 chr1 = seq1.charAt(i);
205   
206  97 chr2 = seq2.charAt(i);
207  97 agap = isGap(chr1) || isGap(chr2);
208  97 if ('a' <= chr1 && chr1 <= 'z')
209    {
210    // TO UPPERCASE !!!
211    // Faster than toUpperCase
212  35 chr1 -= caseShift;
213    }
214  97 if ('a' <= chr2 && chr2 <= 'z')
215    {
216    // TO UPPERCASE !!!
217    // Faster than toUpperCase
218  48 chr2 -= caseShift;
219    }
220   
221  97 if (chr1 != chr2)
222    {
223  30 if (agap)
224    {
225  18 if (ungappedOnly)
226    {
227  4 elen--;
228    }
229  14 else if (!wcGaps)
230    {
231  2 bad++;
232    }
233    }
234    else
235    {
236  12 bad++;
237    }
238    }
239   
240    }
241  12 if (elen < 1)
242    {
243  0 return 0f;
244    }
245  12 return ((float) 100 * (elen - bad)) / elen;
246    }
247   
248    /**
249    * Answers true if the supplied character is a recognised gap character, else
250    * false. Currently hard-coded to recognise '-', '-' or ' ' (hyphen / dot /
251    * space).
252    *
253    * @param c
254    *
255    * @return
256    */
 
257  7280113 toggle public static final boolean isGap(char c)
258    {
259  7308710 return (c == GAP_DASH || c == GAP_DOT || c == GAP_SPACE) ? true : false;
260    }
261   
262    /**
263    * Overloaded method signature to test whether a single sequence is nucleotide
264    * (that is, more than 85% CGTA)
265    *
266    * @param seq
267    * @return
268    */
 
269  562 toggle public static final boolean isNucleotide(SequenceI seq)
270    {
271  562 return isNucleotide(new SequenceI[] { seq });
272    }
273   
274    /**
275    * Answers true if more than 85% of the sequence residues (ignoring gaps) are
276    * A, G, C, T or U, else false. This is just a heuristic guess and may give a
277    * wrong answer (as AGCT are also amino acid codes).
278    *
279    * @param seqs
280    * @return
281    */
 
282  1802 toggle public static final boolean isNucleotide(SequenceI[] seqs)
283    {
284  1802 if (seqs == null)
285    {
286  1 return false;
287    }
288   
289  1801 int ntCount = 0;
290  1801 int aaCount = 0;
291  1801 for (SequenceI seq : seqs)
292    {
293  30238 if (seq == null)
294    {
295  1 continue;
296    }
297    // TODO could possibly make an informed guess just from the first sequence
298    // to save a lengthy calculation
299  30237 int len = seq.getLength();
300  1836832 for (int i = 0; i < len; i++)
301    {
302  1806595 char c = seq.getCharAt(i);
303  1806595 if (isNucleotide(c))
304    {
305  483609 ntCount++;
306    }
307  1322986 else if (!isGap(c))
308    {
309  1120053 aaCount++;
310    }
311    }
312    }
313   
314    /*
315    * Check for nucleotide count > 85% of total count (in a form that evades
316    * int / float conversion or divide by zero).
317    */
318  1801 if (ntCount * 100 > EIGHTY_FIVE * (ntCount + aaCount))
319    {
320  319 return true;
321    }
322    else
323    {
324  1482 return false;
325    }
326   
327    }
328   
329    /**
330    * Answers true if the character is one of aAcCgGtTuU
331    *
332    * @param c
333    * @return
334    */
 
335  2858241 toggle public static boolean isNucleotide(char c)
336    {
337  2857442 if ('a' <= c && c <= 'z')
338    {
339  126624 c -= TO_UPPER_CASE;
340    }
341   
342  2857671 switch (c)
343    {
344  256294 case 'A':
345  120034 case 'C':
346  188709 case 'G':
347  181199 case 'T':
348  12429 case 'U':
349  758165 return true;
350    }
351  2104408 return false;
352    }
353   
354    /**
355    * Answers true if every character in the string is one of aAcCgGtTuU, or
356    * (optionally) a gap character (dot, dash, space), else false
357    *
358    * @param s
359    * @param allowGaps
360    * @return
361    */
 
362  21 toggle public static boolean isNucleotideSequence(String s, boolean allowGaps)
363    {
364  21 if (s == null)
365    {
366  1 return false;
367    }
368  83 for (int i = 0; i < s.length(); i++)
369    {
370  69 char c = s.charAt(i);
371  69 if (!isNucleotide(c))
372    {
373  11 if (!allowGaps || !isGap(c))
374    {
375  6 return false;
376    }
377    }
378    }
379  14 return true;
380    }
381   
382    /**
383    * Convenience overload of isNucleotide
384    *
385    * @param seqs
386    * @return
387    */
 
388  18 toggle public static boolean isNucleotide(SequenceI[][] seqs)
389    {
390  18 if (seqs == null)
391    {
392  1 return false;
393    }
394  17 List<SequenceI> flattened = new ArrayList<SequenceI>();
395  17 for (SequenceI[] ss : seqs)
396    {
397  27 for (SequenceI s : ss)
398    {
399  54 flattened.add(s);
400    }
401    }
402  17 final SequenceI[] oneDArray = flattened
403    .toArray(new SequenceI[flattened.size()]);
404  17 return isNucleotide(oneDArray);
405    }
406   
407    /**
408    * Compares two residues either case sensitively or case insensitively
409    * depending on the caseSensitive flag
410    *
411    * @param c1
412    * first char
413    * @param c2
414    * second char to compare with
415    * @param caseSensitive
416    * if true comparison will be case sensitive otherwise its not
417    * @return
418    */
 
419  27115 toggle public static boolean isSameResidue(char c1, char c2,
420    boolean caseSensitive)
421    {
422  27115 if (caseSensitive)
423    {
424  3 return (c1 == c2);
425    }
426    else
427    {
428  27112 return Character.toUpperCase(c1) == Character.toUpperCase(c2);
429    }
430    }
431    }