Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # pylint: disable=invalid-name
  3. """Reads UMNSRS datasets where CUIs are used, add corresponding MeSHes to the
  4. CUIs"""
  5.  
  6. from pathlib import Path
  7. import csv
  8. from tqdm import tqdm
  9. from BMET.uts_api_client import UtsClient
  10.  
  11. file_in = Path('')
  12. while not file_in.is_file():
  13. user_input = input("Enter path to a csv reference file containing CUIs: ")
  14. file_in = Path(user_input)
  15. file_out = Path(file_in.with_suffix('').as_posix() + '_mesh' +
  16. file_in.suffix)
  17.  
  18. # Gather all the CUI codes
  19. uts_client = UtsClient()
  20. rows = sum(1 for line in open(file_in)) - 1
  21. with open(file_in) as fin, open(file_out, 'w') as fout:
  22. csv_reader = csv.DictReader(fin)
  23. csv_writer = csv.writer(fout, quotechar='"')
  24. csv_writer.writerow(csv_reader.fieldnames + ['MESH1', 'MESH2'])
  25. pbar = tqdm(total=rows)
  26. for flds in csv_reader:
  27. pbar.update()
  28. for i in '12':
  29. cui = flds['CUI'+i]
  30. tok = flds['TERM'+i]
  31. try:
  32. rst = uts_client.get_concept_mesh_atoms(cui, tok)
  33. if rst is None:
  34. flds['MESH'+i] = 'None'
  35. else:
  36. mesh = rst['result'][0]['code'].split('/')[-1]
  37. flds['MESH'+i] = mesh
  38. except Exception as e:
  39. print(f"Error: {e} Cui: {cui} Rec: {flds}")
  40. raise
  41. csv_writer.writerow(flds.values())
  42. pbar.close()
  43. print(f"Finished writing records on {file_out}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement