Thursday, April 9, 2009

Ex1

Dear class,

If you are a chemical engineering grad student, I am going to put your Exercise 1 results in your boxes so that you have the comments as you begin to work on Exercise 2. If you are in a different department or are an undergrad, you can see me in my office to get yours back, or you can just wait until Tuesday.

There were only two main coding points that I want to emphasize.

The first is to keep in mind that Python indexing starts at 0, not 1. Moreover, slice notation is exclusive of the upper bound. So to take text from the third to and including the 10th columns in a pdb file, use line[2:10]. The 2 and 10 designate the third and 11th characters in line, but the upper bound is not included in the slice.

The second is to keep in mind that Python enables you to iterate over lists directly. Instead of:
for i in range(len(PdbFiles)):
PdbFile = PdbFiles[i]
...
use the following:
for PdbFile in PdbFiles:
...
In addition, we can use list constructors to make our code much clearer. Consider the following code:
Phobics = ["ALA", "CYS", ...]
IsPhobic = []
for Res in ResNames:
if Res in Phobics:
IsPhobic.append(True)
else:
IsPhobic.append(False)
This can be made much shorter and clearer using:
Phobics = ["ALA", "CYS", ...]
IsPhobic = [Res in Phobics for Res in ResNames]
These hints aren't to curb your programming style, but rather help you take advantage of Python's programming features so that ultimately you will be able to write code very efficiently. Moreover, when you take advantage of Python's and NumPy's built-in routines and syntax, your code actually executes much faster.

Hope that helps as you are beginning Exercise 2.

Cheers,
MSS

No comments: