def write(self, fmt, filename, csv_filename=None, **kwargs): """Outputs molecules to a file Parameters ---------- file_type: string Type of molecular file ligands_file: string Path to a output file csv_filename: string Optional path to a CSV file """ if fmt == 'mol2' and toolkit.backend == 'ob': if 'opt' in kwargs: kwargs['opt']['c'] = None else: kwargs['opt'] = {'c': None} output_mol_file = toolkit.Outputfile(fmt, filename, **kwargs) if csv_filename: f = open(csv_filename, 'w') csv_file = None for mol in self.fetch(): if csv_filename: data = mol.data.to_dict() # filter some internal data blacklist_keys = ['OpenBabel Symmetry Classes', 'MOL Chiral Flag', 'PartialCharges', 'TORSDO', 'REMARK'] for b in blacklist_keys: if b in data: del data[b] if len(data) > 0: data['name'] = mol.title else: print("There is no data to write in CSV file", file=sys.stderr) return False if csv_file is None: csv_file = csv.DictWriter(f, data.keys(), **kwargs) csv_file.writeheader() csv_file.writerow(data) # write ligand output_mol_file.write(mol) output_mol_file.close() if csv_filename: f.close() # if 'keep_pipe' in kwargs and kwargs['keep_pipe']: if isfile(filename): kwargs.pop('overwrite') # this argument is unsupported in readfile self._pipe = toolkit.readfile(fmt, filename, **kwargs)
def write(self, fmt, filename, csv_filename=None, **kwargs): """Outputs molecules to a file Parameters ---------- file_type: string Type of molecular file ligands_file: string Path to a output file csv_filename: string Optional path to a CSV file """ output_mol_file = toolkit.Outputfile(fmt, filename, **kwargs) if csv_filename: f = open(csv_filename, 'w') csv_file = None for mol in self.fetch(): if csv_filename: data = dict(mol.data) #filter some internal data blacklist_keys = [ 'OpenBabel Symmetry Classes', 'MOL Chiral Flag', 'PartialCharges', 'TORSDO', 'REMARK' ] for b in blacklist_keys: if data.has_key(b): del data[b] if len(data) > 0: data['name'] = mol.title else: print "There is no data to write in CSV file" return False if csv_file is None: csv_file = csv.DictWriter(f, data.keys(), **kwargs) csv_file.writeheader() csv_file.writerow(data) # write ligand output_mol_file.write(mol) output_mol_file.close() if csv_filename: f.close() # if kwargs.has_key('keep_pipe') and kwargs['keep_pipe']: #FIXME destroys data self._pipe = toolkit.readfile(fmt, filename)