Trouble With Writing A List And Then Reading From It
1
1
Entering edit mode
11.9 years ago
s.charonis ▴ 100

Hello Biostar community,

I have a small issue with a program that reads PDB files, extractes atomic coordinates, and then writes them to a list. I have structured my program with three functions, one for iteratively parsing ~500 PDB-like files from a directory and storing atomic coordinates in a list, one for iteratively appending the list created for each file to the existing one, and one for reading the list. The program looks as follows:

#!/usr/local/bin

import os,string 
from sys import version 

z_coords1 = [] # Global list variable that needs to be known to 3 function 

def serialize_pipeline_model(f): 
""" This function extracts the necessary atomic coords"""" 
....
.... BUNCH OF CODE 
.... code to create z_coords1 
return z_coords1 

import pickle

def write_to_binary(z_coords1):
""" iteratively write successively generated z_coords1 to a binary file """
print '\nPickling z-coordinates list'
f = open("z_coords1.dat", "ab") 
pickle.dump(z_coords1, f) 
f.close() 
return 

def read_from_binary(z_coords1) 
.... SAME AS write_to_binary except that I UNPICKLE the list 

### LOOP OVER DIRECTORY 
for f in os.listdir('/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels/'):
  serialize_pipeline_model(f) 
  write_to_binary(z_coords1)

read_binary(z_coords1) 
print '\n Z-VALUES FOR ALL CHARGED RESIDUES'  
print z_coords1

When I run the program the unpickling returns an empty list. Strangely, when I check the binary file, z_coords1.dat it has been created in my directory, but it isn't being read correctly. Does anyone have any idea as to what is happening?

Many thanks for your time.

Best,
Spyros

python • 2.2k views
ADD COMMENT
0
Entering edit mode

In the serializepipelinemodel function, are you modifying the zcoords1 list? Or is it returning new data. If it is returning new data, it doesn't look like you are placing it anywhere. If you are modifying the zcoords1 list and you are not clearing the list after every file, aren't you outputting incrementally larger files every iteration?

ADD REPLY
0
Entering edit mode

@Dk: I initialize the list at the top because it need to be global and build it at the end of the serializepiplinemodel() function. I don't modify it, just specify how its built (put together previously created lists - the code would be too verbose to post it!). Practically, I need to build 500 lists and merge them into one super-list (hence I write each list to a binary file after its created). Do I need to clear the list after each iteration for this? Sorry perhaps I didn't understand what you wrote, many thanks for your reply! Spyros

ADD REPLY
1
Entering edit mode
11.9 years ago

I think the problem is with zcoords1 arguments for your write/read binary functions. You don't need them since you are just using the global list you've declared. When you declared zcoords1 in those functions as arguments, it supercedes the global and essentially makes them a blank list in the scope of the function.

So just change the two functions so there are no arguments:

def write_to_binary():
def read_from_binary()
ADD COMMENT
0
Entering edit mode

@Dk: Thanks, I see what you mean. I corrected that, and as a diagnostic included a print statement in my first function so that I can see the list that is created for each of the 500 files (=500 lists on the screen). The list still comes back empty, and I'm not sure why? The scopes are fixed now and I know the code inside serializepipelinemodel works.

ADD REPLY

Login before adding your answer.

Traffic: 1942 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6