Repository navigation
Expand file tree
/
Copy pathDNAStringMatching.java
More file actions
290 lines (264 loc) · 9.03 KB
/
Copy pathDNAStringMatching.java
File metadata and controls
290 lines (264 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class DNAStringMatching {
private File enzimasRestriccion, busqueda;
private String adn;
private ArrayList<String[][]> tablasKMP;
private ArrayList<int[]> tablasKMPIndex;
private ArrayList<String[]> enzimas;
private ArrayList<Integer> coincidencias;
public ArrayList<String[]> getEnzima(){
return this.enzimas;
}
//Recibe los dos archivos
public void ingresaTexto() {
//Pedir path de archivo con enzimas de restricción
String possiblePath = "";
//Testing: String possiblePath = "C:\\Users\\Hector\\Documents\\Algoritmos\\ProyectoAlgoritmos\\EnzimasRestriccion.txt";
do {
possiblePath = JOptionPane.showInputDialog("Ingresa el path del archivo que contenga las enzimas de restricción a buscar");
if(possiblePath == null) {
System.exit(0);
}
} while(possiblePath.equals(""));
this.enzimasRestriccion = new File(possiblePath);
//Pedir path de archivo con ADN donde se va a buscar
possiblePath = "";
//Testing: possiblePath = "C:\\Users\\Hector\\Documents\\Algoritmos\\ProyectoAlgoritmos\\chlorella_vulgaris.dat";
do {
possiblePath = JOptionPane.showInputDialog("Ingresa el path del archivo que contenga el genoma donde se realizará la "
+ "busqueda");
if(possiblePath == null) {
System.exit(0);
}
} while(possiblePath.equals(""));
this.busqueda = new File(possiblePath);
}
//Procesar archivo de busqueda
public void procesarArchivo() {
System.out.println("Path ingresado: " + this.busqueda.getPath());
System.out.println("Eliminando espacios y caracteres basura");
this.adn = convertFile(this.busqueda);
System.out.println("...");
System.out.println("Texto con formato correcto");
System.out.println();
System.out.println();
}
//La entrada es el número de enzima a buscar
public void busquedaGeneral(int seleccion) {
this.buscarEnzima(this.adn, seleccion);
}
public String getAdn() {
return this.adn;
}
public ArrayList<Integer> getIndexes() {
return coincidencias;
}
//Para buscar una sola enzima
private void buscarEnzima(String adn, int seleccion) {
System.out.println("--------------------Comenzando busqueda--------------------");
System.out.println("Buscando para " + this.enzimas.get(seleccion)[0]);
System.out.println("...");
int coincidencias = this.fuerzaBruta(adn, this.enzimas.get(seleccion)[1], seleccion);
System.out.println("Se encontraron " + coincidencias + " coindidencias de los caracteres " + this.enzimas.get(seleccion)[1]);
System.out.println();
System.out.println("Busqueda finalizada");
System.out.println();
System.out.println();
}
//Para buscar 1 o más enzimas
private void buscarEnzimas(String adn, int seleccion) {
System.out.println("Comenzando busqueda");
System.out.println();
for(int nEnzimas = 0; nEnzimas < this.enzimas.size(); nEnzimas++) {
String[] enzimaActual = this.enzimas.get(nEnzimas);
System.out.println("Buscando para " + enzimaActual[0]);
System.out.println("...");
int coincidencias = this.fuerzaBruta(adn, enzimaActual[1], seleccion);
System.out.println("Se encontraron " + coincidencias + " coindidencias de los caracteres " + enzimaActual[1]);
System.out.println();
}
System.out.println("Busqueda finalizada");
}
//Alamecna las enzimas de restriccion
private ArrayList<String[]> almacenarEnzimas(File archivo) {
ArrayList<String[]> enzimas = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(archivo.toPath())) {
String line = null;
while ((line = reader.readLine()) != null) {
String[] enzima = new String[2];
int indice = 0;
String nombreEnzima = "";
while(line.charAt(indice) != ' ') {
nombreEnzima += line.charAt(indice);
indice++;
}
enzima[0] = nombreEnzima;
String dataEnzima = "";
indice++;
while(indice < line.length()) {
dataEnzima += line.charAt(indice);
indice++;
}
enzima[1] = dataEnzima.toLowerCase();
enzimas.add(enzima);
}
} catch (IOException x) {
//throw new IOException("El archivo " + archivo.getPath() + " no se pudo leer");
}
return enzimas;
}
private int fuerzaBruta(String strTexto, String strPatron, int enzima) {
this.coincidencias = new ArrayList<Integer>();
int busquedaActual = 0;
int coincidencias = 0;
char[] texto = strTexto.toCharArray();
char[] patron = strPatron.toCharArray();
int endPos = texto.length - patron.length+1;
OUTER:
for(int pos = 0; pos < endPos; pos++) {
for(int i = 0; i < patron.length; i++) {
if(texto[pos + i] != patron[busquedaActual]) {
pos += (busquedaActual == 0) ? busquedaActual : busquedaActual - 1;
busquedaActual = this.tablasKMPIndex.get(enzima)[busquedaActual];
continue OUTER;
}
busquedaActual++;
}
System.out.println("Coincidencia en la posición: " + pos);
this.coincidencias.add(pos);
coincidencias++;
pos += busquedaActual - 1;
busquedaActual = 0;
}
return coincidencias;
}
//Procesar archivo de busqueda
private String convertFile(File archivo) {
BufferedReader lector;
StringBuilder builder = new StringBuilder();
String ultimo = null;
try {
lector = new BufferedReader(new FileReader(archivo));
String linea = lector.readLine();
while (linea != null) {
int blanks = 0;
char[] line = linea.toCharArray();
for (int i = 10; i < line.length; i++) {
if (Character.isWhitespace(line[i])) {
blanks++;
}
}
char[] newLine = new char[line.length - (10 + blanks)];
int cont = 0;
for (int i = 10; i < line.length; i++) {
if (!Character.isWhitespace(line[i])) {
newLine[cont] = line[i];
cont++;
}
}
builder.append(new String(newLine));
linea = lector.readLine();
if (linea.length() == 0) {
break;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ultimo = builder.toString();
return ultimo;
}
//Crea las tablas pertinentes y almacena la información de las enzimas
public void crearTablas() {
this.tablasKMP = new ArrayList<>();
this.tablasKMPIndex = new ArrayList<>();
//Almacenar las enzimas en un arreglo con nombre y secuencia
this.enzimas = this.almacenarEnzimas(this.enzimasRestriccion);
//Crea la tabla KMP para la enzima
this.hacerMultiplesTablas();
}
private void hacerUnicaTabla(int seleccion) {
String[] enzimaActual = this.enzimas.get(seleccion);
System.out.println("Creando tabla KMP para enzima " + enzimaActual[0]);
System.out.println("Cadena: " + enzimaActual[1]);
System.out.println();
this.llenarTablas(enzimaActual[1]);
this.imprimirKMP(this.tablasKMP.get(seleccion), seleccion);
System.out.println("Tabla creada");
System.out.println();
System.out.println();
}
private void hacerMultiplesTablas() {
for(String[] enz: this.enzimas) {
System.out.println("Enzima: " + enz[0]);
}
System.out.println();
for(int largo = 0; largo < this.enzimas.size(); largo++) {
String[] enzimaActual = this.enzimas.get(largo);
System.out.println("Creando tabla KMP para enzima " + enzimaActual[0]);
System.out.println("Cadena: " + enzimaActual[1]);
System.out.println();
this.llenarTablas(enzimaActual[1]);
this.imprimirKMP(this.tablasKMP.get(largo), largo);
System.out.println("Tabla creada");
System.out.println();
System.out.println();
}
}
private void llenarTablas(String patron) {
String[][] tablaKMP = new String[3][patron.length()+1];
int[] kmpTableIndex = new int[patron.length()+1];
for(int charAt = 0; charAt < patron.length()+1; charAt++) {
tablaKMP[0][charAt] = patron.substring(0, charAt);
kmpTableIndex[charAt] = this.coincidenCaracteres(tablaKMP[0][charAt]);
tablaKMP[1][charAt] = patron.substring(0, kmpTableIndex[charAt]);
}
this.tablasKMP.add(tablaKMP);
this.tablasKMPIndex.add(kmpTableIndex);
}
//Analiza si en un patron coinciden los caracteres
private int coincidenCaracteres(String patron) {
int coincidencias = 0;
int largo = patron.length();
int prefijoPosible = largo-1;
int index = largo-2;
while(index >= 0) {
if(coincidencias > 0 && patron.charAt(index) != patron.charAt(prefijoPosible)) {
index += coincidencias-1;
coincidencias = 0;
prefijoPosible = largo-1;
}
char h1 = patron.charAt(index);
char h2 = patron.charAt(prefijoPosible);
if(h1 == h2) {
coincidencias++;
prefijoPosible--;
}
index--;
}
return coincidencias;
}
private void imprimirKMP(String[][] tablaKMP, int nTablasIndex) {
System.out.println("Imprimiendo tabla: ");
System.out.println();
for(int i = 0; i < tablaKMP[0].length; i++) {
System.out.print(i + " ");
System.out.print(tablaKMP[0][i] + " ");
System.out.print(tablaKMP[1][i] + " ");
System.out.print(this.tablasKMPIndex.get(nTablasIndex)[i]);
System.out.println();
}
System.out.println();
}
}