2

I am working on a secondary project that is scouring the protein data bank for peptides matching specific conditions.

I have a folder containing a large portion of these .pdb files and my game plan is to set this file on the desktop, then use a for-loop to scan through all the files within this folder and bank all necessary data. I am stalled however at the import stage. The file/directory is not being recognized. I am attaching the offending code below:

import os
# - - - - -
#Sector C - Iteration through a folder containing all .pdb files. 
for fi in os.listdir('C:/Users/David/Desktop/pdb_Files'):
    if os.path.isfile(os.path.join('C:/Users/David/Desktop/pdb_Files',  fi)):        
    listatom,R3_coordinates,posg=[],[],[]
    for line in open(fi): # < - - - Issue occurring here.
        ist = line.split()
        id = ist[0]
        if id == 'ATOM':
            typ = ist[2]
            if Peptide1 == 'PRO':
                if typ == 'CA':
                    res,toc,ac=ist[3:6]
                    pos = [float(i) for i in ist[6:9]]
                    if ac >= '0':
                        listatom.append([int(ist[1]),typ,res,toc,ac,np.array(pos)])
                        R3_coordinates.append([int(ist[1]),np.array(pos)]) #List of ALL coordinates.
                    if Plot == True:
                        posg.append(pos)

All help appreciated.

James
  • 274
  • 4
  • 12
  • Possible duplicate of [Looping through a directory of files in Python](http://stackoverflow.com/questions/13480316/looping-through-a-directory-of-files-in-python) – SparkAndShine May 14 '16 at 02:59
  • Possible duplicate of [How to list all files of a directory in Python](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python) – Rushy Panchal May 14 '16 at 03:17

2 Answers2

2

This is python. Before you write something to grab all the files in a directory, think if that is a common problem that others have dealt with before.

import glob
print glob.glob("C:/Users/David/Desktop/pdb_Files/*.pdb")
#this returns ["C:/Users/David/Desktop/pdb_Files/file1.pdb", "C:/Users/David/Desktop/pdb_Files/file2.pdb", ...]

The glob module lets you wildcard match on files in a directory, so the above line with return a list of all files in the pdb_Files folder that end with .pdb.

gnicholas
  • 2,041
  • 1
  • 21
  • 32
  • I will investigate this module more, this may be of use for both this project and another I am working on. – James May 14 '16 at 02:53
1

You need to provide the full path to open:

path = os.path.join('C:/Users/David/Desktop/pdb_Files',  fi)
if os.path.isfile(path):        
    listatom,R3_coordinates,posg=[],[],[]
    for line in open(path):
niemmi
  • 17,113
  • 7
  • 35
  • 42