forked from ctSkennerton/minced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFASTAReader.java
160 lines (139 loc) · 3.8 KB
/
FASTAReader.java
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
import java.io.*;
import java.util.*;
public class FASTAReader
{
private String fileName;
private String sequence;
private String name;
private String desc;
private String newHeader;
private int readNum;
private BufferedReader inputFile;
public FASTAReader(String f)
{
fileName = f;
readNum = 0;
sequence = "";
name = "";
desc = "";
newHeader = "";
}
//does not check that all characters are ACGT
public boolean isFASTA()
{
try
{
BufferedReader testFile = new BufferedReader(new FileReader(fileName));
String firstLine = testFile.readLine();
while (firstLine.length() == 0)
{
firstLine = testFile.readLine();
}
if (firstLine == null)
{
testFile.close();
return false;
}
if (firstLine.charAt(0) != '>')
{
testFile.close();
return false;
}
String secondLine = testFile.readLine();
if (secondLine == null)
{
testFile.close();
return false;
}
testFile.close();
} catch (Exception e) { e.printStackTrace(); }
return true;
}
public boolean read()
{
try
{
if ( readNum == 0 )
inputFile = new BufferedReader(new FileReader(fileName));
readNum++;
sequence = "";
StringBuffer sb = new StringBuffer();
while( true )
{
String currLine = inputFile.readLine();
// Process informative line
if ( currLine == null )
{
if ( sb.toString().length() > 0 )
{
// Last sequence
sequence = sb.toString();
return true;
}
else
{
// No more sequences
inputFile.close();
return false;
}
}
else
{
// Remove leading and trailing spaces
currLine = currLine.trim();
if ( currLine.length() == 0 )
{
// Skip empty lines
continue;
}
else if ( currLine.charAt(0) == '>' )
{
// Prepare new sequence
newHeader = currLine;
// Finish previous sequence
if ( sb.toString().length() > 0 )
{
sequence = sb.toString();
return true;
}
}
else
{
// Continuation of a sequence
sb.append(currLine);
if ( newHeader.length() > 0 )
{
String[] array = newHeader.substring(1, newHeader.length()).split("\\s", 2);
if ( array.length >= 1 )
name = array[0];
if ( array.length >= 2 )
desc = array[1];
newHeader = "";
}
}
}
}
} catch (Exception e) { e.printStackTrace(); }
return true;
}
public String getSequence()
{
return sequence;
}
public String getHeader()
{
return name + ' ' + desc;
}
public String getName()
{
return name;
}
public String getDesc()
{
return desc;
}
public int getNum()
{
return readNum;
}
}