def impute(request): '''Impute one or more SNPs. dbsnp will be one or more dbSNP identifiers (only rsIDs), separated by commas. ''' dbsnps = helpers.check_dbsnp_array(request.REQUEST.get('dbsnps', None)) user_dbsnps = helpers.check_dbsnp_array( request.REQUEST.get('user_dbsnps', None)) population = helpers.check_population( request.REQUEST.get('population', None)) if None in (dbsnps, population, user_dbsnps): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() phases = {} for index, dbsnp in enumerate(dbsnps): query_snp_in_hapmap = helpers.get_individuals(cursor, dbsnp, population) anchor_snp_in_hapmap = helpers.get_individuals(cursor, user_dbsnps[index], population) phase = helpers.get_best_phases(query_snp_in_hapmap, anchor_snp_in_hapmap) phase['user_snp'] = user_dbsnps[index] phases[dbsnp] = phase return http.HttpResponse(simplejson.dumps(phases), mimetype="application/json")
def submit(request): request_dict = request.GET.copy() try: exercise = request_dict.pop('exercise')[0] except KeyError: return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() if exercise in ('butte_diabetes', 'selection', 'assimes_cad', 'neandertal', 'eqtl', 'snyder_binding', 'mignot_narcolepsy', 'longevity', 'kim_aging'): population = helpers.check_population( request_dict.pop('population')[0]) cursor.execute('SELECT MAX(sid) FROM interpretome_exercises.unified;') sid = cursor.fetchone().values()[0] + 1 statements = [] for k, v in request_dict.items(): string = "('%s', '%s', '%s', '%s', %d)" % (helpers.sanitize( k), helpers.sanitize(v), population, exercise, int(sid)) statements.append(string) query = ''' INSERT INTO interpretome_exercises.unified (`key`, `value`, population, exercise, sid) VALUES %s;''' % ', '.join(statements) cursor.execute(query) return http.HttpResponse()
def linked(request): dbsnps = helpers.check_dbsnp_array(request.REQUEST.get('dbsnps', None)) population = helpers.check_population( request.REQUEST.get('population', None)) ld_cutoff = helpers.check_float(request.REQUEST.get('ld_cutoff', None)) if None in (dbsnps, population, ld_cutoff): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() results = {} for dbsnp in dbsnps: query = ''' SELECT * FROM var_hapmap.hapmap_phased_%s WHERE dbSNP = %s; ''' % (population, dbsnp) cursor.execute(query) hapmap_result = cursor.fetchone() query = ''' SELECT dbSNP1, dbSNP2, R_square FROM var_ld_data.ld_%s WHERE dbSNP1 = %d OR dbSNP2 = %d AND R_square >= %s ORDER BY R_square DESC; ''' % (population, dbsnp, dbsnp, ld_cutoff) cursor.execute(query) raw_result = cursor.fetchall() if hapmap_result is None or raw_result is None: results[dbsnp] = [] continue result = [] for entry in raw_result: if entry['dbSNP1'] == dbsnp: other_dbsnp = entry['dbSNP2'] else: other_dbsnp = entry['dbSNP1'] query = ''' SELECT * FROM var_hapmap.hapmap_phased_%s WHERE dbSNP = %s; ''' % (population, other_dbsnp) cursor.execute(query) if cursor.fetchone() is not None: result.append(entry) results[dbsnp] = result return http.HttpResponse(simplejson.dumps(results), mimetype='application/json')
def linked(request): dbsnps = helpers.check_dbsnp_array(request.REQUEST.get('dbsnps', None)) population = helpers.check_population(request.REQUEST.get('population', None)) ld_cutoff = helpers.check_float(request.REQUEST.get('ld_cutoff', None)) if None in (dbsnps, population, ld_cutoff): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() results = {} for dbsnp in dbsnps: query = ''' SELECT * FROM var_hapmap.hapmap_phased_%s WHERE dbSNP = %s; ''' % (population, dbsnp) cursor.execute(query) hapmap_result = cursor.fetchone() query = ''' SELECT dbSNP1, dbSNP2, R_square FROM var_ld_data.ld_%s WHERE dbSNP1 = %d OR dbSNP2 = %d AND R_square >= %s ORDER BY R_square DESC; ''' % (population, dbsnp, dbsnp, ld_cutoff) cursor.execute(query) raw_result = cursor.fetchall() if hapmap_result is None or raw_result is None: results[dbsnp] = [] continue result = [] for entry in raw_result: if entry['dbSNP1'] == dbsnp: other_dbsnp = entry['dbSNP2'] else: other_dbsnp = entry['dbSNP1'] query = ''' SELECT * FROM var_hapmap.hapmap_phased_%s WHERE dbSNP = %s; ''' % (population, other_dbsnp) cursor.execute(query) if cursor.fetchone() is not None: result.append(entry) results[dbsnp] = result return http.HttpResponse(simplejson.dumps(results), mimetype = 'application/json')
def get_rare_variants(request): population = helpers.check_population(request.GET.get('population', None)) cutoff = helpers.check_float(request.GET.get('cutoff', None)) if None in (population, cutoff): return http.HttpResponseBadRequest() query = '''SELECT rsid, refallele, otherallele, otherallele_freq FROM var_hapmap.snp_frequencies_%s WHERE otherallele_freq <= %s''' % (population.lower(), cutoff) cursor = connections['default'].dict_cursor() cursor.execute(query) return http.HttpResponse(simplejson.dumps(cursor.fetchall()), mimetype = "application/json")
def get_rare_variants(request): population = helpers.check_population(request.GET.get('population', None)) cutoff = helpers.check_float(request.GET.get('cutoff', None)) if None in (population, cutoff): return http.HttpResponseBadRequest() query = '''SELECT rsid, refallele, otherallele, otherallele_freq FROM var_hapmap.snp_frequencies_%s WHERE otherallele_freq <= %s''' % (population.lower(), cutoff) cursor = connections['default'].dict_cursor() cursor.execute(query) return http.HttpResponse(simplejson.dumps(cursor.fetchall()), mimetype="application/json")
def get_allele_frequencies(request): dbsnps = helpers.check_dbsnp_array(request.GET.get('snps', None)) population = helpers.check_population(request.GET.get('population', None)) if None in (population, dbsnps): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() frequencies = {} for dbsnp in dbsnps: query = ''' SELECT refallele, refallele_freq, otherallele, otherallele_freq FROM var_hapmap.allele_freqs_%s WHERE rsid=%s ''' % (population, dbsnp) cursor.execute(query) data = cursor.fetchone() frequencies[dbsnp] = data return http.HttpResponse(simplejson.dumps(frequencies), mimetype = "application/json")
def get_gwas_catalog(request): population = helpers.check_population(request.GET.get('population', None)) if population is None: return http.HttpResponseBadRequest() studies = get_possible_studies(population) like_string = "%' or `initial_sample_size` like '%".join(studies) query = ''' SELECT pubmedid, link, disease_trait, initial_sample_size, reported_genes, or_or_beta, strongest_snp, risk_allele, p_value FROM interpretome_clinical.gwas_catalog WHERE initial_sample_size LIKE '%%%s%%' ORDER BY disease_trait ''' % like_string cursor = connections['default'].dict_cursor() cursor.execute(query) snps = cursor.fetchall() return http.HttpResponse(simplejson.dumps(snps), mimetype = "application/json")
def get_allele_frequencies(request): dbsnps = helpers.check_dbsnp_array(request.GET.get('snps', None)) population = helpers.check_population(request.GET.get('population', None)) if None in (population, dbsnps): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() frequencies = {} for dbsnp in dbsnps: query = ''' SELECT refallele, refallele_freq, otherallele, otherallele_freq FROM var_hapmap.allele_freqs_%s WHERE rsid=%s ''' % (population, dbsnp) cursor.execute(query) data = cursor.fetchone() frequencies[dbsnp] = data return http.HttpResponse(simplejson.dumps(frequencies), mimetype="application/json")
def get_gwas_catalog(request): population = helpers.check_population(request.GET.get('population', None)) if population is None: return http.HttpResponseBadRequest() studies = get_possible_studies(population) like_string = "%' or `initial_sample_size` like '%".join(studies) query = ''' SELECT pubmedid, link, disease_trait, initial_sample_size, reported_genes, or_or_beta, strongest_snp, risk_allele, p_value FROM interpretome_clinical.gwas_catalog WHERE initial_sample_size LIKE '%%%s%%' ORDER BY disease_trait ''' % like_string cursor = connections['default'].dict_cursor() cursor.execute(query) snps = cursor.fetchall() return http.HttpResponse(simplejson.dumps(snps), mimetype="application/json")
def impute(request): '''Impute one or more SNPs. dbsnp will be one or more dbSNP identifiers (only rsIDs), separated by commas. ''' dbsnps = helpers.check_dbsnp_array(request.REQUEST.get('dbsnps', None)) user_dbsnps = helpers.check_dbsnp_array(request.REQUEST.get('user_dbsnps', None)) population = helpers.check_population(request.REQUEST.get('population', None)) if None in (dbsnps, population, user_dbsnps): return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() phases = {} for index, dbsnp in enumerate(dbsnps): query_snp_in_hapmap = helpers.get_individuals(cursor, dbsnp, population) anchor_snp_in_hapmap = helpers.get_individuals(cursor, user_dbsnps[index], population) phase = helpers.get_best_phases(query_snp_in_hapmap, anchor_snp_in_hapmap) phase['user_snp'] = user_dbsnps[index] phases[dbsnp] = phase return http.HttpResponse(simplejson.dumps(phases), mimetype = "application/json")
def submit(request): request_dict = request.GET.copy() try: exercise = request_dict.pop('exercise')[0] except KeyError: return http.HttpResponseBadRequest() cursor = connections['default'].dict_cursor() if exercise in ( 'butte_diabetes', 'selection', 'assimes_cad', 'neandertal', 'eqtl', 'snyder_binding', 'mignot_narcolepsy', 'longevity', 'kim_aging' ): population = helpers.check_population(request_dict.pop('population')[0]) cursor.execute('SELECT MAX(sid) FROM interpretome_exercises.unified;') sid = cursor.fetchone().values()[0] + 1 statements = [] for k, v in request_dict.items(): string = "('%s', '%s', '%s', '%s', %d)" % (helpers.sanitize(k), helpers.sanitize(v), population, exercise, int(sid)) statements.append(string) query = ''' INSERT INTO interpretome_exercises.unified (`key`, `value`, population, exercise, sid) VALUES %s;''' % ', '.join(statements) cursor.execute(query) return http.HttpResponse()