use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class BedGraphDataWriter method run.
public void run() {
/*
* We need to sort the chromosomes alphabetically rather than by their
* natural order
*/
Chromosome[] chrs = SeqMonkApplication.getInstance().dataCollection().genome().getAllChromosomes();
Arrays.sort(chrs, new Comparator<Chromosome>() {
public int compare(Chromosome o1, Chromosome o2) {
return (o1.name().compareTo(o2.name()));
}
});
PrintWriter pr = null;
for (int i = 0; i < data.length; i++) {
try {
pr = new PrintWriter(new BufferedWriter(new FileWriter(files[i])));
} catch (IOException ex) {
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressExceptionReceived(ex);
}
pr.close();
return;
}
try {
for (int c = 0; c < chrs.length; c++) {
// Tell the listeners how far we've got
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressUpdated("Saving BedGraph Files", (i * chrs.length) + c, chrs.length * data.length);
}
Probe[] probes = list.getProbesForChromosome(chrs[c]);
for (int p = 0; p < probes.length; p++) {
// See if we need to bail out early
if (cancel) {
pr.close();
files[i].delete();
Enumeration<ProgressListener> el = listeners.elements();
while (el.hasMoreElements()) {
el.nextElement().progressCancelled();
}
return;
}
pr.println("chr" + probes[p].chromosome().name() + "\t" + probes[p].start() + "\t" + probes[p].end() + "\t" + data[i].getValueForProbe(probes[p]));
}
}
} catch (SeqMonkException ex) {
pr.close();
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressExceptionReceived(ex);
}
return;
}
pr.close();
}
// Write out the chromsome sizes file.
try {
pr = new PrintWriter(new BufferedWriter(new FileWriter(chrSizeFile)));
for (int c = 0; c < chrs.length; c++) {
pr.println("chr" + chrs[c].name() + "\t" + chrs[c].length());
}
pr.close();
} catch (IOException ex) {
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressExceptionReceived(ex);
}
pr.close();
return;
}
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressComplete("save_bedgraph", null);
}
}
Aggregations