Search in sources :

Example 36 with Feature

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

the class FeatureProbeGroupGenerator method nextSet.

/**
 * Provides the next set of probe from this generator
 *
 * @return A list of probes in the next set.
 */
public Probe[] nextSet() {
    if (featureIndex >= features.length) {
        return null;
    }
    Feature thisFeature = features[featureIndex];
    int thisStartIndex = probeStartIndex;
    if (thisStartIndex >= probes.length) {
        thisStartIndex = probes.length - 1;
    }
    v.clear();
    // 5kb before the current feature
    while (thisStartIndex > 0) {
        if (!probes[thisStartIndex].chromosome().name().equals(thisFeature.chromosomeName())) {
            thisStartIndex++;
            break;
        }
        if (probes[thisStartIndex].end() < thisFeature.location().start() - 5000)
            break;
        thisStartIndex--;
    }
    if (thisStartIndex < 0)
        thisStartIndex = 0;
    while (true) {
        if (thisStartIndex >= probes.length)
            break;
        if (!probes[thisStartIndex].chromosome().name().equals(thisFeature.chromosomeName()))
            break;
        if (SequenceRead.overlaps(probes[thisStartIndex].packedPosition(), thisFeature.location().packedPosition())) {
            v.add(probes[thisStartIndex]);
        }
        thisStartIndex++;
    }
    probeStartIndex = thisStartIndex;
    ++featureIndex;
    return v.toArray(new Probe[0]);
}
Also used : Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)

Example 37 with Feature

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

Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)37 Vector (java.util.Vector)23 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)23 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)20 Location (uk.ac.babraham.SeqMonk.DataTypes.Genome.Location)13 SplitLocation (uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation)11 ProbeSet (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeSet)9 AnnotationSet (uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet)7 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)7 QuantitationStrandType (uk.ac.babraham.SeqMonk.DataTypes.Sequence.QuantitationStrandType)5 Hashtable (java.util.Hashtable)4 AbstractTableModel (javax.swing.table.AbstractTableModel)4 TableModel (javax.swing.table.TableModel)4 LongVector (uk.ac.babraham.SeqMonk.Utilities.LongVector)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 Enumeration (java.util.Enumeration)3 HashSet (java.util.HashSet)3 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)3 FileInputStream (java.io.FileInputStream)2