<commit_before>#! /usr/bin/env python

# Copyright (c) 2014 <NAME>-<NAME>

from __future__ import division
import click
from flask import Flask, render_template, request
import h5py
import os
import json
from collections import defaultdict
import numpy as np
import math
import re
import pysam
import csv

app = Flask(__name__)
cfg = dict()


def get_smpl_names(h5_fns, use_fns):
    if use_fns:
        return [os.path.basename(fn) for fn in h5_fns]
    ss = []
    for h5_fn in h5_fns:
        with h5py.File(h5_fn) as f:
            ss.append(f.attrs['sample'])
    return ss


def get_common_chroms(s1, s2):
    with h5py.File(cfg['smpl_to_file'][s1]) as f:
        chroms1 = set(c for c in f)
    with h5py.File(cfg['smpl_to_file'][s2]) as f:
        chroms2 = set(c for c in f)
    return sorted(list(chroms1 & chroms2))


@app.route('/chroms/<s1>/<s2>')
def chroms(s1, s2):
    return json.dumps(get_common_chroms(s1, s2))

chr2_re = re.compile(r'CHR2=([^;\s]+)')
end_re = re.compile(r'\bEND=(\d+)')
ct_re = re.compile(r'\bCT=([^;\s]+)')

@app.route('/calls/<chrom>')
def calls(chrom):
    dataset = []
    if cfg['vcf']:
        f = pysam.Tabixfile(cfg['vcf'])
        for row in csv.reader(f.fetch(str(chrom)), delimiter='\t'):
            chrom2 = chr2_re.search(row[7]).group(1)
            if chrom2 != chrom:
                continue
            start_call = int(row[1])
            end_call = int(end_re.search(row[7]).group(1))
            m = ct_re.search(row[7])
            if m:
                ct = m.group(1)
            else:
                ct = 'none'
            sv_type = row[4].lstrip('<').rstrip('>')
            dataset.append({
                'start': start_call,
                'end': end_call,
                'type': sv_type,
                'ct': ct
            })

    return json.dumps(dataset)

@app.route('/depth/<s1>/<s2>/<c>')
def depth(s1, s2, c):
    d = {
        'chrom': c,
        'chrom_len': -1,
    }
    with h5py.File(cfg['smpl_to_file'][s1], 'r') as f1, \
            h5py.File(cfg['smpl_to_file'][s2], 'r') as f2:
        assert c in f1 and c in f2
        d['chrom_len'] = int(f1[c].attrs['length'])
        # TODO: this needs some more thought...
        s = request.args.get('start', 1, type=int)
        e = request.args.get('end', d['chrom_len'], type=int)
        n_req = request.args.get('n', 10000, type=int)

        bin_start = (s-1) // 100
        bin_end = (e-1) // 100
        n_bins = bin_end - bin_start + 1

        print s, e, n_req, bin_start, bin_end

        # TODO:<commit_msg>Truncate passed coordinates to [1,chrom_length]<commit_after>#! /usr/bin/env python

# Copyright (c) 2014 <NAME>-<NAME>

from __future__ import division
import click
from flask import Flask, render_template, request
import h5py
import os
import json
from collections import defaultdict
import numpy as np
import math
import re
import pysam
import csv

app = Flask(__name__)
cfg = dict()


def get_smpl_names(h5_fns, use_fns):
    if use_fns:
        return [os.path.basename(fn) for fn in h5_fns]
    ss = []
    for h5_fn in h5_fns:
        with h5py.File(h5_fn) as f:
            ss.append(f.attrs['sample'])
    return ss


def get_common_chroms(s1, s2):
    with h5py.File(cfg['smpl_to_file'][s1]) as f:
        chroms1 = set(c for c in f)
    with h5py.File(cfg['smpl_to_file'][s2]) as f:
        chroms2 = set(c for c in f)
    return sorted(list(chroms1 & chroms2))


@app.route('/chroms/<s1>/<s2>')
def chroms(s1, s2):
    return json.dumps(get_common_chroms(s1, s2))

chr2_re = re.compile(r'CHR2=([^;\s]+)')
end_re = re.compile(r'\bEND=(\d+)')
ct_re = re.compile(r'\bCT=([^;\s]+)')

@app.route('/calls/<chrom>')
def calls(chrom):
    dataset = []
    if cfg['vcf']:
        f = pysam.Tabixfile(cfg['vcf'])
        for row in csv.reader(f.fetch(str(chrom)), delimiter='\t'):
            chrom2 = chr2_re.search(row[7]).group(1)
            if chrom2 != chrom:
                continue
            start_call = int(row[1])
            end_call = int(end_re.search(row[7]).group(1))
            m = ct_re.search(row[7])
            if m:
                ct = m.group(1)
            else:
                ct = 'none'
            sv_type = row[4].lstrip('<').rstrip('>')
            dataset.append({
                'start': start_call,
                'end': end_call,
                'type': sv_type,
                'ct': ct
            })

    return json.dumps(dataset)

@app.route('/depth/<s1>/<s2>/<c>')
def depth(s1, s2, c):
    d = {
        'chrom': c,
        'chrom_len': -1,
    }
    with h5py.File(cfg['smpl_to_file'][s1], 'r') as f1, \
            h5py.File(cfg['smpl_to_file'][s2], 'r') as f2:
        assert c in f1 and c in f2
        d['chrom_len'] = int(f1[c].attrs['length'])
        # TODO: this needs some more thought...
        s = request.args.get('start', 1, type=int)
        if s < 1:
            s = 1
        e = request.args.get('end', d['chrom_len'], type=int)
        if e > d['chrom_len']:
            e = d['chrom_len']
        n_req = request.args.get('n', 10000, type=int)

        bin_start = (s-1) // 100
        bin_end = (e-1) // 100
        n_bins = bin_end - bin_start + 1

        print s, e, n_req, bin_start, bin_end

        # TODO:
{'fraction_non_alphanumeric': 0.15979381443298968, 'fraction_numerical': 0.026918671248568157, 'mean_word_length': 2.1503307276007217, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88865', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1967, 'n_words': 445}