Пример #1
0
def transfer_file(data_file):
    """Transfer contents of soakdb.sqlite file to xcdb into separate
    (crystal, lab, refinement, dimple and data_processing tables.

    :param data_file: File-path(?) pointing towards a soakdb.sqlite file
    :type data_file: str
    :return: Function should not return anything but will write to a database.
    :rtype: None
    """
    maint_exists = db_functions.check_table_sqlite(data_file, 'mainTable')
    if maint_exists == 1:
        db_functions.transfer_table(translate_dict=db_functions.crystal_translations(), filename=data_file,
                                    model=Crystal)
        db_functions.transfer_table(translate_dict=db_functions.lab_translations(), filename=data_file,
                                    model=Lab)
        db_functions.transfer_table(translate_dict=db_functions.refinement_translations(), filename=data_file,
                                    model=Refinement)
        db_functions.transfer_table(translate_dict=db_functions.dimple_translations(), filename=data_file,
                                    model=Dimple)
        db_functions.transfer_table(translate_dict=db_functions.data_processing_translations(),
                                    filename=data_file, model=DataProcessing)

    soakdb_query = SoakdbFiles.objects.get(filename=data_file)
    soakdb_query.status = 2
    soakdb_query.save()
Пример #2
0
def transfer_file(data_file):
    maint_exists = db_functions.check_table_sqlite(data_file, 'mainTable')
    if maint_exists == 1:
        db_functions.transfer_table(
            translate_dict=db_functions.crystal_translations(),
            filename=data_file,
            model=Crystal)
        db_functions.transfer_table(
            translate_dict=db_functions.lab_translations(),
            filename=data_file,
            model=Lab)
        db_functions.transfer_table(
            translate_dict=db_functions.refinement_translations(),
            filename=data_file,
            model=Refinement)
        db_functions.transfer_table(
            translate_dict=db_functions.dimple_translations(),
            filename=data_file,
            model=Dimple)
        db_functions.transfer_table(
            translate_dict=db_functions.data_processing_translations(),
            filename=data_file,
            model=DataProcessing)

    soakdb_query = SoakdbFiles.objects.get(filename=data_file)
    soakdb_query.status = 2
    soakdb_query.save()
Пример #3
0
    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('')
Пример #4
0
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())