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
}
}
}
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
}
}
}
Aggregations