Search in sources :

Example 16 with DataStore

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

the class DESeqFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    // We need to make a temporary directory, save the data into it, write out the R script
    // and then run it an collect the list of results, then clean up.
    // Make up the list of DataStores in each replicate set
    DataStore[][] storeGroups = new DataStore[replicateSets.length][];
    for (int r = 0; r < replicateSets.length; r++) {
        storeGroups[r] = replicateSets[r].dataStores();
    }
    File tempDir;
    try {
        progressUpdated("Creating temp directory", 0, 1);
        tempDir = TempDirectory.createTempDirectory();
        // System.err.println("Temp dir is "+tempDir.getAbsolutePath());
        progressUpdated("Writing R script", 0, 1);
        // Get the template script
        Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Filters/DESeqFilter/deseq_template.r"));
        // Substitute in the variables we need to change
        template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
        // Say which p value column we're filtering on
        if (multiTest) {
            template.setValue("CORRECTED", "padj");
        } else {
            template.setValue("CORRECTED", "pvalue");
        }
        if (independentFiltering) {
            template.setValue("INDEPENDENT", "TRUE");
        } else {
            template.setValue("INDEPENDENT", "FALSE");
        }
        StringBuffer sb = new StringBuffer();
        for (int s = 0; s < storeGroups.length; s++) {
            for (int i = 0; i < storeGroups[s].length; i++) {
                if (!(s == 0 && i == 0)) {
                    sb.append(",");
                }
                sb.append("\"group" + s + "\"");
            }
        }
        template.setValue("CONDITIONS", sb.toString());
        template.setValue("PVALUE", "" + cutoff);
        // Write the script file
        File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
        PrintWriter pr = new PrintWriter(scriptFile);
        pr.print(template.toString());
        pr.close();
        // Write the count data
        File countFile = new File(tempDir.getAbsoluteFile() + "/counts.txt");
        pr = new PrintWriter(countFile);
        sb = new StringBuffer();
        sb.append("probe");
        for (int s = 0; s < storeGroups.length; s++) {
            for (int i = 0; i < storeGroups[s].length; i++) {
                sb.append("\t");
                sb.append("group");
                sb.append(s);
                sb.append("_");
                sb.append(i);
            }
        }
        pr.println(sb.toString());
        progressUpdated("Writing count data", 0, 1);
        Probe[] probes = startingList.getAllProbes();
        float value;
        for (int p = 0; p < probes.length; p++) {
            if (p % 1000 == 0) {
                progressUpdated("Writing count data", p, probes.length);
            }
            sb = new StringBuffer();
            sb.append(p);
            for (int s = 0; s < storeGroups.length; s++) {
                for (int i = 0; i < storeGroups[s].length; i++) {
                    sb.append("\t");
                    value = storeGroups[s][i].getValueForProbe(probes[p]);
                    if (value != (int) value) {
                        progressExceptionReceived(new IllegalArgumentException("Inputs to the DESeq filter MUST be raw, incorrected counts, not things like " + value));
                        pr.close();
                        return;
                    }
                    sb.append(value);
                }
            }
            pr.println(sb.toString());
        }
        pr.close();
        progressUpdated("Running R Script", 0, 1);
        RScriptRunner runner = new RScriptRunner(tempDir);
        RProgressListener listener = new RProgressListener(runner);
        runner.addProgressListener(new ProgressRecordDialog("R Session", runner));
        runner.runScript();
        while (true) {
            if (listener.cancelled()) {
                progressCancelled();
                return;
            }
            if (listener.exceptionReceived()) {
                progressExceptionReceived(listener.exception());
                return;
            }
            if (listener.complete())
                break;
            Thread.sleep(500);
        }
        // We can now parse the results and put the hits into a new probe list
        ProbeList newList;
        newList = new ProbeList(startingList, "", "", "FDR");
        File hitsFile = new File(tempDir.getAbsolutePath() + "/hits.txt");
        BufferedReader br = new BufferedReader(new FileReader(hitsFile));
        String line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] sections = line.split("\t");
            int probeIndex = Integer.parseInt(sections[0]);
            float pValue = Float.parseFloat(sections[sections.length - 1]);
            newList.addProbe(probes[probeIndex], pValue);
        }
        br.close();
        runner.cleanUp();
        // TODO: Show log of R session?
        filterFinished(newList);
    } catch (Exception ioe) {
        progressExceptionReceived(ioe);
        return;
    }
// filterFinished(newList);
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 17 with DataStore

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

the class DifferencesFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
protected void generateProbeList() {
    Probe[] probes = startingList.getAllProbes();
    ProbeList newList = new ProbeList(startingList, "Filtered Probes", "", "Difference");
    HashSet<DataStore> allStoresSet = new HashSet<DataStore>();
    for (int i = 0; i < fromStores.length; i++) {
        allStoresSet.add(fromStores[i]);
    }
    for (int i = 0; i < toStores.length; i++) {
        allStoresSet.add(toStores[i]);
    }
    DataStore[] allStores = allStoresSet.toArray(new DataStore[0]);
    PROBE: for (int p = 0; p < probes.length; p++) {
        progressUpdated(p, probes.length);
        if (cancel) {
            cancel = false;
            progressCancelled();
            return;
        }
        for (int s = 0; s < allStores.length; s++) {
            if (!allStores[s].hasValueForProbe(probes[p]))
                continue PROBE;
            try {
                if (Float.isNaN(allStores[s].getValueForProbe(probes[p])))
                    continue PROBE;
            } catch (SeqMonkException sme) {
                continue;
            }
        }
        int count = 0;
        float d = 0;
        for (int fromIndex = 0; fromIndex < fromStores.length; fromIndex++) {
            for (int toIndex = 0; toIndex < toStores.length; toIndex++) {
                if (fromStores[fromIndex] == toStores[toIndex])
                    continue;
                switch(differenceType) {
                    case AVERAGE:
                        d += getDifferenceValue(toStores[toIndex], fromStores[fromIndex], probes[p]);
                        count++;
                        break;
                    case MAXIMUM:
                        float dt1 = getDifferenceValue(toStores[toIndex], fromStores[fromIndex], probes[p]);
                        if (count == 0 || dt1 > d)
                            d = dt1;
                        count++;
                        break;
                    case MINIMUM:
                        float dt2 = getDifferenceValue(toStores[toIndex], fromStores[fromIndex], probes[p]);
                        if (count == 0 || dt2 < d)
                            d = dt2;
                        count++;
                        break;
                    default:
                        progressExceptionReceived(new SeqMonkException("Unknown difference type " + differenceType));
                }
            }
        }
        if (differenceType == AVERAGE) {
            if (count > 0) {
                d /= count;
            }
        }
        // Now we have the value we need to know if it passes the test
        if (upperLimit != null)
            if (d > upperLimit) {
                continue;
            }
        if (lowerLimit != null)
            if (d < lowerLimit) {
                continue;
            }
        newList.addProbe(probes[p], new Float(d));
    }
    filterFinished(newList);
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) HashSet(java.util.HashSet)

Example 18 with DataStore

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

the class SmallRNAQCPreferencesDialog method actionPerformed.

public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("plot")) {
        Object[] featureObjects = featureList.getSelectedValues();
        String[] features = new String[featureObjects.length];
        for (int i = 0; i < featureObjects.length; i++) {
            features[i] = (String) featureObjects[i];
        }
        Object[] selectedObjects = dataList.getSelectedValues();
        if (selectedObjects.length == 0) {
            JOptionPane.showMessageDialog(this, "No data stores were selected", "Oops", JOptionPane.ERROR_MESSAGE);
            return;
        }
        DataStore[] selectedStores = new DataStore[selectedObjects.length];
        for (int i = 0; i < selectedObjects.length; i++) {
            selectedStores[i] = (DataStore) selectedObjects[i];
        }
        int minLength = 20;
        int maxLength = 30;
        if (minLengthField.getText().trim().length() > 0) {
            minLength = Integer.parseInt(minLengthField.getText().trim());
        }
        if (maxLengthField.getText().trim().length() > 0) {
            maxLength = Integer.parseInt(maxLengthField.getText().trim());
        }
        if (minLength > maxLength) {
            int temp = minLength;
            minLength = maxLength;
            maxLength = temp;
        }
        lowLength = minLength;
        highLength = maxLength;
        SmallRNAQCCalcualtor calc = new SmallRNAQCCalcualtor(collection, features, minLength, maxLength, selectedStores);
        calc.addListener(this);
        calc.addListener(new ProgressDialog("Small RNA QC Plot", calc));
        calc.startCalculating();
        setVisible(false);
    }
}
Also used : DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) ProgressDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressDialog.ProgressDialog)

Example 19 with DataStore

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

the class StrandBiasPlotDialog method actionPerformed.

/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("close")) {
        setVisible(false);
        dispose();
    } else if (ae.getActionCommand().equals("sublists")) {
        // Select a set of sublists from the current probe list to highlight
        // in the plot
        OrderedListSelector selector = new OrderedListSelector(this, probeList, subLists);
        // It's modal so by the time we get here the selection has been made
        subLists = selector.getOrderedLists();
        // try to highlight anything.
        if (subLists != null && subLists.length == 0) {
            subLists = null;
        }
        selector.dispose();
        actionPerformed(new ActionEvent(this, 1, "plot"));
    } else if (ae.getActionCommand().equals("plot")) {
        DataStore xStore = (DataStore) stores.getSelectedItem();
        getContentPane().remove(strandBiasPlotPanel);
        strandBiasPlotPanel = new StrandBiasPlotPanel(xStore, probeList, subLists, dotSizeSlider.getValue());
        getContentPane().add(strandBiasPlotPanel, BorderLayout.CENTER);
        validate();
    } else if (ae.getActionCommand().equals("save_probe_list")) {
        if (strandBiasPlotPanel instanceof StrandBiasPlotPanel) {
            ProbeList list = ((StrandBiasPlotPanel) strandBiasPlotPanel).getFilteredProbes(collection.probeSet());
            if (list.getAllProbes().length == 0) {
                JOptionPane.showMessageDialog(this, "No probes were selected", "No probes", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            // Ask for a name for the list
            String groupName = null;
            while (true) {
                groupName = (String) JOptionPane.showInputDialog(this, "Enter list name", "Found " + list.getAllProbes().length + " probes", JOptionPane.QUESTION_MESSAGE, null, null, list.name());
                if (groupName == null) {
                    // Remove the list which will have been created by this stage
                    list.delete();
                    // They cancelled
                    return;
                }
                if (groupName.length() == 0)
                    // Try again
                    continue;
                break;
            }
            list.setName(groupName);
        }
    } else if (ae.getActionCommand().equals("save_image")) {
        ImageSaver.saveImage(strandBiasPlotPanel);
    } else {
        throw new IllegalArgumentException("Unknown command " + ae.getActionCommand());
    }
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) ActionEvent(java.awt.event.ActionEvent) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) OrderedListSelector(uk.ac.babraham.SeqMonk.Dialogs.OrderedListSelector)

Example 20 with DataStore

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

the class ReplicateSetStatsFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    Chromosome[] chromosomes = collection.genome().getAllChromosomes();
    // Make up the list of DataStores in each replicate set
    DataStore[][] stores = new DataStore[replicateSets.length][];
    for (int i = 0; i < replicateSets.length; i++) {
        stores[i] = replicateSets[i].dataStores();
    }
    Vector<ProbeTTestValue> newListProbesVector = new Vector<ProbeTTestValue>();
    for (int c = 0; c < chromosomes.length; c++) {
        progressUpdated("Processing probes on Chr" + chromosomes[c].name(), c, chromosomes.length);
        Probe[] probes = startingList.getProbesForChromosome(chromosomes[c]);
        for (int p = 0; p < probes.length; p++) {
            if (cancel) {
                cancel = false;
                progressCancelled();
                return;
            }
            double[][] values = new double[replicateSets.length][];
            for (int i = 0; i < replicateSets.length; i++) {
                values[i] = new double[stores[i].length];
                for (int j = 0; j < stores[i].length; j++) {
                    try {
                        values[i][j] = stores[i][j].getValueForProbe(probes[p]);
                    } catch (SeqMonkException e) {
                    }
                }
            }
            double pValue = 0;
            try {
                if (replicateSets.length == 1) {
                    pValue = TTest.calculatePValue(values[0], 0);
                } else if (replicateSets.length == 2) {
                    pValue = TTest.calculatePValue(values[0], values[1]);
                } else {
                    pValue = AnovaTest.calculatePValue(values);
                }
            } catch (SeqMonkException e) {
                throw new IllegalStateException(e);
            }
            newListProbesVector.add(new ProbeTTestValue(probes[p], pValue));
        }
    }
    ProbeTTestValue[] newListProbes = newListProbesVector.toArray(new ProbeTTestValue[0]);
    // Do the multi-testing correction if necessary
    if (multiTest) {
        BenjHochFDR.calculateQValues(newListProbes);
    }
    ProbeList newList;
    if (multiTest) {
        newList = new ProbeList(startingList, "", "", "Q-value");
        for (int i = 0; i < newListProbes.length; i++) {
            if (newListProbes[i].q <= cutoff) {
                newList.addProbe(newListProbes[i].probe, new Float(newListProbes[i].q));
            }
        }
    } else {
        newList = new ProbeList(startingList, "", "", "P-value");
        for (int i = 0; i < newListProbes.length; i++) {
            if (newListProbes[i].p <= cutoff) {
                newList.addProbe(newListProbes[i].probe, new Float(newListProbes[i].p));
            }
        }
    }
    filterFinished(newList);
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) ProbeTTestValue(uk.ac.babraham.SeqMonk.Analysis.Statistics.ProbeTTestValue) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Vector(java.util.Vector)

Aggregations

DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)46 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)21 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)19 Vector (java.util.Vector)15 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)15 DataGroup (uk.ac.babraham.SeqMonk.DataTypes.DataGroup)11 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)11 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)11 JPanel (javax.swing.JPanel)10 ReplicateSet (uk.ac.babraham.SeqMonk.DataTypes.ReplicateSet)10 GridBagConstraints (java.awt.GridBagConstraints)9 JLabel (javax.swing.JLabel)9 GridBagLayout (java.awt.GridBagLayout)8 File (java.io.File)8 ActionEvent (java.awt.event.ActionEvent)7 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 JComboBox (javax.swing.JComboBox)7 JScrollPane (javax.swing.JScrollPane)7 HiCDataStore (uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore)7