-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastaSequence.java
More file actions
134 lines (121 loc) · 3.14 KB
/
Copy pathFastaSequence.java
File metadata and controls
134 lines (121 loc) · 3.14 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
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
import java.util.List;
import java.util.Random;
import java.util.HashSet;
import java.util.Set;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.TreeMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
public class FastaSequence
{
private String header;
private String sequence;
public FastaSequence(String header, String sequence)
{
this.header = header;
this.sequence = sequence;
}
public String getHeader()
{
return this.header;
}
public String getSequence()
{
String tempString = sequence.toString();
return(tempString);
}
@Override
public String toString()
{
return(this.header + "\n" + this.sequence + "\n");
}
public float getGCRatio()
{
int counter = 0;
for (char base : sequence.toCharArray())
{
if ((base == 'G')||(base == 'C'))
{
counter++;
}
}
float counterF = counter;
float lengthF = sequence.toString().length();
return(counterF/lengthF);
}
public int countBase(char queryBase)
{
int counter = 0;
for (char base : sequence.toCharArray())
{
if (base == queryBase)
{
counter++;
}
}
return(counter);
}
public static List<FastaSequence> readFastaFile(String filename) throws Exception
{
//Set up variables needed for parsing
BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
List<FastaSequence> list = new ArrayList<FastaSequence>();
String nextLine = reader.readLine();
//temp var to speed up string concatenation
StringBuffer buffer = new StringBuffer();
while (nextLine != null)
{
FastaSequence fs = new FastaSequence("", "");
list.add(fs);
fs.header = nextLine;
nextLine = reader.readLine();
while ((nextLine != null) && (!nextLine.startsWith(">")))
{
//fs.sequence.append(nextLine);
buffer.append(nextLine);
nextLine = reader.readLine();
}
fs.sequence = buffer.toString();
buffer.setLength(0);
}
reader.close();
return(list);
}
public static void writeTableSummary( List<FastaSequence> list, File outputFile) throws Exception
{
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
//so i dont have to type "\t" so often
String tab = "\t";
//write header
writer.write("Sequence ID" + tab + "numA" + tab + "numC" + tab + "numG" + tab + "numT" + "\n" );
for (FastaSequence fs : list)
{
writer.write(fs.getHeader() + tab);
writer.write(fs.countBase('A') + tab);
writer.write(fs.countBase('C') + tab);
writer.write(fs.countBase('G') + tab);
writer.write(fs.countBase('T') + tab);
writer.write(fs.getSequence() + "\n");
}
}
public static void main(String[] args) throws Exception
{
List<FastaSequence> fastaList = FastaSequence.readFastaFile("Testing.fasta");
for( FastaSequence fs : fastaList)
{
System.out.println(fs.getHeader());
System.out.println(fs.getSequence());
System.out.println(fs.getGCRatio());
}
File myFile = new File("Testing_Output.txt");
writeTableSummary( fastaList, myFile);
}
}