Package: genomictools
genomictools - Tools for processing genomic rangesThe genomictools package provides an easy solution to all data types with a genomic range.Installationpip install genomictoolsBasic usageThe basic class of a genomic range is calledGenomicPos. The start and stop in GenomicPos are all 1-based coordinate.r=GenomicPos("chr1",1,100)print(str(r),r.name,r.start,r.stop)# "chr1:1-100" "chr1", 1, 100r=GenomicPos("chr1:1-100")print(str(r),r.name,r.start,r.stop)# "chr1:1-100" "chr1", 1, 100r=GenomicPos("chr1:1")print(str(r),r.name,r.start,r.stop)# "chr1:1-1" "chr1", 1, 1To avoid confusion, it also provides the way to get 0-based or 1-based coordinatezstart,ostart,zstop,ostop. For example, in BED, we usually have 0-based start coordinate and 1-based stop coordinate.r=GenomicPos("chr1",1,100)print(r.name,r.zstart,r.ostop)# "chr1" 0 100To store a list of genomic ranges, we can useGenomicCollection.fromgenomictoolsimportGenomicPos,GenomicCollectionr1=GenomicPos("chr1:1-100")r2=GenomicPos("chr3:1000-2000")r3=GenomicPos("chr1:51-200")regions=GenomicCollection([r1,r2,r3])print(len(regions))# 3# When iterating through the regions, they will be sorted by name, start and stop.forrinregions:print(str(r))# chr1:1-100# chr1:51-200# chr3:1000-2000# One can check if a region overlaps any entry within the genomic collectionprint(regions.overlaps(GenomicPos("chr1:201-300")))# Falseprint(regions.overlaps(GenomicPos("chr1:2-3")))# Trueprint(regions.overlaps(GenomicPos("chr2:2-3")))# False# One can extract all entries from the genomic collection that overlap with the target regionforrinregions.find_overlaps(GenomicPos("chr1:1-3")):print(str(r))# chr1:1-100forrinregions.find_overlaps(GenomicPos("chr1:26-75")):print(str(r))# chr1:1-100# chr1:51-200For any data entry with an associated genomic range, it will implementGenomicAnnotation, where anyGenomicAnnotationinstance will have a propertygenomic_posfrombiodata.bedimportBEDbed=BED("chr1",0,100,name="R1")print(str(bed.genomic_pos))# chr1:1-100One can useGenomicAnnotationas entries inGenomicCollection.frombiodata.bedimportBEDfromgenomictoolsimportGenomicPos,GenomicCollectionbeds=GenomicCollection([BED("chr1",0,100,name="R1"),BED("chr3",1999,2000,name="R2"),BED("chr1",50,200,name="R3")])forbedinbeds:r=bed.genomic_posprint(bed.name,str(r))# R1 chr1:1-100# R3 chr1:51-200# R2 chr3:2000-2000forbedinbeds.find_overlaps(GenomicPos("chr1:26-75")):r=bed.genomic_posprint(bed.name,str(r))# R1 chr1:1-100# R3 chr1:51-200The base class of genomic range with strand isStrandedGenomicPos, which extendsGenomicPos.For any data entry with an associated stranded genomic range, it will implementStrandedGenomicAnnotation, where anyStrandedGenomicAnnotationinstance will have propertiesstranded_genomic_posandgenomic_pos. The strand should be+for positive strand,-for negative strand and.for unspecified strand.frombiodata.bedimportBEDbed=BED("chr1",0,100,name="R1",strand="-")print(str(bed.stranded_genomic_pos))# chr1:1-100:-print(str(bed.genomic_pos))# chr1:1-100One could also useGenomicCollectionto store genomic ranges data easily:frombiodata.bedimportBEDReaderfromgenomictoolsimportGenomicCollectionbeds=BEDReader.read_all(GenomicCollection,filename)