Skip to main content

A python package for common biological data I/O

Project description

biodata - A standard biological data processing package

The biodata package provides a standard API to access all different kinds of biological data using similar syntax. For each data type, data is processed by the corresponding reader (XX-Reader) and writer (XX-Writer) as a stream of entries. For example, FASTAReader is used to process FASTA file. A call to the method read() from FASTAReader yields a FASTA object. For indexed file, random access is supported through the XX-IReader. For example, an indexed FASTA file can be access by FASTAIReader.

Installation

pip install biodata

Quick Start

For more advanced use, please see the Basic Usage section.

# Reading file contents
from biodata.fasta import FASTAReader
seq_dict = FASTAReader.read_all(lambda fr: {f.name:f.seq for f in fr}, "input.fa") 

from biodata.bed import BEDReader
from genomictools import GenomicCollection
beds = BEDReader.read_all(GenomicCollection, "input.bed")

from biodata.gff import GFF3Reader
from genomictools import GenomicCollection
gff3s = GFF3Reader.read_all(GenomicCollection, "input.gff3")

from biodata.gff import GTFReader
from genomictools import GenomicCollection
gtfs = GTFReader.read_all(GenomicCollection, "input.gtf")

Basic usage

We will demonstrate the use of biodata package using FASTA file.

>seq1
ACGT
>seq2
CCCGGGAAA

Read the first entry

from biodata.fasta import FASTAReader
with FASTAReader(filename) as fr:
	f = fr.read()
	print(f.name, f.seq) # seq1 ACGT

Read entry by entry

from biodata.fasta import FASTAReader
with FASTAReader(filename) as fr:
	for f in fr:
		print(f.name, f.seq)
# seq1 ACGT
# seq2 CCCGGGAAA

with BEDReader(bedfile) as br:
	for b in br:
		print(b.name, str(b.genomic_pos))

Read all entries at once

from biodata.fasta import FASTAReader
fasta_entries = FASTAReader.read_all(list, filename) # list of FASTA

seq_dict = FASTAReader.read_all(lambda fr: {f.name:f.seq for f in fr}, filename) 
# A dictionary with fasta name as key and fasta sequence as value
# {"seq1": "ACGT", "seq2": "CCCGGGAAA"}

# For genomic range data, one could also use GenomicCollection to store them:
from biodata.bed import BEDReader
from genomictools import GenomicCollection
beds = BEDReader.read_all(GenomicCollection, filename)

Peek an entry

from biodata.fasta import FASTAReader
with FASTAReader(filename) as fr:
	f = fr.peek() # Only peek the entry without proceeding to the next entry
	print(f.name, f.seq) # seq1 ACGT
	f = fr.read() # Read the entry and proceed to the next entry
	print(f.name, f.seq) # seq1 ACGT
	f = fr.read()
	print(f.name, f.seq) # seq2 CCCGGGAAA

Read an entry from StringIO

# TextIOBase can be used as input
import io
from biodata.fasta import FASTAReader
FASTAReader.read_all(list, io.StringIO(">seq1\nACGT\n>seq2\nCCCGGGAAA\n"))

Read an indexed file

from biodata.fasta import FASTAIReader
from genomictools import GenomicPos, StrandedGenomicPos
from biodata.bed import BED

fir = FASTAIReader(filename, faifilename) # fai file can be created using 'samtools faidx filename'
f = fir[GenomicPos("seq2:1-4")] # Read from a region without strand
print(f.name, f.seq) # seq2:1-4 CCCG
f = fir[StrandedGenomicPos("seq2:1-4:-")] # Read from a region with strand
print(f.name, f.seq) # seq2:1-4:- CGGG
f = fir[BED("seq2", 0, 4, strand="-")] # Equivalent to StrandedGenomicPos but a BED entry is used
print(f.name, f.seq) # seq2:1-4:- CGGG
fir.close()

Write entry by entry

from biodata.fasta import FASTA, FASTAWriter
with FASTAWriter(output_file) as fw:
	fw.write(FASTA("seq1", "ACGT"))
	fw.write(FASTA("seq2", "CCCGGGAAA"))

Write all entries at once

from biodata.fasta import FASTA, FASTAWriter
fasta_entries = [FASTA("seq1", "ACGT"), fw.write(FASTA("seq2", "CCCGGGAAA"))]
FASTAWriter.write_all(fasta_entries, output_file)

List of supported format

  1. Delimited - tsv, csv (biodata.delimited)
  2. FASTA, FASTQ (biodata.fasta)
  3. BED3, BED, BEDX, BEDGraph, BEDPE (biodata.bed)
  4. GFF3, GTF (GFF2) (biodata.gff)
  5. bwa FastMap (biodata.bwa.fastmap)
  6. MEME Motif Format (biodata.meme)

Future supported formats.

  1. VCF (biodata.vcf)
  2. BigBed (biodata.bed)
  3. BigWig (biodata.bigwig)

Extension of BaseReader

Users can extend the BaseReader and BaseWriter class easily.

class ExampleNode(object):
	def __init__(self, value1, value2):
		self.value1 = value1
		self.value2 = value2

class ExampleNodeReader(BaseReader):
	def __init__(self, filename):
		super(ExampleNodeReader, self).__init__(filename)
	def _read(self):
		if self.line is None:
			return None
		words_array = self.line.split('\t')
		value1 = words_array[0]
		value2 = words_array[1]
		self.proceed_next_line()
		return ExampleNode(value1, value2)

filename = "SomeDocument.txt"
with ExampleNodeReader(filename) as er:
	for node in er:
		print(node.value1, node.value2)

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

biodata-0.1.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

biodata-0.1.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file biodata-0.1.1.tar.gz.

File metadata

  • Download URL: biodata-0.1.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.2 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for biodata-0.1.1.tar.gz
Algorithm Hash digest
SHA256 18beb92069b5b5231467e354c5a4d3e59723fb03ac8b8c06cb90c43d96b0ef0c
MD5 f8758d7266da4d0b9db1d9d1c4d4a142
BLAKE2b-256 31d95121cea13b8665395f191fb1e073d08a00c1845bb00573fb76c9f529ac94

See more details on using hashes here.

File details

Details for the file biodata-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: biodata-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.2 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for biodata-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b5589c4f6ad4831007895e795fc294f9800949024840fd7715caa0193b32688
MD5 165f8bc205392acba62e0aa8f127c699
BLAKE2b-256 87a5c54ba4a95f799799b2e6b135768b5a4d4dc29225b420a8b586c0cea9c65a

See more details on using hashes here.

Supported by

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