| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package uk.ac.babraham.FastQC.Sequence.Contaminant; |
| 21 |
|
| 22 |
public class Contaminant { |
| 23 |
|
| 24 |
private String name; |
| 25 |
private char [] forward; |
| 26 |
private char [] reverse; |
| 27 |
|
| 28 |
public Contaminant (String name, String sequence) { |
| 29 |
this.name = name; |
| 30 |
|
| 31 |
sequence = sequence.toUpperCase(); |
| 32 |
forward = sequence.toCharArray(); |
| 33 |
reverse = new char[forward.length]; |
| 34 |
for (int c=0;c<forward.length;c++) { |
| 35 |
int revPos = (reverse.length-1)-c; |
| 36 |
switch (forward[c]) { |
| 37 |
case 'G': |
| 38 |
reverse[revPos] = 'C'; |
| 39 |
break; |
| 40 |
case 'A': |
| 41 |
reverse[revPos] = 'T'; |
| 42 |
break; |
| 43 |
case 'T': |
| 44 |
reverse[revPos] = 'A'; |
| 45 |
break; |
| 46 |
case 'C': |
| 47 |
reverse[revPos] = 'G'; |
| 48 |
break; |
| 49 |
default: |
| 50 |
throw new IllegalArgumentException("Contaminant contained the illegal character '"+forward[c]+"'"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
public ContaminantHit findMatch (String query) { |
| 57 |
query = query.toUpperCase(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
if (query.length()<20 && query.length()>=8) { |
| 62 |
if ((new String(forward)).contains(query)) { |
| 63 |
return new ContaminantHit(this, ContaminantHit.FORWARD, query.length(), 100); |
| 64 |
} |
| 65 |
if ((new String(reverse)).contains(query)) { |
| 66 |
return new ContaminantHit(this, ContaminantHit.REVERSE, query.length(), 100); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
char [] q = query.toCharArray(); |
| 73 |
|
| 74 |
ContaminantHit bestHit = null; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
for (int offset=0-(forward.length-20);offset<q.length-20;offset++) { |
| 80 |
ContaminantHit thisHit = findMatch(forward,q,offset,ContaminantHit.FORWARD); |
| 81 |
|
| 82 |
if (thisHit == null) continue; |
| 83 |
if (bestHit == null || thisHit.length()>bestHit.length()) { |
| 84 |
bestHit = thisHit; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
for (int offset=0-(forward.length-20);offset<q.length-20;offset++) { |
| 89 |
ContaminantHit thisHit = findMatch(reverse,q,offset,ContaminantHit.REVERSE); |
| 90 |
if (thisHit == null) continue; |
| 91 |
if (bestHit == null || thisHit.length()>bestHit.length()) { |
| 92 |
bestHit = thisHit; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return bestHit; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
private ContaminantHit findMatch (char [] ca, char [] cb, int offset, int direction) { |
| 101 |
|
| 102 |
ContaminantHit bestHit = null; |
| 103 |
|
| 104 |
int mismatchCount = 0; |
| 105 |
int start = 0; |
| 106 |
int end = 0; |
| 107 |
|
| 108 |
for (int i=0;i<ca.length;i++) { |
| 109 |
if (i+offset < 0) { |
| 110 |
start=i+1; |
| 111 |
continue; |
| 112 |
} |
| 113 |
if (i+offset >= cb.length) break; |
| 114 |
|
| 115 |
if (ca[i] == cb[i+offset]) { |
| 116 |
end = i; |
| 117 |
} |
| 118 |
else { |
| 119 |
++mismatchCount; |
| 120 |
if (mismatchCount>1) { |
| 121 |
|
| 122 |
if (1+(end-start) > 20) { |
| 123 |
int id = (((1+(end-start))-(mismatchCount-1))*100)/(1+(end-start)); |
| 124 |
if (bestHit == null || bestHit.length()< 1+(end-start) || (bestHit.length() == 1+(end-start) && bestHit.percentID()<id)) { |
| 125 |
|
| 126 |
bestHit = new ContaminantHit(this, direction, 1+(end-start), id); |
| 127 |
} |
| 128 |
} |
| 129 |
start = i+1; |
| 130 |
end = i+1; |
| 131 |
mismatchCount = 0; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (1+(end-start) > 20) { |
| 138 |
int id = (((1+(end-start))-mismatchCount)*100)/(1+(end-start)); |
| 139 |
if (bestHit == null || bestHit.length()< 1+(end-start) || (bestHit.length() == 1+(end-start) && bestHit.percentID()<id)) { |
| 140 |
|
| 141 |
bestHit = new ContaminantHit(this, direction, 1+(end-start), id); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return bestHit; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
public String name () { |
| 150 |
return name; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
} |