I trying to print sequence but getting KeyError? in python
<p>I am getting only first input file correct output but I have A , B, C, D chain in my first file and getting right output, in this case in second input file does't have c and d chain in this case, our code is copying same data of first file data of C and D.? and also I am unable to increase this ids: chainIDs = ['A', 'B', 'C', 'D']</p>

<p>I want to search A to Z chain ID, If I replace with chainIDs = ['A', 'B', 'C', 'D'] to chainIDs = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'].</p>

<p>If any chain id is not present in input file then code just ignore and continue for others.</p>

<p>Showing error: </p>

<pre><code>f.write(d[chainID][atomIDs[i+j]]+ '\n')  
KeyError: 'E'
</code></pre>

<p>Script:</p>

<pre><code>import os


d = {}
atomIDs = ['C4B', 'O4B', 'C1B', 'C2B', 'C3B', 'C4B', 'O4B', 'C1B']
chainIDs = ['A', 'B', 'C', 'D', 'E', 'F']
with open('filename.txt') as pdbline:
    for line in pdbline:
        filenames=line[:8]        
        with open(filenames) as pdbfile:
            for line in map(str.rstrip, pdbfile):                
                if line[:6] != "HETATM":
                    continue
                chainID = line[21:22]
                atomID = line[13:16].strip()
                if chainID not in chainIDs:
                    continue
                if atomID not in atomIDs:
                    continue
                try:
                    d[chainID][atomID] = line
                except KeyError:
                    d[chainID] = {atomID: line}
        n = 4
        for chainID in chainIDs:
            for i in range(len(atomIDs)-n+1):
                for j in range(n):
                    f = open(filenames+'out.pdb', 'a')
                    f.write(d[chainID][atomIDs[i+j]]+ '\n')  
                    f.close()
</code></pre>

<p>First Input File:</p>

<pre><code>HETATM15207  C4B NAD A 501      47.266 101.038   7.214  1.00 11.48           C  
HETATM15208  O4B NAD A 501      46.466 100.713   8.371  1.00 11.48           O  
HETATM15209  C3B NAD A 501      47.659  99.689   6.567  1.00 11.48           C  
HETATM15211  C2B NAD A 501      46.447  98.835   6.988  1.00 11.48           C  
HETATM15213  C1B NAD A 501      46.221  99.300   8.426  1.00 11.48           C  
HETATM15252  C4B NAD B 501      36.455 115.053  36.671  1.00 11.25           C  
HETATM15253  O4B NAD B 501      35.930 114.469  35.492  1.00 11.25           O  
HETATM15254  C3B NAD B 501      35.307 115.837  37.367  1.00 11.25           C  
HETATM15256  C2B NAD B 501      34.172 114.876  37.039  1.00 11.25           C  
HETATM15258  C1B NAD B 501      34.524 114.613  35.551  1.00 11.25           C  
HETATM15297  C4B NAD C 501      98.229 130.106  18.332  1.00 12.28           C  
HETATM15298  O4B NAD C 501      98.083 131.545  18.199  1.00 12.28           O  
HETATM15299  C3B NAD C 501      99.346 129.675  17.343  1.00 12.28           C  
HETATM15301  C2B NAD C 501     100.220 130.922  17.375  1.00 12.28           C  
HETATM15303  C1B NAD C 501      99.125 132.008  17.317  1.00 12.28           C  
HETATM15342  C4B NAD D 501      77.335 156.939  25.788  1.00 11.99           C  
HETATM15343  O4B NAD D 501      78.705 156.544  25.901  1.00 11.99           O  
HETATM15344  C3B NAD D 501      77.106 158.059  26.824  1.00 11.99           C  
HETATM15346  C2B NAD D 501      78.536 158.632  26.878  1.00 11.99           C  
HETATM15348  C1B NAD D 501      79.351 157.345  26.900  1.00 11.99           C  
</code></pre>

<p>Second Input File:</p>

<pre><code>HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C  
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O  
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C  
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C  
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C  
</code></pre>

<p>Getting Result for Second File:</p>

<pre><code>HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM15252  C4B NAD B 501      36.455 115.053  36.671  1.00 11.25           C
HETATM15253  O4B NAD B 501      35.930 114.469  35.492  1.00 11.25           O
HETATM15258  C1B NAD B 501      34.524 114.613  35.551  1.00 11.25           C
HETATM15256  C2B NAD B 501      34.172 114.876  37.039  1.00 11.25           C
HETATM15253  O4B NAD B 501      35.930 114.469  35.492  1.00 11.25           O
HETATM15258  C1B NAD B 501      34.524 114.613  35.551  1.00 11.25           C
HETATM15256  C2B NAD B 501      34.172 114.876  37.039  1.00 11.25           C
HETATM15254  C3B NAD B 501      35.307 115.837  37.367  1.00 11.25           C
HETATM15258  C1B NAD B 501      34.524 114.613  35.551  1.00 11.25           C
HETATM15256  C2B NAD B 501      34.172 114.876  37.039  1.00 11.25           C
HETATM15254  C3B NAD B 501      35.307 115.837  37.367  1.00 11.25           C
HETATM15252  C4B NAD B 501      36.455 115.053  36.671  1.00 11.25           C
HETATM15256  C2B NAD B 501      34.172 114.876  37.039  1.00 11.25           C
HETATM15254  C3B NAD B 501      35.307 115.837  37.367  1.00 11.25           C
HETATM15252  C4B NAD B 501      36.455 115.053  36.671  1.00 11.25           C
HETATM15253  O4B NAD B 501      35.930 114.469  35.492  1.00 11.25           O
HETATM15254  C3B NAD B 501      35.307 115.837  37.367  1.00 11.25           C
HETATM15252  C4B NAD B 501      36.455 115.053  36.671  1.00 11.25           C
HETATM15253  O4B NAD B 501      35.930 114.469  35.492  1.00 11.25           O
HETATM15258  C1B NAD B 501      34.524 114.613  35.551  1.00 11.25           C
HETATM15297  C4B NAD C 501      98.229 130.106  18.332  1.00 12.28           C
HETATM15298  O4B NAD C 501      98.083 131.545  18.199  1.00 12.28           O
HETATM15303  C1B NAD C 501      99.125 132.008  17.317  1.00 12.28           C
HETATM15301  C2B NAD C 501     100.220 130.922  17.375  1.00 12.28           C
HETATM15298  O4B NAD C 501      98.083 131.545  18.199  1.00 12.28           O
HETATM15303  C1B NAD C 501      99.125 132.008  17.317  1.00 12.28           C
HETATM15301  C2B NAD C 501     100.220 130.922  17.375  1.00 12.28           C
HETATM15299  C3B NAD C 501      99.346 129.675  17.343  1.00 12.28           C
HETATM15303  C1B NAD C 501      99.125 132.008  17.317  1.00 12.28           C
HETATM15301  C2B NAD C 501     100.220 130.922  17.375  1.00 12.28           C
HETATM15299  C3B NAD C 501      99.346 129.675  17.343  1.00 12.28           C
HETATM15297  C4B NAD C 501      98.229 130.106  18.332  1.00 12.28           C
HETATM15301  C2B NAD C 501     100.220 130.922  17.375  1.00 12.28            C
HETATM15299  C3B NAD C 501      99.346 129.675  17.343  1.00 12.28           C
HETATM15297  C4B NAD C 501      98.229 130.106  18.332  1.00 12.28           C
HETATM15298  O4B NAD C 501      98.083 131.545  18.199  1.00 12.28           O
HETATM15299  C3B NAD C 501      99.346 129.675  17.343  1.00 12.28           C
HETATM15297  C4B NAD C 501      98.229 130.106  18.332  1.00 12.28           C
HETATM15298  O4B NAD C 501      98.083 131.545  18.199  1.00 12.28           O
HETATM15303  C1B NAD C 501      99.125 132.008  17.317  1.00 12.28           C
HETATM15342  C4B NAD D 501      77.335 156.939  25.788  1.00 11.99           C
HETATM15343  O4B NAD D 501      78.705 156.544  25.901  1.00 11.99           O
HETATM15348  C1B NAD D 501      79.351 157.345  26.900  1.00 11.99           C
HETATM15346  C2B NAD D 501      78.536 158.632  26.878  1.00 11.99            C
HETATM15343  O4B NAD D 501      78.705 156.544  25.901  1.00 11.99           O
HETATM15348  C1B NAD D 501      79.351 157.345  26.900  1.00 11.99           C
HETATM15346  C2B NAD D 501      78.536 158.632  26.878  1.00 11.99           C
HETATM15344  C3B NAD D 501      77.106 158.059  26.824  1.00 11.99           C
HETATM15348  C1B NAD D 501      79.351 157.345  26.900  1.00 11.99           C
HETATM15346  C2B NAD D 501      78.536 158.632  26.878  1.00 11.99           C
HETATM15344  C3B NAD D 501      77.106 158.059  26.824  1.00 11.99           C
HETATM15342  C4B NAD D 501      77.335 156.939  25.788  1.00 11.99           C
HETATM15346  C2B NAD D 501      78.536 158.632  26.878  1.00 11.99           C
HETATM15344  C3B NAD D 501      77.106 158.059  26.824  1.00 11.99           C
HETATM15342  C4B NAD D 501      77.335 156.939  25.788  1.00 11.99           C
HETATM15343  O4B NAD D 501      78.705 156.544  25.901  1.00 11.99           O
HETATM15344  C3B NAD D 501      77.106 158.059  26.824  1.00 11.99           C
HETATM15342  C4B NAD D 501      77.335 156.939  25.788  1.00 11.99           C
HETATM15343  O4B NAD D 501      78.705 156.544  25.901  1.00 11.99           O
HETATM15348  C1B NAD D 501      79.351 157.345  26.900  1.00 11.99           C
</code></pre>

<p><strong>Expected Output: I want to print all chain ID in present input file in following sequence (Only present chain id):</strong></p>

<pre><code>HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2475  C2B NAD A 352      91.477  22.027  50.635  1.00 48.07           C
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2473  C3B NAD A 352      90.818  23.341  50.501  1.00 49.46           C
HETATM 2471  C4B NAD A 352      91.432  24.158  51.658  1.00 51.58           C
HETATM 2472  O4B NAD A 352      92.697  23.519  52.005  1.00 47.28           O
HETATM 2477  C1B NAD A 352      92.868  22.416  51.075  1.00 49.66           C
</code></pre>

<p>Just change the output loop to something like this:</p>

<pre><code>for chainID in chainIDs:
    if chainID in d:
        for atom_id in d[chainID]:
            with open(filenames+'out.pdb', 'a') as f:
                f.write(d[chainID][atom_id] + '\n')                  
</code></pre>

python python-2.7 python-3.x bioinformatics