Beispiel #1
0
def snpchip():
    print("Execute snpchip.")
    data = json.loads(request.stream.read())
    snps = data['snps']
    platforms = data['platforms']
    token = request.args.get('token', False)
    print('snps: ' + snps)
    print('platforms: ' + platforms)
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = str(data['reference'])
            print('request: ' + reference)
            snplst = tmp_dir + 'snps' + reference + '.txt'
            with open(snplst, 'w') as f:
                f.write(snps.lower())
            try:
                snp_chip = calculate_chip(snplst, platforms, web, reference)
                out_json = json.dumps(snp_chip, sort_keys=True, indent=2)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + str(random.randint(
            0, 10000))
        print('request: ' + reference)
        snplst = tmp_dir + 'snps' + reference + '.txt'
        with open(snplst, 'w') as f:
            f.write(snps.lower())
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            snp_chip = calculate_chip(snplst, platforms, web, reference)
            # display api out
            try:
                # unlock token then display api output
                resultFile = "./tmp/details" + reference + ".txt"
                with open(resultFile, "r") as fp:
                    content = fp.read()
                toggleLocked(token, 0)
                return content
            except:
                # unlock token then display error message
                out_json = json.dumps(snp_chip, sort_keys=True, indent=2)
                output = json.loads(out_json)
                toggleLocked(token, 0)
                return sendTraceback(output["error"])
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return current_app.response_class(out_json, mimetype='application/json')
Beispiel #2
0
def ldproxy():
    print('Execute ldproxy.')
    var = request.args.get('var', False)
    pop = request.args.get('pop', False)
    r2_d = request.args.get('r2_d', False)
    token = request.args.get('token', False)
    print('var: ' + var)
    print('pop: ' + pop)
    print('r2_d: ' + r2_d)
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = request.args.get('reference', False)
            print('request: ' + str(reference))
            try:
                out_script, out_div = calculate_proxy(var, pop, reference, web,
                                                      r2_d)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + str(random.randint(
            0, 10000))
        print('request: ' + str(reference))
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            out_script, out_div = calculate_proxy(var, pop, reference, web,
                                                  r2_d)
            # display api out
            try:
                # unlock token then display api output
                with open('./tmp/proxy' + reference + '.txt', "r") as fp:
                    content = fp.read()
                toggleLocked(token, 0)
                return content
            except:
                # unlock token then display error message
                with open(tmp_dir + "proxy" + reference + ".json") as f:
                    json_dict = json.load(f)
                toggleLocked(token, 0)
                return sendTraceback(json_dict["error"])
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return out_script + "\n " + out_div
Beispiel #3
0
def ldpop():
    print('Execute ldpop.')
    var1 = request.args.get('var1', False)
    var2 = request.args.get('var2', False)
    pop = request.args.get('pop', False)
    r2_d = request.args.get('r2_d', False)
    token = request.args.get('token', False)
    print('var1: ' + var1)
    print('var2: ' + var2)
    print('pop: ' + pop)
    print('r2_d: ' + r2_d)
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = request.args.get('reference', False)
            print('request: ' + str(reference))
            try:
                out_json = calculate_pop(var1, var2, pop, r2_d, web, reference)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + str(random.randint(
            0, 10000))
        print('request: ' + str(reference))
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            out_json = calculate_pop(var1, var2, pop, r2_d, web, reference)
            # display api out
            try:
                # unlock token then display api output
                with open('./tmp/LDpop_' + reference + '.txt', "r") as fp:
                    content = fp.read()
                toggleLocked(token, 0)
                return content
            except:
                # unlock token then display error message
                # output = json.loads(out_json)
                toggleLocked(token, 0)
                return sendTraceback(out_json["error"])
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return current_app.response_class(out_json, mimetype='application/json')
def snpclip():
    print 'Execute snpclip.'
    data = json.loads(request.stream.read())
    snps = data['snps']
    pop = data['pop']
    r2_threshold = data['r2_threshold']
    maf_threshold = data['maf_threshold']
    token = request.args.get('token', False)
    print 'snps: ' + snps
    print 'pop: ' + pop
    print 'r2_threshold: ' + r2_threshold
    print 'maf_threshold: ' + maf_threshold
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = str(data['reference'])
            snpfile = str(tmp_dir + 'snps' + reference + '.txt')
            snplist = snps.splitlines()
            with open(snpfile, 'w') as f:
                for s in snplist:
                    s = s.lstrip()
                    if (s[:2].lower() == 'rs' or s[:3].lower() == 'chr'):
                        f.write(s.lower() + '\n')
            try:
                clip = {}
                (snps, snp_list,
                 details) = calculate_clip(snpfile, pop, reference, web,
                                           float(r2_threshold),
                                           float(maf_threshold))
                clip["snp_list"] = snp_list
                clip["details"] = details
                clip["snps"] = snps
                clip["filtered"] = collections.OrderedDict()
                with open(tmp_dir + "clip" + reference + ".json") as f:
                    json_dict = json.load(f)
                if "error" in json_dict:
                    clip["error"] = json_dict["error"]
                else:
                    for snp in snps:
                        clip["filtered"][snp[0]] = details[snp[0]]
                    if "warning" in json_dict:
                        clip["warning"] = json_dict["warning"]
                with open('tmp/snp_list' + reference + '.txt', 'w') as f:
                    for rs_number in snp_list:
                        f.write(rs_number + '\n')
                with open('tmp/details' + reference + '.txt', 'w') as f:
                    f.write("RS Number\tPosition\tAlleles\tDetails\n")
                    if (type(details) is collections.OrderedDict):
                        for snp in snps:
                            f.write(snp[0] + "\t" + "\t".join(details[snp[0]]))
                            f.write("\n")
                out_json = json.dumps(clip, sort_keys=False)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + ` random.randint(0, 10000) `
        snpfile = str(tmp_dir + 'snps' + reference + '.txt')
        snplist = snps.splitlines()
        with open(snpfile, 'w') as f:
            for s in snplist:
                s = s.lstrip()
                if (s[:2].lower() == 'rs' or s[:3].lower() == 'chr'):
                    f.write(s.lower() + '\n')
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            (snps, snp_list, details) = calculate_clip(snpfile, pop,
                                                       reference, web,
                                                       float(r2_threshold),
                                                       float(maf_threshold))
            with open(tmp_dir + "clip" + reference + ".json") as f:
                json_dict = json.load(f)
            with open('tmp/details' + reference + '.txt', 'w') as f:
                f.write("RS Number\tPosition\tAlleles\tDetails\n")
                if (type(details) is collections.OrderedDict):
                    for snp in snps:
                        f.write(snp[0] + "\t" + "\t".join(details[snp[0]]))
                        f.write("\n")
            # display api out
            try:
                # unlock token then display api output
                resultFile = "./tmp/details" + reference + ".txt"
                with open(resultFile, "r") as fp:
                    content = fp.read()
                with open(tmp_dir + "clip" + reference + ".json") as f:
                    json_dict = json.load(f)
                    if "error" in json_dict:
                        toggleLocked(token, 0)
                        return sendTraceback(json_dict["error"])
                toggleLocked(token, 0)
                return content
            except:
                # unlock token then display error message
                toggleLocked(token, 0)
                return sendTraceback(None)
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return current_app.response_class(out_json, mimetype='application/json')
def ldmatrix():
    print 'Execute ldmatrix.'
    # differentiate POST or GET request
    if request.method == 'POST':
        # POST REQUEST
        data = json.loads(request.stream.read())
        if 'snps' in data:
            snps = data['snps']
        else:
            snps = False
        if "pop" in data:
            pop = data['pop']
        else:
            pop = False
        if "reference" in data:
            reference = data['reference']
        else:
            reference = False
        if "r2_d" in data:
            r2_d = data['r2_d']
        else:
            r2_d = False
    else:
        # GET REQUEST
        snps = request.args.get('snps', False)
        pop = request.args.get('pop', False)
        reference = request.args.get('reference', False)
        r2_d = request.args.get('r2_d', False)
    token = request.args.get('token', False)
    print 'snps: ' + snps
    print 'pop: ' + pop
    print 'r2_d: ' + r2_d
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = request.args.get('reference', False)
            print 'request: ' + str(reference)
            snplst = tmp_dir + 'snps' + str(reference) + '.txt'
            with open(snplst, 'w') as f:
                f.write(snps.lower())
            try:
                out_script, out_div = calculate_matrix(snplst, pop,
                                                       reference, web,
                                                       str(request.method),
                                                       r2_d)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + ` random.randint(0, 10000) `
        print 'request: ' + str(reference)
        snplst = tmp_dir + 'snps' + str(reference) + '.txt'
        with open(snplst, 'w') as f:
            f.write(snps.lower())
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            out_script, out_div = calculate_matrix(snplst, pop, reference, web,
                                                   str(request.method), r2_d)
            # display api out
            try:
                # unlock token then display api output
                resultFile = ""
                if r2_d == "d":
                    resultFile = "./tmp/d_prime_" + reference + ".txt"
                else:
                    resultFile = "./tmp/r2_" + reference + ".txt"
                with open(resultFile, "r") as fp:
                    content = fp.read()
                toggleLocked(token, 0)
                return content
            except:
                # unlock token then display error message
                with open(tmp_dir + "matrix" + reference + ".json") as f:
                    json_dict = json.load(f)
                toggleLocked(token, 0)
                return sendTraceback(json_dict["error"])
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return out_script + "\n " + out_div
def ldhap():
    print 'Execute ldhap.'
    # print 'Request User Agent: ', request.user_agent
    # print 'Request User Agent Platform: ', request.user_agent.platform
    # print 'Request User Agent Browser: ', request.user_agent.browser

    snps = request.args.get('snps', False)
    pop = request.args.get('pop', False)
    token = request.args.get('token', False)
    print 'snps: ' + snps
    print 'pop: ' + pop
    web = False
    # differentiate web or api request
    if 'LDlinkRestWeb' in request.path:
        # WEB REQUEST
        if request.user_agent.browser is not None:
            web = True
            reference = request.args.get('reference', False)
            print 'request: ' + str(reference)
            snplst = tmp_dir + 'snps' + reference + '.txt'
            with open(snplst, 'w') as f:
                f.write(snps.lower())
            try:
                out_json = calculate_hap(snplst, pop, reference, web)
            except:
                return sendTraceback(None)
        else:
            return sendJSON(
                "This web API route does not support programmatic access. Please use the API routes specified on the API Access web page."
            )
    else:
        # API REQUEST
        web = False
        reference = str(time.strftime("%I%M%S")) + ` random.randint(0, 10000) `
        print 'request: ' + str(reference)
        snplst = tmp_dir + 'snps' + reference + '.txt'
        with open(snplst, 'w') as f:
            f.write(snps.lower())
        try:
            # lock token preventing concurrent requests
            toggleLocked(token, 1)
            out_json = calculate_hap(snplst, pop, reference, web)
            # display api out
            try:
                # unlock token then display api output
                resultFile1 = "./tmp/snps_" + reference + ".txt"
                resultFile2 = "./tmp/haplotypes_" + reference + ".txt"
                with open(resultFile1, "r") as fp:
                    content1 = fp.read()
                with open(resultFile2, "r") as fp:
                    content2 = fp.read()
                toggleLocked(token, 0)
                return content1 + "\n" + "#####################################################################################" + "\n\n" + content2
            except:
                # unlock token then display error message
                output = json.loads(out_json)
                toggleLocked(token, 0)
                return sendTraceback(output["error"])
        except:
            # unlock token if internal error w/ calculation
            toggleLocked(token, 0)
            return sendTraceback(None)
    return sendJSON(out_json)