Mercurial > repos > bgruening > openbabel_remduplicates
comparison distance_finder.py @ 13:d44de092fef3 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
| author | bgruening |
|---|---|
| date | Mon, 19 Oct 2020 14:36:22 +0000 |
| parents | 8c4a4e9e173c |
| children | f3099132512d |
comparison
equal
deleted
inserted
replaced
| 12:8c4a4e9e173c | 13:d44de092fef3 |
|---|---|
| 9 # That would encode 3 points. | 9 # That would encode 3 points. |
| 10 # Each record in the SDF input is read and the closest heavy atom to each of the reference points is recorded as | 10 # Each record in the SDF input is read and the closest heavy atom to each of the reference points is recorded as |
| 11 # a property named distance1 where the numeric part is the index (starting from 1) of the points (in that example | 11 # a property named distance1 where the numeric part is the index (starting from 1) of the points (in that example |
| 12 # there would be properties for distance1, distance2 and distance3. | 12 # there would be properties for distance1, distance2 and distance3. |
| 13 | 13 |
| 14 import argparse, os, sys, math | 14 import argparse |
| 15 import math | |
| 16 import sys | |
| 15 | 17 |
| 16 from openbabel import pybel | 18 from openbabel import pybel |
| 17 | 19 |
| 18 | 20 |
| 19 def log(*args, **kwargs): | 21 def log(*args, **kwargs): |
| 28 :param points_file: A file with the points to consider. | 30 :param points_file: A file with the points to consider. |
| 29 :param outfile: The name of the file for the SDF output | 31 :param outfile: The name of the file for the SDF output |
| 30 :return: | 32 :return: |
| 31 """ | 33 """ |
| 32 | 34 |
| 33 | |
| 34 points = [] | 35 points = [] |
| 35 | 36 |
| 36 # read the points | 37 # read the points |
| 37 with open(points_file, 'r') as f: | 38 with open(points_file, 'r') as f: |
| 38 for line in f.readlines(): | 39 for line in f.readlines(): |
| 39 line.strip() | 40 line.strip() |
| 40 if line: | 41 if line: |
| 41 p = line.split() | 42 p = line.split() |
| 42 if len(p) == 3: | 43 if len(p) == 3: |
| 43 points.append((float(p[0]), float(p[1]), float(p[2]))) | 44 points.append((float(p[0]), float(p[1]), float(p[2]))) |
| 44 log("Read points",p) | 45 log("Read points", p) |
| 45 continue | 46 continue |
| 46 log("Failed to read line:", line) | 47 log("Failed to read line:", line) |
| 47 log('Found', len(points), 'atom points') | 48 log('Found', len(points), 'atom points') |
| 48 | 49 |
| 49 sdf_writer = pybel.Outputfile("sdf", outfile, overwrite=True) | 50 sdf_writer = pybel.Outputfile("sdf", outfile, overwrite=True) |
| 54 if count % 50000 == 0: | 55 if count % 50000 == 0: |
| 55 log('Processed', count) | 56 log('Processed', count) |
| 56 | 57 |
| 57 try: | 58 try: |
| 58 # print("Processing mol", mol.title) | 59 # print("Processing mol", mol.title) |
| 59 | |
| 60 clone = pybel.Molecule(mol) | 60 clone = pybel.Molecule(mol) |
| 61 clone.removeh() | 61 clone.removeh() |
| 62 | 62 |
| 63 coords = [] | 63 coords = [] |
| 64 for atom in clone.atoms: | 64 for atom in clone.atoms: |
| 80 mol.data['distance' + str(p)] = min_distance | 80 mol.data['distance' + str(p)] = min_distance |
| 81 | 81 |
| 82 sdf_writer.write(mol) | 82 sdf_writer.write(mol) |
| 83 | 83 |
| 84 except Exception as e: | 84 except Exception as e: |
| 85 log('Failed to handle molecule: '+ str(e)) | 85 log('Failed to handle molecule: ' + str(e)) |
| 86 continue | 86 continue |
| 87 | 87 |
| 88 sdf_writer.close() | 88 sdf_writer.close() |
| 89 log('Wrote', count, 'molecules') | 89 log('Wrote', count, 'molecules') |
| 90 | 90 |
| 91 | 91 |
| 92 def main(): | 92 def main(): |
| 93 global work_dir | 93 global work_dir |
| 94 | 94 |
| 95 parser = argparse.ArgumentParser(description='XChem distances - measure distances to particular points') | 95 parser = argparse.ArgumentParser(description='XChem distances - measure distances to particular points') |
| 96 | |
| 97 parser.add_argument('-i', '--input', help="SDF containing the 3D molecules to score)") | 96 parser.add_argument('-i', '--input', help="SDF containing the 3D molecules to score)") |
| 98 parser.add_argument('-p', '--points', help="PDB format file with atoms") | 97 parser.add_argument('-p', '--points', help="PDB format file with atoms") |
| 99 parser.add_argument('-o', '--outfile', default='output.sdf', help="File name for results") | 98 parser.add_argument('-o', '--outfile', default='output.sdf', help="File name for results") |
| 100 | |
| 101 | 99 |
| 102 args = parser.parse_args() | 100 args = parser.parse_args() |
| 103 log("XChem distances args: ", args) | 101 log("XChem distances args: ", args) |
| 104 | 102 |
| 105 execute(args.input, args.points, args.outfile) | 103 execute(args.input, args.points, args.outfile) |
