Search in sources :

Example 91 with SeqMonkException

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);
    }
}
Also used : Enumeration(java.util.Enumeration) FileWriter(java.io.FileWriter) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) IOException(java.io.IOException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) BufferedWriter(java.io.BufferedWriter) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) PrintWriter(java.io.PrintWriter)

Aggregations

SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)91 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)49 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)30 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)22 Vector (java.util.Vector)21 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)20 File (java.io.File)19 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)17 BufferedReader (java.io.BufferedReader)16 FileReader (java.io.FileReader)16 ChromosomeWithOffset (uk.ac.babraham.SeqMonk.Utilities.ChromosomeWithOffset)14 PairedDataSet (uk.ac.babraham.SeqMonk.DataTypes.PairedDataSet)13 FileInputStream (java.io.FileInputStream)11 IOException (java.io.IOException)11 InputStreamReader (java.io.InputStreamReader)11 GZIPInputStream (java.util.zip.GZIPInputStream)11 HiCDataStore (uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore)8 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)8 FileNotFoundException (java.io.FileNotFoundException)7 SequenceReadWithChromosome (uk.ac.babraham.SeqMonk.DataTypes.Sequence.SequenceReadWithChromosome)7