-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileConverter.java
More file actions
86 lines (72 loc) · 1.84 KB
/
FileConverter.java
File metadata and controls
86 lines (72 loc) · 1.84 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
package dna;
import java.io.*;
import java.util.*;
public class FileConverter
{
private File fastq;
private File fasta;
public FileConverter(File fq, File fa) {
this.fastq = fq;
this.fasta = fa;
}
//
// Writes a fasta file consisting of conversion of all records from the fastq with
// sufficient quality, valid formatting and unique defline.
//
public void convert() throws IOException
{
// Build chain of readers.
FileReader fr = new FileReader(this.fastq);
BufferedReader br = new BufferedReader(fr);
FastqReader fqr = new FastqReader(br);
// Build chain of writers.
FileWriter fw = new FileWriter(this.fasta);
PrintWriter pw = new PrintWriter(fw);
FastaWriter faw = new FastaWriter(pw);
// While loop that reads fastq files until it reports null, only writes valid fasta files
boolean done = false;
while(!done) {
try {
FastqRecord rec = fqr.readRecord();
if(rec == null) {
done = true;
}
else if (!rec.qualityIsLow()){
FastaRecord fa = new FastaRecord(rec);
faw.writeRecord(fa);
}
}
catch(RecordFormatException x) {
System.out.println(x.getMessage());
}
}
// Close fr, br, fw, and pw in reverse order of creation.
pw.close();
fw.close();
br.close();
fr.close();
}
public static void main(String[] args)
{
System.out.println("Starting");
try
{
System.out.println("Test 1");
File fastq = new File("data/HW4.fastq");
if (!fastq.exists())
{
System.out.println("Can't find input file " + fastq.getAbsolutePath());
System.exit(1);
}
File fasta = new File("data/HW4.fasta");
FileConverter converter = new FileConverter(fastq, fasta);
converter.convert();
System.out.println("Test 2");
}
catch (IOException x)
{
System.out.println(x.getMessage());
}
System.out.println("Done");
}
}