def test_soakdb_query(self): print('test_soakdb_query') results = soakdb_query(self.db) results_list = [dict(ix) for ix in results] print(results_list) print(self.json_file) self.assertTrue(len(results_list) == 1) self.assertTrue(len(self.json_file) == 1) for key in results_list[0].keys(): if key not in self.json_file[0].keys(): print('key: "' + str(key) + '" not found') else: print('checking ' + str(key) + '...') self.assertEqual(results_list[0][key], self.json_file[0][key]) print('\n')
def run(self): out_err_file = os.path.join(DirectoriesConfig().log_directory, str(str(self.filename.split('/')[3]) + '_' + str(self.filename.split('/')[4]) + '_' + str(self.filename.split('/')[5]) + '_' + str(misc_functions.get_mod_date(self.filename)) + str(self.model).replace("<class '", '').replace("'>", '') + '.txt')) print(out_err_file) results = db_functions.soakdb_query(self.filename) try: print('Number of rows from file = ' + str(len(results))) proteins = list(set([protein for protein in [protein['ProteinName'] for protein in results]])) print('Unique targets in soakdb file: ' + str(proteins)) translations = {Lab: db_functions.lab_translations(), Refinement: db_functions.refinement_translations(), DataProcessing: db_functions.data_processing_translations(), Dimple: db_functions.dimple_translations()} translation = translations[self.model] error_dict = { 'crystal': [], 'soakdb_field': [], 'model_field': [], 'soakdb_value': [], 'model_value': [] } for row in results: lab_object = self.model.objects.filter(crystal_name__crystal_name=row['CrystalName'], crystal_name__visit__filename=str(self.filename), crystal_name__compound__smiles=row['CompoundSMILES']) if len(lab_object) > 1: raise Exception('Multiple Crystals!') if len(lab_object) == 0: if self.model == Dimple and not row['DimplePathToPDB'] and not row['DimplePathToMTZ']: pass else: raise Exception('No entry for ' + str(row['CrystalName'] + ' ' + row['DimplePathToPDB'] + ' ' + row['DimplePathToMTZ'])) for key in translation.keys(): test_xchem_val = eval(str('lab_object[0].' + key)) soakdb_val = row[translation[key]] if key == 'outcome': pattern = re.compile('-?\d+') try: soakdb_val = int(pattern.findall(str(soakdb_val))[0]) except: continue if translation[key] == 'CrystalName': test_xchem_val = lab_object[0].crystal_name.crystal_name if translation[key] == 'DimpleReferencePDB' and soakdb_val: test_xchem_val = lab_object[0].reference if test_xchem_val is not None: test_xchem_val = lab_object[0].reference.reference_pdb if soakdb_val == '' or soakdb_val == 'None' or not soakdb_val: continue if isinstance(test_xchem_val, float): if float(test_xchem_val) == float(soakdb_val): continue if isinstance(test_xchem_val, int): if int(soakdb_val) == int(test_xchem_val): continue if test_xchem_val != soakdb_val: if soakdb_val in [None, 'None', '', '-', 'n/a', 'null', 'pending', 'NULL', '#NAME?', '#NOM?', 'None\t', 'Analysis Pending', 'in-situ']: continue else: error_dict['crystal'].append(str(lab_object[0].crystal_name.crystal_name)) error_dict['soakdb_field'].append(translation[key]) error_dict['model_field'].append(key) error_dict['soakdb_value'].append(soakdb_val) error_dict['model_value'].append(test_xchem_val) if error_dict['crystal']: pd.DataFrame.from_dict(error_dict).to_csv(out_err_file) except IndexError: if 'No item with that key' in traceback.format_exc(): pass else: with open(out_err_file, 'w') as f: f.write(traceback.format_exc()) with open(out_err_file, 'a') as f: f.write('\n' + str(key)) except AttributeError: with open(out_err_file, 'w') as f: f.write(traceback.format_exc()) with open(out_err_file, 'a') as f: f.write('\n' + str(lab_object)) except: with open(out_err_file, 'w') as f: f.write(traceback.format_exc()) with self.output().open('w') as f: f.write('')
def check_file_upload(filename, model, log_directory = DirectoriesConfig().log_directory): """Check if a soakdb file has been uploaded to a given django model :param filename: filename to check, :class:`transfer_soakdb.CheckFileUpload` self.filename :type filename: str :param model: model to check if file had uploaded correctly to, :class:`transfer_soakdb.CheckFileUpload` self.model :type model: str or model class, not sure tbh, I didn't write the code! :return: Should check if file is uploaded correctly :rtype: None """ out_err_file = os.path.join(log_directory, str(str(filename.split('/')[3]) + '_' + str(filename.split('/')[4]) + '_' + str(filename.split('/')[5]) + '_' + str(misc_functions.get_mod_date(filename)) + str(model).replace("<class '", '').replace("'>", '') + '.txt')) print(out_err_file) results = db_functions.soakdb_query(filename) try: print(f"Number of rows from file = {len(results)}") translations = {Lab: db_functions.lab_translations(), Refinement: db_functions.refinement_translations(), DataProcessing: db_functions.data_processing_translations(), Dimple: db_functions.dimple_translations()} translation = translations[model] # different from what is in class... error_dict = dict(crystal=[], soakdb_field=[], model_field=[], soakdb_value=[], model_value=[]) for row in results: lab_object = model.objects.filter(crystal_name__crystal_name=row['CrystalName'], crystal_name__visit__filename=str(filename), crystal_name__compound__smiles=row['CompoundSMILES']) if len(lab_object) > 1: raise Exception('Multiple Crystals!') if len(lab_object) == 0: if model == Dimple and not row['DimplePathToPDB'] and not row['DimplePathToMTZ']: pass else: raise Exception( f"No entry for {row['CrystalName']}, {row['DimplePathToPDB']}, {row['DimplePathToMTZ']}") for key in translation.keys(): test_xchem_val = eval(f"lab_objects[0].{key}") soakdb_val = row[translation[key]] if key == 'outcome': pattern = re.compile('-?\d+') try: soakdb_val = int(pattern.findall(str(soakdb_val))[0]) except: continue if translation[key] == 'CrystalName': test_xchem_val = lab_object[0].crystal_name.crystal_name if translation[key] == 'DimpleReferencePDB' and soakdb_val: test_xchem_val = lab_object[0].reference if test_xchem_val is not None: test_xchem_val = lab_object[0].reference.reference_pdb if soakdb_val == '' or soakdb_val == 'None' or not soakdb_val: continue if isinstance(test_xchem_val, float): if float(test_xchem_val) == float(soakdb_val): continue if isinstance(test_xchem_val, int): if int(soakdb_val) == int(test_xchem_val): continue if test_xchem_val != soakdb_val: if soakdb_val in [None, 'None', '', '-', 'n/a', 'null', 'pending', 'NULL', '#NAME?', '#NOM?', 'None\t', 'Analysis Pending', 'in-situ']: continue else: error_dict['crystal'].append(str(lab_object[0].crystal_name.crystal_name)) error_dict['soakdb_field'].append(translation[key]) error_dict['model_field'].append(key) error_dict['soakdb_value'].append(soakdb_val) error_dict['model_value'].append(test_xchem_val) if error_dict['crystal']: pd.DataFrame.from_dict(error_dict).to_csv(out_err_file) except IndexError: if 'No item with that key' in traceback.format_exc(): pass else: with open(out_err_file, 'w') as f: f.write(traceback.format_exc()) with open(out_err_file, 'a') as f: f.write('\n' + str(key)) except AttributeError: with open(out_err_file, 'w') as f: f.write(traceback.format_exc()) with open(out_err_file, 'a') as f: f.write('\n' + str(lab_object)) except: with open(out_err_file, 'w') as f: f.write(traceback.format_exc())