Best Python code snippet using lemoncheesecake
AmberLoader.py
Source:AmberLoader.py
1#!/bin/env python2# -*- coding: utf-8 -*-3import os4import sys5import re6from Sire.IO import *7from Sire.Mol import *8from Sire.CAS import *9from Sire.System import *10from Sire.Move import *11from Sire.MM import *12from Sire.FF import *13from Sire.Units import *14from Sire.Vol import *15from Sire.Maths import *16from Sire.Base import *17from Sire.Qt import *18from Sire.ID import *19from Sire.Config import *20import Sire.Stream21from Sire.Tools import Parameter, resolveParameters22from Sire.Tools.WaterChanger import convertTip3PtoTip4P23###################################24# Parameters used by this module #25###################################26dobonds = Parameter("move bonds", True, """Whether or not to move the ligands bonds""")27doangles = Parameter("move angles", True, """Whether or not to move the ligands angles""")28dodihedrals = Parameter("move dihedrals", True, """Whether or not to move the ligands dihedrals""")29water_model = Parameter("water model", None,30 """The water model to use. Note, by default the water model is read from31 the protein and water crd/top files. If you want to force a change32 in water model, then set it here, e.g. if you are loading a TIP3P box33 but want to use TIP4P, then set this parameter to "tip4p".""")34BASE_DIHEDRALH_FLEX = Parameter("h dihedral flex", 30*degrees, "Base dihedral rotation for H")35BASE_DIHEDRAL_FLEX = Parameter("dihedral flex", 20*degrees, "Base dihedral rotation")36BASE_ANGLE_FLEX = Parameter("angle flex", 0.25*degrees, "Base angle rotation")37BASE_BOND_FLEX = Parameter("bond flex", 0.025*angstroms, "Base bond stretch amount")38BASE_TRANSLATION = Parameter("translation", 0.75*angstroms, "Base translation delta amount")39BASE_ROTATION = Parameter("rotation", 30*degrees, "Base rigid body rotation")40BASE_MAXVAR = Parameter("maxvar", 10, "Maximum number of degrees of freedom to move at once")41BASE_MAXVAR_B = Parameter("maxvar bonds", 2, "Maximum number of bonds to move at once")42BASE_MAXVAR_A = Parameter("maxvar angles", 4, "Maximum number of angles to move at once")43BASE_MAXVAR_D = Parameter("maxvar dihedrals", 4, "Maximum number of dihedrals to move at once")44###################################45def getResidueNames(molecule):46 nres = molecule.nResidues()47 resnams = []48 for i in range(0, nres):49 resnams.append( str( molecule.residue(ResIdx(i)).name().value()).upper() )50 return resnams51class NamingScheme:52 def __init__(self):53 self._protein_names = ["GLH", "ILE", "GLN", "GLY", "GLU",54 "CYS", "HIS", "HID", "SER", "LYS",55 "LYN", "PRO", "CYX", "HIE", "ASH",56 "ASN", "HIP", "VAL", "THR", "ASP",57 "TRP", "PHE", "ALA", "MET", "LEU",58 "ARG", "TYR", "NME", "ACE"]59 self._water_names = [ "WAT", "T3P", "T4P", "HOH" ]60 self._ion_names = [ "NA+", "Na+", "CA+", "Ca+", "CAL", "CL-", "Cl-" ]61 self._solute_names = [ "LIG" ]62 def proteinsGroupName(self):63 return MGName("protein")64 def solutesGroupName(self):65 return MGName("solute")66 def solventsGroupName(self):67 return MGName("solvent")68 def watersGroupName(self):69 return MGName("water")70 def ionsGroupName(self):71 return MGName("ions")72 def allMoleculesGroupName(self):73 return MGName("all")74 def fixedMoleculesGroupName(self):75 return MGName("fixed_molecules")76 def boundaryMoleculesGroupName(self):77 return MGName("boundary_molecules")78 def mobileProteinSidechainsGroupName(self):79 return MGName("protein_sidechains")80 def mobileProteinBackbonesGroupName(self):81 return MGName("protein_backbones")82 def mobileSolutesGroupName(self):83 return MGName("mobile_solutes")84 def mobileSolventsGroupName(self):85 return MGName("mobile_solvents")86 def addProteinResidueName(self, name):87 self._protein_names.append( name.upper() )88 def addWaterResidueName(self, name):89 self._water_names.append( name.upper() )90 def addSoluteResidueName(self, name):91 self._solute_names.append( name.upper() )92 def addIonResidueName(self, name):93 self._ion_names.append( name.upper() )94 def proteinResidueNames(self):95 return self._protein_names96 def waterResidueNames(self):97 return self._water_names98 def soluteResidueNames(self):99 return self._solute_names100 def ionResidueNames(self):101 return self._ion_names102 def setProteinResidueNames(self, names):103 self._protein_names = []104 for name in names:105 self.addProteinResidueName(name)106 def setWaterResidueNames(self, names):107 self._water_names = []108 for name in names:109 self.addWaterResidueName(name)110 def setSoluteResidueNames(self, name):111 self._solute_names = []112 for name in names:113 self.addSoluteResidueName(name)114 def setIonResidueNames(self, name):115 self._ion_names = []116 for name in names:117 self.addIonResidueName(name)118 def _isType(self, molecule, names, max_residues = None):119 try:120 resnams = getResidueNames(molecule)121 except:122 resnams = molecule123 if max_residues:124 if len(resnams) > max_residues:125 return False126 for resnam in resnams:127 if resnam in names:128 return True129 try:130 if str(molecule.name().value()).upper() in names:131 return True132 else:133 return False134 except:135 return False136 def isProtein(self, molecule):137 return self._isType(molecule, self._protein_names)138 def isWater(self, molecule):139 return self._isType(molecule, self._water_names, 1)140 def isIon(self, molecule):141 return self._isType(molecule, self._ion_names, 1)142 def isSolute(self, molecule):143 return self._isType(molecule, self._solute_names)144def findMolecule(system, molname):145 molecules = system.molecules()146 molname = molname.upper()147 for molnum in molecules.molNums():148 molecule = molecules[molnum][0].molecule()149 if str(molecule.name().value()).upper() == molname:150 return molecule151 resnams = getResidueNames(molecule)152 for resnam in resnams:153 if resnam == molname:154 return molecule155 return None156def addMoleculeToSystem(molecule, system, naming_scheme = NamingScheme()):157 """This function adds the passed molecule to the passed system158 using the passed naming scheme to assign the molecule to the 159 correct molecule group"""160 resnams = getResidueNames(molecule)161 system.add(molecule, MGName(naming_scheme.allMoleculesGroupName().value()))162 if naming_scheme.isSolute(resnams):163 system.add(molecule, MGName(naming_scheme.solutesGroupName().value()))164 elif naming_scheme.isProtein(resnams):165 system.add(molecule, MGName(naming_scheme.proteinsGroupName().value()))166 elif naming_scheme.isWater(resnams):167 system.add(molecule, MGName(naming_scheme.watersGroupName().value()))168 system.add(molecule, MGName(naming_scheme.solventsGroupName().value())) 169 elif naming_scheme.isIon(resnams):170 system.add(molecule, MGName(naming_scheme.ionsGroupName().value()))171 system.add(molecule, MGName(naming_scheme.solventsGroupName().value()))172 elif molecule.nResidues() == 1:173 system.add(molecule, MGName(naming_scheme.solventsGroupName().value()))174 else:175 system.add(molecule, MGName(naming_scheme.solutesGroupName().value()))176def createSystemFrom(molecules, space, system_name, naming_scheme = NamingScheme()):177 """Create a new System from the passed molecules and space,178 sorting the molecules into different molecule groups based on the179 passed naming scheme"""180 system = System(system_name)181 # If requested, change the water model for all water molecules182 if water_model.val == "tip4p":183 molnums = molecules.molNums()184 new_molecules = Molecules()185 print("Forcing all water molecules to use the %s water model..." % water_model.val)186 print("Converting %d molecules..." % len(molnums))187 i = 0188 for molnum in molnums:189 molecule = molecules[molnum].molecule()190 if i % 100 == 0:191 print("%d" % i) 192 sys.stdout.flush()193 elif i % 10 == 0:194 print(".", end=' ')195 sys.stdout.flush()196 i += 1197 if molecule.nAtoms() == 3:198 #Â this could be a TIP3P water199 resname =str(molecule.residue().name().value()).lower()200 if resname == "wat" or resname == "t3p":201 new_molecule = convertTip3PtoTip4P(molecule)202 if new_molecule:203 molecule = new_molecule204 new_molecules.add(molecule)205 print("%d" % i)206 molecules = new_molecules207 nmols = molecules.nMolecules()208 print("Number of molecules == %s" % nmols)209 print("System space == %s" % space)210 if nmols == 0:211 return system212 print("Assigning molecules to molecule groups...")213 solute_group = MoleculeGroup(naming_scheme.solutesGroupName().value())214 protein_group = MoleculeGroup(naming_scheme.proteinsGroupName().value())215 solvent_group = MoleculeGroup(naming_scheme.solventsGroupName().value())216 water_group = MoleculeGroup(naming_scheme.watersGroupName().value())217 ion_group = MoleculeGroup(naming_scheme.ionsGroupName().value())218 all_group = MoleculeGroup(naming_scheme.allMoleculesGroupName().value())219 # The all molecules group has all of the molecules220 all_group.add(molecules)221 system.add(all_group)222 # Run through each molecule and decide what type it is...223 molnums = molecules.molNums()224 molnums.sort()225 central_molecule = None226 solutes = []227 proteins = []228 solvents = []229 waters = []230 ions = []231 for molnum in molnums:232 molecule = molecules[molnum].molecule()233 resnams = getResidueNames(molecule)234 if naming_scheme.isSolute(resnams):235 solutes.append(molecule)236 elif naming_scheme.isProtein(resnams):237 proteins.append(molecule)238 elif naming_scheme.isWater(resnams):239 waters.append(molecule)240 elif naming_scheme.isIon(resnams):241 ions.append(molecule)242 elif molecule.nResidues() == 1:243 solvents.append(molecule)244 else:245 solutes.append(molecule)246 # Ok - we have now divided everything up into groups247 for solute in solutes:248 solute_group.add(solute)249 for protein in proteins:250 protein_group.add(protein)251 for water in waters:252 solvent_group.add(water)253 water_group.add(water)254 for solvent in solvents:255 solvent_group.add(solvent)256 257 for ion in ions:258 solvent_group.add(ion)259 ion_group.add(ion)260 if solute_group.nMolecules() > 0:261 system.add(solute_group)262 if protein_group.nMolecules() > 0:263 system.add(protein_group)264 if solvent_group.nMolecules() > 0:265 system.add(solvent_group)266 if water_group.nMolecules() > 0:267 system.add(water_group)268 if ion_group.nMolecules() > 0:269 system.add(ion_group) 270 print("Number of solute molecules == %s" % solute_group.nMolecules()) 271 print("Number of protein molecules == %s" % protein_group.nMolecules())272 print("Number of ions == %s" % ion_group.nMolecules())273 print("Number of water molecules == %s" % water_group.nMolecules())274 print("Number of solvent molecules == %s" % solvent_group.nMolecules())275 print("(solvent group is waters + ions + unidentified single-residue molecules)")276 system.setProperty("space", space)277 system.add( SpaceWrapper( Vector(0), all_group ) )278 system.applyConstraints()279 print("Returning the constructed system")280 return system281def createSystem(top_file, crd_file, naming_scheme = NamingScheme()):282 """Create a new System from the molecules read in from the passed amber 283 topology and coordinate files. This sorts the molecules into different284 molecule groups based on the passed naming scheme"""285 286 system = MoleculeParser.read(top_file,crd_file)287 # Load all of the molecules and their parameters from288 # the topology and coordinate files289 print("Loading the molecules from the files \"%s\" and \"%s\"..." % \290 (crd_file, top_file))291 return createSystemFrom(system[MGIdx(0)], system.property("space"), top_file, naming_scheme)292def centerSystem(system, molecule):293 print("Setting the origin of the system to the center of molecule %s (%s)..." % (molecule, molecule.number()))294 center = molecule.evaluate().centerOfMass()295 print("This requires translating everything by %s..." % (-center)) 296 297 moved_mols = Molecules()298 for molnum in system.molNums():299 molecule = system[molnum][0].molecule()300 molecule = molecule.move().translate(-center).commit()301 moved_mols.add(molecule)302 system.update(moved_mols)303 return system304def guessTranslation( solute ):305 natoms = solute.nAtoms()306 return (BASE_TRANSLATION.val) / ( natoms / 5 + 1)307def guessRotation( solute ):308 natoms = solute.nAtoms()309 sphere_radius = solute.evaluate().boundingSphere().radius()310 return (BASE_ROTATION.val) / ( sphere_radius ** 2)311def generateFlexibility(solute):312 connectivity = solute.property('connectivity')313 all_bonds = connectivity.getBonds()314 all_angles = connectivity.getAngles()315 all_dihedrals = connectivity.getDihedrals()316 flexibility = Flexibility(solute)317 flexibility.setRotation( guessRotation(solute) )318 flexibility.setTranslation( guessTranslation(solute) )319 try:320 flexibility.setMaximumVar( BASE_MAXVAR.val )321 except:322 flexibility.setMaximumBondVar( BASE_MAXVAR_B.val )323 flexibility.setMaximumAngleVar( BASE_MAXVAR_A.val )324 flexibility.setMaximumDihedralVar( BASE_MAXVAR_D.val )325 # Redundant torsions are discarded according to the following algorithm326 # 1) Do not sample a torsion at0-at1-at2-at3 if a variable torsion has 327 # already been defined around at1-at2 or at2-at1.328 # 2) Do not sample a torsion if it would break a ring329 #330 if dodihedrals.val:331 var_dihedrals = []332 for dihedral in all_dihedrals:333 #print dihedral334 tomove = True335 # print dihedral336 at0 = dihedral.atom0()337 at1 = dihedral.atom1()338 at2 = dihedral.atom2()339 at3 = dihedral.atom3()340 # See if a one of the variable dihedral 341 # already rotates around the same torsion342 for vardih in var_dihedrals:343 if ( ( at1 == vardih.atom1() and at2 == vardih.atom2() ) or 344 ( at2 == vardih.atom1() and at1 == vardih.atom2() ) ):345 # Yes so will not move this torsion 346 tomove = False347 break348 # If still wondering...See if a rotation around this dihedral would break a ring 349 if tomove:350 try:351 dihbond = BondID(at1, at2)352 #print dihbond353 solute.move().change(dihbond,1*degrees)354 except UserWarning as error:355 # extract the type of the errror356 error_type = re.search(r"(Sire\w*::\w*)", str(error)).group(0)357 if error_type == "SireMol::ring_error":358 # print "This dof would move a ring and is therefore skipped"359 tomove = False360 else:361 # re-throw the exception362 raise error 363 if tomove:364 # Find out how many atoms would move 365 #print dihedral366 gr0, gr1 = connectivity.split(at1, at2)367 ngr0 = gr0.nSelected()368 ngr1 = gr1.nSelected()369 if (ngr0 <= ngr1):370 smallgroup = gr0371 else:372 smallgroup = gr1373 smallgroup = smallgroup.subtract(at1)374 smallgroup = smallgroup.subtract(at2)375 factor = smallgroup.nSelected()376 flexibility.add(dihedral, BASE_DIHEDRAL_FLEX.val/factor)377 var_dihedrals.append(dihedral)378 # And the angles ....379 if doangles.val:380 moved_atoms = []381 for angle in all_angles:382 # print angle383 at0 = angle.atom0()384 at2 = angle.atom2()385 # Do not sample that dof if an existing dof would already move this atom386 if ( ( at0 in moved_atoms) and (at2 in moved_atoms) ):387 continue388 # Test if the angle breaks a ring, if so do not sample it389 try:390 solute.move().change(angle,1*degrees)391 except UserWarning as error:392 # extract the type of the errror393 error_type = re.search(r"(Sire\w*::\w*)", str(error)).group(0)394 if error_type == "SireMol::ring_error":395 # print "This dof would move a ring and is therefore skipped"396 continue397 else:398 # re-throw the exception399 raise error 400 gr0, gr1 = connectivity.split(at0, angle.atom1(), at2)401 ngr0 = gr0.nSelected()402 ngr1 = gr1.nSelected()403 if (ngr0 <= ngr1):404 smallgroup = gr0405 else:406 smallgroup = gr1407 factor = smallgroup.nSelected()408 flexibility.add(angle, BASE_ANGLE_FLEX.val/factor)409 if at0 not in moved_atoms:410 moved_atoms.append(at0)411 if at2 not in moved_atoms:412 moved_atoms.append(at2) 413 # And the bonds...414 if dobonds.val:415 for bond in all_bonds:416 try:417 solute.move().change(bond,1*angstrom)418 except UserWarning as error:419 # extract the type of the errror420 error_type = re.search(r"(Sire\w*::\w*)", str(error)).group(0)421 if error_type == "SireMol::ring_error":422 # print "This dof would move a ring and is therefore skipped"423 continue424 else:425 # re-throw the exception426 raise error 427 gr0, gr1 = connectivity.split(bond.atom0(), bond.atom1() )428 ngr0 = gr0.nSelected()429 ngr1 = gr1.nSelected()430 if (ngr0 <= ngr1):431 smallgroup = gr0432 else:433 smallgroup = gr1434 factor = smallgroup.nSelected()435 flexibility.add(bond, BASE_BOND_FLEX.val/factor)436 return flexibility437def getCoordGroup(atoms, coords_property="coordinates"):438 coords = []439 for i in range(0, atoms.count()):440 atom = atoms[i]441 coords.append(atom.property(coords_property))442 return CoordGroup(coords)443def getAtomNearCOG( molecule ):444 445 mol_centre = molecule.evaluate().center()446 mindist = 99999.0447 448 for x in range(0, molecule.nAtoms()):449 atom = molecule.atoms()[x] 450 at_coords = atom.property('coordinates')451 dist = Vector().distance2(at_coords, mol_centre)452 if dist < mindist:453 mindist = dist454 nearest_atom = atom455 456 return nearest_atom457def addFlexibility(system, reflection_center=None, reflection_radius=None, \458 naming_scheme=NamingScheme()):459 460 print("Adding flexibility to the system...")461 # create a group for all of the fixed molecules and residues462 fixed_group = MoleculeGroup( naming_scheme.fixedMoleculesGroupName().value() )463 # create a group for the fixed residues that are bonded to the mobile residues464 boundary_group = MoleculeGroup( naming_scheme.boundaryMoleculesGroupName().value() )465 if reflection_center is None or reflection_radius is None:466 print ("No reflection radius or reflection molecule specified, so moving all "467 "molecules and residues in the system.")468 reflection_radius = None469 reflection_center = None470 else:471 print(("Only moving molecules/residues that are within a distance %s A "472 "of the point %s.") % (reflection_radius.value(), reflection_center))473 system.setProperty("reflection center", AtomCoords(CoordGroup(1,reflection_center)))474 system.setProperty("reflection sphere radius", VariantProperty(reflection_radius.to(angstroms)))475 # fit the protein z-matrix templates to all of the protein molecules and add the mobile476 #Â residues to the mobile_sc_group and mobile_bb_group for mobile sidechains and backbones477 if naming_scheme.proteinsGroupName() in system.mgNames():478 protein_group = system[naming_scheme.proteinsGroupName()]479 # create a zmatrix maker that will be used to build the z-matrices for each protein molecule480 zmat_maker = ZmatrixMaker()481 zmat_maker.loadTemplates( os.path.join(parameter_directory, "amber.zmatrices") )482 # now create the molecule groups that hold the flexible side chains and flexible backbone groups483 mobile_sc_group = MoleculeGroup(naming_scheme.mobileProteinSidechainsGroupName().value())484 mobile_bb_group = MoleculeGroup(naming_scheme.mobileProteinBackbonesGroupName().value())485 # the extra atoms moved as part of a backbone move486 hn_atoms = AtomName("N", CaseInsensitive) * AtomName("H", CaseInsensitive) * \487 AtomName("HN", CaseInsensitive) * AtomName("HN1", CaseInsensitive) * \488 AtomName("HN2", CaseInsensitive) * AtomName("HN3", CaseInsensitive)489 # loop over each protein molecule490 for molnum in protein_group.molNums():491 protein_mol = protein_group[molnum].molecule()492 print("Applying residue templates for protein %s" % molnum)493 protein_mol = zmat_maker.applyTemplates(protein_mol)494 system.update(protein_mol)495 if reflection_radius:496 space = Cartesian()497 mobile_resnums = []498 # only move side chains within "sc_radius" and backbones within "bb_radius" of the ligand molecule499 print("Looking for which residues are within the reflection sphere...")500 for i in range(0, protein_mol.nResidues()):501 res = protein_mol.residue( ResIdx(i) )502 distance = space.minimumDistance(CoordGroup(1,reflection_center), getCoordGroup(res.atoms()))503 504 if distance < reflection_radius.value():505 # add the residue to the mobile sidechains group506 mobile_sc_group.add(res)507 mobile_resnums.append( res.number() )508 # now add the atoms needed from the residue to the mobile backbones group509 atoms = protein_mol.select(ResIdx(i)).selection()510 511 # for the backbone move to work, the residue must contain512 #Â AtomName("CA", CaseInsensitive) and AtomName("N", CaseInsensitive) )513 has_backbone = False514 try:515 if atoms.selected( AtomName("CA", CaseInsensitive) ) and \516 atoms.selected( AtomName("N", CaseInsensitive) ):517 has_backbone = True518 except:519 pass520 if has_backbone:521 if i < (protein_mol.nResidues()-1):522 try:523 atoms.deselect( hn_atoms + ResIdx(i) )524 except:525 pass526 if i > 0:527 try:528 atoms.select( hn_atoms + ResIdx(i+1) )529 except:530 pass531 mobile_bb_group.add( PartialMolecule(protein_mol, atoms) )532 else:533 print("Not moving backbone of %s as it doesn't contain atoms N or CA" % protein_mol.residue(ResIdx(i)))534 # now loop over all of the residues and work out which ones are fixed, and which ones535 # are bonded to fixed residues536 connectivity = protein_mol.property("connectivity")537 for i in range(0, protein_mol.nResidues()):538 res = protein_mol.residue( ResIdx(i) )539 if not res.number() in mobile_resnums:540 # is this residue bonded to any of the mobile residues? If so, then it is a boundary residue541 is_boundary = False542 for bonded_res in connectivity.connectionsTo( res.number() ):543 bonded_resnum = protein_mol.residue(bonded_res).number()544 if bonded_resnum in mobile_resnums:545 is_boundary = True546 break547 if is_boundary:548 boundary_group.add(res)549 else:550 fixed_group.add(res)551 else:552 # assume that the backbone and side chains of all residues are flexible553 for i in range(0, protein_mol.nResidues()):554 res = protein_mol.residue( ResIdx(i) )555 mobile_sc_group.add(res)556 atoms = protein_mol.select(ResIdx(i)).selection()557 558 if i < (protein_mol.nResidues()-1):559 try:560 atoms.deselect( hn_atoms + ResIdx(i) )561 except:562 pass563 if i > 0:564 try:565 atoms.select( hn_atoms + ResIdx(i+1) )566 except:567 pass568 mobile_bb_group.add( PartialMolecule(protein_mol, atoms) )569 if mobile_sc_group.nMolecules() > 0:570 system.add(mobile_sc_group)571 if mobile_bb_group.nMolecules() > 0:572 system.add(mobile_bb_group)573 print("The number of residues with flexible sidechains equals %s" % mobile_sc_group.nViews())574 print("The number of residues with flexible backbones equals %s" % mobile_bb_group.nViews())575 print("The number of boundary residues equals %s" % boundary_group.nViews())576 print("The number of fixed residues equals %s" % fixed_group.nViews())577 # add all of the mobile solute molecules to the mobile_solute_group and auto-generate578 #Â the z-matricies of all of the mobile solutes579 if naming_scheme.solutesGroupName() in system.mgNames():580 solute_group = system[naming_scheme.solutesGroupName()]581 mobile_solute_group = MoleculeGroup( naming_scheme.mobileSolutesGroupName().value() )582 # store the average solute translation and rotation deltas583 avg_trans_delta = 0584 avg_rot_delta = 0585 for molnum in solute_group.molNums():586 solute_mol = solute_group[molnum].molecule()587 move_solute = True588 # Only move the solute if it is within the sphere cutoff of the ligand (if a ligand and solvent 589 # radius have been specified...)590 if reflection_radius:591 move_solute = (Vector.distance(reflection_center, \592 solute_mol.evaluate().centerOfMass()) < reflection_radius.value())593 if move_solute:594 print("\nAuto-detecting the flexible degrees of freedom for solute %s" % molnum)595 # auto-generate the flexibility - bonds, angles and dihedrals596 flexibility = generateFlexibility(solute_mol)597 solute_mol = solute_mol.edit().setProperty("flexibility", flexibility).commit()598 print("\nFlexibility of solute %s equals:" % molnum)599 flex = solute_mol.property("flexibility")600 print(flex)601 avg_trans_delta += flex.translation().to(angstrom)602 avg_rot_delta += flex.rotation().to(degrees)603 system.update(solute_mol)604 mobile_solute_group.add(solute_mol)605 else:606 print("Not moving solute %s as it is outside the spherical solvent cutoff of the ligand." % solute_mol)607 fixed_group.add(solute_mol)608 if mobile_solute_group.nMolecules() > 0:609 system.add(mobile_solute_group)610 system.setProperty("average solute translation delta", \611 VariantProperty(avg_trans_delta / mobile_solute_group.nMolecules()))612 system.setProperty("average solute rotation delta", \613 VariantProperty(avg_rot_delta / mobile_solute_group.nMolecules()))614 print("\nNumber of mobile solute molecules equals %s" % mobile_solute_group.nMolecules())615 # add all of the mobile solvent molecules to the mobile_solvent_group616 if naming_scheme.solventsGroupName() in system.mgNames():617 solvent_group = system[ naming_scheme.solventsGroupName() ] 618 mobile_solvent_group = MoleculeGroup( naming_scheme.mobileSolventsGroupName().value() )619 print("Adding flexibility to the solvent...")620 if reflection_radius:621 for molnum in solvent_group.molNums():622 solvent_mol = solvent_group[molnum]623 if Vector.distance(reflection_center, solvent_mol.evaluate().centerOfMass()) < reflection_radius.value():624 mobile_solvent_group.add(solvent_mol)625 else:626 fixed_group.add(solvent_mol)627 else:628 mobile_solvent_group.add( solvent_group.molecules() )629 if mobile_solvent_group.nMolecules() > 0:630 system.add(mobile_solvent_group)631 print("\nNumber of mobile solvent molecules equals %s" % mobile_solvent_group.nMolecules())632 # All finished - just need to add in the fixed and boundary groups633 if fixed_group.nMolecules() > 0:634 system.add(fixed_group)635 if boundary_group.nMolecules() > 0:636 system.add(boundary_group) 637 print("\nNumber of fixed (or partially fixed) molecules equals %s" % fixed_group.nMolecules())638 return system639def printGroupInfo(system, group_name):640 try:641 group = system[MGName(group_name)]642 print("%s : nMolecules() == %d" % (str(group), group.nMolecules()))643 except:...
renaming.py
Source:renaming.py
1import os, logging, shutil2from .util import zero_prefix_int as padnum3from .util import replace_bad_chars4from .util import normpath5from .util import ensure_utf86from .util import safe_make_dirs7from .util import samefile8from .util import prune_dirs9from .util import make_symlink10from .util import safe_rename11from .dbguy import TVDatabase12from .texceptions import FileExistsError, InvalidDirectoryError13from .texceptions import NoSuchDatabaseError14from . import appconfig15log = logging.getLogger('humblebee')16class NamingScheme(object):17 def ep_filename(self, ep):18 """19 Get bottom level filename for episode.20 """21 raise NotImplementedError22 def season_filename(self, ep):23 """24 Filename of season directory.25 """26 raise NotImplementedError27 def series_filename(self, ep):28 """29 Filename of series directory.30 """31 raise NotImplementedError32 def full_path(self, ep, root=None):33 """34 Get full series/season/ep path.35 Result should be treated as relative to 36 whatver root dir is.37 If `root` is passed, the resulting path will be 38 an absolute path.39 """40 eu = ensure_utf841 fp = os.path.join(42 eu(self.series_filename(ep)),43 eu(self.season_filename(ep)),44 eu(self.ep_filename(ep))45 )46 if root:47 fp = normpath(os.path.join(root, fp)) 48 return fp49class Friendly(NamingScheme):50 """51 Series Name (year)/s01/Series Name s01e02 Episode Title.avi52 """53 ep_mask = u'%(series_title)s s%(season_number)se%(ep_number)s%(extra_ep_number)s %(title)s%(ext)s'54 series_mask = u'%(series_title)s (%(series_start_date)s)'55 season_mask = u'season %(season_number)s'56 57 def ep_filename(self, ep):58 epd = dict(ep.items())59 eep = epd['extra_ep_number']60 epd['season_number'] = padnum(epd['season_number'])61 epd['ep_number'] = padnum(epd['ep_number'])62 if eep:63 epd['extra_ep_number'] = 'e'+padnum(eep)64 else:65 epd['extra_ep_number'] = '' 66 p = ep.path()67 if os.path.isdir(p):68 epd['ext'] = ''69 else:70 epd['ext'] = os.path.splitext(p)[1]71 return replace_bad_chars(self.ep_mask % epd)72 def season_filename(self, ep):73 epd = dict(ep.items())74 epd['season_number'] = padnum(ep['season_number'])75 return replace_bad_chars(self.season_mask % epd)76 def series_filename(self, ep):77 """78 Get a series foldername from ep.79 """80 epd = dict(ep.items())81 firstair = ep['series_start_date']82 if firstair:83 epd['series_start_date'] = firstair.year84 else:85 epd['series_start_date'] = 'no-date'86 if ep['series_title'].endswith('(%s)' % epd['series_start_date']):87 epd['series_title'] = ep['series_title'][:-7]88 return replace_bad_chars(self.series_mask % epd)89class Structured(NamingScheme):90 """91 Series Name (year)/s01/Series Name s01e02 Episode Title.avi92 """93 ep_mask = u'%(series_title)s s%(season_number)se%(ep_number)s%(extra_ep_number)s %(title)s%(ext)s'94 series_mask = u'%(series_title)s'95 season_mask = u'season %(season_number)s'96 97 def ep_filename(self, ep):98 epd = dict(ep.items())99 eep = epd['extra_ep_number']100 epd['season_number'] = padnum(epd['season_number'])101 epd['ep_number'] = padnum(epd['ep_number'])102 if eep:103 epd['extra_ep_number'] = 'e'+padnum(eep)104 else:105 epd['extra_ep_number'] = '' 106 p = ep.path()107 if os.path.isdir(p):108 epd['ext'] = ''109 else:110 epd['ext'] = os.path.splitext(p)[1]111 return replace_bad_chars(self.ep_mask % epd)112 def season_filename(self, ep):113 epd = dict(ep.items())114 epd['season_number'] = padnum(ep['season_number'])115 return replace_bad_chars(self.season_mask % epd)116 def series_filename(self, ep):117 """118 Get a series foldername from ep.119 """120 epd = dict(ep.items())121 return replace_bad_chars(self.series_mask % epd)122 123naming_schemes = {124 'friendly' : Friendly,125 'structured' : Structured126 }127class Renamer(object):128 """129 Handles renaming/moving of episodes in both filesystem and database.130 """ 131 def __init__(self, rootdir, destdir, naming_scheme='friendly'):132 self.db = TVDatabase(rootdir)133 self.destdir = normpath(destdir)134 self.naming_scheme = naming_schemes[naming_scheme]()135 safe_make_dirs(self.destdir)136 if not samefile(self.destdir, self.db.directory):137 self.destdb = TVDatabase(self.destdir)138 self.destdb.create_database(soft=True)139 else:140 self.destdb = self.db141 self.clutter = appconfig.get('scanner', 'clutter').split(',')142 def update_db_path(self, ep, newpath):143 """144 Update file_path in database for given episode.145 """146 ep['file_path'] = newpath147 return self.destdb.upsert_episode(ep)148 def spare_dest_file(self, fn):149 """150 Move file to _unknown dir.151 """152 pj = os.path.join153 unknown = normpath(154 pj(self.destdir, '_unknown')155 )156 safe_make_dirs(unknown)157 destfile = normpath(158 pj(unknown, os.path.split(fn)[1])159 )160 safe_rename(fn, destfile)161 162 def move_episode(self, ep, force=False):163 """164 Path will be moved to `destdir` in a filename structure 165 decided by `naming_scheme`.166 If `destdir` is the same as `db.directory`, path will also be updated 167 in database. 168 Containing directory of ep is pruned afterwards.169 If the new path currently exists, it will be moved to '_unknown' before 170 ep is put in its place. Unless `force` is True, then it is overwritten.171 """ 172 oldfile = ep.path()173 olddir = os.path.dirname(oldfile)174 newfile = self.naming_scheme.full_path(ep, root=self.destdir)175 if samefile(oldfile, newfile):176 return ep177 log.debug('Renaming "%s" -> "%s"', oldfile, newfile)178 pathindb = self.db.path_exists(ep.path('db')) 179 if os.path.exists(newfile) and not force:180 self.spare_dest_file(newfile)181 if os.path.isdir(newfile) and force:182 shutil.rmtree(newfile)183 safe_make_dirs(os.path.dirname(newfile))184 os.rename(oldfile, newfile)185 #if samefile(self.destdir, self.db.directory):186 self.update_db_path(ep, newfile)187 prune_dirs(olddir, root=self.db.directory, clutter=self.clutter)188 return ep189class SymlinkRenamer(Renamer):190 """191 Safer version of Renamer. Creates symlinks in destdir instead of actually moving 192 any files.193 `rootdir` may not be the same as `destdir`194 """ 195 def __init__(self, rootdir, destdir, naming_scheme='friendly'): 196 super(SymlinkRenamer, self).__init__(rootdir,destdir,naming_scheme)197 if samefile(self.destdir, self.db.directory):198 raise InvalidDirectoryError(199 'rootdir and destdir can not be the same directory.'200 )201 def move_episode(self, ep, force=True):202 oldfile = ep.path()203 newfile = self.naming_scheme.full_path(ep, root=self.destdir)204 make_symlink(oldfile, newfile, overwrite=True)205def make_unknown_dir(db, destdir):206 """nn207 When symlinks, make a virtual dir based on 208 unparsed_episode table.209 """210 pj = os.path.join211 root = normpath(212 pj(destdir, '_unknown')213 )214 safe_make_dirs(root) 215 q = 'SELECT * FROM unparsed_episode'216 for row in db.execute_query(q):217 path = row['child_path']218 rpath = pj(db.directory, path)219 vpath = pj(root, path)220 if os.path.isdir(rpath):221 safe_make_dirs(vpath)222 else:223 make_symlink(rpath, vpath)224def make_symlinkfs(rootdir, destdir, naming_scheme='friendly'):225 """226 Make a symlinkfs in destdir based on existing database in rootdir.227 """228 db = TVDatabase(rootdir)229 if not db.db_file_exists():230 raise NoSuchDatabaseError(231 'No tv database in "%s", please import first.' % rootdir232 )233 if not os.path.exists(destdir):234 safe_make_dirs(destdir)235 renamer = SymlinkRenamer(rootdir, destdir, naming_scheme=naming_scheme)236 for ep in db.get_episodes():237 renamer.move_episode(ep) 238 make_unknown_dir(db, destdir)239def renamer_all(rootdir, destdir, force=False, naming_scheme='friendly'):240 """241 Rename all episodes in database in `rootdir` to `destdir`.242 """243 db = TVDatabase(rootdir)244 if not db.db_file_exists():245 raise NoSuchDatabaseError(246 'No tv database in "%s", please import first.' % rootdir247 )248 if not os.path.exists(destdir):249 safe_make_dirs(destdir)250 renamer = Renamer(rootdir, destdir, naming_scheme=naming_scheme)251 for ep in db.get_episodes():252 renamer.move_episode(ep, force=force)...
builder.py
Source:builder.py
...145 Inject a fixture into a suite. If no fixture name is specified then the name of the variable holding146 the injected fixture will be used.147 """148 return InjectedFixture(fixture_name)149def _default_naming_scheme(name, description, parameters, nb):150 return name + "_%d" % nb, description + " #%d" % nb151def _format_naming_scheme(name_fmt, description_fmt):152 def naming_scheme(_, __, parameters, ___):153 return name_fmt.format(**parameters), description_fmt.format(**parameters)154 return naming_scheme155class _Parametrized(object):156 def __init__(self, parameters_source, naming_scheme):157 self._parameters_source = parameters_source158 self.naming_scheme = naming_scheme159 @property160 def parameters_source(self):161 source = iter(self._parameters_source)162 try:163 first_item = next(source)164 except StopIteration:165 return166 if type(first_item) is dict:167 yield first_item168 for item in source:169 yield item170 else:171 if type(first_item) in STRING_TYPES:172 names = [s.strip() for s in first_item.split(",")]173 else:174 names = first_item # assume list or tuple175 for values in source:176 yield dict(zip(names, values))177def parametrized(parameter_source, naming_scheme=_default_naming_scheme):178 # type: (Iterable, Optional[Union[Callable[[str, str, dict, int], Tuple[str, str]], Sequence]]) -> Any179 """180 Decorator, make the test parametrized.181 :param parameter_source: it can be either:182 - an iterable of dicts, each dict representing the arguments passed to the test183 - a CSV-like mode, where the first element of the list represents the argument names as an str or a sequence184 (example: ``"i,j"`` or ``("i", "j")``) and the remaining elements are sequences of the arguments to be185 passed to the test.186 :param naming_scheme: optional, it can be either:187 - a optional function that takes as parameters the base test name, description, parameters, index188 and must return the expanded test name and description as a two elements list189 - a tuple/list of two (name + description) format strings, example: ``("test_{i}_plus_{j}", "Test {i} plus {j}")``190 """191 def wrapper(obj):192 md = get_metadata(obj)193 md.parametrized = _Parametrized(194 parameter_source,195 naming_scheme if callable(naming_scheme) else _format_naming_scheme(*naming_scheme)196 )197 return obj...
votes.py
Source:votes.py
1import os2import datetime3from zipfile import ZipFile4from billy.scrape.votes import VoteScraper, Vote5class NCVoteScraper(VoteScraper):6 jurisdiction = 'nc'7 def scrape(self, chamber, session):8 # Unfortunately, you now have to request access to FTP.9 # This method of retrieving votes needs to be be changed or10 # fall back to traditional web scraping.11 if session == '2009':12 # 2009 files have a different delimiter and naming scheme.13 vote_data_url = 'ftp://www.ncleg.net/Bill_Status/Vote Data 2009.zip'14 naming_scheme = '{session}{file_label}.txt'15 delimiter = ";"16 else:17 vote_data_url = 'ftp://www.ncleg.net/Bill_Status/Votes%s.zip' % session18 naming_scheme = '{file_label}_{session}.txt'19 delimiter = "\t"20 fname, resp = self.urlretrieve(vote_data_url)21 # fname = "/Users/brian/Downloads/Vote Data 2009.zip"22 zf = ZipFile(fname)23 chamber_code = 'H' if chamber == 'lower' else 'S'24 # Members_YYYY.txt: tab separated25 # 0: id (unique only in chamber)26 # 1: H or S27 # 2: member name28 # 3-5: county, district, party29 # 6: mmUserId30 member_file = zf.open(naming_scheme.format(file_label='Members', session=session))31 members = {}32 for line in member_file.readlines():33 data = line.split(delimiter)34 if data[1] == chamber_code:35 members[data[0]] = data[2]36 # Votes_YYYY.txt37 # 0: sequence number38 # 1: chamber (S/H)39 # 2: date40 # 3: prefix41 # 4: bill_id42 # 5: yes votes43 # 6: no votes44 # 7: excused absences45 # 8: excused votes46 # 9: didn't votes47 # 10: total yes+no48 # 11: sponsor49 # 12: reading info50 # 13: info51 # 20: PASSED/FAILED52 # 21: legislative day53 vote_file = zf.open(naming_scheme.format(file_label='Votes', session=session))54 bill_chambers = {'H':'lower', 'S':'upper'}55 votes = {}56 for line in vote_file.readlines():57 data = line.split(delimiter)58 if len(data) < 24:59 self.warning('line too short %s', data)60 continue61 if data[1] == chamber_code:62 date = datetime.datetime.strptime(data[2][:16],63 '%Y-%m-%d %H:%M')64 if data[3][0] not in bill_chambers:65 # skip votes that aren't on bills66 self.log('skipping vote %s' % data[0])67 continue68 votes[data[0]] = Vote(chamber, date, data[13],69 'PASS' in data[20],70 int(data[5]),71 int(data[6]),72 int(data[7])+int(data[8])+int(data[9]),73 bill_chamber=bill_chambers[data[3][0]],74 bill_id=data[3]+data[4], session=session)75 member_vote_file = zf.open(naming_scheme.format(file_label='MemberVotes', session=session))76 # 0: member id77 # 1: chamber (S/H)78 # 2: vote id79 # 3: vote chamber (always same as 1)80 # 4: vote (Y,N,E,X)81 # 5: pair ID (member)82 # 6: pair order83 # If a vote is paired then it should be counted as an 'other'84 for line in member_vote_file.readlines():85 data = line.split(delimiter)86 if data[1] == chamber_code:87 try:88 member_voting = members[data[0]]89 except KeyError:90 self.debug('Member %s not found.' % data[0])91 continue92 try:93 vote = votes[data[2]]94 except KeyError:95 self.debug('Vote %s not found.' % data[2])96 continue97 # -1 votes are Lt. Gov, not included in count, so we add them98 if data[4] == 'Y' and not data[5]:99 if data[0] == '-1':100 vote['yes_count'] += 1101 vote.yes(member_voting)102 elif data[4] == 'N' and not data[5]:103 if data[0] == '-1':104 vote['no_count'] += 1105 vote.no(member_voting)106 else:107 # for some reason other_count is high for paired votes108 if data[5]:109 vote['other_count'] -= 1110 # is either E: excused, X: no vote, or paired (doesn't count)111 vote.other(member_voting)112 for vote in votes.itervalues():113 #vote.validate()114 vote.add_source(vote_data_url)115 self.save_vote(vote)116 # remove file117 zf.close()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!