Search in sources :

Example 1 with Containment

use of org.dishevelled.bio.assembly.gfa1.Containment 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 2 with Containment

use of org.dishevelled.bio.assembly.gfa1.Containment in project dishevelled-bio by heuermh.

the class IdentifyGfa1 method call.

@Override
public Integer call() throws Exception {
    BufferedReader reader = null;
    PrintWriter writer = null;
    try {
        reader = reader(inputGfa1File);
        writer = writer(outputGfa1File);
        final PrintWriter w = writer;
        final AtomicLong count = new AtomicLong();
        Gfa1Reader.stream(reader, new Gfa1Listener() {

            @Override
            public boolean record(final Gfa1Record gfa1Record) {
                String id = String.valueOf(count.getAndIncrement());
                if (gfa1Record instanceof Containment) {
                    Containment containment = (Containment) gfa1Record;
                    Gfa1Writer.write(new Containment(containment.getContainer(), containment.getContained(), containment.getPosition(), containment.getOverlap(), addId(id, containment.getAnnotations())), w);
                } else if (gfa1Record instanceof Link) {
                    Link link = (Link) gfa1Record;
                    Gfa1Writer.write(new Link(link.getSource(), link.getTarget(), link.getOverlap(), addId(id, link.getAnnotations())), w);
                } else if (gfa1Record instanceof Traversal) {
                    Traversal traversal = (Traversal) gfa1Record;
                    Gfa1Writer.write(new Traversal(traversal.getPathName(), traversal.getOrdinal(), traversal.getSource(), traversal.getTarget(), traversal.getOverlap(), addId(id, traversal.getAnnotations())), w);
                } else {
                    Gfa1Writer.write(gfa1Record, w);
                }
                return true;
            }
        });
        return 0;
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        // ignore
        }
        try {
            writer.close();
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : Gfa1Record(org.dishevelled.bio.assembly.gfa1.Gfa1Record) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferedReader(java.io.BufferedReader) Traversal(org.dishevelled.bio.assembly.gfa1.Traversal) Gfa1Listener(org.dishevelled.bio.assembly.gfa1.Gfa1Listener) Link(org.dishevelled.bio.assembly.gfa1.Link) CommandLineParseException(org.dishevelled.commandline.CommandLineParseException) PrintWriter(java.io.PrintWriter) Containment(org.dishevelled.bio.assembly.gfa1.Containment)

Aggregations

PrintWriter (java.io.PrintWriter)2 Containment (org.dishevelled.bio.assembly.gfa1.Containment)2 Link (org.dishevelled.bio.assembly.gfa1.Link)2 CommandLineParseException (org.dishevelled.commandline.CommandLineParseException)2 BufferedReader (java.io.BufferedReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Annotation (org.dishevelled.bio.annotation.Annotation)1 Gfa1Adapter (org.dishevelled.bio.assembly.gfa1.Gfa1Adapter)1 Gfa1Listener (org.dishevelled.bio.assembly.gfa1.Gfa1Listener)1 Gfa1Record (org.dishevelled.bio.assembly.gfa1.Gfa1Record)1 Header (org.dishevelled.bio.assembly.gfa1.Header)1 Path (org.dishevelled.bio.assembly.gfa1.Path)1 Segment (org.dishevelled.bio.assembly.gfa1.Segment)1 Traversal (org.dishevelled.bio.assembly.gfa1.Traversal)1 Alignment (org.dishevelled.bio.assembly.gfa2.Alignment)1 Position (org.dishevelled.bio.assembly.gfa2.Position)1