package versusSNP.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import versusSNP.genome.Genome;
import versusSNP.genome.ORF;
import versusSNP.genome.Sequence;
import versusSNP.gui.UICaption;
import versusSNP.gui.widgets.ProgressBar;
public class FastaFile implements FileIO, Runnable {
public static final int LINE_CHARS = 60;
private String path;
private Genome genome;
private ArrayList<Sequence> sequences;
private ProgressBar progressBar;
public FastaFile() {
super();
sequences = new ArrayList<Sequence>();
}
public FastaFile(String path, Genome genome) {
this();
this.path = path;
this.genome = genome;
}
public FastaFile(String path, Genome genome, ProgressBar progressBar) {
this(path, genome);
this.progressBar = progressBar;
}
public ArrayList<Sequence> getSequences() {
return sequences;
}
@Override
public boolean readFile(String path, Genome genome) {
String name = null;
StringBuffer seq = new StringBuffer();
String line = null;
try {
FileInputStream fis = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((line = br.readLine())!=null) {
if (line.length() != 0 && line.charAt(0)=='>') {
if (name != null && !name.equals("")) {
if (seq.length()!=0)
sequences.add(new Sequence(name, seq.toString()));
}
name = line.substring(1);
seq.delete(0, seq.length());
}
else {
appendSequence(seq, line);
}
}
//add the last sequence in the file
if (name != null && !name.equals("")) {
if (seq.length()!=0)
sequences.add(new Sequence(name, seq.toString()));
}
if (sequences.size()==0) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_fasta_contains_no_sequence, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return false;
}
br.close();
fis.close();
return true;
}
catch (IOException e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_exception_file_io_fasta, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return false;
}
}
public static boolean writeFile(String path, Genome genome) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(new File(path)))));
for (Iterator<ORF> iter = genome.getOrfList().iterator(); iter.hasNext();) {
ORF orf = iter.next();
if (orf.getSequence() == null) continue;
out.println(">" + orf.getName());
intoLines(orf.getSequence().getSequence(), out);
}
out.close();
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_error_file_not_writable, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
return false;
}
}
protected void appendSequence(StringBuffer seq, String line) {
for (int i = 0; i != line.length(); ++i) {
char ch = line.charAt(i);
if (Character.isLetter(ch))
seq.append(ch);
}
}
private static void intoLines(String sequence, PrintWriter out) {
int length = sequence.length();
int i;
for (i = 0; i < length - LINE_CHARS; i += LINE_CHARS) {
out.println(sequence.substring(i, i+LINE_CHARS));
}
out.println(sequence.substring(i, length));
}
@Override
public void run() {
boolean createProgressBar = false;
if (progressBar == null)
createProgressBar = true;
if (createProgressBar)
progressBar = new ProgressBar(UICaption.progress_caption_load_fasta, UICaption.progress_label_load_fasta, true);
progressBar.setVisible(true);
if (readFile(path, genome)) {
genome.attachSequences(sequences);
}
if (createProgressBar)
progressBar.dispose();
}
}