Skip to main content

BioC data structures and encoder/decoder for Python

Project description

Build status Latest version on PyPI Downloads Coverage Requirements Status License Hits

BioC XML / JSON 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

Getting started

Installing bioc

$ pip install bioc

XML

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)

Compact encoding:

import bioc
bioc.dumps(collection, pretty_print=False)

Incremental BioC serialisation:

import bioc
with bioc.BioCXMLDocumentWriter(filename) as writer:
    writer.write_collection_info(collection)
    for document in collection.documents:
        writer.write_document(document)

Decoding the BioC XML file:

import bioc
# Deserialize ``s`` to a BioC collection object.
collection = bioc.loads(s)

# Deserialize ``fp`` to a BioC collection object.
with open(filename, 'r') as fp:
    collection = bioc.load(fp)

Incrementally decoding the BioC XML file:

import bioc
with bioc.BioCXMLDocumentReader(filename) as reader:
    collection_info = reader.get_collection_info()
    for document in reader:
        # process document
        ...

get_collection_info can be called after the construction of the BioCXMLDocumentReader anytime.

Together with Python coroutines, this can be used to generate BioC XML in an asynchronous, non-blocking fashion.

import bioc
with bioc.BioCXMLDocumentReader(source) as reader, \
     bioc.BioCXMLDocumentWriter(dest) as writer:
    collection_info = reader.get_collection_info()
    writer.write_collection_info(collection_info)
    for document in reader:
        # modify the document
        ...
        writer.write_document(document)

Json

Encoding the BioC collection object collection:

import biocjson
# Serialize ``collection`` to a BioC Json formatted ``str``.
biocjson.dumps(collection, indent=2)

# Serialize ``collection`` as a BioC Json formatted stream to ``fp``.
with open(filename, 'w') as fp
    biocjson.dump(collection, fp, indent=2)

Compact encoding:

import biocjson
biocjson.dumps(collection)

Decoding the BioC Json file:

import biocjson
# Deserialize ``s`` to a BioC collection object.
collection = biocjson.loads(s)

# Deserialize ``fp`` to a BioC collection object.
with open(filename, 'r') as fp:
    collection = biocjson.load(fp)

Json Lines

Incrementally encoding the BioC structure:

from bioc.biocjson import BioCJsonIterWriter
with BioCJsonIterWriter(filename, level=bioc.PASSAGE) as writer:
    for doc in collection.documents:
         for passage in doc.passages:
             writer.write(passage)

or

from bioc.biocjson import toJSON
import jsonlines
with jsonlines.open(filename, 'w') as writer:
    for doc in collection.documents:
         for passage in doc.passages:
             writer.write(toJSON(passage))

Incrementally decoding the BioC Json lines file:

from bioc.biocjson import BioCJsonIterReader
with BioCJsonIterReader(filename, level=bioc.PASSAGE) as reader:
    for passage in reader:
        # process passage
        ...

or

from bioc.biocjson import fromJSON
import jsonlines
with jsonlines.open(filename) as reader:
    for obj in reader:
        passage = fromJSON(obj, level=bioc.PASSAGE)
        ...

Developers

Acknowledgment

Webpage

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.

Reference

If you use bioc in your research, please cite the following paper:

  • Peng,Y., Tudor,C., Torii,M., Wu,C.H., Vijay-Shanker,K. (2014) iSimp in BioC standard format: Enhancing the interoperability of a sentence simplification system. Database: The Journal of Biological Databases and Curation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bioc-1.3.3.tar.gz (15.6 kB view details)

Uploaded Source

File details

Details for the file bioc-1.3.3.tar.gz.

File metadata

  • Download URL: bioc-1.3.3.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.2

File hashes

Hashes for bioc-1.3.3.tar.gz
Algorithm Hash digest
SHA256 91c577d79ba4e5b6b36d22243d65493403f0a63de044d4492913535a1856fe77
MD5 d95b01d07c541fdb25b9d6ef7df36a16
BLAKE2b-256 7d41749e4c3244bf26b0e21a76282ae566845508fb31b6b1a0f717122f308976

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page