Search in sources :

Example 6 with AnnotationSet

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

Example 7 with AnnotationSet

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

the class SeqMonkParser method parseAnnotation.

/**
 * Parses an external set of annotations
 *
 * @param sections The tab split initial annotation line
 * @throws SeqMonkException
 * @throws IOException Signals that an I/O exception has occurred.
 */
private AnnotationSet parseAnnotation(String[] sections) throws SeqMonkException, IOException {
    if (sections.length != 3) {
        throw new SeqMonkException("Annotation line didn't contain 3 sections");
    }
    AnnotationSet set = new AnnotationSet(application.dataCollection().genome(), sections[1]);
    int featureCount = Integer.parseInt(sections[2]);
    for (int i = 0; i < featureCount; i++) {
        if (i % 1000 == 0) {
            progressUpdated("Parsing annotation in " + set.name(), i, featureCount);
        }
        sections = br.readLine().split("\\t");
        Chromosome c;
        try {
            c = application.dataCollection().genome().getChromosome(sections[1]).chromosome();
        } catch (Exception sme) {
            Enumeration<ProgressListener> e = listeners.elements();
            while (e.hasMoreElements()) {
                e.nextElement().progressWarningReceived(new SeqMonkException("Annotation feature could not be mapped to chromosome '" + sections[1] + "'"));
            }
            continue;
        }
        Feature f = new Feature(sections[0], c.name());
        // TODO: Can we improve this to not use a Split Location each time?
        f.setLocation(new SplitLocation(sections[2]));
        for (int a = 3; a + 1 < sections.length; a += 2) {
            f.addAttribute(sections[a], sections[a + 1]);
        }
        set.addFeature(f);
    }
    set.finalise();
    return set;
}
Also used : Enumeration(java.util.Enumeration) SplitLocation(uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException)

Example 8 with AnnotationSet

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

the class SeqMonkParser method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    genomeLoaded = false;
    exceptionReceived = null;
    try {
        String line;
        String[] sections;
        Vector<AnnotationSet> annotationSets = new Vector<AnnotationSet>();
        while ((line = br.readLine()) != null) {
            sections = line.split("\\t");
            // Now we look where to send this...
            if (sections[0].equals("SeqMonk Data Version")) {
                parseDataVersion(sections);
            } else if (sections[0].equals("Features")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseFeatures(sections);
            } else if (sections[0].equals("Samples")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseSamples(sections);
            } else if (sections[0].equals("Annotation")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                annotationSets.add(parseAnnotation(sections));
            } else if (sections[0].equals("Data Groups")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                try {
                    parseGroups(sections);
                } catch (SeqMonkException ex) {
                    if (ex.getMessage().contains("ambiguous")) {
                        Enumeration<ProgressListener> e = listeners.elements();
                        while (e.hasMoreElements()) {
                            e.nextElement().progressWarningReceived(ex);
                        }
                    } else {
                        throw ex;
                    }
                }
            } else if (sections[0].equals("Replicate Sets")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseReplicates(sections);
            } else if (sections[0].equals("Probes")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseProbes(sections);
            } else if (sections[0].equals("Lists")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseLists(sections);
            } else if (sections[0].equals("Genome")) {
                if (forcedAssembly) {
                    genomeLoaded = true;
                    continue;
                }
                parseGenome(sections);
                if (exceptionReceived != null) {
                    Enumeration<ProgressListener> e = listeners.elements();
                    while (e.hasMoreElements()) {
                        // In this case we put out a dummy empty dataset since
                        // we've already entered the data into the collection by now
                        e.nextElement().progressCancelled();
                    }
                    return;
                }
            } else if (sections[0].equals("Visible Stores")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                parseVisibleStores(sections);
            } else if (sections[0].equals("Display Preferences")) {
                if (!genomeLoaded) {
                    throw new SeqMonkException("No genome definition found before data");
                }
                // Add any annotation sets we've parsed at this point
                application.dataCollection().genome().annotationCollection().addAnnotationSets(annotationSets.toArray(new AnnotationSet[0]));
                parseDisplayPreferences(sections);
            } else {
                throw new SeqMonkException("Didn't recognise section '" + sections[0] + "' in seqmonk file");
            }
        }
        // We're finished with the file
        br.close();
        cleanUpFeatureTracks();
    } catch (Exception ex) {
        Enumeration<ProgressListener> e = listeners.elements();
        while (e.hasMoreElements()) {
            e.nextElement().progressExceptionReceived(ex);
        }
        try {
            br.close();
        } catch (IOException e1) {
            throw new IllegalStateException(e1);
        }
        return;
    }
    Enumeration<ProgressListener> e = listeners.elements();
    while (e.hasMoreElements()) {
        // In this case we put out a dummy empty dataset since
        // we've already entered the data into the collection by now
        e.nextElement().progressComplete("datasets_loaded", new DataSet[0]);
    }
    application.resetChangesWereMade();
}
Also used : Enumeration(java.util.Enumeration) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) IOException(java.io.IOException) Vector(java.util.Vector) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException)

Example 9 with AnnotationSet

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

the class DataViewer method mousePressed.

/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
public void mousePressed(MouseEvent me) {
    JTree tree = (JTree) me.getSource();
    tree.setSelectionRow(tree.getRowForLocation(me.getX(), me.getY()));
    // Check if they right-clicked
    if ((me.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
        // I'm not sure if this is a timing issue, but we can get the selection path being null
        if (tree.getSelectionPath() == null)
            return;
        Object clickedItem = tree.getSelectionPath().getLastPathComponent();
        if (clickedItem instanceof DataSet) {
            new DataPopupMenu((DataSet) clickedItem).show(dataTree, me.getX(), me.getY());
        } else if (clickedItem instanceof DataGroup) {
            new GroupPopupMenu((DataGroup) clickedItem).show(dataTree, me.getX(), me.getY());
        } else if (clickedItem instanceof ReplicateSet) {
            new ReplicatePopupMenu((ReplicateSet) clickedItem).show(dataTree, me.getX(), me.getY());
        } else if (clickedItem instanceof ProbeList) {
            new ProbePopupMenu((ProbeList) clickedItem).show(probeSetTree, me.getX(), me.getY());
        } else if (clickedItem instanceof AnnotationSet) {
            new AnnotationPopupMenu((AnnotationSet) clickedItem).show(dataTree, me.getX(), me.getY());
        }
    } else // Check if they double clicked
    if (me.getClickCount() == 2) {
        // I'm not sure if this is a timing issue, but we can get the selection path being null
        if (tree.getSelectionPath() == null)
            return;
        Object clickedItem = tree.getSelectionPath().getLastPathComponent();
        if (clickedItem instanceof DataSet) {
            new DataPopupMenu((DataSet) clickedItem).actionPerformed(new ActionEvent(this, 0, "properties"));
        } else if (clickedItem instanceof DataGroup) {
            new GroupPopupMenu((DataGroup) clickedItem).actionPerformed(new ActionEvent(this, 0, "properties"));
        } else if (clickedItem instanceof ReplicateSet) {
            new ReplicatePopupMenu((ReplicateSet) clickedItem).actionPerformed(new ActionEvent(this, 0, "properties"));
        } else if (clickedItem instanceof ProbeList) {
            new ProbePopupMenu((ProbeList) clickedItem).actionPerformed(new ActionEvent(this, 0, "view"));
        } else if (clickedItem instanceof AnnotationSet) {
            new AnnotationPopupMenu((AnnotationSet) clickedItem).actionPerformed(new ActionEvent(this, 0, "properties"));
        }
    }
}
Also used : DataGroup(uk.ac.babraham.SeqMonk.DataTypes.DataGroup) ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) DataSet(uk.ac.babraham.SeqMonk.DataTypes.DataSet) ActionEvent(java.awt.event.ActionEvent) ReplicateSet(uk.ac.babraham.SeqMonk.DataTypes.ReplicateSet) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) CoreAnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet) JTree(javax.swing.JTree)

Example 10 with AnnotationSet

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

the class FindFeaturesByNameDialog 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("search")) {
        Thread t = new Thread(this);
        t.start();
    } else if (ae.getActionCommand().equals("save_annotation_all")) {
        // Find a name for the type of feature they want to create
        String name = (String) JOptionPane.showInputDialog(this, "Feature type", "Make Annotation Track", JOptionPane.QUESTION_MESSAGE, null, null, "Name matched features");
        // They cancelled
        if (name == null)
            return;
        // Now we can go ahead and make the new annotation set
        AnnotationSet searchAnnotations = new AnnotationSet(dataCollection.genome(), "Named features search results");
        for (int f = 0; f < lastHits.length; f++) {
            Feature feature = new Feature(name, lastHits[f].chromosomeName());
            feature.setLocation(lastHits[f].location());
            AnnotationTagValue[] tags = lastHits[f].getAnnotationTagValues();
            for (int t = 0; t < tags.length; t++) {
                feature.addAttribute(tags[t].tag(), tags[t].value());
            }
            searchAnnotations.addFeature(feature);
        }
        dataCollection.genome().annotationCollection().addAnnotationSets(new AnnotationSet[] { searchAnnotations });
    } else if (ae.getActionCommand().equals("save_annotation_selected")) {
        Feature[] selectedHits = viewer.getSelectedFeatures();
        if (selectedHits.length == 0) {
            JOptionPane.showMessageDialog(this, "There are no selected features from which to make a track", "Can't make track", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        // Find a name for the type of feature they want to create
        String name = (String) JOptionPane.showInputDialog(this, "Feature type", "Make Annotation Track", JOptionPane.QUESTION_MESSAGE, null, null, "Selected name matched features");
        // They cancelled
        if (name == null)
            return;
        // Now we can go ahead and make the new annotation set
        AnnotationSet searchAnnotations = new AnnotationSet(dataCollection.genome(), "Named features search results");
        for (int f = 0; f < selectedHits.length; f++) {
            Feature feature = new Feature(name, selectedHits[f].chromosomeName());
            feature.setLocation(selectedHits[f].location());
            AnnotationTagValue[] tags = selectedHits[f].getAnnotationTagValues();
            for (int t = 0; t < tags.length; t++) {
                feature.addAttribute(tags[t].tag(), tags[t].value());
            }
            searchAnnotations.addFeature(feature);
        }
        dataCollection.genome().annotationCollection().addAnnotationSets(new AnnotationSet[] { searchAnnotations });
    }
}
Also used : AnnotationTagValue(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationTagValue) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)

Aggregations

AnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet)11 Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)7 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)6 Enumeration (java.util.Enumeration)5 IOException (java.io.IOException)4 Vector (java.util.Vector)4 DataGroup (uk.ac.babraham.SeqMonk.DataTypes.DataGroup)3 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)3 CoreAnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet)3 Location (uk.ac.babraham.SeqMonk.DataTypes.Genome.Location)3 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)3 ReplicateSet (uk.ac.babraham.SeqMonk.DataTypes.ReplicateSet)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 UnknownHostException (java.net.UnknownHostException)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 AnnotationTagValue (uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationTagValue)2