modifyPatients.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #a script to modify a patient from list. Current implementation deletes
  2. #all data for patients identified by study regulatory number from study
  3. regulatoryNumber="X000"
  4. #basic python
  5. import os
  6. import subprocess
  7. import re
  8. import datetime
  9. import sys
  10. fhome=os.path.expanduser('~')
  11. sys.path.insert(1,fhome+'/software/src/labkeyInterface')
  12. import labkeyInterface
  13. import labkeyDatabaseBrowser
  14. net=labkeyInterface.labkeyInterface()
  15. net.init(fhome+'/.labkey/network.json')
  16. db=labkeyDatabaseBrowser.labkeyDB(net)
  17. #by default uses .labkey/Remote.json configuration
  18. project="IPNUMMprospektiva/Study"
  19. #study section ################
  20. #select patients enroled under regulatory number
  21. filters=[]
  22. regulatoryFilter={\
  23. 'variable':'regulatoryNumber',\
  24. 'value':regulatoryNumber,\
  25. 'oper':'eq'}
  26. filters.append(regulatoryFilter)
  27. ds=db.selectRows(project,"study","demographicData",filters)
  28. ids=[row['ParticipantId'] for row in ds['rows']]
  29. idCode=""
  30. for id in ids:
  31. if len(idCode)>0:
  32. idCode+=";"
  33. idCode+=id
  34. idFilter={\
  35. 'variable':'ParticipantId',\
  36. 'value':idCode,\
  37. 'oper':'in'}
  38. #print("idCode: {}".format(idCode))
  39. dsts=db.selectRows(project,"study","Datasets",[])
  40. for row in dsts['rows']:
  41. dsId=db.selectRows(project,"study",row['Name'],[idFilter])
  42. rows=[r for r in dsId['rows']]
  43. print("[{}]: {}".format(row['Name'],len(rows)))
  44. if len(rows)==0:
  45. continue
  46. #this is for security only
  47. #db.modifyRows('delete',project,"study",row['Name'],rows)
  48. # list section ########################
  49. ds=db.selectRows(project,"lists","crfEntry",[regulatoryFilter])
  50. masterRows=[row for row in ds['rows']]
  51. crfs=[row['entryId'] for row in masterRows]
  52. crfCode=""
  53. for crf in crfs:
  54. if len(crfCode)>0:
  55. crfCode+=";"
  56. crfCode+=crf
  57. print("crfCode: {}".format(crfCode))
  58. crfFilter={\
  59. 'variable':'crfRef',\
  60. 'value':crfCode,\
  61. 'oper':'in'}
  62. dsts=db.selectRows(project,"lists","inputLists",[])
  63. for row in dsts['rows']:
  64. dsId=db.selectRows(project,"lists",row['queryName'],[crfFilter])
  65. rows=[r for r in dsId['rows']]
  66. print("[{}]: {}".format(row['queryName'],len(rows)))
  67. if len(rows)==0:
  68. continue
  69. #this is for security only
  70. #db.modifyRows('delete',project,"lists",row['queryName'],rows)
  71. #this is for security only
  72. #ds=db.modifyRows('delete',project,"lists","crfEntry",masterRows)
  73. print("Done")
  74. quit()