def main():
    if len(sys.argv) != 3: # if no input
        print "Syntax: python db2_to_mol2.py db2file.db2 outprefix ";
        return

    filename  = sys.argv[1];
    outfileprefix  = sys.argv[2];

    print "filename: " + filename 

    db2mols = read_Moldb2_file(filename)
    allmol2s = convert_db2_to_mol2(db2mols)
    filecount = 0
    for mol2mols in allmol2s:
       outfilename = outfileprefix + "." + str(filecount) + ".mol2"
       print "writing "+ outfilename 
       count = 0
       for mol2mol in mol2mols:
          if count == 0: 
             mol2.write_mol2(mol2mol,outfilename)
          else:
             mol2.append_mol2(mol2mol,outfilename)
          count = count + 1
       filecount = filecount + 1
    return;
def shift_mol2(fileprefix, ori_mol, xtran, ytran, ztran):
    mol = copy.copy(ori_mol)
    namemol2 = fileprefix + "_" + str(xtran) + "_" + str(ytran) + "_" + str(
        ztran) + ".mol2"
    for i in range(len(mol.atom_list)):
        mol.atom_list[i].X = mol.atom_list[i].X + xtran
        mol.atom_list[i].Y = mol.atom_list[i].Y + ytran
        mol.atom_list[i].Z = mol.atom_list[i].Z + ztran
    mol2.write_mol2(mol, namemol2)
    return
def shift_mol2(fileprefix, ori_mol):
    mol = copy.copy(ori_mol)
    step = 0.03
    name = 0.03
    for i in range(10):
        #outmol2 = open(fileprefix+"."+str(var)+".mol2",'w')
        namemol2 = fileprefix + "." + str(name) + ".mol2"
        print len(mol.atom_list)
        for i in range(len(mol.atom_list)):
            mol.atom_list[i].X = mol.atom_list[i].X + step
        mol2.write_mol2(mol, namemol2)
        name = name + 0.03
    return
def main():
    if len(sys.argv) != 6:  # if no input
        print(" This script needs the following:")
        print(" (1) input mol2 file")
        print(" (2) output mol2 file")
        print(" (3-5) x, y, z rotation angles")
        print(" rotation order: x, y, then z with be rotated in that order. ")
        return

    mol2file = sys.argv[1]
    outputfile = sys.argv[2]
    xangle = float(sys.argv[3])
    yangle = float(sys.argv[4])
    zangle = float(sys.argv[5])

    cos_x = math.cos(xangle)
    xsign = 1.0
    if xangle < 0.0:
        xsign = -1.0

    cos_y = math.cos(yangle)
    ysign = 1.0
    if yangle < 0.0:
        ysign = -1.0

    cos_z = math.cos(zangle)
    zsign = 1.0
    if zangle < 0.0:
        zsign = -1.0

    mols = mol2.read_Mol2_file(mol2file)
    first = True
    for m in mols:
        center = mol2.centre_of_mass(m)
        translate(m, -center[0], -center[1], -center[2])
        rotate_x(m.atom_list, cos_x, xsign)
        rotate_y(m.atom_list, cos_y, ysign)
        rotate_z(m.atom_list, cos_z, zsign)
        translate(m, center[0], center[1], center[2])
        if first:
            mol2.write_mol2(m, outputfile)
            first = False
        else:
            mol2.append_mol2(m, outputfile)

    return
def modify_charges_mol2_file(mol2file, atom_list_hex, outputprefix):
    ## read in mol2 file
    mol = mol2.read_Mol2_file(mol2file)[0]

    n = len(atom_list_hex)
    if n != len(mol.atom_list):
        print "Error: n != len(mol.atom_list) : " + str(n) + " !=" + str(
            len(mol.atom_list))
        exit()

    for i in range(n):
        charge = atom_list_hex[i][2]
        print mol.atom_list[i].Q, charge
        mol.atom_list[i].Q = float(charge)

    filename = outputprefix + '.mol2'
    mol2.write_mol2(mol, filename)
    return
Exemple #6
0
def modify_mol2_file(mol2file, outputprefix):
    ## read in mol2 file
    frist = True
    mollist = mol2.read_Mol2_file(mol2file)
    for mol in mollist:
        ori_formal_charge = mol2.formal_charge(mol)
        n = len(mol.atom_list)
        Si_atoms_index = []
        for i in range(n):
            if mol.atom_list[i].name == 'CB' or mol.atom_list[i].name == 'SG':
                print(mol.atom_list[i].type, mol.atom_list[i].name)
                Si_atoms_index.append(i)

        if len(Si_atoms_index) != 2:
            print("error")
            exit()

        count_h = [0,
                   0]  # remember how meny atoms are connected to the both Si
        Hlist = [
        ]  # remember which the atom index that are the connected bonds.
        Si1 = Si_atoms_index[0]
        Si2 = Si_atoms_index[1]
        for bond in mol.bond_list:
            if bond.a1_num - 1 == Si1:
                if (mol.atom_list[bond.a2_num - 1].type == 'H'):
                    count_h[0] = count_h[0] + 1
                    Hlist.append(bond.a2_num)
            if bond.a2_num - 1 == Si1:
                if (mol.atom_list[bond.a1_num - 1].type == 'H'):
                    count_h[0] = count_h[0] + 1
                    Hlist.append(bond.a1_num)
            if bond.a1_num - 1 == Si2:
                if (mol.atom_list[bond.a2_num - 1].type == 'H'):
                    count_h[1] = count_h[1] + 1
                    Hlist.append(bond.a2_num)
            if bond.a2_num - 1 == Si2:
                if (mol.atom_list[bond.a1_num - 1].type == 'H'):
                    count_h[1] = count_h[1] + 1
                    Hlist.append(bond.a1_num)
        print(Hlist)
        print(count_h)

        # (1) remove 5 hydrogen atoms,
        # (2) change Si to Du and the atom name,

        new_atomlist = []
        for i, atom in enumerate(mol.atom_list):
            print(i, atom.num)
            #exit()
            if atom.num in Hlist:
                continue
            if atom.num - 1 == Si1:
                atom.type = 'Du'
                atom.Q = 0.0
                print("Hcount ==", count_h[0])
                if count_h[0] == 3:
                    atom.name = 'D2'
                elif count_h[0] == 0:  # SG will just have no H
                    #elif count_h[0] == 2:
                    atom.name = 'D1'
            if atom.num - 1 == Si2:
                atom.type = 'Du'
                atom.Q = 0.0
                print("Hcount ==", count_h[1])
                if count_h[1] == 3:
                    atom.name = 'D2'
                #elif count_h[1] == 2: #
                elif count_h[1] == 0:  # SG will just have no H
                    atom.name = 'D1'

            new_atomlist.append(copy.copy(atom))
        # (3) generate mapping from old to new atom numbering to be used in the bond modification.
        atom_num_map = {}
        for i, atom in enumerate(new_atomlist):
            atom_num_map[atom.num] = i + 1
            atom.num = i + 1

        # (4) remove bonds that contain any of the removed hydrogens.
        new_bondlist = []
        for i, bond in enumerate(mol.bond_list):
            if bond.a1_num in Hlist or bond.a2_num in Hlist:
                continue
            new_bondlist.append(copy.copy(bond))

        # (5) renumber the bonds, map old atom numbering to new atom number for bonds
        i = 1
        for bond in new_bondlist:
            bond.num = i
            bond.a1_num = atom_num_map[bond.a1_num]
            bond.a2_num = atom_num_map[bond.a2_num]
            i = i + 1

        mol.atom_list = new_atomlist
        mol.bond_list = new_bondlist

        # ajust Si-Si-C bond angle.  Looks like it is 90 deg.
        #ajust_angle(mol,ang)

        # ajust the partial charge to make it an integer.
        make_integer_charge(mol, ori_formal_charge)

        #exit()
        filename = outputprefix + '.mol2'
        if frist:
            mol2.write_mol2(mol, filename)
            frist = False
        else:
            mol2.append_mol2(mol, filename)

    return
for j, mol_3 in enumerate(mol_3list):
    print "molecule %d" % (j)
    #mol_3_new = copy.deepcopy(mol_3)
    atomlist = []
    for i, move in enumerate(maping):
        #print i,move,maping[i]
        atom = mol_3.atom_list[move]
        atom.num = i + 1
        atomlist.append(atom)

    #mol_3_new.atom_list = atomlist
    #bondlist     = copy.deepcopy(mol_2.bond_list)
    bondlist = mol_2.bond_list
    header = mol_3.header
    name = mol_3.name
    residuelist = mol_3.residue_list
    mol_3_new = mol2.Mol(header, name, atomlist, bondlist, residuelist)
    print mol_3.header, mol_3_new.header
    #  for bond in mol_3_new.bond_list:
    #      print bond.a1_num, bond.a2_num, bond.num, bond.type
    #      print atomlist[bond.a1_num-1].num, atomlist[bond.a2_num-1].num, bond.num, bond.type
    if j == 0:
        mol2.write_mol2(mol_3_new, outfile)
    else:
        mol2.append_mol2(mol_3_new, outfile)

#print len(mol_list)
#mol = mol2.remove_hydrogens( mol_list[0] )
#mol2.write_mol2(mol,outfile)
Exemple #8
0
def zernike_moments(mol2_file, nmax=20, np=50, uniform=True, fix_dx=False, np_on_grid=20, shift=False, buildmap=False, coef_out=True, calc_intensity=True, external_rmax=-1):
  base = mol2_file.split('.')[0]
  splat_range = 0
  fraction = 0.9
  default_dx = 0.5

  models = mol2.read_Mol2_file( mol2_file )
  if(len( models )== 0):
    return None,None,None
  ## for testing purpose, here only the first model in the mol2 file is converted ##
  this_molecule = models[0]
  ## models should be a list of molecules, same ligand at different conformations ##

  all_atoms = this_molecule.atom_list
  if(len( all_atoms )== 0):
    return None,None,None

  # predefine some arrays we will need
  xyz = flex.vec3_double()
  charges = flex.double()
  # keep track of the atom types we have encountered
  for atom in all_atoms:
    xyz.append( ( atom.X, atom.Y, atom.Z ) )
    charges.append( atom.Q )

  if(xyz.size() == 0):
    return None,None,None
  if (uniform):
    density=flex.double(xyz.size(),1.0)
  else: # use charges
    density=charges

  voxel_obj = math.sphere_voxel(np,splat_range,uniform,fix_dx,external_rmax, default_dx, fraction,xyz,density)
  np = voxel_obj.np()
  rmax=voxel_obj.rmax()/fraction

  #print base, "RMAX: ", voxel_obj.rmax()

  #print time.time()
  grid_obj = math.sphere_grid(np, nmax)
  pdb_out = False
  grid_obj.clean_space(voxel_obj, pdb_out)
  #print time.time(), "END GRID CONST"
  grid_obj.construct_space_sum()

  #print time.time(), "SS CALC"
  mom_obj = math.zernike_moments(grid_obj, nmax)
  #print time.time(), "END MOM CALC"

  moments = mom_obj.moments()
  if(coef_out):
    nn = mom_obj.fnn()
    easy_pickle.dump(base+'.nlm.pickle', moments.coefs() )
    easy_pickle.dump(base+'.nn.pickle', nn.coefs() )

  #
####### The following will be optional ###
  if(shift):
    shift = [rmax, rmax, rmax]
    centered_xyz = voxel_obj.xyz() + shift
    out_mol2_filename =base+'_centered.mol2'
    for a,xyz in zip( all_atoms, centered_xyz):
      a.X = xyz[0]
      a.Y = xyz[1]
      a.Z = xyz[2]

    mol2.write_mol2( this_molecule, out_mol2_filename )

    original_map = voxel_obj.map()
    xplor_map_type( original_map, np, rmax, file_name=base+'_pdb.xplor')


######## Calculate Intnesity Profile ############
  if(calc_intensity):
    q_array = flex.double( range(51) )/100.0
    z_model = zm.zernike_model( moments, q_array, rmax, nmax)
    nn = mom_obj.fnn()
    intensity = z_model.calc_intensity(nn)
    #iq_file = open(base+"_"+str(nmax)+"_"+str(int(rmax*fraction))+".zi", 'w')
    iq_file = open(base+"_"+str(nmax)+".zi", 'w')
    for qq, ii in zip(q_array, intensity):
      print>>iq_file, qq,ii

    iq_file.close()
####### END of Intensity Calculation ###########

  if(buildmap):
    print "grid setting up..."
    zga = math.zernike_grid(np_on_grid, nmax, False)
    print "grid setting up...Done"
    zga.load_coefs(moments.nlm(), moments.coefs() )
    print "start reconstruction"
    map=flex.abs(zga.f() )
    print "finished reconstruction"
    xplor_map_type( map, np_on_grid, rmax, file_name=base+'.xplor')
    ccp4_map_type( map, np_on_grid, rmax, file_name=base+'.ccp4' )

  return mom_obj, voxel_obj, models
Exemple #9
0
import mol2
import sys

print "this file requiers the mol2 libary writen by trent balius and sudipto mukherjee"

print "syntex: mol2_removeH.py input_file output_file"

infile = sys.argv[1]
outfile = sys.argv[2]
mol_list = mol2.read_Mol2_file(infile)
print len(mol_list)
mol = mol2.remove_hydrogens( mol_list[0] )
mol2.write_mol2(mol,outfile)