Here is my code:
def read_Coordinates_Atoms2(fileName, only_CA = True):
'''
in : PDB file
out : matrix with coordinates of atoms
'''
with open(fileName, 'r') as infile:
for line in infile :
if only_CA == True :
if line.startswith('ATOM') and line[13:15] == 'CA':
try: # matrix fill-up
CoordAtoms = np.vstack([CoordAtoms, [float(line[30:38]), float(line[38:46]), float(line[46:54])]]) # np.append
except NameError: # matrix declaration
CoordAtoms = np.array([[line[30:38],line[38:46], line[46:54]]], float)
else :
if line.startswith('ATOM'):
try: # matrix fill-up
CoordAtoms = np.vstack([CoordAtoms, [float(line[30:38]), float(line[38:46]), float(line[46:54])]]) # np.append
except NameError: # matrix declaration
CoordAtoms = np.array([[line[30:38],line[38:46], line[46:54]]], float)
return CoordAtoms
Is there a more efficient way to do this ? I mean, a way where I don't have to write twice the same lines? I think the code should look more like this :
def foo(file, condition2 = True): if condition1 and condition2 : # do lots of instructions elif condition1 : # do the same lots of instructions (but different output)