-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastaFileConsole.java
More file actions
91 lines (78 loc) · 3.1 KB
/
FastaFileConsole.java
File metadata and controls
91 lines (78 loc) · 3.1 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
package biomarker1;
/*
a Fastfile class for reading
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class FastaFileConsole { //a fasta file reader
ArrayList<Secuencia> secuencias;
public void use(String path) {
ArchivoFasta af = new ArchivoFasta();
this.secuencias = af.AbrirArchivo(path); //stablish the path for file read
int t = af.Tamaño();
System.out.println("Número de secuencias: " + t);
}
public ArrayList armaSeqs() {
ArrayList seqs = new ArrayList();
for (int x = 0; x < secuencias.size(); x++) {
seqs.add((String) secuencias.get(x)._secuencia); //build an arrayList with the sequences
}
return seqs;
}
public ArrayList armaNames() { //build an arrayList with the sequences names
ArrayList names = new ArrayList();
for (int x = 0; x < secuencias.size(); x++) {
names.add((String) secuencias.get(x)._nombre);
}
return names;
}
public static class Secuencia {
public String _nombre;
public String _secuencia;
}
public static class ArchivoFasta {
public ArrayList<Secuencia> secuencias;
public ArrayList<Secuencia> AbrirArchivo(String ruta) {
secuencias = new ArrayList<Secuencia>();
int numero = 1;
try {
BufferedReader in = new BufferedReader(new FileReader(ruta));
String line = null;
while ((line = in.readLine()) != null) { //read the file while there is still a line left
int n = line.length();
if (n > 0) {
boolean i = line.startsWith(">");
if (i) {
Secuencia _s = new Secuencia();
_s._nombre = line;
secuencias.add(_s);
} else {
if (secuencias.get(secuencias.size() - 1)._secuencia == null) {
secuencias.get(secuencias.size() - 1)._secuencia = line;
} else {
secuencias.get(secuencias.size() - 1)._secuencia += line;
}
}
}
}
in.close();
} catch (IOException ex) {
System.out.println(numero);
System.out.println(ex.getMessage());
}
return secuencias;
}
public Secuencia BuscarCadena(int n) {
if (n < Tamaño()) { //returns a given sequence with its index number
return secuencias.get(n);
} else {
return null;
}
}
public int Tamaño() {
return secuencias.size();
}
}
}