Package Gnumed :: Package importers :: Module gmClinicaExporter |
[frames] | no frames]

# Source Code for Module Gnumed.importers.gmClinicaExporter

      1  #------------------------------------------------
      2  # Clinica -> GNUmed-SQL exporter
      3  #
      4  # SQLite3 database
      5  #
      6  # http://pysqlite.googlecode.com/svn/doc/sqlite3.html
      7  #
      8  #------------------------------------------------
      9
     10  import sys
     11  import os
     12  import logging
     13  import pysqlite2.dbapi2 as sqlite
     14
     15
     16  # GNUmed
     17  if __name__ == '__main__':
     18          sys.path.insert(0, '../../')
     19
     20  from Gnumed.pycommon import gmLog2
     21  from Gnumed.pycommon import gmI18N
     22  from Gnumed.pycommon import gmTools
     23  from Gnumed.pycommon import gmDateTime
     24
     25
     26  _log = logging.getLogger('gm-clinica')
     27
     28
     29  gender_map = {'MALE': 'm', 'FEMALE': 'f'}
     30  Clinica_encounter_type = '[Clinica] encounter'
     31  Clinica_episode = '[Clinica] episode'
     32  #------------------------------------------------
     33 -def create_visit_sql(pk_patient, clinica_db):
     34          # CREATE TABLE visits (subsequent_checks TEXT, systemic_therapy TEXT, ID INTEGER PRIMARY KEY, laboratory_exam TEXT, diagnosis TEXT, histopathology TEXT, anamnesis TEXT, date TEXT, patient INTEGER, physical_examination TEXT, topical_therapy TEXT);
     35
     36          curs = clinica_db.cursor()
     37          cmd = 'SELECT * FROM visits WHERE patient = %s' % pk_patient
     38          curs.execute(cmd)
     39          keys = [ r[0] for r in curs.description ]
     40          row = curs.fetchone()
     41
     42          if row is None:
     43                  print '-- no visits for patient'
     44                  return
     45
     46          row = dict(zip(keys, row))
     47          row['date'] = '%s-%s-%s:%s.%s' % (
     48                  row['date'][:4],
     49                  row['date'][5:7],
     50                  row['date'][8:13],
     51                  row['date'][14:16],
     52                  row['date'][17:]
     53          )
     54          print '-- visit encounter'
     55          print "INSERT INTO clin.encounter (fk_patient, fk_type, started, last_affirmed) VALUES (currval('dem.identity_pk_seq'), (SELECT pk FROM clin.encounter_type WHERE description = '%s' LIMIT 1), '%s'::timestamp with time zone, '%s'::timestamp with time zone + '1m'::interval);" % (
     56                  Clinica_encounter_type,
     57                  row['date'],
     58                  row['date']
     59          )
     60          print '-- import episode'
     61          print "INSERT INTO clin.episode (fk_health_issue, description, is_open, fk_encounter) VALUES (NULL, '%s', True, currval('clin.encounter_pk_seq'));" % Clinica_episode
     62
     63          while row is not None:
     64                  print '-- visit SOAP'
     65                  print "INSERT INTO clin.clin_narrative (soap_cat, clin_when, narrative, fk_encounter, fk_episode) VALUES ('s', '%s'::timestamp with time zone, E'%s', currval('clin.encounter_pk_seq'), currval('clin.episode_pk_seq'));" % (
     66                          row['date'],
     67                          row['anamnesis']
     68                  )
     69                  sOap = _('%s\\n\\nLab work:\\n%s\\n\\nHistopathology:\\n%s') % (
     70                          row['physical_examination'],
     71                          row['laboratory_exam'],
     72                          row['histopathology']
     73                  )
     74                  print "INSERT INTO clin.clin_narrative (soap_cat, clin_when, narrative, fk_encounter, fk_episode) VALUES ('o', '%s'::timestamp with time zone, E'%s', currval('clin.encounter_pk_seq'), currval('clin.episode_pk_seq'));" % (
     75                          row['date'],
     76                          sOap
     77                  )
     78                  print "INSERT INTO clin.clin_narrative (soap_cat, clin_when, narrative, fk_encounter, fk_episode) VALUES ('a', '%s'::timestamp with time zone, E'%s', currval('clin.encounter_pk_seq'), currval('clin.episode_pk_seq'));" % (
     79                          row['date'],
     80                          row['diagnosis']
     81                  )
     82                  soaP = _('Topical therapy:\\n%s\\n\\nSystemic therapy:\\n%s\\n\\nSubsequent checks:\\n%s') % (
     83                          row['topical_therapy'],
     84                          row['systemic_therapy'],
     85                          row['subsequent_checks']
     86                  )
     87                  print "INSERT INTO clin.clin_narrative (soap_cat, clin_when, narrative, fk_encounter, fk_episode) VALUES ('p', '%s'::timestamp with time zone, E'%s', currval('clin.encounter_pk_seq'), currval('clin.episode_pk_seq'));" % (
     88                          row['date'],
     89                          soaP
     90                  )
     91
     92                  row = curs.fetchone()
     93                  if row is not None:
     94                          row['date'] = '%s-%s-%s:%s.%s' % (
     95                                  row['date'][:4],
     96                                  row['date'][5:7],
     97                                  row['date'][8:13],
     98                                  row['date'][14:16],
     99                                  row['date'][17:]
    100                          )
    101  #------------------------------------------------
    102 -def sanitize_patient_row(row):
    103          # CREATE TABLE patients (gender TEXT, doctor INTEGER, surname TEXT, ID INTEGER PRIMARY KEY, identification_code TEXT, phone TEXT, given_name TEXT, birth_date TEXT, residence_address TEXT);
    104
    105          if row['gender'] is None:
    106                  row['gender'] = 'h'
    107          elif row['gender'].upper() not in ['MALE', 'FEMALE']:
    108                  row['gender'] = 'h'
    109          else:
    110                  row['gender'] = gender_map[row['gender']]
    111
    112          row['surname'] = gmTools.coalesce (
    113                  row['surname'],
    114                  'Clinica [#%s]' % row['ID'],
    115                  none_equivalents = [None, ''],
    116                  function4value = ('strip', None)
    117          )
    118
    119          row['given_name'] = gmTools.coalesce (
    120                  row['given_name'],
    121                  '#%s' % row['ID'],
    122                  none_equivalents = [None, ''],
    123                  function4value = ('strip', None)
    124          )
    125
    126          row['identification_code'] = gmTools.coalesce (
    127                  row['identification_code'],
    128                  None,
    129                  none_equivalents = [None, ''],
    130                  function4value = ('strip', None)
    131          )
    132
    133          row['phone'] = gmTools.coalesce (
    134                  row['phone'],
    135                  None,
    136                  none_equivalents = [None, ''],
    137                  function4value = ('strip', None)
    138          )
    139
    140          row['birth_date'] = gmTools.coalesce (
    141                  row['birth_date'],
    142                  None,
    143                  none_equivalents = [None, ''],
    144                  function4value = ('strip', None)
    145          )
    146          row['birth_date'] = '%s-%s-%s:%s.%s' % (
    147                  row['birth_date'][:4],
    148                  row['birth_date'][5:7],
    149                  row['birth_date'][8:13],
    150                  row['birth_date'][14:16],
    151                  row['birth_date'][17:]
    152          )
    153
    154          row['residence_address'] = gmTools.coalesce (
    155                  row['residence_address'],
    156                  None,
    157                  none_equivalents = [None, ''],
    158                  function4value = ('strip', None)
    159          )
    160
    161          return row
    162  #------------------------------------------------
    163 -def create_gnumed_import_sql(filename):
    164          # CREATE TABLE patients (gender TEXT, doctor INTEGER, surname TEXT, ID INTEGER PRIMARY KEY, identification_code TEXT, phone TEXT, given_name TEXT, birth_date TEXT, residence_address TEXT);
    165
    166          print ''
    167          print 'set default_transaction_read_only to off;'
    168          print ''
    169          print "begin;"
    170          print ''
    171
    172          now = gmDateTime.pydt_now_here().isoformat()
    173
    174          clinica_db = sqlite.connect(database = filename)
    175          curs = clinica_db.cursor()
    176          cmd = 'select * from patients'
    177          curs.execute(cmd)
    178          keys = [ r[0] for r in curs.description ]
    179          row = curs.fetchone()
    180
    181          if row is None:
    182                  print "-- no patients in database"
    183                  return
    184
    185          row = sanitize_patient_row(dict(zip(keys, row)))
    186          print '-- import-related encounter type'
    187          print "INSERT INTO clin.encounter_type (description) SELECT '%s' WHERE NOT EXISTS (SELECT 1 FROM clin.encounter_type WHERE description = '%s' LIMIT 1);" % (
    188                  Clinica_encounter_type,
    189                  Clinica_encounter_type
    190          )
    191
    192          while row is not None:
    193                  print ''
    194                  print '-- next patient'
    195                  print "INSERT INTO dem.identity (gender, dob, comment) VALUES ('%s', NULL, 'Clinica import @ %s');" % (
    196                          row['gender'],
    197                          now
    198                  )
    199                  if row['birth_date'] is not None:
    200                          if row['birth_date'].strip() != '':
    201                                  print """UPDATE dem.identity SET dob = '%s'::timestamp with time zone WHERE pk = currval('dem.identity_pk_seq');""" % row['birth_date']
    202                  print """SELECT dem.add_name(currval('dem.identity_pk_seq')::integer, '%s'::text, '%s'::text, True);""" % (
    203                          row['given_name'],
    204                          row['surname']
    205                  )
    206                  print """INSERT INTO dem.lnk_identity2ext_id (id_identity, external_id, fk_origin) VALUES (currval('dem.identity_pk_seq'), '%s', dem.add_external_id_type('Clinica primary key', 'Clinica EMR'));""" % row['ID']
    207                  if row['identification_code'] is not None:
    208                          print """INSERT INTO dem.lnk_identity2ext_id (id_identity, external_id, fk_origin) VALUES (currval('dem.identity_pk_seq'), '%s', dem.add_external_id_type('Clinica-external ID', 'Clinica EMR'));""" % row['identification_code']
    209                  if row['phone'] is not None:
    210                          print """INSERT INTO dem.lnk_identity2comm (fk_identity, url, fk_type) VALUES (currval('dem.identity_pk_seq'), '%s', dem.create_comm_type('homephone'));""" % row['phone']
    211                  if row['residence_address'] is not None:
    212                          print """INSERT INTO dem.lnk_identity2comm (fk_identity, url, fk_type) VALUES (currval('dem.identity_pk_seq'), '%s', dem.create_comm_type('Clinica address'));""" % row['residence_address']
    213
    214                  create_visit_sql(row['ID'], clinica_db)
    215
    216                  row = curs.fetchone()
    217                  if row is not None:
    218                          row = sanitize_patient_row(dict(zip(keys, row)))
    219
    220          print ''
    221          print '-- comment this out when you are ready to *really* run the data import:'
    222          print 'rollback;'
    223          print ''
    224          print 'commit;'
    225  #------------------------------------------------
    226  gmDateTime.init()
    227  gmI18N.activate_locale()
    228  gmI18N.install_domain(domain = 'gnumed')
    229  try:
    230          filename = sys.argv[1]
    231          print '-- exporting from DB file:', sys.argv[1]
    232  except IndexError:
    233          filename = os.path.expanduser('~/.config/clinica/clinica.db')
    234          print '-- exporting from Clinica default DB:', filename
    235  create_gnumed_import_sql(filename)
    236