Search in sources :

Example 76 with SeqMonkException

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

the class ScatterPlotPanel method getFilteredProbes.

/**
 * Gets the filtered probes.
 *
 * @param probeset the probeset
 * @return the filtered probes
 */
public ProbeList getFilteredProbes(ProbeSet probeset) {
    double minDiff = Math.min(diffStart, diffEnd);
    double maxDiff = Math.max(diffStart, diffEnd);
    ProbeList list = new ProbeList(probeList, "Difference between " + df.format(minDiff) + " and " + df.format(maxDiff), "Difference between " + xStore.name() + " and " + yStore.name() + " was between " + df.format(minDiff) + " and " + df.format(maxDiff), null);
    if (madeSelection) {
        Probe[] probes = probeList.getAllProbes();
        for (int p = 0; p < probes.length; p++) {
            try {
                double diff = xStore.getValueForProbe(probes[p]) - yStore.getValueForProbe(probes[p]);
                if (diff < minDiff)
                    continue;
                if (diff > maxDiff)
                    continue;
                list.addProbe(probes[p], null);
            } catch (SeqMonkException e) {
                e.printStackTrace();
            }
        }
    }
    return list;
}
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 77 with SeqMonkException

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

the class ScatterPlotPanel method calculateNonredundantSet.

/**
 * This collapses individual points which are over the same
 * pixel when redrawing the plot at a different scale
 */
private synchronized void calculateNonredundantSet() {
    closestPoint = null;
    ProbePairValue[][] grid = new ProbePairValue[getWidth()][getHeight()];
    Probe[] probes = probeList.getAllProbes();
    try {
        for (int p = 0; p < probes.length; p++) {
            float xValue = xStore.getValueForProbe(probes[p]);
            float yValue = yStore.getValueForProbe(probes[p]);
            if (Float.isNaN(xValue) || Float.isInfinite(xValue) || Float.isNaN(yValue) || Float.isInfinite(yValue)) {
                continue;
            }
            int x = getX(xValue);
            int y = getY(yValue);
            if (grid[x][y] == null) {
                grid[x][y] = new ProbePairValue(xValue, yValue, x, y);
                grid[x][y].setProbe(probes[p]);
            } else {
                // belong to
                if (subLists == null)
                    grid[x][y].count++;
                // As we have multiple probes at this point we remove the
                // specific probe annotation.
                grid[x][y].setProbe(null);
            }
        }
        if (subLists != null) {
            for (int s = 0; s < subLists.length; s++) {
                Probe[] subListProbes = subLists[s].getAllProbes();
                for (int p = 0; p < subListProbes.length; p++) {
                    float xValue = xStore.getValueForProbe(subListProbes[p]);
                    float yValue = yStore.getValueForProbe(subListProbes[p]);
                    int x = getX(xValue);
                    int y = getY(yValue);
                    if (grid[x][y] == null) {
                        // This messes up where we catch it in the middle of a redraw
                        continue;
                    // throw new IllegalArgumentException("Found subList position not in main list");
                    }
                    // 1 = no list so 2 is the lowest sublist index
                    grid[x][y].count = s + 2;
                }
            }
        }
    } catch (SeqMonkException e) {
        throw new IllegalStateException(e);
    }
    // Now we need to put all of the ProbePairValues into
    // a single array;
    int count = 0;
    for (int x = 0; x < grid.length; x++) {
        for (int y = 0; y < grid[x].length; y++) {
            if (grid[x][y] != null)
                count++;
        }
    }
    ProbePairValue[] nonred = new ProbePairValue[count];
    count--;
    for (int x = 0; x < grid.length; x++) {
        for (int y = 0; y < grid[x].length; y++) {
            if (grid[x][y] != null) {
                nonred[count] = grid[x][y];
                count--;
            }
        }
    }
    Arrays.sort(nonred);
    // Work out the 95% percentile count
    int minCount = 1;
    int maxCount = 2;
    if (nonred.length > 0) {
        minCount = nonred[0].count;
        maxCount = nonred[((nonred.length - 1) * 95) / 100].count;
    }
    // Go through every nonred assigning a suitable colour
    ColourGradient gradient = new HotColdColourGradient();
    for (int i = 0; i < nonred.length; i++) {
        if (subLists == null) {
            nonred[i].color = gradient.getColor(nonred[i].count, minCount, maxCount);
        } else {
            if (nonred[i].count > subLists.length + 1) {
                throw new IllegalArgumentException("Count above threshold when showing sublists");
            }
            if (nonred[i].count == 1) {
                nonred[i].color = VERY_LIGHT_GREY;
            } else {
                nonred[i].color = ColourIndexSet.getColour(nonred[i].count - 2);
            }
        }
    }
    nonRedundantValues = nonred;
    lastNonredWidth = getWidth();
    lastNonredHeight = getHeight();
// System.out.println("Nonred was "+nonRedundantValues.length+" from "+probes.length);
}
Also used : HotColdColourGradient(uk.ac.babraham.SeqMonk.Gradients.HotColdColourGradient) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) ColourGradient(uk.ac.babraham.SeqMonk.Gradients.ColourGradient) HotColdColourGradient(uk.ac.babraham.SeqMonk.Gradients.HotColdColourGradient)

Example 78 with SeqMonkException

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

the class VariancePlotPanel method getFilteredProbes.

/**
 * Gets the filtered probes.
 *
 * @param probeset the probeset
 * @return the filtered probes
 */
public ProbeList getFilteredProbes(ProbeSet probeset) {
    double minDiff = Math.min(diffStart, diffEnd);
    double maxDiff = Math.max(diffStart, diffEnd);
    String varianceName = "Standard Deviation";
    switch(varianceMeasure) {
        case VARIANCE_COEF:
            {
                varianceName = "Coefficient of variation";
                break;
            }
        case VARIANCE_SEM:
            {
                varianceName = "Standard error of the mean";
                break;
            }
        case VARIANCE_QUARTILE_DISP:
            {
                varianceName = "Quartile dispersion";
                break;
            }
        case VARIANCE_NUMBER_UNMEASURED:
            {
                varianceName = "Number of unmeasured data stores";
                break;
            }
    }
    ProbeList list = new ProbeList(probeList, varianceName + " difference between " + df.format(minDiff) + " and " + df.format(maxDiff), varianceName + " difference in " + repSet.name() + " was between " + df.format(minDiff) + " and " + df.format(maxDiff), varianceName + " diff");
    if (madeSelection) {
        Probe[] probes = probeList.getAllProbes();
        for (int p = 0; p < probes.length; p++) {
            try {
                float varianceMeasure = getYValue(probes[p]);
                double diff = varianceMeasure - smoothedTrend.getSmoothedValueForX(repSet.getValueForProbeExcludingUnmeasured(probes[p]));
                if (diff < minDiff)
                    continue;
                if (diff > maxDiff)
                    continue;
                list.addProbe(probes[p], new Float(diff));
            } catch (SeqMonkException e) {
                e.printStackTrace();
            }
        }
    }
    return list;
}
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 79 with SeqMonkException

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

the class VariancePlotPanel method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    // We need to find the mix/max counts, and get a non-overlapping
    // set of probes
    Probe[] probes = probeList.getAllProbes();
    // We need to get rid of any probes which don't have a value associated with them (ie NaN values)
    Vector<Probe> validProbes = new Vector<Probe>();
    try {
        for (int p = 0; p < probes.length; p++) {
            if (!Float.isNaN(repSet.getValueForProbeExcludingUnmeasured(probes[p]))) {
                validProbes.add(probes[p]);
            }
        }
    } catch (SeqMonkException sme) {
        return;
    }
    probes = validProbes.toArray(new Probe[0]);
    // If there aren't any probes there's no point going any further
    if (probes.length == 0) {
        noData = true;
        repaint();
        return;
    }
    boolean someXValueSet = false;
    boolean someYValueSet = false;
    try {
        // We extract the data to allow the calculation of the smoothed values
        float[] xData = new float[probes.length];
        float[] yData = new float[probes.length];
        for (int p = 0; p < probes.length; p++) {
            xData[p] = repSet.getValueForProbeExcludingUnmeasured(probes[p]);
            yData[p] = getYValue(probes[p]);
            if (!someXValueSet && !(Float.isNaN(xData[p]) || Float.isInfinite(xData[p]))) {
                minValueX = xData[p];
                maxValueX = xData[p];
                someXValueSet = true;
            }
            if (!someYValueSet && !(Float.isNaN(yData[p]) || Float.isInfinite(yData[p]))) {
                minValueY = yData[p];
                maxValueY = yData[p];
                someYValueSet = true;
            }
            if (!(Float.isNaN(xData[p]) || Float.isInfinite(xData[p]))) {
                if (xData[p] < minValueX)
                    minValueX = xData[p];
                if (xData[p] > maxValueX)
                    maxValueX = xData[p];
            }
            if (!(Float.isNaN(yData[p]) || Float.isInfinite(yData[p]))) {
                if (yData[p] < minValueY)
                    minValueY = yData[p];
                if (yData[p] > maxValueY)
                    maxValueY = yData[p];
            }
        }
        // Do a sanity check that something has a non-NaN value in it
        boolean someRealData = false;
        for (int i = 0; i < xData.length; i++) {
            if (!Float.isNaN(xData[i])) {
                someRealData = true;
                break;
            }
        }
        if (!someRealData) {
            // There's no point doing anything else
            return;
        }
        // Calculate the smoothed values
        smoothedTrend = new SmoothedVarianceDataset(repSet, probes, varianceMeasure, xData.length / 100);
    } catch (SeqMonkException e) {
        e.printStackTrace();
    }
    readyToDraw = true;
    repaint();
}
Also used : SmoothedVarianceDataset(uk.ac.babraham.SeqMonk.Analysis.Statistics.SmoothedVarianceDataset) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) Vector(java.util.Vector)

Example 80 with SeqMonkException

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

the class GenericAnnotationParser method parseAnnotation.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.AnnotationParsers.AnnotationParser#parseAnnotation(java.io.File, uk.ac.babraham.SeqMonk.DataTypes.Genome.Genome)
	 */
public AnnotationSet[] parseAnnotation(File file, Genome genome) throws Exception {
    this.file = file;
    if (options == null) {
        options = new JDialog(SeqMonkApplication.getInstance());
        options.setModal(true);
        options.setContentPane(new GenericAnnotationParserOptions(options));
        options.setSize(700, 400);
        options.setLocationRelativeTo(null);
    }
    // We have to set cancel to true as a default so we don't try to
    // proceed with processing if the user closes the options using
    // the X on the window.
    options.setTitle("Format for " + file.getName() + "...");
    cancel = true;
    options.setVisible(true);
    if (cancel) {
        progressCancelled();
        return null;
    }
    // We keep track of how many types have been added
    // to catch if someone sets the wrong field and makes
    // loads of different feature types.
    HashSet<String> addedTypes = new HashSet<String>();
    BufferedReader br;
    if (file.getName().toLowerCase().endsWith(".gz")) {
        br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
    } else {
        br = new BufferedReader(new FileReader(file));
    }
    String line;
    // First skip the header lines
    for (int i = 0; i < startRowValue; i++) {
        line = br.readLine();
        if (line == null) {
            throw new Exception("Ran out of file before skipping all of the header lines");
        }
    }
    int maxIndexValue = 0;
    if (chrColValue > maxIndexValue)
        maxIndexValue = chrColValue;
    if (startColValue > maxIndexValue)
        maxIndexValue = startColValue;
    if (endColValue > maxIndexValue)
        maxIndexValue = endColValue;
    if (strandColValue > maxIndexValue)
        maxIndexValue = strandColValue;
    if (typeColValue > maxIndexValue)
        maxIndexValue = typeColValue;
    if (nameColValue > maxIndexValue)
        maxIndexValue = nameColValue;
    if (descriptionColValue > maxIndexValue)
        maxIndexValue = descriptionColValue;
    Vector<AnnotationSet> annotationSets = new Vector<AnnotationSet>();
    AnnotationSet currentAnnotation = new AnnotationSet(genome, file.getName());
    annotationSets.add(currentAnnotation);
    int lineCount = 0;
    // Now process the rest of the file
    while ((line = br.readLine()) != null) {
        ++lineCount;
        if (cancel) {
            progressCancelled();
            return null;
        }
        if (lineCount % 1000 == 0) {
            progressUpdated("Read " + lineCount + " lines from " + file.getName(), 0, 1);
        }
        if (lineCount > 1000000 && lineCount % 1000000 == 0) {
            progressUpdated("Caching...", 0, 1);
            currentAnnotation.finalise();
            currentAnnotation = new AnnotationSet(genome, file.getName() + "[" + annotationSets.size() + "]");
            annotationSets.add(currentAnnotation);
        }
        String[] sections = line.split(delimitersValue, -1);
        // Check to see if we've got enough data to work with
        if (maxIndexValue >= sections.length) {
            progressWarningReceived(new SeqMonkException("Not enough data (" + sections.length + ") to get a probe name on line '" + line + "'"));
            // Skip this line...
            continue;
        }
        int strand;
        int start;
        int end;
        try {
            start = Integer.parseInt(sections[startColValue]);
            end = Integer.parseInt(sections[endColValue]);
            // End must always be later than start
            if (end < start) {
                int temp = start;
                start = end;
                end = temp;
            }
            if (useStrand) {
                if (sections[strandColValue].equals("+") || sections[strandColValue].equals("1") || sections[strandColValue].equals("FF") || sections[strandColValue].equals("F")) {
                    strand = Location.FORWARD;
                } else if (sections[strandColValue].equals("-") || sections[strandColValue].equals("-1") || sections[strandColValue].equals("RF") || sections[strandColValue].equals("R")) {
                    strand = Location.REVERSE;
                } else {
                    progressWarningReceived(new SeqMonkException("Unknown strand character '" + sections[strandColValue] + "' marked as unknown strand"));
                    strand = Location.UNKNOWN;
                }
            } else {
                strand = Location.UNKNOWN;
            }
        } catch (NumberFormatException e) {
            progressWarningReceived(new SeqMonkException("Location " + sections[startColValue] + "-" + sections[endColValue] + " was not an integer"));
            continue;
        }
        ChromosomeWithOffset c;
        try {
            c = genome.getChromosome(sections[chrColValue]);
        } catch (IllegalArgumentException sme) {
            progressWarningReceived(sme);
            continue;
        }
        end = c.position(end);
        start = c.position(start);
        // We also don't allow readings which are beyond the end of the chromosome
        if (end > c.chromosome().length()) {
            int overrun = end - c.chromosome().length();
            progressWarningReceived(new SeqMonkException("Reading position " + end + " was " + overrun + "bp beyond the end of chr" + c.chromosome().name() + " (" + c.chromosome().length() + ")"));
            continue;
        }
        if (start < 1) {
            progressWarningReceived(new SeqMonkException("Reading start position " + start + " was less than 1"));
            continue;
        }
        // Now we can add the type, name and description
        String type;
        // If there's a column containing the type then use that
        if (typeColValue >= 0) {
            type = sections[typeColValue];
        } else // If not then use the manually specified type if they set it
        if (featureType != null && featureType.length() > 0) {
            type = featureType;
        } else // If all else fails use the file name as the type
        {
            type = file.getName();
        }
        if (!addedTypes.contains(type)) {
            addedTypes.add(type);
            if (addedTypes.size() > 100) {
                throw new SeqMonkException("More than 100 different types of feature added - you don't want to do this!");
            }
        }
        String name = null;
        if (nameColValue >= 0) {
            name = sections[nameColValue];
        }
        String description = null;
        if (descriptionColValue >= 0) {
            description = sections[descriptionColValue];
        }
        // We can now make the new annotation
        Feature feature = new Feature(type, c.chromosome().name());
        if (name != null) {
            feature.addAttribute("name", name);
        }
        if (description != null)
            feature.addAttribute("description", description);
        feature.setLocation(new Location(start, end, strand));
        currentAnnotation.addFeature(feature);
    // System.out.println("Added probe "+newProbe.name()+" on "+newProbe.chromosome()+" at pos "+newProbe.position());
    }
    // We're finished with the file.
    br.close();
    options.dispose();
    options = null;
    return annotationSets.toArray(new AnnotationSet[0]);
}
Also used : ChromosomeWithOffset(uk.ac.babraham.SeqMonk.Utilities.ChromosomeWithOffset) InputStreamReader(java.io.InputStreamReader) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) FileInputStream(java.io.FileInputStream) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Vector(java.util.Vector) JDialog(javax.swing.JDialog) HashSet(java.util.HashSet) Location(uk.ac.babraham.SeqMonk.DataTypes.Genome.Location)

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