beginner in python

Andy Cheesman Andy.cheesman at bristol.ac.uk
Thu Aug 2 18:16:15 CEST 2007


It looks like you are doing some sort of molecular science thing.
Have you come across openbabel and the python bindings pybel
(http://openbabel.sourceforge.net/wiki/Python#Pybel). I don't know how
well they work but they might save you some effort. Also, check out
pymol,(http://pymol.sourceforge.net/) they might have some of the
functionality which you desire already coded!

HTH
Andy

Steve Holden wrote:
> Beema shafreen wrote:
>> hi everybody,
>>    I am beginner in python
>>  I have to calculate the euclidean distance between the atoms from a pdb 
>> file
>> i have written the the code and its shows me some error ,
>> the code:
>> import re
>> import string
>> import math
>> ab =[]
>> x_value = []
>> y_value = []
>> z_value = []
>> fh = open("1K5N.pdb",'r')
>> for atom in fh.readlines():
>>    a = atom.strip()
>>    pattern= re.compile('^ATOM.*')
>>    atom_file= pattern.search(a)
>>    if  atom_file:
>>        atom_data = atom_file.group()
>>        x_coordinate = atom_data[31:38]
>>        y_coordinate = atom_data[39:46]
>>        z_coordinate = atom_data[47:54]
>>        x_value.append(x_coordinate)
>>        y_value.append(y_coordinate)
>>        z_value.append(z_coordinate)
>> for x in range(len(x_value)):
>>              x_co = float(x_value[x])-float(x_value[x+1])
>>              y_co = float(y_value[x])-float(y_value[x+1])
>>              z_co = float(z_value[x])-float(z_value[x+1])
>>              data = math.sqrt(x_co)*(x_co)+(y_co)*(y_co)+(z_co)*(z_co)
>>              print data
>> ~
>> and the error ,message
>>  File "pdb_fetching.py", line 22, in ?
>>     x_co = float(x_value[x])-float(x_value[x+1])
>> IndexError: list index out of range
>>
>>
>> can you suggest me the mistake i have made
>>
> suppose you have an x_value list of length 6. Valid indexes go from 0 to 
> 5. Then x is going to start at 0 and go up to 5. The last time around 
> the loop the expression "x_value[x+1]" is going to try and use 6 as an 
> index, thus trying to address past the end of the list.
> 
> Since the data values are the RMS differences between successive points, 
> there are only five differences for a six-element list.
> 
> Try using
> 
>      for x in range(len(x_value)-1):
> 
> instead.
> 
> By the way, you presented your question very well - all necessary 
> information was there, and you didn't put in any mistaken guesses about 
> what might be going wrong. Well done, and welcome to Python! You will 
> find you can learn it very quickly.
> 
> regards
>   Steve



More information about the Python-list mailing list