Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving a trajectory as a gif file #1158

Open
sunerawijeratne opened this issue Feb 13, 2025 · 2 comments
Open

Saving a trajectory as a gif file #1158

sunerawijeratne opened this issue Feb 13, 2025 · 2 comments

Comments

@sunerawijeratne
Copy link

Hello,

So, I have a trajectory of a MD simulation, that I have visualized using the nglview.show_MDAnalysis method after providing the relevant PSF and DCD files. I have also coloured the trajectory to my liking before visualization and now wish to save this coloured trajectory as a .gif file. I did try using the MovieMaker class as was suggested in the GitHub page, but that did not create anything although the code rand without any errors. Is there any easier way to save a trajectory as a .gif using nglview? I can provide my code if required.

Oh, I should also point out that I'm using nglview on VSCode.

@hainm
Copy link
Collaborator

hainm commented Feb 13, 2025

can you please paste exact code you used in here? thanks.

@sunerawijeratne
Copy link
Author

Hello,

Of course, Here is the code I used in VSCode:

########################################
""" VISUALIZE TRUNCATED DCD FOR GIF """
########################################

from nglview.contrib.movie import MovieMaker

loc_DCD = os.path.expanduser("~/Documents/Prof_Venditti/ssDNA_0_ALKB7_50/filtered_traj.dcd")

filtered_u = mda.Universe(PSF,loc_DCD)

Use ParmEd to load the PDB and PSF files and select protein residues

pmd = load_parmed(fname=f"{main_file}/{sim_folder}/{PSF_file_name}") # Performed to obtain the protein residues in from the PDB file

protein_res = [] # Initialize list to store the no. of protein residues

for index,atom in enumerate(pmd.atoms): # Set iterative loop to collect atoms from the pmd object
res_Name = atom.name # Get residue names
if len(res_Name) == 2: # If residue name == 2 ; a protein residue
protein_res.append(index+1) # Collect the indices, correct to normal numbering and append
else:
break

Apply selection to universe object

selection_string = "resid " + " ".join(map(str, protein_res)) # Set selection string

protein_residues = universe.select_atoms(selection_string) # Select protein residues from the Universe object

Create

block_size = 221 # Obtain protein residue
num_blocks = len(protein_residues) // block_size # Total number of blocks # Obtain the number of protein copies in the

view1 = nv.show_mdanalysis(filtered_u)
view1.clear_representations()

Add unit cell

view1.add_unitcell() # Add unit cell on simulation

Add representation of ssDNA to view object

view1.add_representation('ball+stick', selection = '._NC', color='red', radius=0.7) # Set representation of ssDNA

for i in range(num_blocks):
# Get the residue indices for the current block (block_size=221) # Set iterative loop over no. of protein copies
block_start = i * block_size # Obtain the start residue of a copy
block_end = block_start + block_size # Obtain the end residue of a copy
block_residues = protein_res[block_start:block_end] # Current block residues # Create the select

# Step 2: Add representations for each segment with different colors                                        
# For segment 1 (1-15)
view1.add_representation('ball+stick', selection=range(block_residues[0],block_residues[14]+1), color='#F69A09', radius=0.7)                 # Set N-Terminal 

# For segment 2 (16-205)
view1.add_representation('ball+stick', selection=range(block_residues[15],block_residues[204]+1), color='#AD00FF', radius=0.7)               # Set structured domain colour

# For segment 3 (206-221)
view1.add_representation('ball+stick', selection=range(block_residues[205],block_residues[220]+1), color='#12B52D', radius=0.7)              # Set C-Terminal 

Step 3: Display the NGLView widget

view1._remote_call('setSize', target='Widget', args=['640px', '640px'])
view1.center()
view1

Creating a gif from the trajectory

movie = MovieMaker(view1, output='my.gif', in_memory=True)
movie.make()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants