def update_sqlite(self, skip=[], goto_reaction=None, key_names='all'): for key_values in self.read(skip=skip, goto_reaction=goto_reaction): with CathubSQLite(self.cathub_db) as db: id = db.check(key_values['reaction_energy']) #print('Allready in reaction db with row id = {}'.format(id)) if id is not None: db.update(id, key_values, key_names)
def write(self, skip=[], goto_reaction=None): for key_values in self.read(skip=skip, goto_reaction=goto_reaction): with CathubSQLite(self.cathub_db) as db: id = db.check(key_values['chemical_composition'], key_values['reaction_energy']) #print('Allready in reaction db with row id = {}'.format(id)) if id is None: id = db.write(key_values) print('Written to reaction db row id = {}'.format(id)) elif self.update: db.update(id, key_values) print('Updated reaction db row id = {}'.format(id)) else: print('Allready in reaction db with row id = {}'.format(id))
def get_reactions(columns='all', n_results=20, write_db=False, **kwargs): """ Get reactions from server Give key value strings as arguments """ if write_db or columns == 'all': columns = all_columns['reactions'] queries = {} for key, value in kwargs.items(): key = map_column_names(key) if key == 'distinct': if value in [True, 'True', 'true']: queries.update({key: True}) continue if isinstance(value, int) or isinstance(value, float): queries.update({key: value}) else: queries.update({key: '{0}'.format(value)}) subtables = [] if write_db: subtables = ['reactionSystems', 'publication'] else: subtables = [] data = query(table='reactions', subtables=subtables, columns=columns, n_results=n_results, queries=queries) if not write_db: return data print('Writing result to Reactions.db') unique_ids = [] for row in data['reactions']['edges']: with CathubSQLite('Reactions.db') as db: row = row['node'] key_values = {} for key in all_columns['reactions']: v = row[key] # if isinstance(v, unicode): # v = v.encode('utf-8') try: v = json.loads(v) except BaseException: pass key_values[convert(key)] = v ase_ids = {} energy_corrections = {} for row_rs in row['reactionSystems']: if row_rs['name'] == 'N/A': continue ase_ids[row_rs['name']] = row_rs['aseId'] energy_corrections[row_rs['name']] = row_rs['energyCorrection'] if not ase_ids: ase_ids = None energy_corrections = None else: unique_ids += ase_ids.values() key_values['ase_ids'] = ase_ids key_values['energy_corrections'] = ase_ids # publications pub_key_values = {} row_p = row['publication'] for key in all_columns['publications']: pub_key_values[convert(key)] = row_p[key] db.write_publication(pub_key_values) # reactions and reaction_systems id = db.check(key_values['chemical_composition'], key_values['reaction_energy']) if id is None: id = db.write(key_values) else: db.update(id, key_values) if ase_ids is not None: # Ase structures with ase.db.connect('Reactions.db') as ase_db: con = ase_db.connection cur = con.cursor() cur.execute('SELECT unique_id from systems;') unique_ids0 = cur.fetchall() unique_ids0 = [un[0] for un in unique_ids0] unique_ids = [un for un in unique_ids if un not in unique_ids0] for unique_id in list(set(unique_ids)): # if ase_db.count('unique_id={}'.format(unique_id)) == 0: atomsrow = get_atomsrow_by_id(unique_id) ase_db.write(atomsrow) print('Writing complete!') return data
def get_reactions(n_results=20, write_db=False, **kwargs): queries = {} for key, value in kwargs.items(): key = map_column_names(key) if key == 'distinct': if value in ['True', 'true']: # WARNING: undefined variable name 'query_dict' query_dict.update({key: True}) continue try: value = int(value) queries.update({key: value}) except BaseException: queries.update({key: '{0}'.format(value)}) subtables = [] # if write_local: subtables = ['reactionSystems', 'publication'] data = query(table='reactions', subtables=subtables, columns=all_columns['reactions'], n_results=n_results, queries=queries) if not write_db: return data for row in data['data']['reactions']['edges']: with CathubSQLite('Reactions.db') as db: row = row['node'] key_values = {} for key in all_columns['reactions']: v = row[key] # if isinstance(v, unicode): # v = v.encode('utf-8') try: v = json.loads(v) except BaseException: pass key_values[convert(key)] = v ase_ids = {} energy_corrections = {} for row_rs in row['reactionSystems']: if row_rs['name'] == 'N/A': continue ase_ids[row_rs['name']] = row_rs['aseId'] energy_corrections[row_rs['name']] = row_rs['energyCorrection'] if not ase_ids: ase_ids = None energy_corrections = None key_values['ase_ids'] = ase_ids key_values['energy_corrections'] = ase_ids # publications pub_key_values = {} row_p = row['publication'] for key in all_columns['publications']: pub_key_values[convert(key)] = row_p[key] db.write_publication(pub_key_values) # reactions and reaction_systems id = db.check(key_values['chemical_composition'], key_values['reaction_energy']) if id is None: id = db.write(key_values) else: db.update(id, key_values) if ase_ids is not None: # Ase structures with ase.db.connect('Reactions.db') as ase_db: for unique_id in ase_ids.values(): if ase_db.count('unique_id={}'.format(unique_id)) == 0: atomsrow = get_atomsrow_by_id(unique_id) ase_db.write(atomsrow) return data