CoolFace
Apppublic

mishtert/tracer

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
dtxutils.py344 linesDownload Raw Back to root
1from meshutils import nct_to_mesh_term, mesh_term_to_id, df_mesh, df_mesh_ct2from cid import CaseInsensitiveDict3from dictutils import *4import re5import streamlit as st6 7 8# mesh list extract9def meshtrm_lst_xtract(nct_value):10	try:11		mesh_term = nct_to_mesh_term[nct_value]12		mesh_term_list = list(mesh_term)13		return mesh_term_list14	except:15		pass16 17 18@st.cache(suppress_st_warning=True, allow_output_mutation=True)19# type extract fun20def type_extract(mesh_term_list):21	mesh_term_list = [mesh_term_list] if isinstance(mesh_term_list, str) else mesh_term_list22	# print('mesh_term_list: ',mesh_term_list)23 24	# l2_map_lst=[]25	uid_lst = []26	if mesh_term_list is not None:27		for val in mesh_term_list:28			# print('value inside uid forloop:',val)29			try:30				# print('Inside get uid')31				uid = mesh_term_to_id[val]32				uid_lst.append(uid)33				# print(uid_lst)34				if uid_lst is None:35					uid_lst = []36			except:37				pass38				# print('error in get uid list')39 40				# get mesh num41		mesh_num_xtract_lst = []42 43		for val in uid_lst:44			try:45				# print('Inside get mesh num')46				mesh_num_xtract = df_mesh.loc[df_mesh['ui'] == val, 'mesh_number'].iloc[0]47				mesh_num_xtract_lst.append(mesh_num_xtract)48				# print(mesh_num_xtract_lst)49				if ',' in mesh_num_xtract_lst[0]:50					mesh_num_xtract_lst = mesh_num_xtract_lst[0].split(", ")51					# print('mesh_num_xtract_lst after spltting',mesh_num_xtract_lst)52			except:53				pass54				# print('error in get mesh num')55 56		# mesh number extract l257		l2_map_lst = []58		for val in mesh_num_xtract_lst:59			# print('Inside l2map for loop',val)60			search_value = val[:3]61			# print('printing search value:',search_value)62			try:63				l2_map = df_mesh.loc[df_mesh['mesh_number'] == search_value, 'name'].iloc[0]64				# print(l2_map)65				l2_map_lst.append(l2_map)66				# print(l2_map_lst)67				if l2_map_lst is None:68					l2_map_lst = []69			except:70				pass71 72		l2_map_lst = list(set(l2_map_lst))73		# print('finaloutput',l2_map_lst)74		return l2_map_lst75 76 77def split_values(col_val):78	# """split words seperated by special characters"""79	# print(col_val)80	if col_val != '':81		char_list = ['|', ',', '/', '.', ';', './', ',/', '/ ', ' /']82		# res = ' '.join([ele for ele in char_list if(ele in col_val)])83		res = [ele for ele in char_list if (ele in col_val)]84		# print('printing string of found char',res)85		colstring = str(col_val)86		f_res = []87		try:88			while len(res) > 0:89				res = res[-1]90				f_res = colstring.split(''.join(res))91				# print(f_res)92				# return f_res93				f_res = [x for x in f_res if x is not None]94				return ', '.join(f_res)95		except:96			pass97		else:98			return col_val99 100 101def map_entry_terms(myText):102	obj = CaseInsensitiveDict(entry_dict)103	pattern = re.compile(r'(?<!\w)(' + '|'.join(re.escape(key) for key in obj.keys()) + r')(?!\w)', flags=re.IGNORECASE)104	text = pattern.sub(lambda x: obj[x.group()], myText)105	# text = pattern.sub(lambda x: obj[x.group()], text)106	return text.strip().split('/')107 108 109def remove_none(some_list):110	some_list = [some_list] if isinstance(some_list, str) else some_list111	if some_list is not None:112		some_list = list(filter(lambda x: x != None, some_list))113		return some_list114 115 116def retain_all_ta(some_list):117	some_list = [some_list] if isinstance(some_list, str) else some_list118	# some_list.split(',')119	value = 'all_ta'120	#   print(value)121	if some_list is not None:122		if value in some_list:123			some_list = [value]124			return some_list125		else:126			return some_list127 128 129def unique_list(l):130	l = map(str.strip, l)  # remove whitespace from list element131	# print(l)132	ulist = []133	[ulist.append(x) for x in l if x not in ulist]134	return ulist135 136 137def split_for_type_extract(my_list, char):138	# print('entering the function:',my_list)139	try:140		my_list = [my_list] if isinstance(my_list, str) else my_list141		if my_list is not None:142			# print(my_list)143			my_list = list(map(lambda x: x.split(char)[0], my_list))144			# my_list = [x for x in my_list if x is not None]145			return my_list146	except:147		pass148 149 150def special_ask(col_value):151	col_value = col_value.lower()152	if col_value == 'obesity':153		ta_list = 'met'154		return ta_list.split()155	elif col_value == 'healthy subject':156		ta_list = 'all_ta'157		return ta_list.split()158	elif col_value == 'healthy subjects':159		ta_list = 'all_ta'160		return ta_list.split()161	elif col_value == 'healthy participants':162		ta_list = 'all_ta'163		return ta_list.split()164	elif col_value == 'healthy participant':165		ta_list = 'all_ta'166		return ta_list.split()167	elif col_value == 'inflammation':168		ta_list = 'ai'169		return ta_list.split()170	else:171		pass172 173 174def remove_stopwords(query):175	stopwords = ['acute-on-chronic', 'acute', 'chronic',176	             'diseases of the', '-19', '- 19', '19', '.']177	if query is not None:178		querywords = query.split()179		resultwords = [word for word in querywords if word.lower() not in stopwords]180		result = ' '.join(resultwords)181		return result182	else:183		''184 185 186def gb_2_us(text, mydict):187	try:188		for us, gb in mydict.items():189			text = text.replace(gb, us)190			return text191	except:192		return ''193 194 195def fix_text_with_dict(text, mydict):196	text = ','.join([repl_dict.get(i, i) for i in text.split(', ')])197	return text198 199 200def replace_text(mytext):201	cancer = ['cancer', 'neoplasm', 'carcinoma', 'lymphoma', 'adenoma', 'myoma', 'meningioma',202	          'malignancy', 'tumor', 'malignancies', 'chemotherapy']203	# fracture = ['fractures', 'fracture']204	heart_failure = ['heart failure', 'cardiac']205	ectomy = 'prostatectomy'206	covid = 'covid'207	transplant = 'transplant'208	healthy = 'healthy'209	park = 'parkinson'210	allergy = ['allergy', 'allergic']211	virus = 'virus'212	cornea = ['cornea', 'eye', 'ocular', 'macular']213	vaccine = 'vaccines'214	ureter = 'ureter'215	mutation = 'mutation'216	stemcell = 'stem cells'217	behavior = ['behavior', 'depressive', 'depression', 'anxiety', 'satisfaction', 'grief']218	molar = ['molar', 'dental', 'maxillary']219	diet = 'diet'220	biopsy = 'biopsy'221	physiology = 'physiology'222	infection = ['infection', 'bacteremia', 'fungemia']223	preg = ['pregnancy', 'pregnant', 'labor', 'birth']224	imaging = ['x-ray', 'imaging', 'mri']225	surgery = 'surgery'226	angina = 'angina'227	use_disorder = ['use disorder', 'obsessive', 'panic', 'posttraumatic stress',228	                'post-traumatic stress', 'schizophrenia']229 230	if mytext:231		try:232			if any(text in mytext.lower() for text in cancer):233				mytext = 'neoplasms'234				return mytext235			if any(text in mytext.lower() for text in heart_failure):236				mytext = 'cardiovascular diseases'237				return mytext238			if covid in mytext.lower():239				mytext = 'covid-19'240				return mytext241			if ectomy in mytext.lower():242				mytext = 'urogenital surgical procedures'243				return mytext244			if transplant in mytext.lower():245				mytext = 'body regions'246				return mytext247			if healthy in mytext.lower():248				mytext = 'healthy volunteers'249				return mytext250			if any(text in mytext.lower() for text in allergy):251				mytext = 'immune system diseases'252				return mytext253			if park in mytext.lower():254				mytext = 'parkinson disease'255				return mytext256			if park in mytext.lower():257				mytext = 'immune system diseases'258				return mytext259			if virus in mytext.lower():260				mytext = 'viruses'261				return mytext262			if any(text in mytext.lower() for text in cornea):263				mytext = 'eye diseases'264				return mytext265			if vaccine in mytext.lower():266				mytext = 'vaccines'267				return mytext268			if ureter in mytext.lower():269				mytext = 'ureter'270				return mytext271			if mutation in mytext.lower():272				mytext = 'mutation'273				return mytext274			if stemcell in mytext.lower():275				mytext = 'stem cells'276				return mytext277			if any(text in mytext.lower() for text in behavior):278				mytext = 'behavior'279				return mytext280			if any(text in mytext.lower() for text in molar):281				mytext = 'molar'282				return mytext283			if diet in mytext.lower():284				mytext = 'diet'285				return mytext286			if biopsy in mytext.lower():287				mytext = 'biopsy'288				return mytext289			if physiology in mytext.lower():290				mytext = 'physiology'291				return mytext292			if any(text in mytext.lower() for text in infection):293				mytext = 'infections'294				return mytext295			if any(text in mytext.lower() for text in preg):296				mytext = 'reproductive and urinary physiological phenomena'297				return mytext298			if any(text in mytext.lower() for text in imaging):299				mytext = 'diagnosis'300				return mytext301			if surgery in mytext.lower():302				mytext = 'medicine'303				return mytext304			if angina in mytext.lower():305				mytext = 'angina pectoris'306				return mytext307			if any(text in mytext.lower() for text in use_disorder):308				mytext = 'mental disorders'309				return mytext310			else:311				return mytext312		except:313			return ''314 315 316# For studies in CTgov 317def is_nct(col_value):318	# Returns mesh term list based on NCT ID319	val = col_value[:3]320	if val == 'NCT':321		try:322			if col_value in df_mesh_ct.values:323				mesh_term_list = meshtrm_lst_xtract(col_value)324				l2map = type_extract(mesh_term_list)325				return l2map326		except:327			pass328	else:329		'Study Not in Database, Please enter condition or conditions treated'330	return331 332 333# For studies not in CTgov334def is_not_nct(col_value):335	# Returns mesh term list based on NCT ID336	# Returns disease type l2 tag in Mesh dictionary337	if col_value is not None:338		mesh_term_list = col_value339		l2map = type_extract(mesh_term_list)340		return l2map341	else:342		None343	return344