Search in sources :

Example 21 with SeqMonkException

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

the class ChromosomeViewReport method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    int upstreamContext = 0;
    if (upstreamContextField.getText().length() > 0) {
        upstreamContext = Integer.parseInt(upstreamContextField.getText());
    }
    int downstreamContext = 0;
    if (downstreamContextField.getText().length() > 0) {
        downstreamContext = Integer.parseInt(downstreamContextField.getText());
    }
    // We need an HTML file we're going to make into an index
    JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getSaveLocation());
    chooser.setMultiSelectionEnabled(false);
    chooser.addChoosableFileFilter(new HTMLFileFilter());
    int result = chooser.showSaveDialog(SeqMonkApplication.getInstance());
    if (result == JFileChooser.CANCEL_OPTION)
        return;
    File file = chooser.getSelectedFile();
    SeqMonkPreferences.getInstance().setLastUsedSaveLocation(file);
    if (file.isDirectory())
        return;
    if (!(file.getPath().toLowerCase().endsWith(".html") || file.getPath().toLowerCase().endsWith(".html"))) {
        file = new File(file.getPath() + ".html");
    }
    // Check if we're stepping on anyone's toes...
    if (file.exists()) {
        int answer = JOptionPane.showOptionDialog(SeqMonkApplication.getInstance(), file.getName() + " exists.  Do you want to overwrite the existing file?", "Overwrite file?", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Overwrite and Save", "Cancel" }, "Overwrite and Save");
        if (answer > 0) {
            return;
        }
    }
    // We need to make a folder from the file
    File outputFolder = new File(file.getAbsolutePath().replace(".html", "_files"));
    outputFolder.mkdir();
    System.err.println("Output file is " + file.getAbsolutePath() + " output folder is " + outputFolder.getAbsolutePath());
    Chromosome[] chrs = collection.genome().getAllChromosomes();
    // Save the starting location so we can go back there after we're done
    Chromosome currentChr = DisplayPreferences.getInstance().getCurrentChromosome();
    long currentLocation = DisplayPreferences.getInstance().getCurrentLocation();
    for (int c = 0; c < chrs.length; c++) {
        progressUpdated("Processing Chr" + chrs[c].name(), c, chrs.length);
        Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(chrs[c]);
        // We can now step through the probes making the image for each one
        for (int p = 0; p < probes.length; p++) {
            if (cancel) {
                progressCancelled();
                return;
            }
            int start = probes[p].start();
            int end = probes[p].end();
            start -= upstreamContext;
            end += downstreamContext;
            if (start < 1)
                start = 1;
            if (end < 1)
                end = 1;
            if (start > chrs[c].length())
                start = chrs[c].length();
            if (end > chrs[c].length())
                end = chrs[c].length();
            if (end - start < 100) {
                progressWarningReceived(new SeqMonkException("View for " + probes[p].name() + " was too small to export"));
                continue;
            }
            DisplayPreferences.getInstance().setLocation(chrs[c], SequenceRead.packPosition(start, end, Location.FORWARD));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            }
            File saveFile = new File(outputFolder.getAbsolutePath() + "/" + probes[p].name() + ".png");
            try {
                ImageSaver.savePNG(SeqMonkApplication.getInstance().chromosomeViewer(), saveFile);
            } catch (IOException e) {
                progressExceptionReceived(e);
                return;
            }
        }
    }
    DisplayPreferences.getInstance().setLocation(currentChr, currentLocation);
    reportComplete(null);
}
Also used : JFileChooser(javax.swing.JFileChooser) HTMLFileFilter(uk.ac.babraham.SeqMonk.Utilities.FileFilters.HTMLFileFilter) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) IOException(java.io.IOException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) File(java.io.File)

Example 22 with SeqMonkException

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

the class PercentileNormalisationQuantitation method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    if (!isReady()) {
        progressExceptionReceived(new SeqMonkException("Options weren't set correctly"));
    }
    Probe[] allProbes = application.dataCollection().probeSet().getAllProbes();
    Probe[] calculateProbes = ((ProbeList) calculateFromProbeList.getSelectedItem()).getAllProbes();
    float[][] percentileValues = new float[data.length][];
    float[] minValues = new float[data.length];
    // Work out the value at the appropriate percentile
    for (int d = 0; d < data.length; d++) {
        // Otherwise we'll calculate for each percentage (0-100)
        if (autoPercentileBox.isSelected()) {
            percentileValues[d] = new float[101];
        } else {
            percentileValues[d] = new float[1];
        }
        progressUpdated("Calculating correction for " + data[d].name(), d, data.length);
        float[] theseValues = new float[calculateProbes.length];
        for (int p = 0; p < calculateProbes.length; p++) {
            try {
                theseValues[p] = data[d].getValueForProbe(calculateProbes[p]);
            } catch (SeqMonkException e) {
                progressExceptionReceived(e);
            }
        }
        Arrays.sort(theseValues);
        if (autoPercentileBox.isSelected()) {
            for (int i = 0; i <= 100; i++) {
                // percentileValues[d][i] = theseValues[(int)((theseValues.length-1)*i)/100];
                percentileValues[d][i] = getPercentileValue(theseValues, i);
            }
        } else {
            // percentileValues[d][0] = theseValues[(int)((theseValues.length-1)*percentile)/100];
            percentileValues[d][0] = getPercentileValue(theseValues, percentile);
        }
        minValues[d] = theseValues[0];
    }
    float[] maxPercentiles = new float[percentileValues[0].length];
    for (int i = 0; i < percentileValues.length; i++) {
        for (int j = 0; j < maxPercentiles.length; j++) {
            if (i == 0 || percentileValues[i][j] > maxPercentiles[j]) {
                maxPercentiles[j] = percentileValues[i][j];
            }
        }
    }
    for (int d = 0; d < data.length; d++) {
        // See if we need to quit
        if (cancel) {
            progressCancelled();
            return;
        }
        progressUpdated(d, data.length);
        // Get the correction value
        float[] correctionFactors = new float[percentileValues[0].length];
        if (correctionAction == ADD) {
            for (int i = 0; i < correctionFactors.length; i++) {
                correctionFactors[i] = maxPercentiles[i] - percentileValues[d][i];
            }
        } else if (correctionAction == MULTIPLY) {
            for (int i = 0; i < correctionFactors.length; i++) {
                correctionFactors[i] = (maxPercentiles[i] - minValues[d]) / (percentileValues[d][i] - minValues[d]);
            }
        }
        // Now we work out the correction factor we're actually going to use
        float correctionFactor = SimpleStats.median(correctionFactors);
        // Apply the correction to all probes
        try {
            for (int p = 0; p < allProbes.length; p++) {
                // See if we need to quit
                if (cancel) {
                    progressCancelled();
                    return;
                }
                if (correctionAction == ADD) {
                    data[d].setValueForProbe(allProbes[p], data[d].getValueForProbe(allProbes[p]) + correctionFactor);
                } else if (correctionAction == MULTIPLY) {
                    data[d].setValueForProbe(allProbes[p], minValues[d] + ((data[d].getValueForProbe(allProbes[p]) - minValues[d]) * correctionFactor));
                }
            }
        } catch (SeqMonkException e) {
            progressExceptionReceived(e);
        }
    }
    quantitatonComplete();
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

Example 23 with SeqMonkException

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

the class LogTransformQuantitation method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    if (!isReady()) {
        progressExceptionReceived(new SeqMonkException("Options weren't set correctly"));
    }
    Chromosome[] chromosomes = application.dataCollection().genome().getAllChromosomes();
    Vector<DataStore> quantitatedStores = new Vector<DataStore>();
    DataSet[] sets = application.dataCollection().getAllDataSets();
    for (int s = 0; s < sets.length; s++) {
        if (sets[s].isQuantitated()) {
            quantitatedStores.add(sets[s]);
        }
    }
    DataGroup[] groups = application.dataCollection().getAllDataGroups();
    for (int g = 0; g < groups.length; g++) {
        if (groups[g].isQuantitated()) {
            quantitatedStores.add(groups[g]);
        }
    }
    DataStore[] data = quantitatedStores.toArray(new DataStore[0]);
    for (int c = 0; c < chromosomes.length; c++) {
        // See if we need to quit
        if (cancel) {
            progressCancelled();
            return;
        }
        progressUpdated(c, chromosomes.length);
        Probe[] allProbes = application.dataCollection().probeSet().getProbesForChromosome(chromosomes[c]);
        try {
            for (int p = 0; p < allProbes.length; p++) {
                // See if we need to quit
                if (cancel) {
                    progressCancelled();
                    return;
                }
                for (int d = 0; d < data.length; d++) {
                    data[d].setValueForProbe(allProbes[p], (float) (Math.log(Math.max(data[d].getValueForProbe(allProbes[p]), threshold)) / Math.log(logBase)));
                }
            }
        } catch (SeqMonkException e) {
            progressExceptionReceived(e);
        }
    }
    quantitatonComplete();
}
Also used : DataGroup(uk.ac.babraham.SeqMonk.DataTypes.DataGroup) DataSet(uk.ac.babraham.SeqMonk.DataTypes.DataSet) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Vector(java.util.Vector)

Example 24 with SeqMonkException

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

the class InterstitialProbeGenerator method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    Chromosome[] chromosomes = collection.genome().getAllChromosomes();
    makeEndProbes = makeEndProbesBox.isSelected();
    Vector<Probe> newProbes = new Vector<Probe>();
    try {
        for (int c = 0; c < chromosomes.length; c++) {
            // Time for an update
            updateGenerationProgress("Processed " + c + " chromosomes", c, chromosomes.length);
            Probe[] startingProbes = initialList.getProbesForChromosome(chromosomes[c]);
            // on the chromsoome.
            if (makeEndProbes) {
                if (startingProbes.length > 0) {
                    if (startingProbes[0].start() > 1) {
                        Probe p = makeProbe(chromosomes[c], 1, startingProbes[0].start() - 1, Probe.UNKNOWN);
                        if (p != null) {
                            newProbes.add(p);
                        }
                    }
                } else {
                    Probe p = makeProbe(chromosomes[c], 1, chromosomes[c].length(), Probe.UNKNOWN);
                    if (p != null) {
                        newProbes.add(p);
                    }
                }
            }
            int lastEnd = 1;
            int lastStrand = Probe.UNKNOWN;
            if (startingProbes.length > 0) {
                lastEnd = startingProbes[0].end() + 1;
                lastStrand = startingProbes[0].strand();
            }
            // Now we can make the actual interstitial probes
            for (int i = 0; i < startingProbes.length - 1; i++) {
                if (cancel) {
                    generationCancelled();
                    return;
                }
                if (startingProbes[i].end() + 1 > lastEnd) {
                    lastEnd = startingProbes[i].end() + 1;
                    lastStrand = startingProbes[i].strand();
                }
                if (startingProbes[i + 1].end() <= lastEnd) {
                    continue;
                }
                int strandToUse = Probe.UNKNOWN;
                if (startingProbes[i + 1].strand() == lastStrand) {
                    strandToUse = lastStrand;
                }
                Probe p = makeProbe(chromosomes[c], lastEnd, startingProbes[i + 1].start() - 1, strandToUse);
                if (p != null) {
                    newProbes.add(p);
                }
            }
            // Finally we can make an end probe if we need to
            if (makeEndProbes) {
                if (startingProbes.length > 0) {
                    if (startingProbes[startingProbes.length - 1].end() < chromosomes[c].length()) {
                        Probe p = makeProbe(chromosomes[c], startingProbes[startingProbes.length - 1].end() + 1, chromosomes[c].length(), Probe.UNKNOWN);
                        if (p != null) {
                            newProbes.add(p);
                        }
                    }
                }
            }
        }
    } catch (SeqMonkException e) {
        generationExceptionReceived(e);
    }
    Probe[] finalList = newProbes.toArray(new Probe[0]);
    ProbeSet finalSet = new ProbeSet(getDescription(), finalList);
    generationComplete(finalSet);
}
Also used : ProbeSet(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeSet) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) Vector(java.util.Vector)

Example 25 with SeqMonkException

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

the class WigglePipeline method startPipeline.

protected void startPipeline() {
    // We first need to generate probes over all of the features listed in
    // the feature types.  The probes should cover the whole area of the
    // feature regardless of where it splices.
    boolean logTransform = optionsPanel.logTransform();
    boolean correctTotal = optionsPanel.correctTotal();
    int probeSize = optionsPanel.probeSize();
    int stepSize = optionsPanel.stepSize();
    super.progressUpdated("Making probes", 0, 1);
    Probe[] probes = null;
    String region = optionsPanel.getRegion();
    try {
        if (region.equals("Whole Genome")) {
            probes = makeGenomeProbes(probeSize, stepSize);
        } else if (region.equals("Current Chromosome")) {
            probes = makeChromosomeProbes(DisplayPreferences.getInstance().getCurrentChromosome(), probeSize, stepSize);
        } else if (region.equals("Currently Visible Region")) {
            probes = makeVisibleProbes(DisplayPreferences.getInstance().getCurrentChromosome(), SequenceRead.start(DisplayPreferences.getInstance().getCurrentLocation()), SequenceRead.end(DisplayPreferences.getInstance().getCurrentLocation()), probeSize, stepSize);
        }
    } catch (SeqMonkException sme) {
        progressExceptionReceived(sme);
        return;
    }
    collection().setProbeSet(new ProbeSet("Wiggle probes", probes));
    // method from inside the pipeline rather than doing this again ourselves.
    if (cancel) {
        progressCancelled();
        return;
    }
    bpq = new BasePairQuantitation(application);
    bpq.addProgressListener(this);
    bpq.quantitate(collection(), data, QuantitationStrandType.getTypeOptions()[0], correctTotal, false, false, false, logTransform, false);
}
Also used : ProbeSet(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeSet) BasePairQuantitation(uk.ac.babraham.SeqMonk.Quantitation.BasePairQuantitation) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

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