Mercurial > repos > bgruening > ctb_rdkit_descriptors
comparison sdf_to_tab.py @ 9:87e62bbb4901 draft default tip
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/rdkit commit c1d813d3f0fec60ea6efe8a11e59d98bfdc1636f"
| author | bgruening |
|---|---|
| date | Sat, 04 Dec 2021 16:37:32 +0000 |
| parents | 2d051db1f561 |
| children |
comparison
equal
deleted
inserted
replaced
| 8:2d051db1f561 | 9:87e62bbb4901 |
|---|---|
| 11 | 11 |
| 12 for n in range(len(mols)): | 12 for n in range(len(mols)): |
| 13 if mols[n]: | 13 if mols[n]: |
| 14 d = mols[n].GetPropsAsDict() | 14 d = mols[n].GetPropsAsDict() |
| 15 # filter dict for desired props | 15 # filter dict for desired props |
| 16 if vars.props.strip() == '': # none specified, return all | 16 if vars.props.strip() == "": # none specified, return all |
| 17 d = {prop: val for (prop, val) in d.items() if not any(x in str(val) for x in ['\n', '\t'])} # remove items containing newlines or tabs | 17 d = { |
| 18 prop: val | |
| 19 for (prop, val) in d.items() | |
| 20 if not any(x in str(val) for x in ["\n", "\t"]) | |
| 21 } # remove items containing newlines or tabs | |
| 18 else: | 22 else: |
| 19 d = {prop: val for (prop, val) in d.items() if prop in vars.props.replace(' ', '').split(',')} # remove items not requested via CLI | 23 d = { |
| 24 prop: val | |
| 25 for (prop, val) in d.items() | |
| 26 if prop in vars.props.replace(" ", "").split(",") | |
| 27 } # remove items not requested via CLI | |
| 20 if vars.name: | 28 if vars.name: |
| 21 d['SDFMoleculeName'] = mols[n].GetProp('_Name') | 29 d["SDFMoleculeName"] = mols[n].GetProp("_Name") |
| 22 if vars.smiles: | 30 if vars.smiles: |
| 23 d['SMILES'] = Chem.MolToSmiles(mols[n], isomericSmiles=False) | 31 d["SMILES"] = Chem.MolToSmiles(mols[n], isomericSmiles=False) |
| 24 d['Index'] = int(n) | 32 d["Index"] = int(n) |
| 25 | 33 |
| 26 df = df.append(d, ignore_index=True) | 34 df = df.append(d, ignore_index=True) |
| 27 else: | 35 else: |
| 28 print("Molecule could not be read - skipped.") | 36 print("Molecule could not be read - skipped.") |
| 29 | 37 |
| 30 df = df.astype({'Index': int}).set_index('Index') | 38 df = df.astype({"Index": int}).set_index("Index") |
| 31 sorted_cols = sorted(df.columns.values.tolist()) | 39 sorted_cols = sorted(df.columns.values.tolist()) |
| 32 df.to_csv(vars.out, sep='\t', header=vars.header, columns=sorted_cols) | 40 df.to_csv(vars.out, sep="\t", header=vars.header, columns=sorted_cols) |
| 33 | 41 |
| 34 | 42 |
| 35 def main(): | 43 def main(): |
| 36 parser = argparse.ArgumentParser(description="Convert SDF to tabular") | 44 parser = argparse.ArgumentParser(description="Convert SDF to tabular") |
| 37 parser.add_argument('--inp', '-i', help="The input file", required=True) | 45 parser.add_argument("--inp", "-i", help="The input file", required=True) |
| 38 parser.add_argument('--out', '-o', help="The output file", required=True) | 46 parser.add_argument("--out", "-o", help="The output file", required=True) |
| 39 parser.add_argument('--props', '-p', help="Properties to filter (leave blank for all)", required=True) | 47 parser.add_argument( |
| 40 parser.add_argument('--header', '-t', action='store_true', | 48 "--props", |
| 41 help="Write property name as the first row.") | 49 "-p", |
| 42 parser.add_argument('--smiles', '-s', action='store_true', | 50 help="Properties to filter (leave blank for all)", |
| 43 help="Include SMILES in output.") | 51 required=True, |
| 44 parser.add_argument('--name', '-n', action='store_true', | 52 ) |
| 45 help="Include molecule name in output.") | 53 parser.add_argument( |
| 54 "--header", | |
| 55 "-t", | |
| 56 action="store_true", | |
| 57 help="Write property name as the first row.", | |
| 58 ) | |
| 59 parser.add_argument( | |
| 60 "--smiles", "-s", action="store_true", help="Include SMILES in output." | |
| 61 ) | |
| 62 parser.add_argument( | |
| 63 "--name", "-n", action="store_true", help="Include molecule name in output." | |
| 64 ) | |
| 46 sdf_to_tab(parser.parse_args()) | 65 sdf_to_tab(parser.parse_args()) |
| 47 | 66 |
| 48 | 67 |
| 49 if __name__ == "__main__": | 68 if __name__ == "__main__": |
| 50 main() | 69 main() |
