Search in sources :

Example 21 with ProbeList

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList 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 22 with ProbeList

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList 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 23 with ProbeList

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

the class FeatureNameFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    annotationType = optionsPanel.annotationTypeBox.getSelectedItem().toString();
    stripSuffixes = optionsPanel.stripSuffixesBox.isSelected();
    stripTranscriptSuffixes = optionsPanel.stripTranscriptSuffixesBox.isSelected();
    ProbeList passedProbes = new ProbeList(startingList, "", "", startingList.getValueName());
    // Since we're going to be making the annotations on the
    // basis of position we should go through all probes one
    // chromosome at a time.  We therefore make a stipulation that
    // not only do the feature names have to match, so do the
    // chromosomes.
    Chromosome[] chrs = collection.genome().getAllChromosomes();
    for (int c = 0; c < chrs.length; c++) {
        // We start by building a list of the feature names we're going to
        // check against.
        HashSet<String> featureNames = new HashSet<String>();
        progressUpdated("Processing features on Chr " + chrs[c].name(), c, chrs.length);
        Probe[] probes = startingList.getProbesForChromosome(chrs[c]);
        Feature[] features = collection.genome().annotationCollection().getFeaturesForType(chrs[c], annotationType);
        for (int f = 0; f < features.length; f++) {
            String name = features[f].name();
            if (stripSuffixes) {
                name = name.replaceFirst("_upstream$", "").replaceAll("_downstream$", "").replaceAll("_gene$", "");
            }
            if (stripTranscriptSuffixes) {
                name = name.replaceAll("-\\d\\d\\d$", "");
            }
            featureNames.add(name);
        }
        // We can now step through the probes looking for a match to the stored feature names
        for (int p = 0; p < probes.length; p++) {
            if (cancel) {
                cancel = false;
                progressCancelled();
                return;
            }
            String name = probes[p].name();
            if (stripSuffixes) {
                name = name.replaceFirst("_upstream$", "").replaceAll("_downstream$", "").replaceAll("_gene$", "");
            }
            if (stripTranscriptSuffixes) {
                name = name.replaceAll("-\\d\\d\\d$", "");
            }
            if (featureNames.contains(name)) {
                passedProbes.addProbe(probes[p], startingList.getValueForProbe(probes[p]));
            }
        }
    }
    filterFinished(passedProbes);
}
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) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) HashSet(java.util.HashSet)

Example 24 with ProbeList

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

the class GeneSetDisplay method valueChanged.

public void valueChanged(ListSelectionEvent ae) {
    int viewRow = table.getSelectedRow();
    if (viewRow >= 0) {
        int modelRow = table.convertRowIndexToModel(viewRow);
        ProbeList tempProbeList = new ProbeList(startingProbeList, filterResultsPVals[modelRow].mappedGeneSet.name(), "temp list", "z score");
        Probe[] tempProbes = filterResultsPVals[modelRow].mappedGeneSet.getProbes();
        for (int j = 0; j < tempProbes.length; j++) {
            tempProbeList.addProbe(tempProbes[j], (float) 0);
        }
        if (currentSelectedProbeList != null) {
            currentSelectedProbeList[0].delete();
        }
        currentSelectedProbeList = new ProbeList[1];
        currentSelectedProbeList[0] = tempProbeList;
    } else {
        if (currentSelectedProbeList != null) {
            currentSelectedProbeList[0].delete();
        }
        currentSelectedProbeList = null;
    }
    if (scatterPlotPanel instanceof GeneSetScatterPlotPanel) {
        System.err.println("trying to update the scatter plot");
        plotPanel.remove((GeneSetScatterPlotPanel) scatterPlotPanel);
        // plotPanel.repaint();
        scatterPlotPanel = new GeneSetScatterPlotPanel(fromStore, toStore, startingProbeList, currentSelectedProbeList, true, dotSizeSlider.getValue(), customRegressionValues, simpleRegression);
        // here
        plotPanel.add(scatterPlotPanel, BorderLayout.CENTER);
        plotPanel.revalidate();
        plotPanel.repaint();
    } else if (scatterPlotPanel instanceof ZScoreScatterPlotPanel) {
        ((ZScoreScatterPlotPanel) scatterPlotPanel).setSubLists(currentSelectedProbeList);
    }
// Check to see if we can add anything...
// if (availableList.getSelectedIndices().length>0) {
// System.out.println("Selected index = " + availableList.getSelectedIndices().toString());
// }
}
Also used : GeneSetScatterPlotPanel(uk.ac.babraham.SeqMonk.Filters.GeneSetFilter.GeneSetScatterPlotPanel) ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

Example 25 with ProbeList

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList 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)

Aggregations

ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)79 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)54 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)32 Vector (java.util.Vector)16 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)15 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)12 HashSet (java.util.HashSet)10 JLabel (javax.swing.JLabel)8 ProbeTTestValue (uk.ac.babraham.SeqMonk.Analysis.Statistics.ProbeTTestValue)8 GridBagConstraints (java.awt.GridBagConstraints)7 GridBagLayout (java.awt.GridBagLayout)7 File (java.io.File)7 JComboBox (javax.swing.JComboBox)7 JPanel (javax.swing.JPanel)7 BufferedReader (java.io.BufferedReader)6 FileReader (java.io.FileReader)6 PrintWriter (java.io.PrintWriter)6 JCheckBox (javax.swing.JCheckBox)6 ProgressRecordDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog)6 RProgressListener (uk.ac.babraham.SeqMonk.R.RProgressListener)6