IMP logo
IMP Reference Guide  2.10.1
The Integrative Modeling Platform
create_gmm.py
1 from __future__ import print_function
2 import IMP
3 import IMP.em
4 import IMP.isd
6 import numpy as np
7 try:
8  from argparse import ArgumentParser
9 except ImportError:
10  from IMP._compat_argparse import ArgumentParser
11 import sys,os
12 def parse_args():
13  desc = """
14  Create a GMM from either density file (.mrc), a pdb file (.pdb)
15  Will detect input format from extension.
16  Outputs as text and optionally as a density map
17  see help(-h)
18 """
19  p = ArgumentParser(description=desc)
20 
21  p.add_argument("-t","--covar_type",dest="covar_type",default='full',
22  choices=['spherical', 'tied', 'diag', 'full'],
23  help="covariance type for the GMM")
24  p.add_argument("-m","--out_map",dest="out_map",default='',
25  help="write out the gmm to an mrc file")
26  p.add_argument("-a","--apix",dest="apix",default=1.0,type=float,
27  help="if you don't provide a map, set the voxel_size here (for sampling)")
28  p.add_argument("-n","--num_samples",dest="num_samples",default=1000000,type=int,
29  help="num samples to draw from the density map")
30  p.add_argument("-i","--num_iter",dest="num_iter",default=100,type=int,
31  help="num iterations of GMM")
32  p.add_argument("-s","--threshold",dest="threshold",default=0.0,type=float,
33  help="threshold for the map before sampling")
34 
35  p.add_argument("-f","--force_radii",dest="force_radii",default=-1.0,
36  type=float,
37  help="force radii to be this value (spherical) -1 means deactivated ")
38  p.add_argument("-w","--force_weight",dest="force_weight",default=-1.0,
39  type=float,
40  help="force weight to be this value (spherical) -1 means deactivated ")
41  p.add_argument("-e","--force_weight_frac",dest="force_weight_frac",action="store_true",default=False,
42  help="force weight to be 1.0/(num anchors). takes precedence over -w ")
43  p.add_argument("-o","--out_anchors_txt",dest="out_anchors_txt",default='',
44  help="write final GMM as anchor points (txt)")
45  p.add_argument("-q","--out_anchors_cmm",dest="out_anchors_cmm",default='',
46  help="write final GMM as anchor points (cmm)")
47  p.add_argument("-d","--use_dirichlet",dest="use_dirichlet",default=False,
48  action="store_true",
49  help="use dirichlet process for fit")
50 
51  p.add_argument("-k","--multiply_by_mass",dest="multiply_by_mass",default=False,
52  action="store_true",
53  help="if set, will multiply all weights by the total mass of the particles (PDB ONLY)")
54  p.add_argument("-x","--chain",dest="chain",default=None,
55  help="If you passed a PDB file, read this chain")
56 
57  p.add_argument("-z","--use_cpp",dest="use_cpp",default=False,
58  action="store_true",
59  help="EXPERIMENTAL. Uses the IMP GMM code. Requires isd_emxl")
60  p.add_argument("data_file", help="data file name")
61  p.add_argument("n_centers", type=int, help="number of centers")
62  p.add_argument("out_file", help="output file name")
63  return p.parse_args()
64 
65 def run():
66  args = parse_args()
67  data_fn = args.data_file
68  ncenters = args.n_centers
69  out_txt_fn = args.out_file
70  mdl = IMP.Model()
71 
72  if not os.path.isfile(data_fn):
73  raise Exception("The data file you entered: "+data_fn+" does not exist!")
74 
75  ### get points for fitting the GMM
76  ext = data_fn.split('.')[-1]
77  mass_multiplier = 1.0
78  if ext=='pdb':
80  if args.chain:
81  mps = IMP.atom.Selection(mh,chain=args.chain).get_selected_particles()
82  else:
83  mps = IMP.core.get_leaves(mh)
84 
85  if args.multiply_by_mass:
86  mass_multiplier=sum(IMP.atom.Mass(p).get_mass() for p in mps)
87 
88  pts = [IMP.core.XYZ(p).get_coordinates() for p in mps]
89  bbox = None
90  elif ext=='mrc':
91  dmap = IMP.em.read_map(data_fn,IMP.em.MRCReaderWriter())
92  bbox = IMP.em.get_bounding_box(dmap)
93  dmap.set_was_used(True)
94  print('sampling points')
95  pts = IMP.isd.sample_points_from_density(dmap,args.num_samples,args.threshold)
96  else:
97  print('ERROR: data_fn extension must be pdb, mrc, or npy')
98  sys.exit()
99 
100  ### Do fitting to points
101  if not args.use_cpp:
102  density_ps = []
103  print('fitting gmm')
104  #IMP.isd_emxl.gmm_tools.draw_points(pts,'test_points.bild')
105 
106  if args.force_weight_frac:
107  force_weight = 1.0/ncenters
108  else:
109  force_weight=args.force_weight
110  if force_weight != -1:
111  print('weight forced to',force_weight)
112  if not args.use_dirichlet:
113  gmm = IMP.isd.gmm_tools.fit_gmm_to_points(pts,ncenters,mdl,density_ps,
114  args.num_iter,args.covar_type,
115  force_radii=args.force_radii,
116  force_weight=args.force_weight,
117  mass_multiplier=mass_multiplier)
118  else:
119  gmm = IMP.isd.gmm_tools.fit_dirichlet_gmm_to_points(pts,ncenters,mdl,density_ps,
120  args.num_iter,args.covar_type,
121  mass_multiplier=mass_multiplier)
122 
123  else:
124  try:
125  import isd_emxl
126  except ImportError:
127  print("This option is experimental, only works if you have isd_emxl")
128  gmm_threshold = 0.01
129  density_ps = IMP.isd_emxl.fit_gaussians_to_density(mdl,dmap,args.num_samples,
130  ncenters,args.num_iter,
131  args.threshold,
132  gmm_threshold)
133 
134  ### Write to files
135  comments = ['Created by create_gmm.py, IMP.isd version %s'
137  comments.append('data_fn: ' + IMP.get_relative_path(out_txt_fn, data_fn))
138  comments.append('ncenters: %d' % ncenters)
139  for key in ('covar_type', 'apix', 'num_samples', 'num_iter',
140  'threshold', 'force_radii', 'force_weight',
141  'force_weight_frac', 'use_dirichlet', 'multiply_by_mass',
142  'chain'):
143  comments.append('%s: %s' % (key, repr(getattr(args, key))))
144  IMP.isd.gmm_tools.write_gmm_to_text(density_ps, out_txt_fn, comments)
145  if args.out_map != '':
146  IMP.isd.gmm_tools.write_gmm_to_map(density_ps, args.out_map,
147  args.apix, bbox)
148 
149  if args.out_anchors_txt!='':
150  IMP.isd.gmm_tools.write_gmm_to_anchors(density_ps,args.out_anchors_txt,
151  args.out_anchors_cmm)
152 
153 
154 
155 if __name__=="__main__":
156  run()
Select non water and non hydrogen atoms.
Definition: pdb.h:245
Tools for handling Gaussian Mixture Models.
Definition: gmm_tools.py:1
Add mass to a particle.
Definition: Mass.h:23
double get_mass(ResidueType c)
Get the mass from the residue type.
def fit_dirichlet_gmm_to_points
fit a GMM to some points.
Definition: gmm_tools.py:346
GenericHierarchies get_leaves(Hierarchy mhd)
Get all the leaves of the bit of hierarchy.
void read_pdb(TextInput input, int model, Hierarchy h)
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
def fit_gmm_to_points
fit a GMM to some points.
Definition: gmm_tools.py:231
std::string get_module_version()
std::string get_relative_path(std::string base, std::string relative)
Return a path to a file relative to another file.
Basic utilities for handling cryo-electron microscopy 3D density maps.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
def write_gmm_to_map
write density map from GMM.
Definition: gmm_tools.py:113
algebra::BoundingBoxD< 3 > get_bounding_box(const DensityMap *m)
Definition: DensityMap.h:464
def write_gmm_to_text
write a list of gaussians to text.
Definition: gmm_tools.py:62
Select hierarchy particles identified by the biological name.
Definition: Selection.h:66
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...