Downloading 2bit genomes

The python library for ucscgenome can be used to download genome

pip install ucscgenome
#for local install on servers use --user if you don't have rights to install globaly
pip install --user ucscgenome

Then within python you can download by creating a genome object with the name of the genome you want, which will automatically donwload the genome to ~/.ucscgenome

For example below would download the human genome version 19 and 38

#!/usr/bin/env python

from ucscgenome import Genome

hg19 = Genome('hg19')

hg38 = Genome('hg38')
ls ~/.ucscgenome
hg19.2bit
hg38.2bit

Go to here http://hgdownload.cse.ucsc.edu/downloads.html to see a list of available genomes

Reading 2bit genomes

To read the downloaded 2bit files you can use the python package twobitreader

pip install twobitreader
#for local install on servers use --user if you don't have rights to install globaly
pip install --user twobitreader

Get the sequence of one of the chromosomes

#!/usr/bin/env python

import twobitreader,os
from os.path import expanduser
home = expanduser("~")

hg38_genome = twobitreader.TwoBitFile(os.path.join(home, ".ucscgenome/hg38.2bit") )
chr9 = hg38_genome["chr9"]
print(chr9.get_slice(133255319, 133255568))
GCGTGTCTGTGTATGTAATGGTGTGTCTCTGTGGCGGGGAGGGTGTGTGTGTGATTTTGTGTCTGTGTCTTTCTGTGTGTGTCTGTGTATGTAATGGTGTCTCTGTGGCGGGGAGGGTGTGTGTGTGATTTTGTGTCTGTGTGTGTCTTTCTGTGTGTGTCTGTGTATGTAATGGTGTCTCTGTGGTGGGGAGGGGGTGTGTGTGATTTGAGGTGGGGACGGGGCCTAGGCTTCAGTTACTCACAACAG