def data_from_sdf(sdf_file_path): mol_dict = {} row_id_list = [] row_id_dict = {} col_id_list = [] col_id_dict = {} # first pass, load the identifiers, MOL files, and tag names col = 0 sdf = SDF(sdf_file_path) while 1: rec = sdf.read() if not rec: break mol = rec.get('MOL') # get the unique identifier mol_id = mol[0].strip() # store the MOL record mol_dict[mol_id] = ''.join(mol) # add row (assuming mol_id is unique) row_id_list.append(mol_id) row_id_dict[mol_id] = None # add column (if new) for key in rec.kees: if key != 'MOL': if key not in col_id_dict: col_id_list.append(key) col_id_dict[key] = None # second pass, read the actual data into the table structure content = {} sdf = SDF(sdf_file_path) while 1: rec = sdf.read() if not rec: break mol_id = rec.get('MOL')[0].strip() for key in rec.kees: if key != 'MOL': content[ (mol_id,key) ] = rec.get(key)[0].strip() return { 'content' : content, 'row_id_list' : row_id_list, 'col_id_list' : col_id_list, 'mol_dict' : mol_dict }
def data_from_sdf(sdf_file_path): mol_dict = {} row_id_list = [] row_id_dict = {} col_id_list = [] col_id_dict = {} # first pass, load the identifiers, MOL files, and tag names col = 0 sdf = SDF(sdf_file_path) while 1: rec = sdf.read() if not rec: break mol = rec.get('MOL') # get the unique identifier mol_id = mol[0].strip() # store the MOL record mol_dict[mol_id] = ''.join(mol) # add row (assuming mol_id is unique) row_id_list.append(mol_id) row_id_dict[mol_id] = None # add column (if new) for key in rec.kees: if key != 'MOL': if key not in col_id_dict: col_id_list.append(key) col_id_dict[key] = None # second pass, read the actual data into the table structure content = {} sdf = SDF(sdf_file_path) while 1: rec = sdf.read() if not rec: break mol_id = rec.get('MOL')[0].strip() for key in rec.kees: if key != 'MOL': content[(mol_id, key)] = rec.get(key)[0].strip() return { 'content': content, 'row_id_list': row_id_list, 'col_id_list': col_id_list, 'mol_dict': mol_dict }
def load_annotated_sdf(filename, object=None, state=1, discrete=1, _self=cmd): pymol = _self._pymol cmd = _self # get object name from file prefix if object == None: object = re.sub(r"\.[sS][dD][fF]$", "", filename) # open the SD file inp_sdf = SDF(filename) # create a persistent place to store the annotations if not hasattr(pymol.session, 'annotation'): pymol.session.annotation = {} # create a state-indexed dictionary for this object state_dict = {} pymol.session.annotation[object] = state_dict while 1: # get next record sdf_rec = inp_sdf.read() # if at end of list, break out of loop if not sdf_rec: break # get the MOL portion of the record mol_list = sdf_rec.get('MOL') # load it into PyMOL cmd.read_molstr(string.join(mol_list, ''), object, state, finish=0, discrete=discrete) # populate with tuple containing ordered list of keys # and associated data dictionary anno_list = ["\\955" + object] for key in sdf_rec.kees: if (key != 'MOL'): data = sdf_rec.data[key] print key, data anno_list.append( " \\595%s: \\559%s" % (key, string.join(map(string.strip, sdf_rec.data[key])))) state_dict[state] = anno_list # increment the state index state = state + 1 if state > 1: cmd.zoom(object) cmd.finish_object(object)
def load_annotated_sdf(filename, object=None, state=1, discrete=1, _self=cmd): pymol=_self._pymol cmd=_self # get object name from file prefix if object==None: object = re.sub(r"\.[sS][dD][fF]$","",filename) # open the SD file inp_sdf = SDF(filename) # create a persistent place to store the annotations if not hasattr(pymol.session,'annotation'): pymol.session.annotation = {} # create a state-indexed dictionary for this object state_dict = {} pymol.session.annotation[object] = state_dict while 1: # get next record sdf_rec = inp_sdf.read() # if at end of list, break out of loop if not sdf_rec: break # get the MOL portion of the record mol_list = sdf_rec.get('MOL') # load it into PyMOL cmd.read_molstr(''.join(mol_list),object, state,finish=0,discrete=discrete) # populate with tuple containing ordered list of keys # and associated data dictionary anno_list = [ "\\955"+object ] for key in sdf_rec.kees: if (key!='MOL'): data = sdf_rec.data[key] print(key,data) anno_list.append(" \\595%s: \\559%s"%( key, ' '.join(map(str.strip, sdf_rec.data[key])))) state_dict[state] = anno_list # increment the state index state = state + 1 if state > 1: cmd.zoom(object) cmd.finish_object(object)