Exemple #1
0
def trials_filter_by(run_id, filter_by):
    runner = Runner.get(run_id)
    if runner is None:
        bottle.abort(404)

    if not runner.done:
        bottle.abort(400, "Trial results are not available")

    ncts = runner.get_ncts(restrict='none')
    sess = _get_session()
    run_data = sess.get('runs', {}).get(run_id, {})

    # demographics - get age and gender
    if 'demographics' == filter_by:
        f_gender = run_data.get('gender')
        f_age = int(run_data.get('age', 0))

        for tpl in ncts:
            nct = tpl[0]
            reason = tpl[1] if len(tpl) > 1 else None

            if not reason:
                trial = Trial(nct)
                trial.load()

                # filter gender
                if f_gender:
                    if 'male' == f_gender:
                        if trial.eligibility.gender == 2:
                            reason = "Limited to women"
                    else:
                        if trial.eligibility.gender == 1:
                            reason = "Limited to men"

                # filter age
                if f_age > 0:
                    if trial.eligibility.min_age and trial.eligibility.min_age > f_age:
                        reason = "Patient is too young (min age %d)" % trial.eligibility.min_age
                    elif trial.eligibility.max_age and trial.eligibility.max_age < f_age:
                        reason = "Patient is too old (max age %d)" % trial.eligibility.max_age

            # TODO: REFACTOR into runner class!
            if reason:
                runner.write_trial_reason(nct, reason)

        runner.commit_transactions()

    # problems (only if NLP is on)
    elif 'problems' == filter_by:
        if USE_NLP:
            probs = problems().get('problems', [])

            # extract snomed codes from patient's problem list
            snomed = SNOMEDLookup()
            exclusion_codes = []
            for problem in probs:
                snomed_url = problem.get('sp:problemName',
                                         {}).get('sp:code', {}).get('@id')
                if snomed_url is not None:
                    snomed_code = os.path.basename(snomed_url)
                    exclusion_codes.append(snomed_code)

            # look at trial criteria
            for tpl in ncts:
                nct = tpl[0]
                reason = tpl[1] if len(tpl) > 1 else None

                # if we already have a reason, this trial has already been filtered
                if not reason:
                    trial = Trial(nct)
                    trial.load()

                    # exclusion criterion matched
                    if trial.filter_snomed(exclusion_codes) is not None:
                        reason = 'Matches exclusion criterium "%s" (SNOMED %s)' % (
                            snomed.lookup_code_meaning(match, True,
                                                       True), match)
                        break

                # TODO: REFACTOR
                # runner.write_trial_reason(nct, reason)

    # unknown filtering property
    else:
        return '{"error": "We can not filter by %s"}' % filter_by

    return '{"status": "ok"}'
def trials_filter_by(run_id, filter_by):
	runner = Runner.get(run_id)
	if runner is None:
		bottle.abort(404)
	
	if not runner.done:
		bottle.abort(400, "Trial results are not available")
	
	ncts = runner.get_ncts(restrict='none')
	sess = _get_session()
	run_data = sess.get('runs', {}).get(run_id, {})
	
	# demographics - get age and gender
	if 'demographics' == filter_by:
		f_gender = run_data.get('gender')
		f_age = int(run_data.get('age', 0))
		
		for tpl in ncts:
			nct = tpl[0]
			reason = tpl[1] if len(tpl) > 1 else None
			
			if not reason:
				trial = Trial(nct)
				trial.load()
				
				# filter gender
				if f_gender:
					if 'male' == f_gender:
						if trial.eligibility.gender == 2:
							reason = "Limited to women"
					else:
						if trial.eligibility.gender == 1:
							reason = "Limited to men"
				
				# filter age
				if f_age > 0:
					if trial.eligibility.min_age and trial.eligibility.min_age > f_age:
						reason = "Patient is too young (min age %d)" % trial.eligibility.min_age
					elif trial.eligibility.max_age and trial.eligibility.max_age < f_age:
						reason = "Patient is too old (max age %d)" % trial.eligibility.max_age
			
			# TODO: REFACTOR into runner class!
			if reason:
				runner.write_trial_reason(nct, reason)
		
		runner.commit_transactions()
	
	# problems (only if NLP is on)
	elif 'problems' == filter_by:
		if USE_NLP:
			probs = problems().get('problems', [])
			
			# extract snomed codes from patient's problem list
			snomed = SNOMEDLookup()
			exclusion_codes = []
			for problem in probs:
				snomed_url = problem.get('sp:problemName', {}).get('sp:code', {}).get('@id')
				if snomed_url is not None:
					snomed_code = os.path.basename(snomed_url)
					exclusion_codes.append(snomed_code)
			
			# look at trial criteria
			for tpl in ncts:
				nct = tpl[0]
				reason = tpl[1] if len(tpl) > 1 else None
				
				# if we already have a reason, this trial has already been filtered
				if not reason:
					trial = Trial(nct)
					trial.load()
					
					# exclusion criterion matched
					if trial.filter_snomed(exclusion_codes) is not None:
						reason = 'Matches exclusion criterium "%s" (SNOMED %s)'  % (snomed.lookup_code_meaning(match, True, True), match)
						break
				
				# TODO: REFACTOR
				# runner.write_trial_reason(nct, reason)
	
	# unknown filtering property
	else:
		return '{"error": "We can not filter by %s"}' % filter_by
	
	return '{"status": "ok"}'