Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
load_ligand.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # :noTabs=true:
3 
4 
5 # (c) Copyright Rosetta Commons Member Institutions.
6 # (c) This file is part of the Rosetta software suite and is made available under license.
7 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 ## @file load_ligand.py
12 ## @brief
13 ## @author Evan H. Baugh, Johns Hopkins University
14 
15 # generate_nonstandard_residue_set is adapted from an original script by Sid Chaudhury
16 
17 import os
18 # its in python!
19 import openbabel
20 import molfile_to_params
21 from rosetta import Pose
22 from rosetta import pose_from_pdb
23 from rosetta import init
24 
25 ################################################################################
26 # methods for obtaining ligand chemical files and producing params .files
27 
28 # 1. get the ligand file, as .sdf
29 # retreives sdfdata from rcsb...currently inefficient
30 def load_from_pubchem( cid , sdffilename = '' ):
31  cid = str(cid)
32  try:
33  filename = urllib.urlretrieve('http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=' + cid + '&disopt=3DDisplaySDF')[0]
34  except:
35  raise IOError('Cannot access pubchem database, please check your Internet access')
36  else:
37  if os.path.getsize(filename)>0: # arbitrary...then cid was invalid
38  sdfile = open(filename)
39  sdfdata = sdfile.read()
40  sdfile.close()
41 
42  # default naming
43  if not sdffilename:
44  sdffilename = 'CID_' + cid + '.sdf'
45  sdfile = open(sdffilename,'w')
46  sdfile.write(sdfdata)
47  sdfile.close()
48 
49  print 'CID',cid,'successfully written to .sdf file',sdffilename
50 
51  #mlf = mdl_molfile.read_mdl_molfile(cid+'.sdf')
52  else:
53  raise IOError('Invalid CID code')
54  os.remove(filename) # remove tmp file
55 
56 # 2. generate conformers! UNSUPPORTED
57 #def conformers_from_sdf( sdffilename ):
58 # # returns a list of conformers generated from the input .sdf file
59 # print 'No conformer generation supported, sry we fail HARD at this'
60 # return [sdffilename]
61 
62 # 3, convert to .mol or ,mdl
63 # uses openbabel to convert sdf to mdl
64 def sdf2mdl( sdfile , mdlfilename ):
65  if os.path.exists( os.getcwd() + '/' + sdfile ):
66  converter = openbabel.OBConversion()
67  converter.SetInAndOutFormats('sdf','mdl')
68  mol = openbabel.OBMol()
69  converter.ReadFile(mol,sdfile)
70  print 'if the file',mdlfilename,'already exists, it will be overwritten'
71 # os.system("babel %s %s.pdb"%(sdfile,sdfile[:-4]))
72  converted = converter.WriteFile(mol,mdlfilename)
73  if converted:
74  print '.mdl file',mdlfilename,' successfully written'
75  else:
76  print 'Conversion Failed! could not produce the .mdl file',mdlfilename
77  else:
78  raise IOError('No such file or directory named '+sdfile)
79 
80 # 4. convert .mdl to .params
81 # quick wrapper for molfile_to_params with basic functionality
82 def molfile2params_quick( mdlfile , name ):
83  molfile_to_params.main([mdlfile,'-n'+name])
84 
85 ################################################################################
86 # Temporary solution, load the ligand for this session
87 
88 # a method for producing non-standard ResidueTypeSets
89 # there is a custom (PyRosetta only) method in PyRosetta v2.0beta named
90 # generate_nonstandard_residue_set which will work there, but not for
91 # newer versions, this method supports older and newer versions of PyRosetta
93  """
94  Returns a "custom" ResidueTypeSet with the normal ResidueTypes and any
95  new ones added as a Vector1 of .params filenames,
96  the input <params_list>
97 
98  example(s):
99  res_set = generate_nonstandard_residue_set( Vector1( ['ATP.params'] ) )
100  See Also:
101  Pose
102  Residue
103  ResidueType
104  ResidueTypeSet
105  """
106  res_set = ChemicalManager.get_instance().nonconst_residue_type_set(
107  'fa_standard' )
108  atoms = ChemicalManager.get_instance().atom_type_set( 'fa_standard' )
109  mm_atoms = ChemicalManager.get_instance().mm_atom_type_set( 'fa_standard' )
110  orbitals = ChemicalManager.get_instance().orbital_type_set( 'fa_standard' )
111  try:
112  # then this PyRosetta is a newer version, sorry, element_sets were added
113  # to the chemical database and changed the syntax of read_files
114  elements = ChemicalManager.get_instance().element_set( 'default' )
115  res_set.read_files( params_list , atoms , elements , mm_atoms , orbitals )
116  except:
117  # then this PyRosetta is v2.0 beta or earlier, as this is being written,
118  # we support v2.0 beta, notice the subtle difference below
119  res_set.read_files( params_list , atoms , mm_atoms , orbitals )
120  return res_set
121 
122 # perform the above steps in one function call
123 def params_from_pubchem( cid , name ):
124  filename = 'CID_' + str(cid)
125 
126  # load from pubchem
127  load_from_pubchem( cid )
128 
129  # generate conformers
130  #files = conformers_from_sdf( filename + '.sdf' )
131 
132  # convert to .mdl
133  sdf2mdl( filename + '.sdf' , filename + '.mdl' )
134 
135  # produce .params
136  molfile2params_quick( filename + '.mdl' . name )
137 
138 
139 ################################################################################
140 # Permanent solution, add the .params to the minirosetta_database of PyRosetta
141 
142 # this may not work if you manipulated your path variables
143 database = os.path.abspath( os.environ['PYROSETTA_DATABASE'] )
144 fa_standard = database + '/chemical/residue_type_sets/fa_standard/'
145 fa_custom = 'residue_types/custom'
146 
147 def add_cid_to_database( cid , name ):
148  # change directory to the custom database
149  start_dir = os.getcwd()
150  # if it does not exist
151  if not os.path( database + fa_standard + fa_custom ):
152  # make a "custom" directory
153  os.chdir( database + fa_standard + 'residue_types' )
154  os.mkdir( 'custom' )
155  # edit residue_type_sets.txt
156  os.chdir( database + fa_standard )
157  f = open( 'residue_type_sets.txt' , 'w' )
158  data = f.readlines()
159  data.append( '\n## Custom\n' )
160  f.readlines( data )
161  f.close()
162  os.chdir( database + fa_standard + fa_custom )
163 
164  # get the ligand
165  params_from_pubchem( cid , name )
166 
167  # add the ligand to residue_type_sets.txt
168  os.chdir( database + fa_standard )
169  f = open( 'residue_type_sets.txt' , 'w' )
170  data = f.readlines()
171  data.append( fa_custom + '/' + name + '.params\n' )
172  f.readlines( data )
173  f.close()
174 
175  # return to original dir
176  os.chdir( start )
177 
178  # reinitialize
179  init()
180 
181 ################################################################################
182 # returns a pose of the molecule
183 def pose_from_pubchem( cid , name , temporary = True ):
184  pose = Pose()
185  if temporary:
186  # the temporary solution, create an ephemeral ResidueSet
187  params_from_pubchem( cid , name )
188 
189  # generate ResidueSet
190  res_set = generate_nonstandard_residue_set( [name] )
191 
192  # fill the pose
193  pose_from_pdb( pose , res_set , name + '_0001.pdb')
194  else:
195  # permanent solution, add to .params list
196  add_cid_to_database( cid , name )
197 
198  # fill the pose
199  pose_from_pdb( pose , name + '_0001.pdb' )
200  return pose
201 
202 # returns a pose containing a ligand
203 def pose_from_params( filename , params_list ):
204  res_set = generate_nonstandard_residue_set( params_list )
205  pose = Pose()
206  pose_from_pdb( pose , res_set , filename )
207  return pose
208 
def generate_nonstandard_residue_set
Temporary solution, load the ligand for this session.
Definition: load_ligand.py:92
def load_from_pubchem
methods for obtaining ligand chemical files and producing params .files
Definition: load_ligand.py:30
core::pose::Pose Pose
Definition: supercharge.cc:101
def pose_from_pubchem
returns a pose of the molecule
Definition: load_ligand.py:183
bool open(utility::io::izstream &db_stream, std::string const &db_file, bool warn)
Open a database file on a provided stream.
Definition: open.cc:55
void init()
set global 'init_was_called' to true
Definition: init.cc:26