Search in sources :

Example 6 with ProgressListener

use of uk.ac.babraham.SeqMonk.DataTypes.ProgressListener in project SeqMonk by s-andrews.

the class RScriptRunner method run.

public void run() {
    try {
        // For some reason if we call scriptFile.getAbsolutePath when we launch our R
        // process then on OSX we get wierd problems with line endings being stuck where
        // we have spaces in our cache path.  Since we've already set the working directory
        // for the script we don't need this anyway and we can just use script.r as the
        // path to the script.
        // System.err.println("Creating process");
        ProcessBuilder process = new ProcessBuilder(SeqMonkPreferences.getInstance().RLocation(), "CMD", "BATCH", "script.r");
        // System.err.println("Setting process directory to "+directory.getAbsolutePath());
        process.directory(directory);
        // System.err.println("Starting process");
        Process p = process.start();
        int lastLinesOfOutput = 0;
        long lastFileSize = 0;
        boolean finishNextTime = false;
        while (true) {
            // We go around checking to see if the script has finished
            try {
                // int exit = p.exitValue();
                p.exitValue();
                // We don't exit immediately because we want to show the final
                // output in the output file.
                // System.err.println("Got valid exit state of "+exit);
                Thread.sleep(500);
                finishNextTime = true;
            } catch (IllegalThreadStateException itse) {
            // It's not finished yet.
            // System.err.println("Illegal thread state "+itse.getLocalizedMessage());
            }
            if (cancel) {
                // System.err.println("Cancelling");
                p.destroy();
                Enumeration<ProgressListener> en = listeners.elements();
                while (en.hasMoreElements()) {
                    en.nextElement().progressCancelled();
                }
                // Remove the files we'd made to this point.
                cleanUp();
                return;
            }
            // Read the output file and see if there's any new data
            File outputFile = new File(scriptFile.getAbsolutePath() + ".Rout");
            // System.err.println("Checking for output file "+outputFile.getAbsolutePath());
            if (outputFile.exists()) {
                // System.err.println("Outfile exists");
                if (outputFile.length() > lastFileSize) {
                    // System.err.println("Outfile is bigger than last time");
                    lastFileSize = outputFile.length();
                    // System.err.println("Reading outfile");
                    BufferedReader br = new BufferedReader(new FileReader(outputFile));
                    // System.err.println("Skipping first "+lastLinesOfOutput+" lines");
                    for (int i = 0; i <= lastLinesOfOutput; i++) {
                        br.readLine();
                    }
                    String line;
                    while ((line = br.readLine()) != null) {
                        // Don't keep streaming output if we're cancelling anyway
                        if (cancel)
                            break;
                        ++lastLinesOfOutput;
                        Enumeration<ProgressListener> en = listeners.elements();
                        // System.err.println("Sent new output line "+line);
                        while (en.hasMoreElements()) {
                            en.nextElement().progressUpdated(line, 0, 1);
                            // Make the output scroll by at a sensible rate
                            Thread.sleep(50);
                        }
                    }
                    br.close();
                }
                // Don't poll too often
                Thread.sleep(1000);
            }
            if (finishNextTime)
                break;
        }
        if (p.exitValue() != 0) {
            StringBuffer sb = new StringBuffer();
            File outputFile = new File(scriptFile.getAbsolutePath() + ".Rout");
            if (outputFile.exists()) {
                BufferedReader br = new BufferedReader(new FileReader(outputFile));
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("\n");
                }
                br.close();
            } else {
                sb.append("Found no Rout file at " + scriptFile.getAbsolutePath() + ".Rout");
            }
            throw new RException("R Script failed with exit " + p.exitValue(), sb.toString());
        }
        Enumeration<ProgressListener> en = listeners.elements();
        while (en.hasMoreElements()) {
            en.nextElement().progressComplete("r", directory);
            ;
        }
    } catch (Exception e) {
        Enumeration<ProgressListener> en = listeners.elements();
        while (en.hasMoreElements()) {
            en.nextElement().progressExceptionReceived(e);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) IOException(java.io.IOException) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 7 with ProgressListener

use of uk.ac.babraham.SeqMonk.DataTypes.ProgressListener in project SeqMonk by s-andrews.

the class SeqMonkInformationPanel method actionPerformed.

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("set_temp_dir")) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Select a Cache Directory");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setTempDirectory(chooser.getSelectedFile());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("set_genomes_dir")) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Select a Genomes Directory");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setGenomeBase(chooser.getSelectedFile());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("set_r")) {
        // First try to autodetect the location
        String autoDetect = RVersionTest.autoDetectRLocation();
        if (autoDetect != null) {
            int answer = JOptionPane.showConfirmDialog(this, "Found an R installation at '" + autoDetect + "' use this?", "R detected", JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                SeqMonkPreferences.getInstance().setRLocation(autoDetect);
                try {
                    SeqMonkPreferences.getInstance().savePreferences();
                    populatePanel();
                    return;
                } catch (IOException ioe) {
                    throw new IllegalStateException(ioe);
                }
            }
        }
        // If we can't auto-detect, or if they don't want to use that, then let them
        // choose where R is.
        JOptionPane.showMessageDialog(this, "Couldn't auto-detect an R installation.  Please manually select the location of the R executable");
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Find R executable");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setRLocation(chooser.getSelectedFile().getAbsolutePath());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("install_r")) {
        // R sucks
        // 
        // If the user has R installed as an admin user, but they're running as a normal
        // user then a non-interactive session (such as the ones we use here), won't
        // put up a prompt to create a local library.
        // 
        // To get around this we can create the local library folder within the R script,
        // which is great, except that R checks for the existence of this library at
        // the start of the session, so making it during the session still doesn't allow
        // us to install anything.  We therefore have to run a completely separate script
        // just to make the directory
        // Do the local library script first
        // Now do the actual package install
        Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    File tempDir = TempDirectory.createTempDirectory();
                    // Get the template script
                    Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/create_local_library.r"));
                    // Write the script file
                    File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
                    PrintWriter pr = new PrintWriter(scriptFile);
                    pr.print(template.toString());
                    pr.close();
                    RScriptRunner runner = new RScriptRunner(tempDir);
                    RProgressListener listener = new RProgressListener(runner);
                    runner.addProgressListener(new ProgressRecordDialog("R Session", runner));
                    runner.runScript();
                    while (true) {
                        if (listener.cancelled()) {
                            return;
                        }
                        if (listener.exceptionReceived()) {
                            return;
                        }
                        if (listener.complete())
                            break;
                        Thread.sleep(500);
                    }
                    runner.cleanUp();
                    File tempDir2 = TempDirectory.createTempDirectory();
                    // Get the template script
                    Template template2 = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/load_required_modules.r"));
                    // Write the script file
                    File scriptFile2 = new File(tempDir2.getAbsoluteFile() + "/script.r");
                    PrintWriter pr2 = new PrintWriter(scriptFile2);
                    pr2.print(template2.toString());
                    pr2.close();
                    RScriptRunner runner2 = new RScriptRunner(tempDir2);
                    RProgressListener listener2 = new RProgressListener(runner2);
                    runner2.addProgressListener(new ProgressRecordDialog("R Session", runner2));
                    runner2.runScript();
                    while (true) {
                        if (listener2.cancelled()) {
                            return;
                        }
                        if (listener2.exceptionReceived()) {
                            return;
                        }
                        if (listener2.complete())
                            break;
                        Thread.sleep(500);
                    }
                    runner2.cleanUp();
                    populatePanel();
                } catch (Exception ex) {
                    throw new IllegalStateException(ex);
                }
            }
        });
        t.start();
    } else if (e.getActionCommand().equals("clean_cache")) {
        Thread t = new Thread(new Runnable() {

            public void run() {
                ProgressDialog pd = new ProgressDialog("Cleaning the cache");
                File[] tempFiles = SeqMonkPreferences.getInstance().tempDirectory().listFiles();
                for (int f = 0; f < tempFiles.length; f++) {
                    pd.progressUpdated("Deleting file " + (f + 1) + " out of " + tempFiles.length, f, tempFiles.length);
                    if (Calendar.getInstance().getTimeInMillis() - tempFiles[f].lastModified() < 1000L * 60 * 60 * 12) {
                        // We only deal with things which are at least 12 hours old
                        continue;
                    }
                    // This might be a simple temp data file
                    if (tempFiles[f].isFile() && tempFiles[f].getName().startsWith("seqmonk") && tempFiles[f].getName().endsWith(".temp")) {
                        tempFiles[f].delete();
                    } else // This might be a temp directory
                    if (tempFiles[f].isDirectory() && tempFiles[f].getName().startsWith("seqmonk") && tempFiles[f].getName().contains("temp")) {
                        File[] files = tempFiles[f].listFiles();
                        for (int g = 0; g < files.length; g++) {
                            if (!files[g].delete()) {
                                throw new IllegalStateException(new IOException("Failed to delete " + files[g].getAbsolutePath()));
                            }
                        }
                        // Now remove the directory
                        tempFiles[f].delete();
                    }
                }
                pd.progressComplete("cache_clean", null);
                populatePanel();
            }
        });
        t.start();
    } else if (e.getActionCommand().equals("update_genomes")) {
        updateGenomesButton.setEnabled(false);
        GenomeUpgrader upgrader = new GenomeUpgrader();
        upgrader.addProgressListener(new ProgressDialog("Updating installed genomes"));
        upgrader.addProgressListener(new ProgressListener() {

            public void progressWarningReceived(Exception e) {
            }

            public void progressUpdated(String message, int current, int max) {
            }

            public void progressExceptionReceived(Exception e) {
            }

            public void progressComplete(String command, Object result) {
                updateGenomesButton.setVisible(false);
                updates = null;
                genomeUpdateLabel.setIcon(tickIcon);
                genomeUpdateLabelText.setText("All of your installed genomes are up to date");
            }

            public void progressCancelled() {
            }
        });
        upgrader.upgradeGenomes(updates);
    } else {
        throw new IllegalArgumentException("Didn't understand action '" + e.getActionCommand() + "'");
    }
}
Also used : GenomeUpgrader(uk.ac.babraham.SeqMonk.Network.GenomeUpgrader) IOException(java.io.IOException) ProgressDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressDialog.ProgressDialog) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) JFileChooser(javax.swing.JFileChooser) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 8 with ProgressListener

use of uk.ac.babraham.SeqMonk.DataTypes.ProgressListener in project SeqMonk by s-andrews.

the class HeatmapMatrix method run.

public void run() {
    // First calculate chromosome offsets so we can access the
    // relevant probes more quickly when we come to adding other
    // end data.
    Hashtable<String, Integer> offsets = new Hashtable<String, Integer>();
    offsets.put(probes[0].probe.chromosome().name(), 0);
    for (int i = 1; i < probes.length; i++) {
        if (probes[i].probe.chromosome() != probes[i - 1].probe.chromosome()) {
            offsets.put(probes[i].probe.chromosome().name(), i);
        }
    }
    // We also need an initial list of total counts for all of our probes
    // so we can do the O/E calculations.  We can incorporate into this
    // the ability to correct for linkage.
    int[] totalCisCounts = new int[probes.length];
    int[] totalTransCounts = new int[probes.length];
    getTotalCounts(totalCisCounts, totalTransCounts);
    if (cancel) {
        Enumeration<ProgressListener> en2 = listeners.elements();
        while (en2.hasMoreElements()) {
            en2.nextElement().progressCancelled();
        }
        return;
    }
    // We'll make up an ever expanding list of interactions which we
    // care about
    Vector<InteractionProbePair> filteredInteractions = new Vector<InteractionProbePair>();
    // We'll need to correct for the number of tests we perform, but we're also able
    // to skip a load of tests based on the initial filters we supplied (non-statistical ones)
    // so we're going to keep a record of how many valid tests we actually need to correct for.
    // 
    // The worse case scenario is that we do every possible comparison (but only one way round).
    // After some testing this proved to be a bad idea.  We skipped so many tests where the
    // interaction wasn't observed that we ended up hugely under-correcting our final p-values
    // and making a load of false positive predictions.  We can do this more correctly later,
    // but for now we're either going to correct for every possible test, or for just every
    // possible cis test if we're excluding trans hits.
    long numberOfTestsPerformed = 0;
    if (initialMaxDistance > 0) {
        // We're just counting cis interactions
        int currentChrCount = 1;
        Chromosome currentChr = probes[0].probe.chromosome();
        for (int p = 1; p < probes.length; p++) {
            if (probes[p].probe.chromosome() == currentChr) {
                ++currentChrCount;
            } else {
                numberOfTestsPerformed += (currentChrCount * ((long) currentChrCount - 1)) / 2;
                currentChrCount = 1;
                currentChr = probes[p].probe.chromosome();
            }
        }
        numberOfTestsPerformed += (currentChrCount * ((long) currentChrCount - 1)) / 2;
    } else {
        numberOfTestsPerformed = (probes.length * ((long) probes.length - 1)) / 2;
    }
    for (int p = 0; p < probes.length; p++) {
        if (p % 100 == 0) {
            Enumeration<ProgressListener> en = listeners.elements();
            while (en.hasMoreElements()) {
                en.nextElement().progressUpdated("Processed " + p + " probes", p, probes.length);
            }
            if (cancel) {
                Enumeration<ProgressListener> en2 = listeners.elements();
                while (en2.hasMoreElements()) {
                    en2.nextElement().progressCancelled();
                }
                return;
            }
        }
        // System.err.println("Getting interactions for "+probes[p].probe);
        // We temporarily store the interactions with this probe which means we
        // can make a decision about which ones we're keeping as we go along which
        // drastically reduces the amount we need to store.
        // This is going to be the data structure which holds the information
        // on the pairs of probes which have any interactions.  The key is the
        // index position of the pair (x+(y*no of probes), and the value is the
        // number of times this interaction was seen
        Hashtable<Long, Integer> interactionCounts = new Hashtable<Long, Integer>();
        HiCHitCollection hiCHits = dataSet.getHiCReadsForProbe(probes[p].probe);
        String[] chromosomeNames = hiCHits.getChromosomeNamesWithHits();
        // System.err.println("Found hits on "+chromosomeNames.length+" chromosomes");
        CHROM: for (int c = 0; c < chromosomeNames.length; c++) {
            // Skip all trans reads if there is a max distance set.
            if (initialMaxDistance > 0) {
                if (!probes[p].probe.chromosome().name().equals(chromosomeNames[c])) {
                    continue;
                }
            }
            long[] hitReads = hiCHits.getHitPositionsForChromosome(chromosomeNames[c]);
            SequenceRead.sort(hitReads);
            // We're going to start when we hit the probes for this chromosome
            if (!offsets.containsKey(chromosomeNames[c])) {
                // System.err.println("No probes on chr "+chromosomeNames[c]+" skipping");
                continue;
            }
            int lastIndex = offsets.get(chromosomeNames[c]);
            READ: for (int o = 0; o < hitReads.length; o++) {
                if (cancel) {
                    Enumeration<ProgressListener> en = listeners.elements();
                    while (en.hasMoreElements()) {
                        en.nextElement().progressCancelled();
                    }
                    return;
                }
                // Check against the relevant reads
                int startIndex = lastIndex;
                for (int x = startIndex; x < probes.length; x++) {
                    // Check that we're on the right chromosome
                    if (!probes[x].probe.chromosome().name().equals(chromosomeNames[c])) {
                        // System.err.println("Stopping searching at position "+x+" since we changed to chr "+probes[x].probe.chromosome());
                        continue CHROM;
                    }
                    if (probes[x].probe.start() > SequenceRead.end(hitReads[o])) {
                        // to the next read.
                        continue READ;
                    }
                    if (SequenceRead.overlaps(hitReads[o], probes[x].probe.packedPosition())) {
                        // We overlap with this probe
                        // System.err.println("Found hit to probe "+probes[x].probe);
                        // For probe list probes we could have the same probe several times
                        // so we can add the read to all of the probes which are the same
                        // from this point.
                        // We can skip over interactions where the matched index is less than our index
                        // since we'll duplicate this later anyway.
                        long interactionKey = p + (x * (long) probes.length);
                        if (!interactionCounts.containsKey(interactionKey)) {
                            if (probes[p].index < probes[x].index) {
                                interactionCounts.put(interactionKey, 0);
                            }
                        // else {
                        // System.err.println("Skipping earlier probe hit");
                        // }
                        }
                        // Add one to our counts for this interaction
                        if (probes[p].index < probes[x].index) {
                            interactionCounts.put(interactionKey, interactionCounts.get(interactionKey) + 1);
                        }
                        // Since we found our first hit here we can start looking from here next time.
                        lastIndex = x;
                        // this one.
                        for (x = x + 1; x < probes.length; x++) {
                            if (probes[x].probe.chromosome() != probes[x - 1].probe.chromosome()) {
                                // We've reached the end for this chromosome
                                break;
                            }
                            if (probes[x].probe == probes[x - 1].probe || SequenceRead.overlaps(hitReads[o], probes[x].probe.packedPosition())) {
                                if (probes[p].index >= probes[x].index)
                                    continue;
                                interactionKey = p + (x * (long) probes.length);
                                if (!interactionCounts.containsKey(interactionKey)) {
                                    if (interactionCounts.size() >= MAX_INTERACTIONS) {
                                        continue READ;
                                    }
                                    interactionCounts.put(interactionKey, 0);
                                }
                                interactionCounts.put(interactionKey, interactionCounts.get(interactionKey) + 1);
                            } else {
                                break;
                            }
                        }
                        continue READ;
                    }
                // End if overlaps
                }
            // End check each probe
            }
        // End each hit read
        }
        // End each hit chromosome
        // We can now go through the interactions we saw and decide if we
        // want to keep any of them.
        HiCInteractionStrengthCalculator strengthCalc = new HiCInteractionStrengthCalculator(dataSet, correctLinkage);
        Enumeration<Long> en = interactionCounts.keys();
        while (en.hasMoreElements()) {
            long index = en.nextElement();
            int absoluteValue = interactionCounts.get(index);
            if (absoluteValue < initialMinAbsolute)
                continue;
            ProbeWithIndex probe1 = probes[(int) (index % probes.length)];
            ProbeWithIndex probe2 = probes[(int) (index / probes.length)];
            // We calculate the obs/exp based on the total pair count
            // and the relative counts at each end of the interaction
            // Do the interaction strength calculation
            strengthCalc.calculateInteraction(absoluteValue, totalCisCounts[probe1.index], totalTransCounts[probe1.index], totalCisCounts[probe2.index], totalTransCounts[probe2.index], probe1.probe, probe2.probe);
            // We're not counting this here any more.  We work out theoretical numbers at the top instead
            // ++numberOfTestsPerformed;
            float obsExp = (float) strengthCalc.obsExp();
            float pValue = (float) strengthCalc.rawPValue();
            // interaction objects we have to create.
            if (obsExp < initialMinStrength)
                continue;
            // This isn't the final p-value check, but if the raw pvalue fails then the corrected value is never going to pass.
            if (initialMaxSignificance < 1 && pValue > initialMaxSignificance)
                continue;
            InteractionProbePair interaction = new InteractionProbePair(probe1.probe, probe1.index, probe2.probe, probe2.index, obsExp, absoluteValue);
            interaction.setSignificance(pValue);
            // We check against the current list of filters
            if (passesCurrentFilters(interaction)) {
                // See if the strength of any of the interactions is bigger than our current max
                if (obsExp > maxValue)
                    maxValue = obsExp;
                if (filteredInteractions.size() >= MAX_INTERACTIONS) {
                    Enumeration<ProgressListener> en2 = listeners.elements();
                    while (en2.hasMoreElements()) {
                        en2.nextElement().progressWarningReceived(new SeqMonkException("More than " + MAX_INTERACTIONS + " interactions passed the filters. Showing as many as I can"));
                    }
                } else {
                    filteredInteractions.add(interaction);
                }
            }
        }
    }
    // Put the interactions which worked into an Array
    interactions = filteredInteractions.toArray(new InteractionProbePair[0]);
    // System.err.println("Found a set of "+interactions.length+" initially filtered interactions");
    // Apply multiple testing correction
    Arrays.sort(interactions, new Comparator<InteractionProbePair>() {

        public int compare(InteractionProbePair o1, InteractionProbePair o2) {
            return Float.compare(o1.signficance(), o2.signficance());
        }
    });
    for (int i = 0; i < interactions.length; i++) {
        interactions[i].setSignificance(interactions[i].signficance() * ((float) (numberOfTestsPerformed) / (i + 1)));
        // We can't allow corrected p-values to end up in a different order to the uncorrected ones
        if (i > 0 && interactions[i].signficance() < interactions[i - 1].signficance()) {
            interactions[i].setSignificance(interactions[i - 1].signficance());
        }
    }
    // Now re-filter the interactions to get those which finally pass the p-value filter
    filteredInteractions.clear();
    for (int i = 0; i < interactions.length; i++) {
        if (initialMaxSignificance >= 1 || interactions[i].signficance() < initialMaxSignificance) {
            filteredInteractions.add(interactions[i]);
        } else {
            break;
        }
    }
    // Put the interactions which worked into an Array
    interactions = filteredInteractions.toArray(new InteractionProbePair[0]);
    // System.err.println("Found a set of "+interactions.length+" interactions after multiple testing correction");
    // We've processed all of the reads and the matrix is complete.  We can therefore
    // draw the plot
    Enumeration<ProgressListener> en2 = listeners.elements();
    while (en2.hasMoreElements()) {
        en2.nextElement().progressComplete("heatmap", null);
    }
}
Also used : HiCHitCollection(uk.ac.babraham.SeqMonk.DataTypes.Sequence.HiCHitCollection) Hashtable(java.util.Hashtable) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Vector(java.util.Vector)

Example 9 with ProgressListener

use of uk.ac.babraham.SeqMonk.DataTypes.ProgressListener in project SeqMonk by s-andrews.

the class SeqMonkDataWriter method printPairedDataSet.

private boolean printPairedDataSet(PairedDataSet set, PrintStream p, int index, int indexTotal) throws IOException {
    p.println(set.getTotalReadCount() * 2 + "\t" + set.name());
    // Go through one chromosome at a time.
    Chromosome[] chrs = data.genome().getAllChromosomes();
    for (int c = 0; c < chrs.length; c++) {
        HiCHitCollection hiCHits = set.getHiCReadsForChromosome(chrs[c]);
        // Work out how many of these reads we're actually going to output
        int validReadCount = 0;
        for (int c2 = 0; c2 < chrs.length; c2++) {
            validReadCount += hiCHits.getSourcePositionsForChromosome(chrs[c2].name()).length;
        }
        p.println(chrs[c].name() + "\t" + validReadCount);
        for (int c2 = 0; c2 < chrs.length; c2++) {
            long[] sourceReads = hiCHits.getSourcePositionsForChromosome(chrs[c2].name());
            long[] hitReads = hiCHits.getHitPositionsForChromosome(chrs[c2].name());
            for (int j = 0; j < sourceReads.length; j++) {
                if (cancel) {
                    cancelled(p);
                    return false;
                }
                // TODO: Fix the progress bar
                if ((j % (1 + (validReadCount / 10))) == 0) {
                    Enumeration<ProgressListener> e2 = listeners.elements();
                    while (e2.hasMoreElements()) {
                        e2.nextElement().progressUpdated("Writing data for " + set.name(), index * chrs.length + c, indexTotal * chrs.length);
                    }
                }
                p.println(sourceReads[j] + "\t" + chrs[c2].name() + "\t" + hitReads[j]);
            }
        }
    }
    // Print a blank line after the last chromosome
    p.println("");
    return true;
}
Also used : HiCHitCollection(uk.ac.babraham.SeqMonk.DataTypes.Sequence.HiCHitCollection) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)

Example 10 with ProgressListener

use of uk.ac.babraham.SeqMonk.DataTypes.ProgressListener in project SeqMonk by s-andrews.

the class SeqMonkDataWriter method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    try {
        // Generate a temp file in the same directory as the final
        // destination
        tempFile = File.createTempFile("seqmonk", ".temp", file.getParentFile());
        BufferedOutputStream bos;
        if (SeqMonkPreferences.getInstance().compressOutput()) {
            bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(tempFile), 2048));
        } else {
            bos = new BufferedOutputStream(new FileOutputStream(tempFile));
        }
        PrintStream p = new PrintStream(bos);
        printDataVersion(p);
        printAssembly(p);
        DataSet[] dataSets = data.getAllDataSets();
        DataGroup[] dataGroups = data.getAllDataGroups();
        ReplicateSet[] replicateSets = data.getAllReplicateSets();
        if (!printDataSets(dataSets, p)) {
            // They cancelled
            return;
        }
        printDataGroups(dataSets, dataGroups, p);
        printReplicateSets(dataSets, dataGroups, replicateSets, p);
        AnnotationSet[] annotationSets = data.genome().annotationCollection().anotationSets();
        for (int a = 0; a < annotationSets.length; a++) {
            if (annotationSets[a] instanceof CoreAnnotationSet)
                continue;
            if (!printAnnotationSet(annotationSets[a], p)) {
                // They cancelled
                return;
            }
        }
        Probe[] probes = null;
        if (data.probeSet() != null) {
            probes = data.probeSet().getAllProbes();
        }
        if (probes != null) {
            if (!printProbeSet(data.probeSet(), probes, dataSets, dataGroups, p)) {
                // They cancelled
                return;
            }
        }
        if (visibleStores != null) {
            printVisibleDataStores(dataSets, dataGroups, replicateSets, p);
        }
        if (probes != null) {
            if (!printProbeLists(probes, p)) {
                // They cancelled
                return;
            }
        }
        if (defaultFeatureTracks != null) {
            printDisplayPreferences(p);
        }
        p.close();
        // We can now overwrite the original file
        if (file.exists()) {
            if (!file.delete()) {
                throw new IOException("Couldn't delete old project file when making new one");
            }
        }
        if (!tempFile.renameTo(file)) {
            throw new IOException("Failed to rename temporary file");
        }
        Enumeration<ProgressListener> e = listeners.elements();
        while (e.hasMoreElements()) {
            e.nextElement().progressComplete("data_written", null);
        }
    } catch (Exception ex) {
        Enumeration<ProgressListener> e = listeners.elements();
        while (e.hasMoreElements()) {
            e.nextElement().progressExceptionReceived(ex);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) DataGroup(uk.ac.babraham.SeqMonk.DataTypes.DataGroup) Enumeration(java.util.Enumeration) DataSet(uk.ac.babraham.SeqMonk.DataTypes.DataSet) PairedDataSet(uk.ac.babraham.SeqMonk.DataTypes.PairedDataSet) ReplicateSet(uk.ac.babraham.SeqMonk.DataTypes.ReplicateSet) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) CoreAnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet) IOException(java.io.IOException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) IOException(java.io.IOException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) CoreAnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)18 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)12 IOException (java.io.IOException)10 Enumeration (java.util.Enumeration)7 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)7 File (java.io.File)6 BufferedReader (java.io.BufferedReader)4 FileNotFoundException (java.io.FileNotFoundException)4 FileReader (java.io.FileReader)4 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)4 FileFilter (java.io.FileFilter)3 AnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet)3 CoreAnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet)3 HiCHitCollection (uk.ac.babraham.SeqMonk.DataTypes.Sequence.HiCHitCollection)3 BufferedOutputStream (java.io.BufferedOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 UnknownHostException (java.net.UnknownHostException)2 Vector (java.util.Vector)2 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)2