<jupyter_start><jupyter_text>Structure ParsingFor this project, you will use Python to parse a structure downloaded from the website [PubChem](https://pubchem.ncbi.nlm.nih.gov/). The files from PubChem are in a format called SDF. We will write a script to save the coordinates from the file. We will write these in a format called an xyz file so it can be used for our simulations later.We've included an sdf file for nitrobenzene in the sdf folder, but after you complete this exercise, you can download and try this with other sdf files. The file you should open is in the sdf folder and named 'nitrobenzene.sdf'<jupyter_code># Write code here to build the file path. We want to open a file called 'nitrobenzene' in the sdf folder.
import os

filehandle = 
# Write code here to open and read the file. Save the file contents
# in a variable called data

data =
# This cell will print the file with line numbers
for line_num, line in enumerate(data):
    print(line_num, line)<jupyter_output><empty_output><jupyter_text>For these files, the first number on row 3 (remember counting starts at 0!) is always the number of atoms.For our example, the line looks like this:`3  14 14  0     0  0  0  0  0  0999 V2000`Remember that the first number (3) is the line number, so the line from the file is`14 14  0     0  0  0  0  0  0999 V2000`<jupyter_code># To get the number of atoms, we will want to get information from this line. First, slice the data variable to
# get the fourth line

fourth_line = 
print(fourth_line)
# Next, we'll want to split our line so we can get the number of atoms. Write code to split the line here, and
# save in a variable called words
words = 
# After you have split the line, get the first value in that list to get the number of atoms.

num_atoms = 

# We want num_atoms to be a number (it's a string because we read it from a file), so we save it as an integer
num_atoms = int(num_atoms)
print(F"There are {num_atoms} atoms")<jupyter_output><empty_output><jupyter_text>After the fourth line, the next lines give the coordinates of the atoms. There will be one line for each atom.How can you slice data so that you get the lines with atom information?<jupyter_code>starting_line = 4

# What is the ending line of atom information in the atom information starts on line 4 and there are 
# num_atoms lines?

ending_line = 

# Slice data to get the lines that have coordinates. Save in a variable called atom_lines
atom_lines = 

for line in atom_lines:
    print(line)<jupyter_output><empty_output><jupyter_text>In these lines, the first column is the x coordinate, the second is the y coordinate, the third is the z coordinate, and the fourth is the atom element. We'll first see how to get this information out of one line, then move it into a loop to do for all of the lines.<jupyter_code># Get the first line
first_line = 

# Split the first line on white space
words = 

# Get the atom name and coordinates
atom_name = 
x = 
y = 
z = 

print(atom_name, x, y, z)<jupyter_output><empty_output><jupyter_text>We want to do this for all the lines and write a file called an xyz file. An xyz file always has the number of atoms on the first line. The second line will is always a comment, it doesn't really matter what it says. Then, the coordinates are listed for each atom`element x_coordinate y_coordinate z_coordinate`Loop over the lines in your `atom_lines` variable and print the atom names and coordinates. Save the x coordinate in a list called `x_coord`, the y coordinates in a list called `y_coord`, the z coordinates in a list called `z_coords` and the atom names in a list called `atom_names`,<jupyter_code># Loop over atom_lines and save atom_names, x_coord, y_coord, z_coord<jupyter_output><empty_output><jupyter_text>Saving the information to a file We might want to save the information to a file for later use. The code below opens a file called `output.xyz` and writes the list you created in a file.<jupyter_code>num_atoms = len(atom_names)

# Open an output file called 'output.xyz' for writing.
output_file = open('output.xyz', 'w+')

# First - write the number of atoms
output_file.write(f'{num_atoms}\n')

# Second line - a comment, we will leave empty
output_file.write("\n")


for i in range(num_atoms):
    output_string = atom_names[i] + ' ' + x_coord[i] + ' ' + y_coord[i] + ' ' + z_coord[i] + '\n'
    output_file.write(output_string)

output_file.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06259851384823238, 'fraction_numerical': 0.009457329430308489, 'mean_word_length': 4.3582629674306395, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '507566', 'license': 'MIT', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1307, 'n_words': 668, 'path': 'structure_parsing-student.ipynb', 'repo_name': 'janash/rice-workshop-binder', 'size': '4441'}