Download GTF file from Ensembl, only keeping the chr17 entries.
187029
def get_gtf():
    
    size, md5, url = GTF
    full_gtf = os.path.join(args.data_dir, os.path.basename(url))
    subset_gtf = os.path.join(
        args.data_dir,
        os.path.basename(url).replace('.gtf.gz', '_%s.gtf' % CHROM))

    if not _up_to_date(md5, subset_gtf):
        download(url, full_gtf)
        cmds = [
            'zcat', '<',
            full_gtf,
            '|', 'awk -F "\\t" \'{if ($1 == "%s") print $0}\''
            % CHROM.replace('chr', ''),
            '|', 'awk \'{print "chr"$0}\'', '>', subset_gtf]

        logged_command(cmds)
478207
def download(url, encoding='utf-8'):
    """"""
    import requests
    response = requests.get(url)
    response.encoding = encoding
    return response.text
839679
def tx2genedict(gtf, keep_version=False):
    
    d = {}
    with open_gzipsafe(gtf) as in_handle:
        for line in in_handle:
            if "gene_id" not in line or "transcript_id" not in line:
                continue
            geneid = line.split("gene_id")[1].split(" ")[1]
            geneid = _strip_non_alphanumeric(geneid)
            txid = line.split("transcript_id")[1].split(" ")[1]
            txid = _strip_non_alphanumeric(txid)
            if keep_version and "transcript_version" in line:
                txversion = line.split("transcript_version")[1].split(" ")[1]
                txversion = _strip_non_alphanumeric(txversion)
                txid  += "." + txversion
            if has_transcript_version(line) and not keep_version:
                txid = _strip_feature_version(txid)
                geneid = _strip_feature_version(geneid)
            d[txid] = geneid
    return d
873592
def _download_and_clean_file(filename, url):
  """"""
  temp_file, _ = urllib.request.urlretrieve(url)
  with tf.gfile.Open(temp_file, 'r') as temp_eval_file:
    with tf.gfile.Open(filename, 'w') as eval_file:
      for line in temp_eval_file:
        line = line.strip()
        line = line.replace(', ', ',')
        if not line or ',' not in line:
          continue
        if line[-1] == '.':
          line = line[:-1]
        line += '\n'
        eval_file.write(line)
  tf.gfile.Remove(temp_file)
715135
def Opaque(uri, tc, ps, **keywords):
    '''
    '''
    source = urllib.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ['7bit', '8bit', 'binary']: return source.read()

    data = StringIO.StringIO()
    mimetools.decode(source, data, enc)
    return data.getvalue()
877648
def _unicode_decode_extracted_tb(extracted_tb):
    """"""
    return [(_decode(file), line_number, _decode(function), _decode(text))
            for file, line_number, function, text in extracted_tb]
699016
def download_annotation(genomebuild, gene_file): 
    
    pred_bin = "genePredToBed"
    pred = find_executable(pred_bin)
    if not pred:
        sys.stderr.write("{} not found in path!\n".format(pred_bin))
        sys.exit(1)

    tmp = NamedTemporaryFile(delete=False, suffix=".gz")

    anno = []
    f = urlopen(UCSC_GENE_URL.format(genomebuild))
    p = re.compile(r'\w+.Gene.txt.gz')
    for line in f.readlines():
        m = p.search(line.decode())
        if m:
            anno.append(m.group(0))

    sys.stderr.write("Retrieving gene annotation for {}\n".format(genomebuild))
    url = ""
    for a in ANNOS:
        if a in anno:
            url = UCSC_GENE_URL.format(genomebuild) + a
            break
    if url:
        sys.stderr.write("Using {}\n".format(url))
        urlretrieve(
                url,
                tmp.name
                )
         
        with gzip.open(tmp.name) as f:
            cols = f.readline().decode(errors='ignore').split("\t")

        start_col = 1
        for i,col in enumerate(cols):
            if col == "+" or col == "-":
                start_col = i - 1
                break
        end_col = start_col + 10
       
        cmd = "zcat {} | cut -f{}-{} | {} /dev/stdin {}"
        print(cmd.format(tmp.name, start_col, end_col, pred, gene_file))
        sp.call(cmd.format(
            tmp.name, start_col, end_col, pred, gene_file), 
            shell=True)

    else:
        sys.stderr.write("No annotation found!")
1050511
def chr(self):
    
    if len(self.exons)==0: 
      sys.stderr.write("WARNING can't return chromsome with nothing here\n")
      return None
    return self._rngs[0].chr
333348
def download_release(download_file, release=None):
    """"""
    if release is None:
        release = get_latest_release()
    url = 'http://viewvc.geneontology.org/viewvc/GO-SVN/ontology-releases/%s/go-basic.obo' % release
    #download_file = 'go-basic_%s.obo' % release
    misc.http_download(url, download_file)
694644
function _prep_download_url(){

    var engine = new impl_engine(golr_response);
    //engine.method('GET');
    //engine.use_jsonp(true);
    var gman = new golr_manager(gserv, gconf, engine, 'async');
	
    gman.set_personality('annotation');
    gman.add_query_filter('document_category', 'annotation', ['*']);
	
    // gman.set_results_count(download_count);
    var field_list = [ // from GAF
	'source', // c1
	'bioentity_internal_id', // c2; not bioentity
	'bioentity_label', // c3
	'qualifier', // c4
	'annotation_class', // c5
	'reference', // c6
	'evidence_type', // c7
	'evidence_with', // c8
	'aspect', // c9
	'bioentity_name', // c10
	'synonym', // c11
	'type', // c12
	'taxon', // c13
	'date', // c14
	'assigned_by', // c15
	'annotation_extension_class', // c16
	'bioentity_isoform' // c17
    ];
    // gman.set('fl', field_list.join(','));
    // //manager.set('rows', arg_hash['rows']);
    // gman.set('csv.encapsulator', '');
    // gman.set('csv.separator', '%09');
    // gman.set('csv.header', 'false');
    // gman.set('csv.mv.separator', '|');
    // gman.set('start', _random_number(5));

    //gman.set('start', _random_number(5));
    gman.set_extra('&start=' + _random_number(4));
    //console.log(gman.get_download_url(field_list, { 'rows': lines }));
    return gman.get_download_url(field_list, { 'rows': lines });
}
1336550
def parse(self, limit=None):
        

        files_to_download, ftp = self._get_file_list(
            self.files['anat_entity']['path'],
            self.files['anat_entity']['pattern'])
        for dlname in files_to_download:
            localfile = '/'.join((self.rawdir, dlname))
            with gzip.open(localfile, 'rt', encoding='ISO-8859-1') as fh:
                LOG.info("Processing %s", localfile)
                self._parse_gene_anatomy(fh, limit)
        return
816288
def get_gaf_gene_ontology_file(path):
    
    assert isinstance(path, str)

    version = None
    with misc.smart_open_read(path, encoding='UTF-8', try_gzip=True) as fh:
        for l in fh:
            if l[0] != '!':
                break
            if l.startswith('!GO-version:'):
                version = l.split(' ')[1]
                break
    return version
1126458
def enc(x, codec='ascii'):
    """"""
    x = x.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')
    return x.encode(codec, 'xmlcharrefreplace')
167688
def load_version_as_string():
    """"""
    with open(VERSION_PATH, 'r') as rfile:
        contents = rfile.read().strip()

    _, version = contents.split('=')
    version = version.strip()

    # Remove quotes
    return version.strip('"\'')
689916
def chr_rom(self):
        """"""
        try:
            return self.raw_data[self.chr_rom_start:self.chr_rom_stop]
        except IndexError:
            raise ValueError('failed to read CHR-ROM on ROM.')
1121853
def download_kegg_gene_metadata(gene_id, outdir=None, force_rerun=False):
    
    if not outdir:
        outdir = ''

    # Replace colon with dash in the KEGG gene ID
    outfile = op.join(outdir, '{}.kegg'.format(custom_slugify(gene_id)))

    if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
        raw_text = bs_kegg.get("{}".format(gene_id))
        if raw_text == 404:
            return

        with io.open(outfile, mode='wt', encoding='utf-8') as f:
            f.write(raw_text)

        log.debug('{}: downloaded KEGG metadata file'.format(outfile))
    else:
        log.debug('{}: KEGG metadata file already exists'.format(outfile))

    return outfile
973400
def _slice_bam_chr21(in_bam, data):
    
    sambamba = config_utils.get_program("sambamba", data["config"])
    out_file = "%s-chr%s" % os.path.splitext(in_bam)
    if not utils.file_exists(out_file):
        bam.index(in_bam, data['config'])
        with pysam.Samfile(in_bam, "rb") as bamfile:
            bam_contigs = [c["SN"] for c in bamfile.header["SQ"]]
        chromosome = "21"
        if "chr21" in bam_contigs:
            chromosome = "chr21"
        with file_transaction(data, out_file) as tx_out_file:
            cmd = ("{sambamba} slice -o {tx_out_file} {in_bam} {chromosome}").format(**locals())
            out = subprocess.check_output(cmd, shell=True)
    return out_file