def test_good_coordinates(self): """Correct coordinates are well read. Must be floats""" for test_input in self.good_inputs: atom = atoms.Atom(test_input[0], test_input[1]) self.assertEqual(atom.x(), float(test_input[1][0])) self.assertEqual(atom.y(), float(test_input[1][1])) self.assertEqual(atom.z(), float(test_input[1][2]))
def graphene(na, nb, nc, includePeriodic=True): lattice = 1.42 planar = 3.35 a = numpy.array([lattice * math.sqrt(3), 0., 0.]) b = numpy.array([0., 3. * lattice, 0.]) c = numpy.array([0., 0., planar]) boxSize = numpy.vstack([a, b, c, numpy.array([0., 0., 0.])]) # make two basic atoms a = atoms.Atom() a.aName = 'C' a.rName = 'GRA' a.rNo = 1 a.chain = 'G' a.ffType = 'ca' b = copy.deepcopy(a) b.xyz = numpy.array([0., lattice, 0.]) a.makeBond(b) al = [a, b] cal = copy.deepcopy(al) geometry.moveAtoms( cal, numpy.array([lattice * math.sqrt(3) / 2.0, lattice * 1.5, 0.])) al.extend(cal) boxSize = replicate(al, boxSize, na, nb, nc, cutoff=lattice * 1.05, maxBonds=3, includePeriodic=includePeriodic) return (al, boxSize)
def proc_atom(atName): '''This checks the atomDict for the atom, creates it if necessary, and returns it.''' if atName not in atomDict.keys(): atomDict[atName] = atom_lib.Atom(atName, eqivDict, xyzDict, gij, UijDict, sUijDict) return atomDict[atName]
def alignAtoms(al1,al2,sel1=None,sel2=None): """ another interface to the align function will align atoms in al2 to atoms in al1 optional: sel1: list of indices af atoms in al1 (does not have to be numpy.array) optional: sel2: list of indices af atoms in al2 (does not have to be numpy.array) lengths of these two selections have to be the same """ if sel1 == None: sel1 = range(len(al1)) if sel2 == None: sel2 = range(len(al2)) assert len(sel1) == len(sel2), 'Error: lenghts of the two selections in geometry.alignAtomsAngle is not the same: %d and %d' % (len(sel1), len(sel2)) # create a list of dumme atoms with the target orientations that we are aligning to bl2 = [] for i in range(len(al2)): b = atoms.Atom() bl2.append(b) for i in range(len(sel1)): b = bl2[sel2[i]] b.xyz = al1[sel1[i]].xyz.copy() set1_orig = atoms.getXyzFromList(bl2) set2_orig = atoms.getXyzFromList(al2) set2, RMSD, RMSDnonsel, RMSDall = align( set1_orig, set2_orig, selection = numpy.array(sel2)) atoms.putXyzIntoList(al2,set2) return RMSD
def convert_args(self, args): if len(args) != len(self.types): raise ValueError('%s must have the same length as %s' % (args, self.types)) new_args = [] for ty, arg in zip(self.types, args): if isinstance(ty, EasyType) and not isinstance(arg, Object): new_args.append(ty.get_const(arg)) else: new_args.append(arg) return atoms.Atom(self, new_args)
def sphFileToAtoms(sphereFilename): lines = open(sphereFilename).readlines() al = [] for line in lines[1:]: if line.strip() != '': a = atoms.Atom() a.aName = 'Sc' a.rNo = 1 a.rName = 'SPH' a.chain = 'S' a.xyz = getXyzFromSphLine(line) al.append(a) return al
def readAtomList(filename): """ read single frame from xyz file """ al = [] with open(filename) as f: natoms = int(f.readline()) f.readline() # comment line for i in range(natoms): atom = atoms.Atom() s = f.readline().split() atom.aName = s[0] atom.xyz[0] = float(s[1]) atom.xyz[1] = float(s[2]) atom.xyz[2] = float(s[3]) al.append(atom) return al
def readLine(line): """store bgf line info into atom and return the atom""" a = atoms.Atom() a.rNo = int(line[0:5]) a.rName = line[5:10].strip() a.aName = line[10:15].strip() a.aNo = int(line[15:20]) a.xyz = 10.0 * numpy.array( [float(line[20:28]), float(line[28:36]), float(line[36:44])]) a.velocities = numpy.array( [float(line[44:52]), float(line[52:60]), float(line[60:68])]) return a
def readFile(fileName): with open(fileName, 'r') as f: line = f.readline() while not (line.startswith('@<TRIPOS>MOLECULE') or line == ''): line = f.readline() line = f.readline() line = f.readline() s = line.split() numAtoms = int(s[0]) numBonds = int(s[1]) numRes = int(s[2]) AL = [atoms.Atom() for i in range(numAtoms)] while not (line.startswith('@<TRIPOS>ATOM') or line == ''): line = f.readline() for i in range(numAtoms): line = f.readline() readAtomLine(line, AL[i]) while not (line.startswith('@<TRIPOS>BOND') or line == ''): line = f.readline() for i in range(numBonds): line = f.readline() readBondLine(line, AL) while not (line.startswith('@<TRIPOS>SUBSTRUCTURE') or line == ''): line = f.readline() resRoot = [] resChain = [] for i in range(numRes): line = f.readline() s = line.split() root = int(s[2]) chain = s[5] resRoot.append(root) resChain.append(chain) chainIndex = 0 if len(resChain) > 0: for a in AL: if len(resRoot) > chainIndex + 1: if a.aNo > resRoot[chainIndex + 1]: chainIndex = chainIndex + 1 chain = resChain[chainIndex] a.chain = chain return AL
def readLine(line): """store bgf line info into atom and return the atom""" a = atoms.Atom() if line[0:6] != 'ATOM ' and line[0:6] != 'HETATM': print >> sys.stderr, 'This line is not ATOM/HETATM line: %s' % line sys.exit(1) a.aTag = line[0:6] a.aNo = int(line[6:12]) a.aName = line[13:18].strip() a.rName = line[19:23].strip() a.chain = line[23:24] try: a.rNo = int( line[24:30]) # vmd saves the residue number until characted 30 except ValueError: if line[24:29].strip() == '': a.rNo = 0 else: a.rNo = int( line[24:29]) # but normally character 29 can be a letter a.xyz = numpy.array( [float(line[30:40]), float(line[40:50]), float(line[50:60])]) a.ffType = line[61:66] a.lpair = int(line[69:71]) a.charge = float(line[72:80]) try: fixed = int(line[80:82]) except ValueError: fixed = 0 if fixed == 1: a.fixed = True elif fixed == 0: a.fixed = False else: print >> sys.stderr, 'Value of fixed is invalid %d, should be 0 or 1. ' % fixed sys.exit(1) return a
def readLine(line): """store bgf line info into atom and return the atom""" a = atoms.Atom() if line[0:6] != 'ATOM ' and line[0:6] != 'HETATM': print >> sys.stderr, 'This line is not ATOM/HETATM line: %s' % line sys.exit(1) a.aTag = line[0:6] a.aNo = int(line[6:11]) a.aName = line[12:16].strip() a.rName = line[17:20].strip() a.chain = line[21] a.rNo = int(line[22:26]) a.xyz = numpy.array( [float(line[30:38]), float(line[38:46]), float(line[46:54])]) try: a.occupancy = float(line[54:60]) except ValueError: a.occupancy = 1.0 try: a.beta = float(line[60:66]) except ValueError: a.beta = 0.0 a.lpair = 0 a.charge = 0.0 a.fixed = False try: a.ffType = line[76:78] except ValueError: a.ffType = '' return a
def readOutCoordinates(fileName): '''reads the coordinates sets from the out file, as the structure is minimized, returns xyzlist and al (only with atom names)''' lines = open(fileName).readlines() lineIndex = 0 for i in range(len(lines)): if lines[i].startswith(' Input geometry:'): lineIndex = i xyz = [[]] lineIndex = lineIndex + 3 s = lines[lineIndex].split() al = [] while len(s) > 0: al.append(atoms.Atom()) al[-1].aName = s[0] x = float(s[1]) y = float(s[2]) z = float(s[3]) xyz[0].append([x, y, z]) lineIndex += 1 s = lines[lineIndex].split() while lineIndex < len(lines): if lines[lineIndex].startswith(' new geometry:') or lines[ lineIndex].startswith(' final geometry:'): xyzitem = [] lineIndex += 3 for i in range(lineIndex, lineIndex + len(al)): s = lines[i].split() x = float(s[1]) y = float(s[2]) z = float(s[3]) xyzitem.append([x, y, z]) xyz.append(xyzitem) lineIndex += 1 return numpy.array(xyz), al
def test_distance(self): """Distances between atoms are well calculated""" for pair in self.distance_pairs: atom_A = atoms.Atom(pair[0][0], pair[0][1]) atom_B = atoms.Atom(pair[1][0], pair[1][1]) self.assertAlmostEqual(atom_A.GetDistance(atom_B), pair[2])
def test_set_coordinates_good(self): """Correct atom coordinates are well read into Atom""" for test_input in self.good_inputs: atom = atoms.Atom(test_input[0], test_input[1]) self.assertEqual((atom.x(), atom.y(), atom.z()), test_input[1])
def test_set_element_good(self): """Correct atom types are well read into Atom""" for test_input in self.good_inputs: atom = atoms.Atom(test_input[0], test_input[1]) self.assertEqual(atom.GetType(), test_input[0])
def readTopology(fileName, debug=False): """reading topology only from amber prmtop file bonds between H1 and H2 in WAT are removed """ if debug: print 'Reading amber topology file %s...' % fileName with open(fileName, 'r') as f: # parsing the file line by line # POINTERS line = f.readline() while not line.startswith('%FLAG POINTERS'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() NATOM = int(f.read(8)) # total number of atoms NTYPES = int(f.read(8)) # total number of distinct atom types NBONH = int(f.read(8)) # number of bonds containing hydrogen MBONA = int(f.read(8)) # number of bonds not containing hydrogen NTHETH = int(f.read(8)) # number of angles containing hydrogen MTHETA = int(f.read(8)) # number of angles not containing hydrogen NPHIH = int(f.read(8)) # number of dihedrals containing hydrogen MPHIA = int(f.read(8)) # number of dihedrals not containing hydrogen NHPARM = int(f.read(8)) # currently not used NPARM = int(f.read(8)) # used to determine if addles created prmtop f.readline() NNB = int(f.read(8)) # number of excluded atoms NRES = int(f.read(8)) # number of residues NBONA = int(f.read(8)) # MBONA + number of constraint bonds NTHETA = int(f.read(8)) # MTHETA + number of constraint angles NPHIA = int(f.read(8)) # MPHIA + number of constraint dihedrals NUMBND = int(f.read(8)) # number of unique bond types NUMANG = int(f.read(8)) # number of unique angle types NPTRA = int(f.read(8)) # number of unique dihedral types NATYP = int(f.read( 8)) # number of atom types in parameter file, see SOLTY below NPHB = int( f.read(8)) # number of distinct 10-12 hydrogen bond pair types f.readline() IFPERT = int( f.read(8)) # set to 1 if perturbation info is to be read in NBPER = int(f.read(8)) # number of bonds to be perturbed NGPER = int(f.read(8)) # number of angles to be perturbed NDPER = int(f.read(8)) # number of dihedrals to be perturbed MBPER = int(f.read( 8)) # number of bonds with atoms completely in perturbed group MGPER = int(f.read( 8)) # number of angles with atoms completely in perturbed group MDPER = int( f.read(8) ) # number of dihedrals with atoms completely in perturbed groups IFBOX = int( f.read(8) ) # set to 1 if standard periodic box, 2 when truncated octahedral NMXRS = int(f.read(8)) # number of atoms in the largest residue IFCAP = int( f.read(8)) # set to 1 if the CAP option from edit was specified f.readline() NUMEXTRA = int(f.read(8)) # number of extra points found in topology # NCOPY = int(f.read(8)) # number of PIMD slices / number of beads # ATOM_NAME AL = [atoms.Atom() for i in range(NATOM)] for i in range(len(AL)): AL[i].aNo = i + 1 f.readline() f.readline() f.readline() for i in range(NATOM): AL[i].aName = f.read(4).strip() if (i + 1) % 20 == 0: f.readline() # CHARGE line = f.readline() while not line.startswith('%FLAG CHARGE'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() for i in range(NATOM): AL[i].charge = float(f.read(16)) / 18.2223 if (i + 1) % 5 == 0: f.readline() # MASS # ATOM_TYPE_INDEX # NUMBER_EXCLUDED_ATOMS # NONBONDED_PARM_INDEX # RESIDUE_LABEL while not line.startswith('%FLAG RESIDUE_LABEL'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() resLabel = ['' for i in range(NRES)] for i in range(NRES): resLabel[i] = f.read(4) if (i + 1) % 20 == 0: f.readline() # RESIDUE_POINTER while not line.startswith('%FLAG RESIDUE_POINTER'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() resPointer = [0 for i in range(NRES)] for i in range(NRES): resPointer[i] = int(f.read(8)) if (i + 1) % 10 == 0: f.readline() resPointer.append(1 + NATOM) # now fill the residue info for i in range(NRES): for j in range(resPointer[i], resPointer[i + 1]): AL[j - 1].rName = resLabel[i].strip() AL[j - 1].rNo = i + 1 # BOND_FORCE_CONSTANT # BOND_EQUIL_VALUE # ANGLE_FORCE_CONSTANT # ANGLE_EQUIL_VALUE # DIHEDRAL_FORCE_CONSTANT # DIHEDRAL_PERIODICITY # DIHEDRAL_PHASE # SCEE_SCALE_FACTOR -- not even present in prmtop # SCNB_SCALE_FACTOR -- not even present in prmtop # SOLTY -- currently unused (reserved for future use) # LENNARD_JONES_ACOEF # LENNARD_JONES_BCOEF # BONDS_INC_HYDROGEN while not line.startswith('%FLAG BONDS_INC_HYDROGEN'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() for i in range(NBONH): atom1 = int(f.read(8)) / 3 if (3 * i + 1) % 10 == 0: f.readline() atom2 = int(f.read(8)) / 3 if (3 * i + 2) % 10 == 0: f.readline() f.read(8) if (3 * i + 3) % 10 == 0: f.readline() AL[atom1].makeBond(AL[atom2]) # BONDS_WITHOUT_HYDROGEN while not line.startswith('%FLAG BONDS_WITHOUT_HYDROGEN'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() for i in range(NBONA): atom1 = int(f.read(8)) / 3 if (3 * i + 1) % 10 == 0: f.readline() atom2 = int(f.read(8)) / 3 if (3 * i + 2) % 10 == 0: f.readline() f.read(8) if (3 * i + 3) % 10 == 0: f.readline() AL[atom1].makeBond(AL[atom2]) # ANGLES_INC_HYDROGEN # ANGLES_WITHOUT_HYDROGEN # DIHEDRALS_INC_HYDROGEN # DIHEDRALS_WITHOUT_HYDROGEN # EXCLUDED_ATOMS_LIST # HBOND_ACOEF # HBOND_BCOEF # HBCUT # AMBER_ATOM_TYPE while not line.startswith('%FLAG AMBER_ATOM_TYPE'): line = f.readline() if len(line) == 0: sys.exit(1) line = f.readline() for i in range(NATOM): AL[i].ffType = f.read(4)[0:2] if (i + 1) % 20 == 0: f.readline() # TREE_CHAIN_CLASSIFICATION # JOIN_ARRAY # IROTAT # SOLVENT_POINTERS # ATOMS_PER_MOLECULE # BOX_DIMENSIONS # FLAG RADIUS_SET # RADII # SCREEN if IFCAP > 0: print >> sys.stderr, 'IFCAP > 0 is not implemented (IFCAP is %d here )' % IFCAP sys.exit(1) if IFPERT > 0: print >> sys.stderr, 'IFPERT > 0 is not implemented (IFPERT is %d here )' % IFPERT sys.exit(1) if debug: print '%d atoms read from %s.' % (len(AL), fileName) # removing bonds between H1 and H2 in WAT for a in AL: if a.rName == 'WAT' and a.aName == 'O': if len(a.bonds) != 2: print 'WARNING: atom %s is WATER oxygen, but is not bonded to two atoms' % a else: h1 = a.bonds[0] h2 = a.bonds[1] if (h1.aName not in ['H1', 'H2']) or (h2.aName not in ['H1', 'H2']): print 'WARNING: atom %s is WATER oxygen, the two atoms it is bonded to are not H1, H2' % a else: h1.deleteBond(h2) return AL