<p>OK given , <code>input_connect.pdb</code> , using <strong>Biopython</strong> library</p>
<pre><code>TITLE     sistema05
REMARK   4      COMPLIES WITH FORMAT V. 3.0, 1-DEC-2006
REMARK 888
REMARK 888 WRITTEN BY MAESTRO (A PRODUCT OF SCHRODINGER, LLC)
HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C  
HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  
HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C  
HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  
HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C  
HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  
HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C  
HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  
HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C  
HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  
HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C  
HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  
HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C  
HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  
HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C  
HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  
HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C  
HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  
HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C  
HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C
CONECT    1  212    2   20
CONECT    1   20
CONECT    2    1    3   21
CONECT    2   21
CONECT    3    2  213    4
CONECT    3    4
CONECT    4    3    5   23
CONECT    4    3
CONECT    5    4  214    6
CONECT    5    6
CONECT    6    5    7   25
CONECT    6    5
CONECT    7    6  215    8
CONECT    7    8
CONECT    8    7    9   27
CONECT    8    7
CONECT    9    8  216   10
CONECT   10    9   11   29
CONECT   11   10  217   12
CONECT   12   11   13   31
CONECT   13   12  218   14
CONECT   14   13   15   33
CONECT   14   15
CONECT   15   14  219   16
CONECT   15   14
CONECT   16   15   17   35
CONECT   16   17
CONECT   17   16  220   18
CONECT   17   16
CONECT   18   17   19   37
CONECT   18   19
CONECT   19   18  221   20
CONECT   19   18
CONECT   20    1   19   39
CONECT   21    2   22   40
</code></pre>
<p>and charges list <code>input_charge.txt</code> :</p>
<pre><code>-0.02474906
 0.02291960
-0.02789302
 0.02227110
-0.02831805
 0.02230034
-0.02876149
 0.02225527
-0.02774076
 0.02216971
-0.02074528
 0.02275309
-0.02662750
 0.02177531
-0.03007329
 0.02283631
-0.02779055
 0.02312780
-0.02312680
 0.02315866
</code></pre>
<p>with code :</p>
<pre><code>import Bio
print('########## Biopython  VERSION ##########################################')
print('\n\n BIOPYTHON_VERSION : ', Bio.__version__)
print('###################################################################')

from Bio.PDB import (
                    PDBParser,
                    Atom,
                    
                    )
                    
                    # PDBIO)

import PDBIOcopy

# from Bio.PDB import  PDBIO 

pdb = 'input'

charges = 'input_charge.txt'

charges_list = []

with open(charges , 'r') as handle :
    
    for i in handle.readlines() :

        charges_list.append(i.strip())
        
print(charges_list)

parser = PDBParser(PERMISSIVE=True , QUIET = False)


structure = parser.get_structure(pdb, pdb+'.pdb')


atoms = structure.get_atoms()


idx = 0

for atom in atoms :
    
    print(atom)
    
    atom.set_charge(charges_list[idx])
    
    idx += 1
    

atoms = structure.get_atoms()

for atom in atoms :
    
    print(atom , atom.get_charge())
    

struct_to_save = PDBIOcopy.PDBIO()

# struct_to_save = PDBIO()

struct_to_save.set_structure(structure)

struct_to_save.save('result_input.pdb')
</code></pre>
<p>and modifying <code>Bio.PDB.PDBIO</code> as :</p>
<p>line 22 :</p>
<pre><code>_ATOM_FORMAT_STRING = (
    &quot;%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%s%6.2f      %4s%2s%12s\n&quot;
)  
</code></pre>
<p>line 164 :</p>
<pre><code># charge=&quot;  &quot;, #deleted
pqr_charge     # insertes
</code></pre>
<p>line 213:</p>
<pre><code>charge = atom.pqr_charge  #inserted
</code></pre>
<p>line 389:</p>
<pre><code>atom.pqr_charge,  #inserted
</code></pre>
<p>output <code>result_input.pdb</code>:</p>
<pre><code>HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C -0.02474906
HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  0.02291960
HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C -0.02789302
HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  0.02227110
HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C -0.02831805
HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  0.02230034
HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C -0.02876149
HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  0.02225527
HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C -0.02774076
HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  0.02216971
HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C -0.02074528
HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  0.02275309
HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C -0.02662750
HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  0.02177531
HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C -0.03007329
HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  0.02283631
HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C -0.02779055
HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  0.02312780
HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C -0.02312680
HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C  0.02315866
TER      21      BAS A   1                                                       
END  
</code></pre>
<p>That unfortunately is not a pdb file format anymore:</p>
<p>PDB Format uses for RECORD ATOM CHARGE columns 79-80 , see <a href="https://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html" rel="nofollow noreferrer">Protein Data Bank Contents Guide: Atomic Coordinate Entry Format Description</a></p>
<p>At the end of all of this I realized that using python and just adding the charge values at the end of the pdb file read line by line would be a lot easier &amp; faster but anyway I had the chance to explore <code>PDBIO</code></p>
<p>If you want to add the charges in the <code>bfactor</code> or <code>occupancy</code> columns just use :<code>atom.set_bfactor(charges_list[idx])</code> or <code>atom.set_occupancy(charges_list[idx])</code> be sure to modify <code>PDBIO</code> to make space for your values</p>
<p>There are other examples on SO about same problem too :</p>
<p><a href="https://stackoverflow.com/questions/53938256/how-to-modify-columns-contents-in-a-protein-pdb-file-by-keys-and-values-stored-i">How to modify columns contents in a protein PDB file by keys and values stored in a text file using only bash tools</a></p>
<p>Just Python code :</p>
<pre><code>pdb = 'input_connect.pdb'

charges = 'input_charge.txt'

charges_list = []

with open(charges , 'r') as handle :
    
    for i in handle.readlines() :

        charges_list.append(i.strip())
        
print(charges_list)



with open('input_connect.pdb' , 'r') as handle :
    
    pdb_file = handle.readlines()
    

print(pdb_file)


idx = 0

with open('result_python.pdb' , 'w') as handle :
    
    for i in pdb_file :
        
        i = i.strip()
        
        if i[0:6] == 'HETATM' : 
            
            i = i +'  '+  charges_list[idx] +'\n'
            
            idx += 1
            
            handle.writelines(i)
            
        else :
            
            i = i + '\n'
            
            handle.writelines(i)
</code></pre>
<p>Am sure there is a bash or perl one liner for the code above.</p>
<p>... <a href="https://stackoverflow.com/questions/16394176/how-to-merge-two-files-consistently-line-by-line">How to merge two files consistently line by line</a></p>

CC BY-SA 4.0
2023-11-09T01:26:18.040Z
2023-11-25T14:09:11.843Z
2023-11-25T14:09:11.843Z
pippo1980
<p>The existing answer works I'm sure, however there are existing tools that do this or something quite similar.</p>
<ol>
<li>OpenBabel is able to <a href="https://open-babel.readthedocs.io/en/latest/Command-line_tools/babel.html#options" rel="nofollow noreferrer">add charges</a> using the option <code>--partialcharge</code>, though the <a href="https://open-babel.readthedocs.io/en/latest/Charges/charges.html?highlight=gasteiger" rel="nofollow noreferrer">docs are not impressive</a>.</li>
<li><a href="https://open-babel.readthedocs.io/en/latest/Charges/charges.html?highlight=gasteiger" rel="nofollow noreferrer">Autodock vina</a> has a utility script that adds charges and extends the pdb to pdbqt, which is admittedly not exactly what you want.</li>
</ol>
<p>From the comments: @pippo1980 suggests this page which shows how to use the openbabel Python API to set charges</p>
<p>Openbabel models <strong>(note that it includes <code>fromfile</code> meaning you can supply your own)</strong>:</p>
<pre><code>obabel -L charges
eem    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Bultinck B3LYP/6-31G*/MPA
eem2015ba    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/AIM
eem2015bm    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/MPA
eem2015bn    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/NPA
eem2015ha    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/AIM
eem2015hm    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/MPA
eem2015hn    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/NPA
eqeq    Assign EQEq (charge equilibration) partial charges.
fromfile    Assign charges from file containing {'atom-name', charge} pairs
gasteiger    Assign Gasteiger-Marsili sigma partial charges
mmff94       Assign MMFF94 partial charges
none    Clear all partial charges
qeq    Assign QEq (charge equilibration) partial charges (Rappe and Goddard, 1991)
qtpie    Assign QTPIE (charge transfer, polarization and equilibration) partial charges (Chen and Martinez, 2007)
</code></pre>

CC BY-SA 4.0
2023-11-27T17:52:08.313Z
The issue here is that the charges were already calculated by other software (using a semi-empirical approach that is much better than force-field).
Camps
CC BY-SA 4.0
2023-11-27T17:54:36.500Z
@Camps the indicated `fromfile` approach using openbabel should accommodate inputs of the sort that you indicate in your question. Maybe I am misunderstanding that you have separately computed some estimates that need to be added to a PDB file.
Maximilian Press
CC BY-SA 4.0
2023-11-27T18:33:35.287Z
nice use of obabel here too https://leela-dodda-yale.medium.com/atomic-charges-from-openbabel-8943394f2d62
pippo1980
CC BY-SA 4.0
2023-11-27T15:10:43.197Z
2023-11-27T19:54:06.120Z
2023-11-27T19:54:06.120Z
Maximilian Press
<p>I did some calculations using a PDB file as input. As a result, I got a file with the atomic partial charges (only the values in one column, no other information) and the output PDB file structure without the charge information.</p>
<p>My question: Is there a tool to add the charges to the output PDB file using the separate file?</p>
<p>My PDB file:</p>
<pre><code>    TITLE     sistema05
    REMARK   4      COMPLIES WITH FORMAT V. 3.0, 1-DEC-2006
    REMARK 888
    REMARK 888 WRITTEN BY MAESTRO (A PRODUCT OF SCHRODINGER, LLC)
    HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C  
    HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  
    HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C  
    HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  
    HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C  
    HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  
    HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C  
    HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  
    HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C  
    HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  
    HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C  
    HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  
    HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C  
    HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  
    HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C  
    HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  
    HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C  
    HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  
    HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C  
    HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C  
...
...
...  
    CONECT    1  212    2   20
    CONECT    1   20
    CONECT    2    1    3   21
    CONECT    2   21
    CONECT    3    2  213    4
    CONECT    3    4
    CONECT    4    3    5   23
    CONECT    4    3
    CONECT    5    4  214    6
    CONECT    5    6
    CONECT    6    5    7   25
    CONECT    6    5
    CONECT    7    6  215    8
    CONECT    7    8
    CONECT    8    7    9   27
    CONECT    8    7
    CONECT    9    8  216   10
    CONECT   10    9   11   29
    CONECT   11   10  217   12
    CONECT   12   11   13   31
    CONECT   13   12  218   14
    CONECT   14   13   15   33
    CONECT   14   15
    CONECT   15   14  219   16
    CONECT   15   14
    CONECT   16   15   17   35
    CONECT   16   17
    CONECT   17   16  220   18
    CONECT   17   16
    CONECT   18   17   19   37
    CONECT   18   19
    CONECT   19   18  221   20
    CONECT   19   18
    CONECT   20    1   19   39
    CONECT   20    1
    CONECT   21    2   22   40
    END   
</code></pre>
<p>My CHARGE file:</p>
<pre><code>   -0.02474906
    0.02291960
   -0.02789302
    0.02227110
   -0.02831805
    0.02230034
   -0.02876149
    0.02225527
   -0.02774076
    0.02216971
   -0.02074528
    0.02275309
   -0.02662750
    0.02177531
   -0.03007329
    0.02283631
   -0.02779055
    0.02312780
   -0.02312680
    0.02315866   
</code></pre>

CC BY-SA 4.0
2023-11-08T20:26:49.540Z
You could use the b factor or occupancy value column to store your charge values, dont remember how many digits are available for each of the 2 proprierties. Parse a pdb file with biopython and change it with your charge files, I assume the pdb doesnt have any header or remark just atoms from lines 1 onwards that relate to the list of charges one per line in the second file ?? A small snippet of both files would be of help for anybody interested in helping you out
pippo1980
CC BY-SA 4.0
2023-11-08T20:40:20.480Z
See https://stackoverflow.com/questions/53938256/how-to-modify-columns-contents-in-a-protein-pdb-file-by-keys-and-values-stored-i
pippo1980
CC BY-SA 4.0
2023-11-08T20:43:35.437Z
Sorry just realized pdb has column for that https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/tutorials/pdbintro.html , column 79-80
pippo1980
CC BY-SA 4.0
2023-11-08T20:44:56.677Z
Could use biopython https://biopython.org/docs/1.79/api/Bio.PDB.Atom.html and set the charge for each atom   atom.set_charge( CHARGE) should do
pippo1980
CC BY-SA 4.0
2023-11-08T20:57:57.310Z
Thanks @pippo1980. I edited the post, adding part of the files. I will go through your suggestions.
Camps
CC BY-SA 4.0
2023-11-08T23:24:02.127Z
Mmmh your charges are a lot of digits... . You'll need a custom made pdb thats not a pdb format anymore to store all that and a parser to be able to reload that pdb, are you sure there are not other more suitable formats
pippo1980
<pdb>
How to add charges to a PDB file?
# How to add charges to a PDB file?

(Asked by: username_0 on 2023-11-08)

I did some calculations using a PDB file as input. As a result, I got a file with the atomic partial charges (only the values in one column, no other information) and the output PDB file structure without the charge information.
My question: Is there a tool to add the charges to the output PDB file using the separate file?
My PDB file:
    TITLE     sistema05
    REMARK   4      COMPLIES WITH FORMAT V. 3.0, 1-DEC-2006
    REMARK 888
    REMARK 888 WRITTEN BY MAESTRO (A PRODUCT OF SCHRODINGER, LLC)
    HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C  
    HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  
    HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C  
    HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  
    HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C  
    HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  
    HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C  
    HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  
    HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C  
    HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  
    HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C  
    HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  
    HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C  
    HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  
    HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C  
    HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  
    HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C  
    HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  
    HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C  
    HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C  
...
...
...  
    CONECT    1  212    2   20
    CONECT    1   20
    CONECT    2    1    3   21
    CONECT    2   21
    CONECT    3    2  213    4
    CONECT    3    4
    CONECT    4    3    5   23
    CONECT    4    3
    CONECT    5    4  214    6
    CONECT    5    6
    CONECT    6    5    7   25
    CONECT    6    5
    CONECT    7    6  215    8
    CONECT    7    8
    CONECT    8    7    9   27
    CONECT    8    7
    CONECT    9    8  216   10
    CONECT   10    9   11   29
    CONECT   11   10  217   12
    CONECT   12   11   13   31
    CONECT   13   12  218   14
    CONECT   14   13   15   33
    CONECT   14   15
    CONECT   15   14  219   16
    CONECT   15   14
    CONECT   16   15   17   35
    CONECT   16   17
    CONECT   17   16  220   18
    CONECT   17   16
    CONECT   18   17   19   37
    CONECT   18   19
    CONECT   19   18  221   20
    CONECT   19   18
    CONECT   20    1   19   39
    CONECT   20    1
    CONECT   21    2   22   40
    END   

My CHARGE file:
   -0.02474906
    0.02291960
   -0.02789302
    0.02227110
   -0.02831805
    0.02230034
   -0.02876149
    0.02225527
   -0.02774076
    0.02216971
   -0.02074528
    0.02275309
   -0.02662750
    0.02177531
   -0.03007329
    0.02283631
   -0.02779055
    0.02312780
   -0.02312680
    0.02315866

**Tags:** pdb
> **Comments:**
> **username_1**: You could use the b factor or occupancy value column to store your charge values, dont remember how many digits are available for each of the 2 proprierties. Parse a pdb file with biopython and change it with your charge files, I assume the pdb doesnt have any header or remark just atoms from lines 1 onwards that relate to the list of charges one per line in the second file ?? A small snippet of both files would be of help for anybody interested in helping you out  
> **username_1**: See https://stackoverflow.com/questions/53938256/how-to-modify-columns-contents-in-a-protein-pdb-file-by-keys-and-values-stored-i  
> **username_1**: Sorry just realized pdb has column for that https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/tutorials/pdbintro.html , column 79-80  
> **username_1**: Could use biopython https://biopython.org/docs/1.79/api/Bio.PDB.Atom.html and set the charge for each atom   atom.set_charge( CHARGE) should do  
> **username_0**: Thanks @pippo1980. I edited the post, adding part of the files. I will go through your suggestions.  
> **username_1**: Mmmh your charges are a lot of digits... . You'll need a custom made pdb thats not a pdb format anymore to store all that and a parser to be able to reload that pdb, are you sure there are not other more suitable formats  

---

## 2 answers

### Answer by username_1 on 2023-11-09. Score: 3 | Accepted answer

OK given , input_connect.pdb , using Biopython library
TITLE     sistema05
REMARK   4      COMPLIES WITH FORMAT V. 3.0, 1-DEC-2006
REMARK 888
REMARK 888 WRITTEN BY MAESTRO (A PRODUCT OF SCHRODINGER, LLC)
HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C  
HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  
HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C  
HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  
HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C  
HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  
HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C  
HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  
HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C  
HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  
HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C  
HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  
HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C  
HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  
HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C  
HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  
HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C  
HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  
HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C  
HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C
CONECT    1  212    2   20
CONECT    1   20
CONECT    2    1    3   21
CONECT    2   21
CONECT    3    2  213    4
CONECT    3    4
CONECT    4    3    5   23
CONECT    4    3
CONECT    5    4  214    6
CONECT    5    6
CONECT    6    5    7   25
CONECT    6    5
CONECT    7    6  215    8
CONECT    7    8
CONECT    8    7    9   27
CONECT    8    7
CONECT    9    8  216   10
CONECT   10    9   11   29
CONECT   11   10  217   12
CONECT   12   11   13   31
CONECT   13   12  218   14
CONECT   14   13   15   33
CONECT   14   15
CONECT   15   14  219   16
CONECT   15   14
CONECT   16   15   17   35
CONECT   16   17
CONECT   17   16  220   18
CONECT   17   16
CONECT   18   17   19   37
CONECT   18   19
CONECT   19   18  221   20
CONECT   19   18
CONECT   20    1   19   39
CONECT   21    2   22   40

and charges list input_charge.txt :
-0.02474906
 0.02291960
-0.02789302
 0.02227110
-0.02831805
 0.02230034
-0.02876149
 0.02225527
-0.02774076
 0.02216971
-0.02074528
 0.02275309
-0.02662750
 0.02177531
-0.03007329
 0.02283631
-0.02779055
 0.02312780
-0.02312680
 0.02315866

with code :
import Bio
print('########## Biopython  VERSION ##########################################')
print('\n\n BIOPYTHON_VERSION : ', Bio.__version__)
print('###################################################################')

from Bio.PDB import (
                    PDBParser,
                    Atom,
                    
                    )
                    
                    # PDBIO)

import PDBIOcopy

# from Bio.PDB import  PDBIO 

pdb = 'input'

charges = 'input_charge.txt'

charges_list = []

with open(charges , 'r') as handle :
    
    for i in handle.readlines() :

        charges_list.append(i.strip())
        
print(charges_list)

parser = PDBParser(PERMISSIVE=True , QUIET = False)


structure = parser.get_structure(pdb, pdb+'.pdb')


atoms = structure.get_atoms()


idx = 0

for atom in atoms :
    
    print(atom)
    
    atom.set_charge(charges_list[idx])
    
    idx += 1
    

atoms = structure.get_atoms()

for atom in atoms :
    
    print(atom , atom.get_charge())
    

struct_to_save = PDBIOcopy.PDBIO()

# struct_to_save = PDBIO()

struct_to_save.set_structure(structure)

struct_to_save.save('result_input.pdb')

and modifying Bio.PDB.PDBIO as :
line 22 :
_ATOM_FORMAT_STRING = (
    "%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%s%6.2f      %4s%2s%12s\n"
)  

line 164 :
# charge="  ", #deleted
pqr_charge     # insertes

line 213:
charge = atom.pqr_charge  #inserted

line 389:
atom.pqr_charge,  #inserted

output result_input.pdb:
HETATM    1  C1  BAS A   1      10.151  -3.071  -1.881  1.00  0.00           C -0.02474906
HETATM    2  C2  BAS A   1       9.389  -3.683  -0.874  1.00  0.00           C  0.02291960
HETATM    3  C3  BAS A   1       9.976  -3.936   0.382  1.00  0.00           C -0.02789302
HETATM    4  C4  BAS A   1       9.194  -3.742   1.530  1.00  0.00           C  0.02227110
HETATM    5  C5  BAS A   1       9.781  -3.201   2.695  1.00  0.00           C -0.02831805
HETATM    6  C6  BAS A   1       9.029  -2.357   3.510  1.00  0.00           C  0.02230034
HETATM    7  C7  BAS A   1       9.651  -1.313   4.236  1.00  0.00           C -0.02876149
HETATM    8  C8  BAS A   1       8.948  -0.139   4.488  1.00  0.00           C  0.02225527
HETATM    9  C9  BAS A   1       9.635   1.100   4.502  1.00  0.00           C -0.02774076
HETATM   10  C10 BAS A   1       9.001   2.259   4.094  1.00  0.00           C  0.02216971
HETATM   11  C11 BAS A   1       9.724   3.177   3.329  1.00  0.00           C -0.02074528
HETATM   12  C12 BAS A   1       9.148   3.815   2.228  1.00  0.00           C  0.02275309
HETATM   13  C13 BAS A   1       9.909   3.945   1.081  1.00  0.00           C -0.02662750
HETATM   14  C14 BAS A   1       9.332   3.754  -0.198  1.00  0.00           C  0.02177531
HETATM   15  C15 BAS A   1      10.098   3.266  -1.245  1.00  0.00           C -0.03007329
HETATM   16  C16 BAS A   1       9.501   2.445  -2.232  1.00  0.00           C  0.02283631
HETATM   17  C17 BAS A   1      10.234   1.431  -2.847  1.00  0.00           C -0.02779055
HETATM   18  C18 BAS A   1       9.575   0.238  -3.215  1.00  0.00           C  0.02312780
HETATM   19  C19 BAS A   1      10.249  -0.986  -3.121  1.00  0.00           C -0.02312680
HETATM   20  C20 BAS A   1       9.529  -2.124  -2.713  1.00  0.00           C  0.02315866
TER      21      BAS A   1                                                       
END  

That unfortunately is not a pdb file format anymore:
PDB Format uses for RECORD ATOM CHARGE columns 79-80 , see Protein Data Bank Contents Guide: Atomic Coordinate Entry Format Description
At the end of all of this I realized that using python and just adding the charge values at the end of the pdb file read line by line would be a lot easier & faster but anyway I had the chance to explore PDBIO
If you want to add the charges in the bfactor or occupancy columns just use :atom.set_bfactor(charges_list[idx]) or atom.set_occupancy(charges_list[idx]) be sure to modify PDBIO to make space for your values
There are other examples on SO about same problem too :
How to modify columns contents in a protein PDB file by keys and values stored in a text file using only bash tools
Just Python code :
pdb = 'input_connect.pdb'

charges = 'input_charge.txt'

charges_list = []

with open(charges , 'r') as handle :
    
    for i in handle.readlines() :

        charges_list.append(i.strip())
        
print(charges_list)



with open('input_connect.pdb' , 'r') as handle :
    
    pdb_file = handle.readlines()
    

print(pdb_file)


idx = 0

with open('result_python.pdb' , 'w') as handle :
    
    for i in pdb_file :
        
        i = i.strip()
        
        if i[0:6] == 'HETATM' : 
            
            i = i +'  '+  charges_list[idx] +'\n'
            
            idx += 1
            
            handle.writelines(i)
            
        else :
            
            i = i + '\n'
            
            handle.writelines(i)

Am sure there is a bash or perl one liner for the code above.
... How to merge two files consistently line by line


---

### Answer by username_2 on 2023-11-27. Score: 1

The existing answer works I'm sure, however there are existing tools that do this or something quite similar.

OpenBabel is able to add charges using the option --partialcharge, though the docs are not impressive.
Autodock vina has a utility script that adds charges and extends the pdb to pdbqt, which is admittedly not exactly what you want.

From the comments: @pippo1980 suggests this page which shows how to use the openbabel Python API to set charges
Openbabel models (note that it includes fromfile meaning you can supply your own):
obabel -L charges
eem    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Bultinck B3LYP/6-31G*/MPA
eem2015ba    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/AIM
eem2015bm    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/MPA
eem2015bn    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf B3LYP/6-311G/NPA
eem2015ha    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/AIM
eem2015hm    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/MPA
eem2015hn    Assign Electronegativity Equilization Method (EEM) atomic partial charges. Cheminf HF/6-311G/NPA
eqeq    Assign EQEq (charge equilibration) partial charges.
fromfile    Assign charges from file containing {'atom-name', charge} pairs
gasteiger    Assign Gasteiger-Marsili sigma partial charges
mmff94       Assign MMFF94 partial charges
none    Clear all partial charges
qeq    Assign QEq (charge equilibration) partial charges (Rappe and Goddard, 1991)
qtpie    Assign QTPIE (charge transfer, polarization and equilibration) partial charges (Chen and Martinez, 2007)

> **Comments:**
> **username_0**: The issue here is that the charges were already calculated by other software (using a semi-empirical approach that is much better than force-field).  
> **username_2**: @Camps the indicated `fromfile` approach using openbabel should accommodate inputs of the sort that you indicate in your question. Maybe I am misunderstanding that you have separately computed some estimates that need to be added to a PDB file.  
> **username_1**: nice use of obabel here too https://leela-dodda-yale.medium.com/atomic-charges-from-openbabel-8943394f2d62  

---

