Package: biodata
biodata - A standard biological data processing packageThe 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,FASTAReaderis used to process FASTA file. A call to the methodread()fromFASTAReaderyields aFASTAobject. For indexed file, random access is supported through the XX-IReader. For example, an indexed FASTA file can be access byFASTAIReader.Installationpip install biodataQuick StartFor more advanced use, please see the Basic Usage section.# Reading file contentsfrombiodata.fastaimportFASTAReaderseq_dict=FASTAReader.read_all(lambdafr:{f.name:f.seqforfinfr},"input.fa")frombiodata.bedimportBEDReaderfromgenomictoolsimportGenomicCollectionbeds=BEDReader.read_all(GenomicCollection,"input.bed")frombiodata.gffimportGFF3ReaderfromgenomictoolsimportGenomicCollectiongff3s=GFF3Reader.read_all(GenomicCollection,"input.gff3")frombiodata.gffimportGTFReaderfromgenomictoolsimportGenomicCollectiongtfs=GTFReader.read_all(GenomicCollection,"input.gtf")Basic usageWe will demonstrate the use of biodata package using FASTA file.>seq1
ACGT
>seq2
CCCGGGAAARead the first entryfrombiodata.fastaimportFASTAReaderwithFASTAReader(filename)asfr:f=fr.read()print(f.name,f.seq)# seq1 ACGTRead entry by entryfrombiodata.fastaimportFASTAReaderwithFASTAReader(filename)asfr:forfinfr:print(f.name,f.seq)# seq1 ACGT# seq2 CCCGGGAAAwithBEDReader(bedfile)asbr:forbinbr:print(b.name,str(b.genomic_pos))Read all entries at oncefrombiodata.fastaimportFASTAReaderfasta_entries=FASTAReader.read_all(list,filename)# list of FASTAseq_dict=FASTAReader.read_all(lambdafr:{f.name:f.seqforfinfr},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:frombiodata.bedimportBEDReaderfromgenomictoolsimportGenomicCollectionbeds=BEDReader.read_all(GenomicCollection,filename)Peek an entryfrombiodata.fastaimportFASTAReaderwithFASTAReader(filename)asfr:f=fr.peek()# Only peek the entry without proceeding to the next entryprint(f.name,f.seq)# seq1 ACGTf=fr.read()# Read the entry and proceed to the next entryprint(f.name,f.seq)# seq1 ACGTf=fr.read()print(f.name,f.seq)# seq2 CCCGGGAAARead an entry from StringIO# TextIOBase can be used as inputimportiofrombiodata.fastaimportFASTAReaderFASTAReader.read_all(list,io.StringIO(">seq1\nACGT\n>seq2\nCCCGGGAAA\n"))Read an indexed filefrombiodata.fastaimportFASTAIReaderfromgenomictoolsimportGenomicPos,StrandedGenomicPosfrombiodata.bedimportBEDfir=FASTAIReader(filename,faifilename)# fai file can be created using 'samtools faidx filename'f=fir[GenomicPos("seq2:1-4")]# Read from a region without strandprint(f.name,f.seq)# seq2:1-4 CCCGf=fir[StrandedGenomicPos("seq2:1-4:-")]# Read from a region with strandprint(f.name,f.seq)# seq2:1-4:- CGGGf=fir[BED("seq2",0,4,strand="-")]# Equivalent to StrandedGenomicPos but a BED entry is usedprint(f.name,f.seq)# seq2:1-4:- CGGGfir.close()Write entry by entryfrombiodata.fastaimportFASTA,FASTAWriterwithFASTAWriter(output_file)asfw:fw.write(FASTA("seq1","ACGT"))fw.write(FASTA("seq2","CCCGGGAAA"))Write all entries at oncefrombiodata.fastaimportFASTA,FASTAWriterfasta_entries=[FASTA("seq1","ACGT"),fw.write(FASTA("seq2","CCCGGGAAA"))]FASTAWriter.write_all(fasta_entries,output_file)List of supported formatDelimited - tsv, csv (biodata.delimited)FASTA, FASTQ (biodata.fasta)BED3, BED, BEDX, BEDGraph, BEDPE (biodata.bed)GFF3, GTF (GFF2) (biodata.gff)bwa FastMap (biodata.bwa.fastmap)MEME Motif Format (biodata.meme)Future supported formats.VCF (biodata.vcf)BigBed (biodata.bed)BigWig (biodata.bigwig)Extension of BaseReaderUsers can extend theBaseReaderandBaseWriterclass easily.classExampleNode(object):def__init__(self,value1,value2):self.value1=value1self.value2=value2classExampleNodeReader(BaseReader):def__init__(self,filename):super(ExampleNodeReader,self).__init__(filename)def_read(self):ifself.lineisNone:returnNonewords_array=self.line.split('\t')value1=words_array[0]value2=words_array[1]self.proceed_next_line()returnExampleNode(value1,value2)filename="SomeDocument.txt"withExampleNodeReader(filename)aser:fornodeiner:print(node.value1,node.value2)