INNER CODE UNIT · Python
set_regions
nloyfer/wgbs_tools · src/python/bam2pat.py:49
def set_regions(bam_path, gr, tmp_dir=None):
# if user specified a region, just use it
if gr.region_str:
return [gr.region_str]
# get all chromosomes from the reference genome:
ref_chroms = gr.genome.get_chroms()
if bam_path.endswith('.cram'):
return list(sorted(ref_chroms, key=chromosome_order))
# get all chromosomes present in the bam file header
cmd = f'samtools idxstats {bam_path} | cut -f1 '
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
if p.returncode or not output:
eprint("[wt bam2pat] Failed with samtools idxstats %d\n%s\n%s" % (p.returncode, output.decode(), error.decode()))
eprint(cmd)
eprint('[wt bam2pat] falied to find chromosomes')