Skip to content

Instantly share code, notes, and snippets.

@heuermh
Last active December 10, 2019 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heuermh/27c645b1f8272fc33136735ddfb8a226 to your computer and use it in GitHub Desktop.
Save heuermh/27c645b1f8272fc33136735ddfb8a226 to your computer and use it in GitHub Desktop.
Example for dishevelled-bio assembly gfa1 package
import static org.dishevelled.compress.Readers.reader;
import java.io.*;
import org.dishevelled.bio.assembly.gfa1.*;
void read(@Nullable File file) {
// this Readers.reader method has some magic in it:
// if file is null, use stdin
// handles gzip, bgzf (block-gzipped), and bzip2 compression transparently
try (BufferedReader reader = reader(file)) {
// stream over reader, use callback for each parsed generic record
Gfa1Reader.stream(reader, new Gfa1Listener() {
@Override
public boolean record(final Gfa1Record gfa1Record) {
if (gfa1Record instanceof Path) {
Path path = (Path) gfa1Record;
// ...
}
// return true to continue processing
return true;
}
});
// or use the adapter specific type record callbacks
Gfa1Reader.stream(reader, new Gfa1Adapter() {
@Override
public boolean path(final Path path) {
// access required fields
path.getName();
path.getSegments();
// access optional fields
if (path.hasOverlaps()) {
path.getOverlaps();
}
// access optional fields wrapped in Optional
path.getOverlapsOpt().get();
// return true to continue processing
return true;
}
@Override
public boolean segment(final Segment segment) {
// access required fields
path.getId();
// access optional fields
if (segment.hasSequence()) {
segment.getSequence();
}
// access optional fields wrapped in Optional
segment.getSequenceOpt().get();
// access optional reserved tag fields (e.g. 'LN')
if (segment.containsLn()) { // short field name version
segment.getLn();
}
if (segment.containsLength()) { // long field name version
segment.getLength();
}
// access optional reserved tag fields wrapped in Optional
segment.getLnOpt().get();
segment.getLengthOpt().get();
// access optional non-reserved tag fields as String values, may return null
String zz = segment.getTags().get("zz");
// access typed optional non-reserved tag fields, may throw exception if type does not match
if (segment.containsTagKey("zz")) {
float f = segment.getTagFloat("zz");
}
// access typed optional non-reserved tag fields wrapped in Optional
float f = segment.getTagFloatOpt("zz").getOrElse(0.0f);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment