Search in sources :

Example 1 with Annotation

use of org.dishevelled.bio.annotation.Annotation in project dishevelled by heuermh.

the class AssemblyModel method traversalsFor.

/**
 * Create and return a list of path traversals for the specified path.
 *
 * @param path path, must not be null
 * @return a list of path traversals for the specified path
 */
static List<Traversal> traversalsFor(final Path path) {
    checkNotNull(path);
    int size = path.getSegments().size();
    List<Traversal> traversals = new ArrayList<Traversal>(size);
    Reference source = null;
    Reference target = null;
    String overlap = null;
    Map<String, Annotation> emptyAnnotations = Collections.emptyMap();
    for (int i = 0; i < size; i++) {
        target = path.getSegments().get(i);
        if (i > 0) {
            overlap = (path.getOverlaps() != null && path.getOverlaps().size() > i) ? path.getOverlaps().get(i - 1) : null;
        }
        if (source != null) {
            Traversal traversal = new Traversal(path.getName(), i - 1, source, target, overlap, emptyAnnotations);
            traversals.add(traversal);
        }
        source = target;
    }
    return traversals;
}
Also used : Reference(org.dishevelled.bio.assembly.gfa1.Reference) ArrayList(java.util.ArrayList) Traversal(org.dishevelled.bio.assembly.gfa1.Traversal) Annotation(org.dishevelled.bio.annotation.Annotation)

Example 2 with Annotation

use of org.dishevelled.bio.annotation.Annotation in project dishevelled-bio by heuermh.

the class Gfa1ToGfa2 method call.

@Override
public Integer call() throws Exception {
    PrintWriter writer = null;
    try {
        writer = writer(outputGfa2File);
        final PrintWriter w = writer;
        Gfa1Reader.stream(reader(inputGfa1File), new Gfa1Adapter() {

            @Override
            public boolean header(final Header header) {
                // convert VN:Z:1.0 to VN:Z:2.0 annotation if present
                if (header.getAnnotations().containsKey("VN")) {
                    if (!"1.0".equals(header.getAnnotations().get("VN").getValue())) {
                        throw new RuntimeException("cannot convert input as GFA 1.0, was " + header.getAnnotations().get("VN").getValue());
                    }
                    Map<String, Annotation> annotations = new HashMap<String, Annotation>();
                    annotations.put("VN", new Annotation("VN", "Z", "2.0"));
                    for (Annotation annotation : header.getAnnotations().values()) {
                        if (!"VN".equals(annotation.getName())) {
                            annotations.put(annotation.getName(), annotation);
                        }
                    }
                    Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Header(annotations), w);
                } else {
                    Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Header(header.getAnnotations()), w);
                }
                return true;
            }

            @Override
            public boolean segment(final Segment segment) {
                if (segment.getSequence() != null) {
                    Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Segment(segment.getId(), segment.getSequence().length(), segment.getSequence(), segment.getAnnotations()), w);
                } else if (segment.getAnnotations().containsKey("LN")) {
                    Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Segment(segment.getId(), Integer.parseInt(segment.getAnnotations().get("LN").getValue()), segment.getSequence(), segment.getAnnotations()), w);
                } else {
                    Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Segment(segment.getId(), 0, segment.getSequence(), segment.getAnnotations()), w);
                }
                return true;
            }

            @Override
            public boolean link(final Link link) {
                Position unknown = new Position(0, false);
                Alignment alignment = link.getOverlap() == null ? null : Alignment.valueOf(link.getOverlap());
                Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Edge(null, toGfa2Reference(link.getSource()), toGfa2Reference(link.getTarget()), unknown, unknown, unknown, unknown, alignment, link.getAnnotations()), w);
                return true;
            }

            @Override
            public boolean containment(final Containment containment) {
                Position unknown = new Position(0, false);
                Position targetStart = new Position(containment.getPosition(), false);
                Alignment alignment = containment.getOverlap() == null ? null : Alignment.valueOf(containment.getOverlap());
                Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Edge(null, toGfa2Reference(containment.getContainer()), toGfa2Reference(containment.getContained()), unknown, unknown, targetStart, unknown, alignment, containment.getAnnotations()), w);
                return true;
            }

            @Override
            public boolean path(final Path path) {
                Gfa2Writer.write(new org.dishevelled.bio.assembly.gfa2.Path(path.getName(), toGfa2References(path.getSegments()), path.getAnnotations()), w);
                return true;
            }
        });
        return 0;
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
        // empty
        }
    }
}
Also used : Path(org.dishevelled.bio.assembly.gfa1.Path) Position(org.dishevelled.bio.assembly.gfa2.Position) Annotation(org.dishevelled.bio.annotation.Annotation) Segment(org.dishevelled.bio.assembly.gfa1.Segment) CommandLineParseException(org.dishevelled.commandline.CommandLineParseException) Alignment(org.dishevelled.bio.assembly.gfa2.Alignment) Header(org.dishevelled.bio.assembly.gfa1.Header) Gfa1Adapter(org.dishevelled.bio.assembly.gfa1.Gfa1Adapter) HashMap(java.util.HashMap) Map(java.util.Map) Link(org.dishevelled.bio.assembly.gfa1.Link) PrintWriter(java.io.PrintWriter) Containment(org.dishevelled.bio.assembly.gfa1.Containment)

Example 3 with Annotation

use of org.dishevelled.bio.annotation.Annotation in project dishevelled-bio by heuermh.

the class GafRecord method valueOf.

/**
 * Parse a GAF record from the specified value.
 *
 * @param value value, must not be null
 * @return a GAF record parsed from the specified value
 */
public static GafRecord valueOf(final String value) {
    checkNotNull(value);
    List<String> tokens = Splitter.on("\t").splitToList(value);
    if (tokens.size() < 12) {
        throw new IllegalArgumentException("GAF record value must have at least twelve tokens, was " + tokens.size());
    }
    String queryName = tokens.get(0);
    long queryLength = Long.parseLong(tokens.get(1));
    long queryStart = Long.parseLong(tokens.get(2));
    long queryEnd = Long.parseLong(tokens.get(3));
    char strand = tokens.get(4).charAt(0);
    String pathName = tokens.get(5);
    long pathLength = Long.parseLong(tokens.get(6));
    long pathStart = Long.parseLong(tokens.get(7));
    long pathEnd = Long.parseLong(tokens.get(8));
    long matches = Long.parseLong(tokens.get(9));
    long alignmentBlockLength = Long.parseLong(tokens.get(10));
    int mappingQuality = Integer.parseInt(tokens.get(11));
    ImmutableMap.Builder<String, Annotation> annotations = ImmutableMap.builder();
    for (int i = 12; i < tokens.size(); i++) {
        String token = tokens.get(i);
        if (!token.isEmpty()) {
            Annotation annotation = Annotation.valueOf(tokens.get(i));
            annotations.put(annotation.getName(), annotation);
        }
    }
    return new GafRecord(queryName, queryLength, queryStart, queryEnd, strand, pathName, pathLength, pathStart, pathEnd, matches, alignmentBlockLength, mappingQuality, annotations.build());
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Annotation(org.dishevelled.bio.annotation.Annotation)

Example 4 with Annotation

use of org.dishevelled.bio.annotation.Annotation in project dishevelled-bio by heuermh.

the class GenerateGaf method main.

public static void main(final String[] args) throws Exception {
    PrintWriter writer = null;
    try {
        writer = writer(null);
        for (int i = 0; i < 10_000_000; i++) {
            String queryName = "query";
            long queryLength = 1_000_000L;
            long queryStart = 20_000L;
            long queryEnd = 40_000L;
            char strand = '+';
            String pathName = "path";
            long pathLength = 20_000L;
            long pathStart = 2_000L;
            long pathEnd = 18_000L;
            long matches = 16_000L;
            long alignmentBlockLength = 16_000L;
            int mappingQuality = 60;
            ImmutableMap.Builder<String, Annotation> annotations = ImmutableMap.builder();
            annotations.put("cs", Annotation.valueOf("cs:Z:16000M"));
            writer.println(new GafRecord(queryName, queryLength, queryStart, queryEnd, strand, pathName, pathLength, pathStart, pathEnd, matches, alignmentBlockLength, mappingQuality, annotations.build()).toString());
        }
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
        // empty
        }
    }
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Annotation(org.dishevelled.bio.annotation.Annotation) GafRecord(org.dishevelled.bio.alignment.gaf.GafRecord) PrintWriter(java.io.PrintWriter)

Example 5 with Annotation

use of org.dishevelled.bio.annotation.Annotation in project dishevelled-bio by heuermh.

the class Set method valueOf.

/**
 * Parse a set GFA 2.0 record from the specified value.
 *
 * @param value value, must not be null
 * @return a set GFA 2.0 record parsed from the specified value
 */
public static Set valueOf(final String value) {
    checkNotNull(value);
    checkArgument(value.startsWith("U"), "set value must start with U");
    List<String> tokens = Splitter.on("\t").splitToList(value);
    if (tokens.size() < 3) {
        throw new IllegalArgumentException("set value must have at least three tokens, was " + tokens.size());
    }
    String id = "*".equals(tokens.get(1)) ? null : tokens.get(1);
    java.util.Set<String> ids = ImmutableSet.copyOf(Splitter.on(" ").split(tokens.get(2)));
    ImmutableMap.Builder<String, Annotation> annotations = ImmutableMap.builder();
    for (int i = 3; i < tokens.size(); i++) {
        String token = tokens.get(i);
        if (!token.isEmpty()) {
            Annotation annotation = Annotation.valueOf(token);
            annotations.put(annotation.getName(), annotation);
        }
    }
    return new Set(id, ids, annotations.build());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Annotation(org.dishevelled.bio.annotation.Annotation)

Aggregations

Annotation (org.dishevelled.bio.annotation.Annotation)34 ImmutableMap (com.google.common.collect.ImmutableMap)17 Before (org.junit.Before)13 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 Test (org.junit.Test)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 GafRecord (org.dishevelled.bio.alignment.gaf.GafRecord)1 Containment (org.dishevelled.bio.assembly.gfa1.Containment)1 Gfa1Adapter (org.dishevelled.bio.assembly.gfa1.Gfa1Adapter)1 Header (org.dishevelled.bio.assembly.gfa1.Header)1 Link (org.dishevelled.bio.assembly.gfa1.Link)1 Path (org.dishevelled.bio.assembly.gfa1.Path)1 Reference (org.dishevelled.bio.assembly.gfa1.Reference)1 Segment (org.dishevelled.bio.assembly.gfa1.Segment)1