Skip to content

Instantly share code, notes, and snippets.

@knmkr
Created March 28, 2013 13:41
Show Gist options
  • Select an option

  • Save knmkr/5263200 to your computer and use it in GitHub Desktop.

Select an option

Save knmkr/5263200 to your computer and use it in GitHub Desktop.
Get contig information by `samtools view -H`
def get_contigs(bam_path):
"""Get contig information by `samtools view -H`
Arg:
bam_path: path to a bam-file
RetVal:
['chr1', 'chr2', ..., 'chrM']
"""
contigs = []
com = ['samtools', 'view', '-H', bam_path]
proc = subprocess.Popen(com, stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
record = line.split('\t')
if record[0] == '@SQ':
chrom = record[1].replace('SN:', '')
contigs.append(chrom)
return contigs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment