Debian Codegraph @

uk/ac/babraham/FastQC/Sequence/Contaminant/Contaminant.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // We have a special case for queries between 8 - 20bp where we will allow a hit
60 // if it's an exact substring of this contaminant
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 // We're going to allow only one mismatch and will require
77 // a match of at least 20bp to consider this a match at all
78
79 for (int offset=0-(forward.length-20);offset<q.length-20;offset++) {
80 ContaminantHit thisHit = findMatch(forward,q,offset,ContaminantHit.FORWARD);
81 // System.out.println("Best match from offset "+offset+" was "+thisHit);
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 // That's the end of this match, see if it's worth recording
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 // System.out.println("New best hit from "+start+"-"+end);
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 // See if we ended with a match.
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 // System.out.println("New best hit from "+start+"-"+end);
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 }