Example #1
0
def sync_seq_status():
    args = docopt(__doc__)
    config_data = json.loads(open(args.get('<config>'), 'r').read())
    (login_url, username, password, get_status_url, db_user, db_pw, db_host, database, set_status_url, vflag) = \
        (config_data['login_url'], config_data['username'], config_data['password'], config_data['urlGetStatus'],
         config_data['dbUser'], config_data['dbPw'], config_data['dbHost'], config_data['db'],
         config_data['setStatusUrl'], config_data['vflag'])

    post_client = requests.session()
    (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
    login_data = dict(username=username, password=password)
    r = post_client.post(login_url, login_data, cookies=post_cookies, headers=post_headers)
    # get list of bnids to check bionimbus web for
    if r.status_code == 200:
        sys.stderr.write('Successfully logged in\n')
    else:
        sys.stderr.write('Login failed for url ' + login_url + '\n got error code ' + str(r.status_code) + '\n')
    status_info = post_client.get(get_status_url, params=login_data)
    # check bionimbus web for samples that have been sequenced
    con = db_connect(database, db_user, db_pw, db_host)
    for bnid in status_info.json():
        status = status_info.json()[bnid]
        if status == 'Sample submitted for sequencing':
            seq_date = check_seq_status(con, bnid)
            if seq_date is not None:
                update_status(bnid, str(seq_date[0]), post_client, login_url, set_status_url,
                              'sequence_date', 'SEQUENCED', vflag)
def sync_seq_status():
    args = docopt(__doc__)
    config_data = json.loads(open(args.get('<config>'), 'r').read())
    (login_url, username, password, get_status_url, db_user, db_pw, db_host, database, set_status_url, vflag) = \
        (config_data['login_url'], config_data['username'], config_data['password'], config_data['urlGetStatus'],
         config_data['dbUser'], config_data['dbPw'], config_data['dbHost'], config_data['db'],
         config_data['setStatusUrl'], config_data['vflag'])

    post_client = requests.session()
    (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
    login_data = dict(username=username, password=password)
    r = post_client.post(login_url, login_data, cookies=post_cookies, headers=post_headers)
    # get list of bnids to check bionimbus web for
    if r.status_code == 200:
        sys.stderr.write('Successfully logged in\n')
    else:
        sys.stderr.write('Login failed for url ' + login_url + '\n got error code ' + str(r.status_code) + '\n')
    status_info = post_client.get(get_status_url, params=login_data)
    # check bionimbus web for samples that have been sequenced
    con = db_connect(database, db_user, db_pw, db_host)
    for bnid in status_info.json():
        status = status_info.json()[bnid]
        if status == 'Sample submitted for sequencing':
            seq_date = check_seq_status(con, bnid)
            if seq_date is not None:
                update_status(bnid, str(seq_date[0]), post_client, login_url, set_status_url,
                              'sequence_date', 'SEQUENCED', vflag)
def sync_meta_status():
    args = docopt(__doc__)
    config_data = json.loads(open(args.get('<config>'), 'r').read())
    (login_url, username, password, get_study_url, get_bnid_url, db_user, db_pw, db_host, database,
     post_meta_url, set_status_url, check_status_url, vflag) = (config_data['login_url'], config_data['username'],
         config_data['password'], config_data['urlGetStudy'], config_data['urlGetBnid'], config_data['dbUser'],
         config_data['dbPw'], config_data['dbHost'], config_data['db'], config_data['postMetaUrl'],
        config_data['setStatusUrl'], config_data['checkStatusUrl'], config_data['vflag'])

    post_client = requests.session()
    (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
    login_data = dict(username=username, password=password)
    r = post_client.post(login_url, login_data, cookies=post_cookies, headers=post_headers)
    # get list of studies to check bionimbus web for
    if r.status_code == 200:
        sys.stderr.write('Successfully logged in\n')
    else:
        sys.stderr.write('Login failed for url ' + login_url + '\n got error code ' + str(r.status_code) + '\n')
        exit(1)

    # query bionimbus web for all with project and sub project (study)
    study_info = post_client.get(get_study_url, params=login_data)
    if study_info.status_code != 200:
        sys.stderr.write('Lookup request failed.  Check cookies and tokens and try again\n')
        exit(1)
    con = db_connect(database, db_user, db_pw, db_host)
    # dict of rows to add to mimic sheet submission
    to_add = {'sheet': []}
    # dict for setting date of submission
    date_dict = {}
    # dict to make sure existing metadata has an entry in the status database
    to_check = {}
    for key in study_info.json():
        # adding pk for study to leverage metadata lookup function get_bnid_by_study
        entries = query_bionimbus_web(con, key)
        if len(entries) > 0:
            (to_add, date_dict, to_check) = check_variant_viewer(entries, study_info.json()[key], login_data,
                                                                 get_bnid_url, post_client, to_add, date_dict, to_check)

    # populate variant viewer with metadata for relevant studies if not populated already
    if len(to_add['sheet']) > 0:
        (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
        # post_headers.update({'name': 'sheet'})
        check = post_client.post(post_meta_url, data=json.dumps(to_add), headers=post_headers,
                                 cookies=post_cookies, allow_redirects=False)
        if check.status_code == 500:
            sys.stderr.write('Adding new metadata failed!\n')
            exit(1)
        sys.stderr.write('Created new entries in variant viewer\n')
        # set variant viewer for status submitted for sequencing for newly added stuff
        ct = 0
        for new_entry in to_add['sheet']:
            ct += 1
            bnid = new_entry[2]
            # date_dict has datetime objects, need to convert to to str
            sub_date = str(date_dict[bnid])
            status = 'Sample submitted for sequencing'
            check = update_status(bnid, sub_date, post_client, login_url, set_status_url, 'submit_date', status, vflag)
            if check != 0:
                sys.stderr.write('Could not set seq status')
                exit(1)
        sys.stderr.write(str(ct) + ' samples added to metadata database\n')
    else:
        sys.stderr.write('No new metadata to add.  All up to date!\n')
    if len(to_check) > 0:
        ct = 0
        for bnid in to_check:
            check = check_status(bnid, post_client, login_url, check_status_url, vflag)
            if check == 'No status':
                ct += 1
                status = 'Sample submitted for sequencing'
                success = update_status(bnid, str(to_check[bnid]), post_client, login_url, set_status_url,
                                        'submit_date', status, vflag)
                if success != 0:
                    sys.stderr.write('Could not update submit status for ' + bnid + '\n')
                    exit(1)
        sys.stderr.write(str(ct) + ' additional statuses updated\n')
Example #4
0
def sync_meta_status():
    args = docopt(__doc__)
    config_data = json.loads(open(args.get('<config>'), 'r').read())
    (login_url, username, password, get_study_url, get_bnid_url, db_user, db_pw, db_host, database,
     post_meta_url, set_status_url, check_status_url, vflag) = (config_data['login_url'], config_data['username'],
         config_data['password'], config_data['urlGetStudy'], config_data['urlGetBnid'], config_data['dbUser'],
         config_data['dbPw'], config_data['dbHost'], config_data['db'], config_data['postMetaUrl'],
        config_data['setStatusUrl'], config_data['checkStatusUrl'], config_data['vflag'])

    post_client = requests.session()
    (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
    login_data = dict(username=username, password=password)
    r = post_client.post(login_url, login_data, cookies=post_cookies, headers=post_headers)
    # get list of studies to check bionimbus web for
    if r.status_code == 200:
        sys.stderr.write('Successfully logged in\n')
    else:
        sys.stderr.write('Login failed for url ' + login_url + '\n got error code ' + str(r.status_code) + '\n')
        exit(1)

    # query bionimbus web for all with project and sub project (study)
    study_info = post_client.get(get_study_url, params=login_data)
    if study_info.status_code != 200:
        sys.stderr.write('Lookup request failed.  Check cookies and tokens and try again\n')
        exit(1)
    con = db_connect(database, db_user, db_pw, db_host)
    # dict of rows to add to mimic sheet submission
    to_add = {'sheet': []}
    # dict for setting date of submission
    date_dict = {}
    # dict to make sure existing metadata has an entry in the status database
    to_check = {}
    for key in study_info.json():
        # adding pk for study to leverage metadata lookup function get_bnid_by_study
        entries = query_bionimbus_web(con, key)
        if len(entries) > 0:
            (to_add, date_dict, to_check) = check_variant_viewer(entries, study_info.json()[key], login_data,
                                                                 get_bnid_url, post_client, to_add, date_dict, to_check)

    # populate variant viewer with metadata for relevant studies if not populated already
    if len(to_add['sheet']) > 0:
        (post_csrftoken, post_cookies, post_headers) = set_web_stuff(post_client, login_url, vflag)
        # post_headers.update({'name': 'sheet'})
        check = post_client.post(post_meta_url, data=json.dumps(to_add), headers=post_headers,
                                 cookies=post_cookies, allow_redirects=False)
        if check.status_code == 500:
            sys.stderr.write('Adding new metadata failed!\n')
            exit(1)
        sys.stderr.write('Created new entries in variant viewer\n')
        # set variant viewer for status submitted for sequencing for newly added stuff
        ct = 0
        for new_entry in to_add['sheet']:
            ct += 1
            bnid = new_entry[2]
            # date_dict has datetime objects, need to convert to to str
            sub_date = str(date_dict[bnid])
            status = 'Sample submitted for sequencing'
            check = update_status(bnid, sub_date, post_client, login_url, set_status_url, 'submit_date', status, vflag)
            if check != 0:
                sys.stderr.write('Could not set seq status')
                exit(1)
        sys.stderr.write(str(ct) + ' samples added to metadata database\n')
    else:
        sys.stderr.write('No new metadata to add.  All up to date!\n')
    if len(to_check) > 0:
        ct = 0
        for bnid in to_check:
            check = check_status(bnid, post_client, login_url, check_status_url, vflag)
            if check == 'No status':
                ct += 1
                status = 'Sample submitted for sequencing'
                success = update_status(bnid, str(to_check[bnid]), post_client, login_url, set_status_url,
                                        'submit_date', status, vflag)
                if success != 0:
                    sys.stderr.write('Could not update submit status for ' + bnid + '\n')
                    exit(1)
        sys.stderr.write(str(ct) + ' additional statuses updated\n')