BioC data structures and encoder/decoder for Python
BioC XML format can be used to share text documents and annotations.
bioc exposes an API familiar to users of the standard library marshal and pickle modules.
Development of bioc happens on GitHub: https://github.com/yfpeng/bioc
$ pip install --pre bioc
Encoding the BioC collection object collection:
import bioc # Serialize ``collection`` to a BioC formatted ``str``. bioc.dumps(collection) # Serialize ``collection`` as a BioC formatted stream to ``fp``. with open(filename, 'w') as fp bioc.dump(collection, fp)
import bioc bioc.dumps(collection, pretty_print=False)
Incremental BioC serialisation:
import bioc with bioc.iterwrite(filename, collection) as writer: for document in collection.documents: writer.writedocument(document)
Decoding the BioC XML file:
import json # Deserialize ``s`` to a BioC collection object. collection = json.loads(s) # Deserialize ``fp`` to a BioC collection object. with open(filename, 'r') as fp: bioc.load(fp)
Incrementally decoding the BioC XML file:
import bioc with bioc.iterparse(filename) as parser: collection_info = parser.get_collection_info() for document in parser: # process document ...
get_collection_info can be called after the construction of the
Together with Python coroutines, this can be used to generate BioC XML in an asynchronous, non-blocking fashion.
import bioc with bioc.iterparse(filename) as parser: with bioc.iterwrite(dst, parser.get_collection_info()) as writer: for document in parser: # modify the document ... writer.writedocument(document)
The official BioC webpage is available with all up-to-date instructions, code, and corpora in the BioC format, and other research on, based on and related to BioC.