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 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 Delete_group(self,del_group_id):
        '''
        Delete a group from the group_list and atom_list.
        '''
        if del_group_id in range(len(self.group_list)):
            delete_list=self.group_list[del_group_id].group_list
            atom_serial_list=[self.atom_list[i].atom_serial for i in range(len(self.atom_list))]
            for i in delete_list:
                usage.echo("remove atom %8d, atom name %6s\r" %(i,self.atom_list[atom_serial_list.index(i)].atom_name))
                self.atom_list.remove(self.atom_list[atom_serial_list.index(i)])
                '''remove a atom form atom list'''
                self.group_list[0].group_list.remove(self.group_list[0].group_list[atom_serial_list.index(i)])
                '''remove a atom from group_list[0] which is named system'''
                atom_serial_list.remove(i)
            print "deleted group %s, %6d atoms" %(self.group_list[del_group_id].group_name,len(delete_list))

            self.group_list.remove(self.group_list[del_group_id])
        else:
            print "you input a number not in the list. %d" %del_group_id
Esempio n. 4
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)
Esempio n. 5
0
def pRDF(top_file,trj_file,index_file,trjout_file,solute_index,CUT_OFF=1.0):
    '''
    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
    solute_rest_set  = set(solute_atom_list)
    solvent_list     =Get_solvent_list(atom_list)
    WAT_NUMBER       =0.0

    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)
        sys.exit()

    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
    TEST_SET    = True
    test_count  =0
    solu_set   =set()
    OFF     =0.0
    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].atom_coor_x=x[i-1,0]
            atom_list[i].atom_coor_y=x[i-1,1]
            atom_list[i].atom_coor_z=x[i-1,2]

###############
        if FIRST_FRAME == True:
#            FIRST_FRAME = False
            
            #this cut-off is used to cut the water molecules which are far away from solute molecule.
            # and defined as cut-off = 1.2 *(max-dist from center of solute to WAT_NUMBER of closest 
            #    water molecules)
            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_set.add(solu_temp)
                try:
                    solute_rest_set.remove(solu_temp)
                except:
                    pass
            # print solu_set

        elif TEST_SET == True:
            _tmp_count =0
            SOLUTE_CENTER = Get_solute_center(atom_list,solu_set)
            for solvent in solvent_list:
                if Dist(atom_list,SOLUTE_CENTER,solvent) > OFF :
                    # SKIP_COUNT +=1
                    continue

                # _dist_1,_solu_1 = Min_dist(atom_list,solute_rest_set,solvent)
                _dist_2,_solu_2 = Min_dist(atom_list,solute_atom_list,solvent)
                # if _dist_1 < _dist_2:
                #     print "%d: %d  %d;  size: %d" %(solvent, _solu_1, _solu_2, len(solu_set))
                #     solu_set.add(_solu_1)
                #     sol_dist.append(_dist_1)
                #     sol_dict[_dist_1]=solvent
                #     _tmp_count  += 1
                # else:
                sol_dist.append(_dist_2)
                sol_dict[_dist_2]=solvent

            # if _tmp_count ==0:
            #     test_count += 1
            # if test_count >=20:
            #     TEST_SET = False

        # else:
        #     SOLUTE_CENTER = Get_solute_center(atom_list,solu_set)
        #     for solvent in solvent_list:
        #         if Dist(atom_list,SOLUTE_CENTER,solvent) > OFF :
        #             # SKIP_COUNT +=1
        #             continue            

        #         _dist_2,_solu_2 = Min_dist(atom_list,solu_set,solvent)
        #         sol_dist.append(_dist_2)
        #         sol_dict[_dist_2]=solvent


        sorted_sol_dist=sorted(sol_dist)


        if FIRST_FRAME == True:
            for xx,item in enumerate(sorted_sol_dist):
               if item > CUT_OFF:
                    WAT_NUMBER = xx+1
                    break

            OUTPUT_ATOMS =len(solute_atom_list)+3*WAT_NUMBER
            x_out        =numpy.zeros((OUTPUT_ATOMS,libxdrfile.DIM),dtype=numpy.float32)
            print "WAT_NUMBER: %d ;" %(WAT_NUMBER)

            SOLUTE_CENTER=Get_solute_center(atom_list,solute_atom_list)
#           print SOLUTE_CENTER
            OFF = Get_Cutoff(atom_list,SOLUTE_CENTER,sorted_sol_dist,sol_dict,WAT_NUMBER)



        #########################################
        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=[atom_list[i] for i in solute_atom_list]
            for i in range(WAT_NUMBER):
                new_list.append(atom_list[sol_dict[sorted_sol_dist[i]]])
                new_list.append(atom_list[sol_dict[sorted_sol_dist[i]]+1])
                new_list.append(atom_list[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 % 10 == 0:
        # print ""
        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)
Esempio n. 6
0
def Move_2_center(top_file,trj_file,index_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_Simple_atom_list(top_file)
    index = Index.Read_index_to_Inclass(index_file)
    Index.Print_Index(index)
    while True:
        try:
            solute_index=int(raw_input("Choosing the group for centering:"))
            # solvent_index = int(raw_input("Choosing the solvent group:"))
            break
        except:
            print "You should input a number."
            continue
    solute_group  = index[solute_index].group_list
    # solvent_group = index[solvent_index].group_list
    solute_atoms  =len(solute_group)
    NUM_ATOMS = u.trajectory.numatoms
    # print "\t Reading %d frames from trajectory file: %s" %(nframes,traj_file)    
    START_TIME=Time.time()
    for ts in u.trajectory:
        ref_com =np.zeros((3),dtype=np.float32)    

        # sys.stderr.write("\t Reading frame %8d\r" %ts.frame)
        # sys.stderr.flush()

        for i,ai in list(enumerate(solute_group)):
            ref_com[0] += ts._x[ai-1]
            ref_com[1] += ts._y[ai-1]
            ref_com[2] += ts._z[ai-1]
        ref_com = ref_com/solute_atoms
        dimensions=ts.dimensions
        ref_com = ref_com - np.array([dimensions[0]/2, dimensions[1]/2, dimensions[2]/2])  

        
        # ref_com = np.array([sum(traj_data[:,0])/solute_atoms - dimensions[0]/2,\
            # sum(traj_data[:,1])/solute_atoms - dimensions[1]/2,\
            # sum(traj_data[:,2])/solute_atoms - dimensions[2]/2])  

        for i in range(NUM_ATOMS):
            ts._x[i] = ts._x[i] - ref_com[0] 
            ts._y[i] = ts._y[i] - ref_com[1] 
            ts._z[i] = ts._z[i] - ref_com[2] 

            if (i+1) in solute_group:
                continue

            if ts._x[i] > dimensions[0] or ts._x[i] <0:
                ts._x[i]=ts._x[i]%dimensions[0]

            if ts._y[i] > dimensions[1] or ts._y[i] <0:
                ts._y[i]=ts._y[i]%dimensions[1]

            if ts._z[i] > dimensions[2] or 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 % 10 == 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)
        # del traj_data
#        usage.echo("Writting frame %d\r"  %ts.frame)
    w.close_trajectory()
    print "Converted %r --> %r" % (intrj, outtrj)