Siva Shanmugam
Siva Shanmugam

Reputation: 658

Getting only one result instead of multiple results

Program

f=open('something.txt'.'r')
lines=f.readlines()
for read in range(0,len(lines)):
    c=lines[read]
    d=lines[read]
    if c[0:4]=='ATOM':
        AX1=float(c[32:38])
        AY1=float(c[40:46])
        AZ1=float(c[48:54])
        A1=c[77]
    if d[0:6]=='HETATM':
        HX1=float(d[32:38])
        HY1=float(d[40:46])
        HZ1=float(d[48:54])
        H1=d[77]
    distance=math.sqrt((HX1-AX1)**2+(HY1-AY1)**2+(HZ1-AZ1)**2)
    print 'Distance b/w ',A1,' and ',H1,'',distance

file will be like:

ATOM      1  N   ALA A   4      15.660  74.786  38.080  1.00 29.54           N  
ATOM      2  CA  ALA A   4      15.546  74.666  39.526  1.00 27.64           C  
ATOM      3  C   ALA A   4      16.833  74.137  40.145  1.00 25.88           C  
ATOM      4  O   ALA A   4      17.852  73.890  39.500  1.00 24.05           O  
.
.
.
ATOM   6048  OXT ALA A 780      21.816  67.407  16.290  1.00 13.41           O  

HETATM 6050  C1  GLC A2002       5.415  71.753  22.098  1.00 21.40           C  
HETATM 6051  C2  GLC A2002       3.948  71.252  22.308  1.00 21.24           C  
HETATM 6052  C3  GLC A2002       3.065  71.966  21.254  1.00 20.81           C  
HETATM 6053  C4  GLC A2002       3.266  73.512  21.425  1.00 20.10           C  
HETATM 6054  C5  GLC A2002       4.737  73.893  21.253  1.00 21.47           C  
HETATM 6055  C6  GLC A2002       4.967  75.396  21.466  1.00 22.53           C  
HETATM 6056  O1  GLC A2002       6.224  71.231  23.190  1.00 22.04           O  
HETATM 6057  O2  GLC A2002       3.902  69.831  22.018  1.00 21.46           O  
HETATM 6058  O3  GLC A2002       1.682  71.659  21.493  1.00 21.68           O  
HETATM 6059  O4  GLC A2002       2.509  74.212  20.404  1.00 17.87           O  
HETATM 6060  O5  GLC A2002       5.516  73.179  22.244  1.00 21.76           O  
HETATM 6061  O6  GLC A2002       6.361  75.746  21.230  1.00 24.18           O  

I want to calculate values using the above mentioned formula. It should compare the ATOM-1 to all HETATM and ATOM-2 to all HETATM and goes on.

But i am getting result for only last ATOM and all HETATM

Result

Distance b/w  O  and  C  17.9335824921
Distance b/w  O  and  C  19.2422912617
Distance b/w  O  and  C  19.925505715
Distance b/w  O  and  C  20.1926162247
Distance b/w  O  and  C  18.9312388924
Distance b/w  O  and  C  19.3521031932
Distance b/w  O  and  O  17.4740790888
Distance b/w  O  and  O  18.963047118
Distance b/w  O  and  O  21.2256606258
Distance b/w  O  and  O  20.8804518629
Distance b/w  O  and  O  18.2881409662
Distance b/w  O  and  O  18.2427943583

Upvotes: 0

Views: 84

Answers (1)

mhlester
mhlester

Reputation: 23231

The issue is with this block here:

lines=f.readlines()
for read in range(0,len(lines)):
    c=lines[read]
    d=lines[read]

You're only looping once, and assigning c and d to the same line at a time.

Instead, you want to loop once for c, and once for d. The more pythonic way to do this is looping directly over lines, instead of range(0, len(lines)).

lines=f.readlines()
for c in lines:
    for d in lines:
        # the rest of your code

Also, as pointed out by Ryan, you aren't doing anything with A1 and H1 when there's not a match, so you may inadvertently compare old data from a previous loop. Be sure to skip those cases, perhaps with a continue in an else statement after the failed if:

if c[0:4]=='ATOM':
    AX1=float(c[32:38])
    AY1=float(c[40:46])
    AZ1=float(c[48:54])
    A1=c[77]
else:
    continue

Upvotes: 2

Related Questions