Skip to content

Instantly share code, notes, and snippets.

@fab-genty

fab-genty/README Secret

Created May 21, 2018 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fab-genty/53a814433f6e3d3794d3f2b04c37de66 to your computer and use it in GitHub Desktop.
Save fab-genty/53a814433f6e3d3794d3f2b04c37de66 to your computer and use it in GitHub Desktop.
pysam biostar post
python fct_depth_biostars.py ex1.bam > depth_fct.txt
satools depth ex1.bam > depth_samtools.txt
#!/usr/bin/python
__author__ = "GENTY Fabien"
import os
import sys
import pysam
import time
def get_chromosomes_names(input):
# opening the bam file with pysam
bamfile = pysam.AlignmentFile(input, 'rb')
# query all the names of the chromosomes in a list
list_chromosomes = bamfile.references[0:24]
list_length = bamfile.lengths[0:24]
bamfile.close()
return list_chromosomes, list_length
def count_depth(chr_name, size, input):
"""
Count the depth of the read. For each genomic coordonate return the
number of reads
-----
Parameters :
chr : (str) name of the chromosome
-----
Returns :
none
"""
bamfile = pysam.AlignmentFile(input, 'rb')
for pileupcolumn in bamfile.pileup(chr_name):
pos = pileupcolumn.reference_pos + 1
depth = pileupcolumn.nsegments
print("{} {} {}".format('chr1', pos, depth))
if __name__ == "__main__":
file = sys.argv[1]
# query all the names of the chromosomes in a list
list_chromosomes, list_length = get_chromosomes_names(file)
# for chr, size in zip(list_chromosomes, list_length):
count_depth(list_chromosomes[0], list_length[0], file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment