Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lochness update to accommodate the new Penn CNB REDCap structure #124

Open
wants to merge 3 commits into
base: kcho/pronet
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lochness/redcap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,26 +680,26 @@ def sync(Lochness, subject, dry=False):

if 'UPENN' in redcap_instance:
# UPENN REDCap is set up with its own record_id, but have added
# "session_subid" field to note AMP-SCZ ID
# "src_subject_id" field to note AMP-SCZ ID
redcap_subject_sl = redcap_subject.lower()
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
digits_str = [str(x) for x in digits]
contains_logic = []
for subject_id in [redcap_subject, redcap_subject_sl]:
contains_logic += [
f"contains([session_subid], '{subject_id}_{x}')"
f"contains([src_subject_id], '{subject_id}_{x}')"
for x in digits_str]
contains_logic += [
f"contains([session_subid], '{subject_id}={x}')"
f"contains([src_subject_id], '{subject_id}={x}')"
for x in digits_str]


record_query = {
'token': api_key,
'content': 'record',
'format': 'json',
'filterLogic': f"[session_subid] = '{redcap_subject}' or "
f"[session_subid] = '{redcap_subject_sl}' or "
'filterLogic': f"[src_subject_id] = '{redcap_subject}' or "
f"[src_subject_id] = '{redcap_subject_sl}' or "
f"{' or '.join(contains_logic)}"
}

Expand Down Expand Up @@ -742,9 +742,6 @@ def sync(Lochness, subject, dry=False):
content_dict.pop(field['field_name'])
except:
pass
# print("lochness did not pull " \
# f"{field['field_name']}, which is a " \
# "deidentified field")

content = json.dumps(content_dict_list).encode('utf-8')

Expand All @@ -760,7 +757,7 @@ def sync(Lochness, subject, dry=False):
dst)
# process_and_copy_db(Lochness, subject, dst, proc_dst)
# update_study_metadata(subject, json.loads(content))

else:
# responses are not stored atomically in redcap
crc_src = lochness.crc32(content.decode('utf-8'))
Expand Down
56 changes: 56 additions & 0 deletions tests/lochness_test/redcap/test_new_upenn_redcap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import lochness
import json
from pathlib import Path
import sys
lochness_root = Path(lochness.__path__[0]).parent
scripts_dir = lochness_root / 'scripts'
test_dir = lochness_root / 'tests'
sys.path.append(str(scripts_dir))
sys.path.append(str(test_dir))
from test_lochness import Tokens
from lochness.redcap import post_to_redcap


def test_import():
print(lochness.__file__)


def test_new_format():

token = Tokens()
api_key, api_url = token.read_token_or_get_input('redcap_fake')

redcap_subject = 'YA01508'
redcap_subject_sl = 'ya01508'

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
digits_str = [str(x) for x in digits]
contains_logic = []
for subject_id in [redcap_subject, redcap_subject_sl]:
contains_logic += [
f"contains([src_subject_id], '{subject_id}_{x}')"
for x in digits_str]
contains_logic += [
f"contains([src_subject_id], '{subject_id}={x}')"
for x in digits_str]

record_query = {
'token': api_key,
'content': 'record',
'format': 'json',
'filterLogic': f"[src_subject_id] = '{redcap_subject}' or "
f"[src_subject_id] = '{redcap_subject_sl}' or "
f"{' or '.join(contains_logic)}"
}

# post query to redcap
content = post_to_redcap(api_url,
record_query,
False)

# check if response body is nothing but a sad empty array
content_dict_list = json.loads(content)
# print(content_dict_list)

content = json.dumps(content_dict_list).encode('utf-8')
print(content)
1 change: 1 addition & 0 deletions tests/test_lochness.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def update_for_fake_redcap(self, study):
token = Tokens()
api_token, url = token.read_token_or_get_input('redcap_fake')

self.keyring[f'redcap.{study}'] = {}
self.keyring[f'redcap.{study}']['URL'] = url
self.keyring[f'redcap.{study}']['API_TOKEN'] = {study: api_token}

Expand Down