Search in sources :

Example 1 with CoreAnnotationSet

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

the class GenomeParser method reloadCacheFiles.

private void reloadCacheFiles(SingleGenome genome, File baseLocation) {
    Enumeration<ProgressListener> el = listeners.elements();
    while (el.hasMoreElements()) {
        el.nextElement().progressUpdated("Reloading cache files", 0, 1);
    }
    CoreAnnotationSet coreAnnotation = new CoreAnnotationSet(genome);
    File cacheDir = new File(baseLocation.getAbsoluteFile() + "/cache/");
    // First we need to get the list of chromosomes and set those
    // up before we go on to add the actual feature sets.
    File chrListFile = new File(baseLocation.getAbsoluteFile() + "/cache/chr_list");
    try {
        BufferedReader br = new BufferedReader(new FileReader(chrListFile));
        String line;
        while ((line = br.readLine()) != null) {
            String[] chrLen = line.split("\\t");
            Chromosome c = genome.addChromosome(chrLen[0]);
            c.setLength(Integer.parseInt(chrLen[1]));
        }
        br.close();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    File[] cacheFiles = cacheDir.listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            return pathname.getName().toLowerCase().endsWith(".cache");
        }
    });
    for (int i = 0; i < cacheFiles.length; i++) {
        // Update the listeners
        String name = cacheFiles[i].getName();
        name = name.replaceAll("\\.cache$", "");
        String[] chrType = name.split("%", 2);
        if (chrType.length != 2) {
            throw new IllegalStateException("Cache name '" + name + "' didn't split into chr and type");
        }
        // If the feature name had a forward slash in it we've replaced it with 3 underscores
        chrType[1] = chrType[1].replaceAll("___", "/");
        coreAnnotation.addPreCachedFile(chrType[1], chrType[0], cacheFiles[i]);
    }
    genome.annotationCollection().addAnnotationSets(new AnnotationSet[] { coreAnnotation });
}
Also used : Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) 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) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FileFilter(java.io.FileFilter) File(java.io.File)

Example 2 with CoreAnnotationSet

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

the class GenomeParser method parseGenomeFiles.

private void parseGenomeFiles(SingleGenome genome, File baseLocation) {
    // which defines the size and extent of the chromosomes
    try {
        parseChrListFile(genome, baseLocation);
    } catch (Exception ex) {
        Enumeration<ProgressListener> en = listeners.elements();
        while (en.hasMoreElements()) {
            en.nextElement().progressExceptionReceived(ex);
        }
        return;
    }
    // We need a list of all of the .dat files inside the baseLocation
    File[] files = baseLocation.listFiles(new FileFilter() {

        public boolean accept(File f) {
            if (f.getName().toLowerCase().endsWith(".dat")) {
                return true;
            } else {
                return false;
            }
        }
    });
    AnnotationSet coreAnnotation = new CoreAnnotationSet(genome);
    for (int i = 0; i < files.length; i++) {
        // Update the listeners
        Enumeration<ProgressListener> e = listeners.elements();
        while (e.hasMoreElements()) {
            e.nextElement().progressUpdated("Loading Genome File " + files[i].getName(), i, files.length);
        }
        try {
            processEMBLFile(files[i], coreAnnotation, genome);
        } catch (Exception ex) {
            Enumeration<ProgressListener> en = listeners.elements();
            while (en.hasMoreElements()) {
                en.nextElement().progressExceptionReceived(ex);
            }
            return;
        }
    }
    // Update the listeners
    Enumeration<ProgressListener> e = listeners.elements();
    while (e.hasMoreElements()) {
        e.nextElement().progressUpdated("Caching annotation data", 1, 1);
    }
    // Now do the same thing for gff files.
    // We need a list of all of the .gff/gtf files inside the baseLocation
    files = baseLocation.listFiles(new FileFilter() {

        public boolean accept(File f) {
            if (f.getName().toLowerCase().endsWith(".gff") || f.getName().toLowerCase().endsWith(".gtf") || f.getName().toLowerCase().endsWith(".gff3") || f.getName().toLowerCase().endsWith(".gff.gz") || f.getName().toLowerCase().endsWith(".gtf.gz") || f.getName().toLowerCase().endsWith(".gff3.gz")) {
                return true;
            } else {
                return false;
            }
        }
    });
    GFF3AnnotationParser gffParser = new GFF3AnnotationParser(genome);
    for (int i = 0; i < files.length; i++) {
        // System.err.println("Parsing "+files[i]);
        // Update the listeners
        e = listeners.elements();
        while (e.hasMoreElements()) {
            e.nextElement().progressUpdated("Loading Genome File " + files[i].getName(), i, files.length);
        }
        try {
            AnnotationSet[] newSets = gffParser.parseAnnotation(files[i], genome, "");
            for (int s = 0; s < newSets.length; s++) {
                Feature[] features = newSets[s].getAllFeatures();
                for (int f = 0; f < features.length; f++) {
                    coreAnnotation.addFeature(features[f]);
                }
            }
        } catch (Exception ex) {
            Enumeration<ProgressListener> en = listeners.elements();
            while (en.hasMoreElements()) {
                en.nextElement().progressExceptionReceived(ex);
            }
            return;
        }
    }
    // Update the listeners
    e = listeners.elements();
    while (e.hasMoreElements()) {
        e.nextElement().progressUpdated("Caching annotation data", 1, 1);
    }
    genome.annotationCollection().addAnnotationSets(new AnnotationSet[] { coreAnnotation });
// Debugging - put out some stats
// System.err.println("Made genome with "+genome.getAllChromosomes().length+" chromosomes");
// System.err.println("There are "+genome.annotationCollection().listAvailableFeatureTypes().length+" different feature types");
}
Also used : Enumeration(java.util.Enumeration) AnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet) CoreAnnotationSet(uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) 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) FileFilter(java.io.FileFilter) File(java.io.File)

Example 3 with CoreAnnotationSet

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

Aggregations

IOException (java.io.IOException)3 CoreAnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.CoreAnnotationSet)3 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)3 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)3 File (java.io.File)2 FileFilter (java.io.FileFilter)2 Enumeration (java.util.Enumeration)2 AnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet)2 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 DataGroup (uk.ac.babraham.SeqMonk.DataTypes.DataGroup)1 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)1 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)1 Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)1 PairedDataSet (uk.ac.babraham.SeqMonk.DataTypes.PairedDataSet)1 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)1