def test_move_atom():
    a_atom = Simple_atom.Get_Simple_atom_list("2GKU.pdb")
    b_atom = Simple_atom.Get_Simple_atom_list("2GKU_md1.pdb")
    origin_coor_old, z_axis_old = Move_atom.Init_parm(a_atom, [1])
    print origin_coor_old
    print z_axis_old
    c_atom = Move_atom.Move(b_atom, [1], origin_coor_old, z_axis_old)
    origin_coor, z_axis = Move_atom.Init_parm(c_atom, [1])
    print origin_coor
    print z_axis
    for i in range(20):
        print a_atom[i].atom_2_PDBformat()
        print c_atom[i].atom_2_PDBformat()
def Move_2_center(top_file, trj_file, trjout_file):
    u = Universe(top_file, trj_file)
    TRAJ_FRAMES = u.trajectory.numframes
    w = Writer(trjout_file, u.trajectory.numatoms)

    atoms = Simple_atom.Get_atom_list(top_file)
    SOLUTE_COUNT = 0
    for atom_ID in atoms:
        if atoms[atom_ID].residue_name != "SOL" and atoms[
                atom_ID].residue_name != "WAT":
            SOLUTE_COUNT += 1
        else:
            pass

    print '''Note: this program will read the pbc condition and use the dimensions \
read from trajectory files. You should make sure the dimensions are right or \
it will create a wrong output trajectory file.'''

    START_TIME = Time.time()
    NUM_ATOMS = u.trajectory.numatoms
    # loop through the trajectory and write a frame for every step
    for ts in u.trajectory:

        dimensions = ts.dimensions

        for i in range(SOLUTE_COUNT):
            if ts._x[i] > dimensions[0]:
                ts._x[i] = ts._x[i] - dimensions[0]
            if ts._x[i] < 0:
                ts._x[i] = ts._x[i] + dimensions[0]

            if ts._y[i] > dimensions[1]:
                ts._y[i] = ts._y[i] - dimensions[1]
            if ts._y[i] < 0:
                ts._y[i] = ts._y[i] + dimensions[1]

            if ts._z[i] > dimensions[2]:
                ts._z[i] = ts._z[i] - dimensions[2]
            if ts._z[i] < 0:
                ts._z[i] = ts._z[i] + dimensions[2]

        NOW_TIME = Time.time()
        BIN_TIME = NOW_TIME - START_TIME
        if ts.frame % 100 == 0:
            #            usage.echo("%8.4f   %8.4f   %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2]))
            usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \
                % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) ))
#    for ts in u.trajectory:
        w.write(ts)


#        usage.echo("Writting frame %d\r"  %ts.frame)
    w.close_trajectory()
    print "Converted %r --> %r" % (intrj, outtrj)
def Traj_2_rms(coor_list, traj_list, rmsd_file, atom_list, skip):
    '''
	Reading a trajectory file. output the pairwiseRMSD to rmsd_file.\n
	atom_list : a list of atom serial.\n
	skip: a int number like 1,2,3,10. 
	'''
    Alist = Simple_atom.Get_Simple_atom_list(coor_list[0])
    Blist = []
    for atom in Alist:
        if atom.atom_serial in atom_list:
            Blist.append(atom)
    '''Get the result atom list in Simple_atom class.'''

    if os.path.isfile(rmsd_file):
        print "backup %s to %s\n " % (rmsd_file, "#" + rmsd_file + "#")
        try:
            os.rename(rmsd_file, "#" + rmsd_file + "#")
        except OSError, e:
            print e
            print "the file %s will be overwrited!" % rmsd_file
Beispiel #4
0
def pRDF(traj_file,coor_file,index_file,solute_index,dmax=20,nbins=32):
    '''
    A simple pRDF test here.
    '''

    START_TIME       =Time.time()

    HAS_TRAJFILE = False
    if os.path.isfile(traj_file):
        HAS_TRAJFILE = True
    
    atom_list    =Simple_atom.Get_atom_list(coor_file)
    index_list   =Index.Read_index_to_Inclass(index_file)
    solute_list  =index_list[solute_index].group_list

    solvent_list =Get_solvent_list(atom_list)

    if HAS_TRAJFILE:
        U=MDAnalysis.Universe(coor_file,traj_file)
    else:
        U=MDAnalysis.Universe(coor_file)

    GR=numpy.zeros((100),dtype=numpy.float64)
    Data_file=open("datafile.xvg",'w')
    #step 1 
    # Get the center of the solute. 
    for ts in U.trajectory:

        print "Checking frame number: %d" %(ts.frame)
        solute_atoms = dict()
        
        coor_x=0.0
        coor_y=0.0
        coor_z=0.0
        X_min = ts._x[0]
        X_max = ts._x[0]
        Y_min = ts._y[0]
        Y_max = ts._y[0]
        Z_min = ts._z[0]
        Z_max = ts._z[0]        
        R_solute =0.0

        for i in solute_list:
            coor_x += ts._x[i-1]
            coor_y += ts._y[i-1]
            coor_z += ts._z[i-1]
            u_atom = MDPackage.Coor.unit_atom.unit_atom(atom_coor_x=ts._x[i-1],atom_coor_y=ts._y[i-1],atom_coor_z=ts._z[i-1])
            solute_atoms[i]=u_atom

            if ts._x[i-1] < X_min:
                X_min = ts._x[i-1]
            elif ts._x[i-1] > X_max:
                X_max = ts._x[i-1]

            if ts._y[i-1] < Y_min:
                Y_min = ts._y[i-1]
            elif ts._y[i-1] > Y_max:
                Y_max = ts._y[i-1]

            if ts._z[i-1] < Z_min:
                Z_min = ts._z[i-1]
            elif ts._z[i-1] > Z_max:
                Z_max = ts._z[i-1]

        coor_x /= len(solute_list)
        coor_y /= len(solute_list)
        coor_z /= len(solute_list)

        for i in solute_list:
            _R_tmp = ( solute_atoms[i].atom_coor_x - coor_x ) **2 + \
            ( solute_atoms[i].atom_coor_y - coor_y ) **2 +\
            ( solute_atoms[i].atom_coor_z - coor_z ) **2 
            if _R_tmp > R_solute:
                R_solute = _R_tmp

        R_solute = math.sqrt(R_solute) + dmax
        R_solute = R_solute **2
        # print R_solute
        # sys.exit()


        # print "Step 1 finished."
        #print "center %f\t%f\t%f" %(coor_x,coor_y,coor_z)
        # print X_min,X_max
        #step 2
        #Get the range of the box.
        X_min = X_min - dmax
        Y_min = Y_min - dmax
        Z_min = Z_min - dmax
        X_max = X_max + dmax
        Y_max = Y_max + dmax
        Z_max = Z_max + dmax
        # print X_min,X_max

        # bin   = dmax *2.0 / nbins
        bin = 1
        x_bins = int((X_max - X_min) /bin) +1
        y_bins = int((Y_max - Y_min) /bin) +1
        z_bins = int((Z_max - Z_min) /bin) +1 

        #print "bin:",bin

        #step 4 
        #assign each grid to solute atoms. 
        #grid_in_solute contains that each grid blongs to which solute atom.


        grids_in_which_solute =dict()
        solute_contain_grids  =dict()

        # print x_bins,y_bins,z_bins
        _gauss_value = -1
        for i in range(x_bins * y_bins * z_bins ):
            z_temp = i / ( x_bins * y_bins) 
            y_temp = (i % (x_bins * y_bins)) /  x_bins 
            x_temp = i % x_bins

            grid_site=[X_min+(x_temp+0.5)*bin, Y_min+(y_temp+0.5)*bin, Z_min+(z_temp+0.5)*bin]
            #print grid_site
            min_dist, min_index= Min_dist(solute_atoms,solute_list,grid_site,[coor_x,coor_y,coor_z],R_solute)
            if min_index == 0:
                continue
            # _gauss_value = min_dist
            if i % 300 ==0:
                NOW_TIME=Time.time()
                BIN_TIME=NOW_TIME-START_TIME
                sys.stderr.write("grid ID %10d, time used: %6.2f s\r" %(i,BIN_TIME))
                sys.stderr.flush()
            try:
                grids_in_which_solute[i]=[min_index,min_dist]
            except:
                print "hello to see you"
            try:
                solute_contain_grids[min_index].append(i)
            except:
                solute_contain_grids[min_index]=list()
                solute_contain_grids[min_index].append(i)
                #print solute_contain_grids

        # for i in solute_contain_grids:
        #     print i,len(solute_contain_grids[i])
        # sys.exit()

        # print "\nStep 4 finished."

        #step 5.
        #assign each solvent atom to grids.
        grid_in_solvent=[list() for i in range(x_bins * y_bins * z_bins)]
        for i in solvent_list:
            SV_x = ts._x[i-1]
            SV_y = ts._y[i-1]
            SV_z = ts._z[i-1]

            if SV_x > X_min and SV_x < X_max:
                x_temp = int( (SV_x - X_min) / bin )
            else:
                continue
            if SV_y > Y_min and SV_y < Y_max:    
                y_temp = int( (SV_y - Y_min) / bin )
            else:
                continue
            if SV_z > Z_min and SV_z < Z_max:
                z_temp = int( (SV_z - Z_min) / bin )
            else:
                continue
            grid_in_solvent[z_temp*x_bins*y_bins + y_temp*x_bins + x_temp].append(i)
            # print "append solvent %d" %i

        # print "Step 5 finished."
         # step 6.
        #calculating the g(r) for each solute atom.

        density   = MDAnalysis.core.units.convert(1.0, 'water', 'Angstrom^{-3}')*math.pow(10,3)
        unit_conc = 1/ ((bin)**3 * density) #unit solvent atom density.

        temp1     =list() #A list used to contain grid_dist.
        temp2     =list() #A list used to contain sol number for each grad.
 
        TOTAL_ATOMS = 0
        for i in solute_list:
            try:
                temp_grids=solute_contain_grids[i]
                #print "solute %d, grids number %d" %(i,len(temp_grids))
            except:
                continue
        #rdf_atom=[0 for i in range(50)]
            # bin       =float(dmax)/nbins


        
            for grid in temp_grids:

                sol_number=len(grid_in_solvent[grid])
                # if sol_number ==0:
                #     continue
                #print "   %d" %sol_number,
                # try:
                blabla,dist=grids_in_which_solute[grid]
                # except:
                    # continue
            
                temp1.append(dist)
                temp2.append(sol_number)

            
            # if len(temp1) == 0:
            #     continue
                # print temp1
                # print temp2
        #if i == 10:
        #    sys.exit()
        rdf_atom=hist(temp1, temp2, 0.0, dmax, 100)
        # print rdf_atom
        # print unit_conc
        #print rdf_atom
        if sum(rdf_atom) > 0:
            TOTAL_ATOMS += 1
        rdf_atom=numpy.array(rdf_atom) / unit_conc

        # sys.exit()
        GR += rdf_atom
#        print GR
        plt.clf()
        ax = plt.gca()
        ax.set_xlabel("Distance (nm)")
        ax.set_ylabel("pRDF(r)")
        x_label=[i*dmax/1000. for i in range(100)]
        y_label=[GR[i]/ts.frame for i in range(100)]
        ax.plot(x_label,y_label,'b',)
        plt.draw()
        temp_filename="temp"+"%d"%(ts.frame)+".png"
        plt.savefig(temp_filename)
        for i in range(100):
            Data_file.write("%12.4f" %y_label[i])
        Data_file.write("\n")
        

    GR = GR / TOTAL_ATOMS / U.trajectory.numframes
    Data_file.close()
#   print TOTAL_ATOMS
#        print len(solute_index)
    for i in range(32):
        print "%f\t%f" %(2.0/32*(i+0.5),GR[i])
Beispiel #5
0
def Move_2_center(top_file,trj_file,trjout_file,skip=1):
    u=Universe(top_file,trj_file)
    TRAJ_FRAMES=u.trajectory.numframes
    atom_l=Simple_atom.Get_Simple_atom_list(top_file)

    while True:
        fit_residue=Simple_atom.Get_residue(top_file)
        if len(fit_residue) > 1:
            print "Only 1 residue is aviliable."
            continue
        else:
            break
    while True:
        fit_atom=Simple_atom.Get_atom(atom_l,fit_residue[0])
        if len(fit_atom) > 1:
            print "Only 1 atom is aviliable."
            continue
        else: 
            break

    fitting_group=Simple_atom.Get_residue(top_file)

    fit_atom=fit_atom[0]

    w = Writer(trjout_file, u.trajectory.numatoms)
    NUM_ATOMS=len(atom_l)

    print '''Note: this program will read the pbc condition and use the dimensions \
read from trajectory files. You should make sure the dimensions are right or \
it will create a wrong output trajectory file.'''

    START_TIME=Time.time()
    # loop through the trajectory and write a frame for every step
    for ts in u.trajectory:
        if ts.frame % skip != 1:
            break

        if ts.frame==1:
            label_coordinate=[ts._x[fit_atom],ts._y[fit_atom],ts._z[fit_atom]]

            # origin_list=list()
            # for atom in atom_l:
            #     if atom.residue_serial in fitting_group:
            #         origin_list.append([\
            #                 ts._x[atom_l.index(atom)],\
            #                 ts._y[atom_l.index(atom)],\
            #                 ts._z[atom_l.index(atom)],\
            #                 ])

        else:
            shift=[label_coordinate[0]-ts._x[fit_atom],\
                    label_coordinate[1]-ts._y[fit_atom],\
                    label_coordinate[2]-ts._z[fit_atom]]


            dimensions=ts.dimensions
            usage.echo("%8.4f   %8.4f   %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2]))

            for i in range(NUM_ATOMS):
               ts._x[i] =ts._x[i]+shift[0]
               if ts._x[i] > dimensions[0]:
                   ts._x[i]=ts._x[i]-dimensions[0]
               if ts._x[i] <0:
                   ts._x[i]=ts._x[i]+dimensions[0]

               ts._y[i] =ts._y[i]+shift[1]
               if ts._y[i] > dimensions[1]:
                   ts._y[i]=ts._y[i]-dimensions[1]
               if ts._y[i] <0:
                   ts._y[i]=ts._y[i]+dimensions[1]

               ts._z[i] =ts._z[i]+shift[2]
               if ts._z[i] > dimensions[2]:
                   ts._z[i]=ts._z[i]-dimensions[2]
               if ts._z[i] < 0:
                   ts._z[i]=ts._z[i]+dimensions[2]


#             temp_list=list()
#             for atom in atom_l:
#                 if atom.residue_serial in fitting_group:
#                     temp_list.append([\
#                             ts._x[atom_l.index(atom)],\
#                             ts._y[atom_l.index(atom)],\
#                             ts._z[atom_l.index(atom)],\
#                             ])


# #            [Rotate,shift]=least_squares_fitting.Fitting(temp_list,origin_list)


#             atom_matrix=numpy.array([[ts._x[i],ts._y[i],ts._z[i]]for i in range(NUM_ATOMS)])

# #            resu_matrix=numpy.matrix(atom_matrix) * numpy.matrix(Rotate).T 
# #            resu_matrix=numpy.array(resu_matrix)

# #            for i in range(NUM_ATOMS):
# #                atom_matrix[i,0]=resu_matrix[i,0]+shift[0]
# #                atom_matrix[i,1]=resu_matrix[i,1]+shift[1]
# #                atom_matrix[i,2]=resu_matrix[i,2]+shift[2]
 

#             for i in range(NUM_ATOMS):
#                 ts._x[i]=atom_matrix[i,0]
#                 ts._y[i]=atom_matrix[i,1]
#                 ts._z[i]=atom_matrix[i,2]
        
 
#        w.write(ts)
        NOW_TIME=Time.time()
        BIN_TIME=NOW_TIME-START_TIME
        usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \
                % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) ))
#    for ts in u.trajectory:
        w.write(ts)
#        usage.echo("Writting frame %d\r"  %ts.frame)
    w.close_trajectory()
    print "Converted %r --> %r" % (intrj, outtrj)
def pRDF(traj_file, coor_file, index_file, solute_index, dmax=30, nbins=32):
    '''
    A simple pRDF test here.
    '''

    START_TIME = Time.time()

    HAS_TRAJFILE = False
    if os.path.isfile(traj_file):
        HAS_TRAJFILE = True

    atom_list = Simple_atom.Get_atom_list(coor_file)
    index_list = Index.Read_index_to_Inclass(index_file)
    solute_list = index_list[solute_index].group_list

    solvent_list = Get_solvent_list(atom_list)

    if HAS_TRAJFILE:
        U = MDAnalysis.Universe(coor_file, traj_file)
    else:
        U = MDAnalysis.Universe(coor_file)

    GR = numpy.zeros((100), dtype=numpy.float64)
    Data_file = open("datafile.xvg", 'w')
    #step 1
    # Get the center of the solute.
    for ts in U.trajectory:

        print "Checking frame number: %d" % (ts.frame)
        solute_atoms = dict()

        coor_x = 0.0
        coor_y = 0.0
        coor_z = 0.0
        for i in solute_list:
            coor_x += ts._x[i - 1]
            coor_y += ts._y[i - 1]
            coor_z += ts._z[i - 1]
            u_atom = MDPackage.Coor.unit_atom.unit_atom(
                atom_coor_x=ts._x[i - 1],
                atom_coor_y=ts._y[i - 1],
                atom_coor_z=ts._z[i - 1])
            solute_atoms[i] = u_atom
        coor_x /= len(solute_list)
        coor_y /= len(solute_list)
        coor_z /= len(solute_list)

        print "Step 1 finished."
        #print "center %f\t%f\t%f" %(coor_x,coor_y,coor_z)

        #step 2
        #Get the range of the box.
        X_min = coor_x - dmax
        Y_min = coor_y - dmax
        Z_min = coor_z - dmax
        X_max = coor_x + dmax
        Y_max = coor_y + dmax
        Z_max = coor_z + dmax
        bin = dmax * 2.0 / nbins

        #print "bin:",bin

        print "Step 2 finished."

        #step 3
        # Get the grids
        #grid_number=z*nbins*nbins + y*nbins + x

        print "Step 3 finished."

        #step 4
        #assign each grid to solute atoms.
        #grid_in_solute contains that each grid blongs to which solute atom.

        ###############begin this new algorithm#################################
        #         grid_map = numpy.zeros([nbins**3,2],dtype=numpy.int)
        #         occpiad_dict=dict()

        #         for so in solute_list:
        #             x_temp = int((solute_atoms[so].atom_coor_x - X_min)/bin+0.5)
        #             y_temp = int((solute_atoms[so].atom_coor_y - Y_min)/bin+0.5)
        #             z_temp = int((solute_atoms[so].atom_coor_z - Z_min)/bin+0.5)
        #             ind= z_temp*nbins*nbins + y_temp*nbins + x_temp
        #             if grid_map[ind,0] !=0:
        #             #    print "conflict. %d" %so
        #                 pass
        #             else:
        #                 grid_map[ind,0]     =so
        #                 occpiad_dict[ind] =1
        #             #print "map %d to grid id %d" %(so,ind)
        #         cycle = 0
        #         while True:
        #             occpiad_dict=dict()
        #             for i in range(nbins**3):
        #                 if grid_map[i,0] != 0:
        #                     occpiad_dict[i]=1
        #                 #new_dict = list()
        #             print "cycle %d, len(occ) = %d" %(cycle,len(occpiad_dict))

        #             if len(occpiad_dict) > nbins **3 -1:
        #                 break

        #             for i in occpiad_dict:
        #                 z_temp = i / nbins**2
        #                 y_temp = (i % nbins**2) / nbins
        #                 x_temp = i % nbins

        #                 for step_x in range(-1,2):
        #                     for step_y in range(-1,2):
        #                         for step_z in range(-1,2):
        #                             if abs(step_x)+abs(step_y)+abs(step_z)==0:
        #                                 continue
        #                             if (step_x+x_temp > nbins-1) or (step_x+x_temp <0):
        #                                 continue
        #                             if (step_y+y_temp > nbins-1) or (step_y+y_temp <0):
        #                                 continue
        #                             if (step_z+z_temp > nbins-1) or (step_z+z_temp <0):
        #                                 continue

        #                             new_value=(z_temp+step_z)*nbins*nbins + (y_temp+step_y)*nbins + (x_temp+step_x)

        #                             if grid_map[new_value,0]== 0:
        #                                 grid_map[new_value,0]   = grid_map[i,0]
        #                         #        new_dict.append(new_value)

        #                             else:
        #                                 grid_site=[X_min+(x_temp+step_x+0.5)*bin,\
        #                                  Y_min+(y_temp+ step_y + 0.5)*bin, \
        #                                  Z_min+(z_temp+ step_z + 0.5)*bin]
        #                                 old_atom =solute_atoms[grid_map[i,0]]
        #                                 new_atom =solute_atoms[grid_map[new_value,0]]
        #                                 old_vect =[old_atom.atom_coor_x,old_atom.atom_coor_y,old_atom.atom_coor_z]
        #                                 new_vect =[new_atom.atom_coor_x,new_atom.atom_coor_y,new_atom.atom_coor_z]
        #                                 dist1    = Dist_Square(grid_site,old_vect)
        #                                 dist2    = Dist_Square(grid_site,new_vect)
        #                             #    print dist1, dist2
        #                                 if dist1 < dist2:
        #                                     grid_map[new_value,0]   = grid_map[i,0]
        #                             #        new_dict.append(new_value)

        # #                grid_map[i]=0

        #             if len(occpiad_dict) < nbins **3 :
        # #                for i in occpiad_dict:
        # #                    grid_map[i,0]=0
        # #                    grid_map[i,1]=0
        #                 del occpiad_dict
        #                 cycle += 1
        # #                print "cycle %d, len(occ) = %d" %(cycle,len(new_dict))
        # #       print occpiad_dict
        # #
        # #        sys.exit()
        #         solute_contain_grids  =dict()
        #         for i in range(nbins**3):
        #             try:
        #                 solute_contain_grids[grid_map[i,0]].append(i)
        #             except:
        #                 solute_contain_grids[grid_map[i,0]]=list()
        #                 solute_contain_grids[grid_map[i,0]].append(i)

        #             z_temp = i / nbins**2
        #             y_temp = (i % nbins**2) / nbins
        #             x_temp = i % nbins
        #             grid_site=[X_min+(x_temp+0.5)*bin,\
        #             Y_min+(y_temp+ 0.5)*bin, \
        #             Z_min+(z_temp+ 0.5)*bin]
        #             old_atom =solute_atoms[grid_map[i,0]]
        #             old_vect =[old_atom.atom_coor_x,old_atom.atom_coor_y,old_atom.atom_coor_z]
        #             dist1    = Dist_Square(grid_site,old_vect)
        #             grid_map[i,1]=math.sqrt(dist1)

        #         grids_in_which_solute=grid_map
        #         for i in solute_contain_grids:
        #             print i,len(solute_contain_grids[i])
        #         sys.exit()

        ###############end this algorithm######################################

        grids_in_which_solute = list()
        solute_contain_grids = dict()

        for i in range(nbins**3):
            z_temp = i / nbins**2
            y_temp = (i % nbins**2) / nbins
            x_temp = i % nbins

            grid_site = [
                X_min + (x_temp + 0.5) * bin, Y_min + (y_temp + 0.5) * bin,
                Z_min + (z_temp + 0.5) * bin
            ]
            #print grid_site
            min_dist, min_index = Min_dist(solute_atoms, solute_list,
                                           grid_site)
            if i % 3000 == 0:
                NOW_TIME = Time.time()
                BIN_TIME = NOW_TIME - START_TIME
                sys.stderr.write("grid ID %10d, time used: %6.2f s\r" %
                                 (i, BIN_TIME))
                sys.stderr.flush()
            grids_in_which_solute.append([min_index, min_dist])
            try:
                solute_contain_grids[min_index].append(i)
            except:
                solute_contain_grids[min_index] = list()
                solute_contain_grids[min_index].append(i)
                #print solute_contain_grids

        # for i in solute_contain_grids:
        #     print i,len(solute_contain_grids[i])
        # sys.exit()

        print "\nStep 4 finished."

        #step 5.
        #assign each solvent atom to grids.
        grid_in_solvent = [list() for i in range(nbins**3)]
        for i in solvent_list:
            SV_x = ts._x[i - 1]
            SV_y = ts._y[i - 1]
            SV_z = ts._z[i - 1]

            if SV_x > X_min and SV_x < X_max:
                x_temp = int((SV_x - X_min) / bin)
            else:
                continue
            if SV_y > Y_min and SV_y < Y_max:
                y_temp = int((SV_y - Y_min) / bin)
            else:
                continue
            if SV_z > Z_min and SV_z < Z_max:
                z_temp = int((SV_z - Z_min) / bin)
            else:
                continue
            grid_in_solvent[z_temp * nbins * nbins + y_temp * nbins +
                            x_temp].append(i)
            # print "append solvent %d" %i

        print "Step 5 finished."
        # step 6.
        #calculating the g(r) for each solute atom.

        TOTAL_ATOMS = 0
        for i in solute_list:
            try:
                temp_grids = solute_contain_grids[i]
                #print "solute %d, grids number %d" %(i,len(temp_grids))
            except:
                continue
        #rdf_atom=[0 for i in range(50)]
            bin = float(dmax) / nbins
            density = MDAnalysis.core.units.convert(
                1.0, 'water', 'Angstrom^{-3}') * math.pow(10, 3)
            unit_conc = 1 / ((float(dmax) * 2 / nbins)**3 * density
                             )  #unit solvent atom density.
            temp1 = list()  #A list used to contain grid_dist.
            temp2 = list()  #A list used to contain sol number for each grad.

            for grid in temp_grids:

                sol_number = len(grid_in_solvent[grid])
                #print "   %d" %sol_number,
                blabla, dist = grids_in_which_solute[grid]

                temp1.append(dist)
                temp2.append(sol_number)
        #print temp1
        #print temp2
        #if i == 10:
        #    sys.exit()
            rdf_atom = hist(temp1, temp2, 0.0, 12.0, 100)
            #print rdf_atom
            if sum(rdf_atom) > 0:
                TOTAL_ATOMS += 1
            rdf_atom = numpy.array(rdf_atom) * unit_conc

            GR += rdf_atom
#        print GR
        plt.clf()
        ax = plt.gca()
        ax.set_xlabel("Distance (nm)")
        ax.set_ylabel("pRDF(r)")
        x_label = [i / 50.0 for i in range(100)]
        y_label = [GR[i] / ts.frame for i in range(100)]
        ax.plot(
            x_label,
            y_label,
            'b',
        )
        plt.draw()
        temp_filename = "temp" + "%d" % (ts.frame) + ".png"
        plt.savefig(temp_filename)
        for i in range(100):
            Data_file.write("%12.4f" % y_label[i])
        Data_file.write("\n")

    GR = GR / TOTAL_ATOMS / U.trajectory.numframes
    Data_file.close()
    #   print TOTAL_ATOMS
    #        print len(solute_index)
    for i in range(32):
        print "%f\t%f" % (2.0 / 32 * (i + 0.5), GR[i])
import os.path
import sys

pdbfile = sys.argv[1]  #PRMpbc
#intrj =sys.argv[2] #TRJpbc_bz2
ext = '.xtc'  # output format determined by extension

root, oldext = greedy_splitext(os.path.basename(pdbfile))
outtrj = root + ext
outpdb = root + '_new.pdb'

u = Universe(pdbfile, )

# create a writer instance for the output trajectory
w = Writer(outtrj, u.trajectory.numatoms)
aa = Simple_atom.Get_Simple_atom_list(pdbfile)
NUM_FRAMES = len(aa) / u.trajectory.numatoms
# loop through the trajectory and write a frame for every step
ts = u.trajectory[0]
for i in range(NUM_FRAMES):
    for j in range(u.trajectory.numatoms):
        ts._x[j] = aa[i * u.trajectory.numatoms + j].atom_coor_x * 10
        ts._y[j] = aa[i * u.trajectory.numatoms + j].atom_coor_y * 10
        ts._z[j] = aa[i * u.trajectory.numatoms + j].atom_coor_z * 10
    w.write(ts)
    print "Converted frame %d" % (i + 1)
w.close_trajectory()
print "Converted %r --> %r" % (pdbfile, outtrj)

# make a pdb file as a simple 'topology'
u.trajectory.rewind()
Beispiel #8
0
def pRDF(top_file,
         trj_file,
         index_file,
         trjout_file,
         solute_index,
         WAT_NUMBER=500):
    '''
    Save the WAT_NUMBER closest water molecules to a new trajectory file.
    '''
    START_TIME = Time.time()

    Atoms = Simple_atom.Get_atom_list(top_file)
    atom_list = copy.deepcopy(Atoms)
    index_list = Index.Read_index_to_Inclass(index_file)
    solute_atom_list = index_list[solute_index].group_list
    solvent_list = Get_solvent_list(atom_list)

    if len(solvent_list) < WAT_NUMBER:
        print "Error: The number of water molecules (%d) is less than the critical number (%d)." % (
            len(solvent_list), WAT_NUMBER)

    natoms = libxdrfile.read_xtc_natoms(trj_file)
    x = numpy.zeros((natoms, libxdrfile.DIM), dtype=numpy.float32)
    box = numpy.zeros((libxdrfile.DIM, libxdrfile.DIM), dtype=numpy.float32)
    XTC = libxdrfile.xdrfile_open(trj_file, 'r')

    OUTPUT_ATOMS = len(solute_atom_list) + 3 * WAT_NUMBER
    XTC_write = libxdrfile.xdrfile_open(trjout_file, 'w')
    x_out = numpy.zeros((OUTPUT_ATOMS, libxdrfile.DIM), dtype=numpy.float32)

    # loop through file until return status signifies end or a problem
    # (it should become exdrENDOFFILE on the last iteration)
    status = libxdrfile.exdrOK
    FIRST_FRAME = True
    solu_list = list()
    while status == libxdrfile.exdrOK:
        status, step, time, prec = libxdrfile.read_xtc(XTC, box, x)
        # do something with x
        sol_dict = dict()
        sol_dist = list()

        ###############
        for i in atom_list:
            u_atom = MDPackage.Coor.unit_atom.unit_atom(atom_coor_x=x[i - 1,
                                                                      0],
                                                        atom_coor_y=x[i - 1,
                                                                      1],
                                                        atom_coor_z=x[i - 1,
                                                                      2])
            atom_list[i] = u_atom


###############

        if FIRST_FRAME == True:
            #            FIRST_FRAME = False
            for solvent in solvent_list:
                #            usage.echo("Checking solvent atom: %d\r" %(solvent))
                min_dist, solu_temp = Min_dist(atom_list, solute_atom_list,
                                               solvent)
                sol_dist.append(min_dist)
                sol_dict[min_dist] = solvent
                solu_list.append(solu_temp)
            solu_list = list(set(solu_list))
        else:
            for solvent in solvent_list:
                min_dist, solu_temp = Min_dist(atom_list, solu_list, solvent)
                sol_dist.append(min_dist)
                sol_dict[min_dist] = solvent

        sorted_sol_dist = sorted(sol_dist)
        STANDARD = sorted_sol_dist[WAT_NUMBER + 1]

        for i in range(len(solute_atom_list)):
            x_out[i, 0] = x[solute_atom_list[i] - 1, 0]
            x_out[i, 1] = x[solute_atom_list[i] - 1, 1]
            x_out[i, 2] = x[solute_atom_list[i] - 1, 2]
        for i in range(WAT_NUMBER):
            for j in range(3):
                x_out[len(solute_atom_list) + 3 * i + j,
                      0] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 0]
                x_out[len(solute_atom_list) + 3 * i + j,
                      1] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 1]
                x_out[len(solute_atom_list) + 3 * i + j,
                      2] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 2]

        status2 = libxdrfile.write_xtc(XTC_write, step, time, box, x_out, prec)

        if FIRST_FRAME == True:
            new_list = [Atoms[i] for i in solute_atom_list]
            for i in range(WAT_NUMBER):
                new_list.append(Atoms[sol_dict[sorted_sol_dist[i]]])
                new_list.append(Atoms[sol_dict[sorted_sol_dist[i]] + 1])
                new_list.append(Atoms[sol_dict[sorted_sol_dist[i]] + 2])

            out_file = string.split(trjout_file, '.')[0] + ".pdb"
            Simple_atom.Save_file(out_file, new_list)

            FIRST_FRAME = False

        NOW_TIME = Time.time()
        BIN_TIME = NOW_TIME - START_TIME
        if step % 1000 == 0:
            sys.stderr.write(
                "step %10d, time %10.2f ps, time used: %6.2f s\r" %
                (step, time, BIN_TIME))
            sys.stderr.flush()

    # finally close file
    print ""
    libxdrfile.xdrfile_close(XTC)
    libxdrfile.xdrfile_close(XTC_write)
        else:
            pass
    if not os.path.isfile(traj_file):
        print "the trajectory file %s is not exist." %traj_file
        sys.exit(0)

    if not os.path.isfile(coor_file):
        print "the structure file %s is not exist." %coor_file
        sys.exit(0)

    if out_coor_file[-4:] not in [".pdb",".gro"]:
        print "error: the output structure must *.pdb or *.gro."
        sys.exit(0)
    
    if not os.path.isfile(index_file):
        atom_list=Simple_atom.Get_Simple_atom_list(coor_file)
        index_list=Index.Atomlist_2_Index(atom_list)
    else:
        index_list=Index.Read_index_to_Inclass(index_file)

    Index.Print_Index(index_list)
    group_ID=raw_input("Choose a group: ")
    try:
        atom_list=index_list[int(group_ID)].group_list
    except:
        print "error"
        sys.exit()
    Traj.Traj_2_coor(coor_file, traj_file, out_coor_file, atom_list, skip)

def Usage():
    print "Usage: Trjconv -p <coor_file> -f <traj_file> -n <index_file> -o <coor_file> --skip num"
    '''
    print " " 
    print "  "*13,"-)","Rotate_2_Z","(-"
    print " " 
    print "  "*12,"-)"," Version: %s " %usage.version ,"(-" 
    print " " 


if __name__=="__main__":
    Print_ProgInfo()
    if len(sys.argv)!=3:
        Usage()
        sys.exit()


    topol =sys.argv[1] #PRMpbc
    resu_file=sys.argv[2]

    atom_l=Simple_atom.Get_Simple_atom_list(topol)
    fitting_group=Simple_atom.Get_residue(topol,True)
    origin_coor,z_axis=Move_atom.Init_parm(atom_l,fitting_group)
    print origin_coor, z_axis

    # loop through the trajectory and write a frame for every step
    Z_axis=(0,0,1)
    origin_coor=(0,0,0)
    b_atom=Move_atom.Move(atom_l, fitting_group, origin_coor,Z_axis  )
    Simple_atom.Save_file(resu_file,b_atom)