How to write a .pdb file using Python
1
0
Entering edit mode
2.7 years ago
Jingtong • 0

I want to write a .pdb file using Python with only the ATOM part and only CA atoms. I have the atom number, coordinates, residue number, residue names stored in lists. Right now I could output an 'out.pdb' by getting structure of my input .pdb file and change the coordinates of it. But the 'out.pdb' has two extra columns at the end. How do I get rid of those two column if it's not needed? Also, the TER residue in my output repeats the last residue 'LEU', and my output has an extra line of END. How can I get rid of those, and have the same structure as the input .pdb file?

Or is there any way that could directly write a pdb file with your data stored in lists?

from Bio.PDB import PDBParser, PDBIO
import warnings

warnings.filterwarnings("ignore")

coords_new = [[45.4056827034036, 14.676620893023902, -13.3284724550434], [46.505193096144595, 18.247741627288004, -13.979246299912498], [43.575086267207794, 19.6514943083501, -11.8916310622377], [44.214366767010596, 17.2892618494996, -8.994544378391799], [47.9484897816242, 17.992764350089598, -9.270155594798], [47.3447896246402, 21.742333312746695, -8.9523510727652], [45.19326743506019, 21.1762785646063, -5.879506089713499], [48.1078836572924, 19.3485028504071, -4.256579653994499], [50.299364376255795, 22.4286344474015, -4.7328270720172], [47.5996555158156, 24.8597494278958, -3.5933434084641993], [46.6566408061724, 23.036559732133597, -0.37956796651809765], [50.3127267231322, 22.534299202101103, 0.5193686983682013], [51.6750662533142, 25.9323855258029, -0.5255611273936994], [48.8830110535566, 27.843520824069998, 1.219842731982901], [46.414040312215995, 27.6037649538902, 4.076231116089902], [43.1132198189748, 26.7556036742906, 2.4312713853718], [40.3729670255654, 26.7845341468773, 5.082178146364102], [37.3227356089808, 26.692945118621502, 2.7809276697403007], [36.12161768531799, 25.0966098419658, -0.45247462809499983], [35.463736805233, 28.521013088423, -1.9540168862693992], [39.0853505490242, 29.4677450869832, -1.2542066166540993], [40.180909784916196, 26.384182975218298, -3.159537995535099], [37.7898490777432, 27.0818519218207, -6.0277556889663995], [39.0315598262322, 30.6593041631666, -6.2089265410048995], [42.701812424836795, 29.678477676785096, -6.282331344257899], [42.003056967193196, 27.080131180919498, -8.9781218218126], [40.218858705509994, 29.5967600398635, -11.2145224455278], [43.0742625692808, 32.0936735016348, -10.822128588550301], [45.4660212405862, 29.3068547001557, -11.8209300828722], [43.554637112814994, 29.023294894688, -15.1054643808607], [41.2191629531948, 26.158011564844198, -14.2068354869642], [37.59890096273399, 26.9374039912445, -15.005689394564799], [35.934412570849396, 24.727026358434998, -12.3838026476557], [32.4846085476058, 24.6116296742309, -10.7622882920378], [32.0251893801684, 24.410814084560297, -6.995329687418997], [30.506389520485996, 20.945217834708, -7.6141112475384976], [33.56237742886319, 19.5811150305358, -9.4145421522234], [35.8834512877582, 20.8631237302413, -6.666571112180398], [33.603435628776396, 19.2883099666904, -4.068448866276699], [33.984852161971595, 15.956479185727598, -5.816414848564098], [37.740402834281596, 16.4640054151807, -6.133295383305398], [38.0125649618062, 16.884137451730904, -2.368540032692399], [36.309588462219196, 13.516555425980197, -1.9717714082907976], [38.2958192698716, 11.802737693120001, -4.733026549913899], [41.6307175229218, 12.9506154415838, -3.3245890054266987], [40.380027802848, 12.118003084003696, 0.17665404338250146], [41.5761404242158, 15.474860119008998, 1.4947171844156004], [41.4860878050742, 16.0176403870303, 5.252480496284402], [41.062971764611795, 18.957052598687998, 7.6377045248856], [44.044492153569195, 19.5959292326961, 9.900483307216804], [44.4526400282834, 21.9956018431723, 12.839028640544997], [46.657578648963195, 24.938571696951097, 12.071979673199099], [48.5116619317092, 26.910202291490698, 14.731675892819702], [45.46073009485279, 28.915896361577897, 15.883748035962302], [42.875530179471, 26.135946435991098, 16.1328299668996], [41.2519571574782, 26.828577348087297, 12.801333446783602], [41.3529822027142, 24.0176857229469, 10.242187453963902], [43.0922502850682, 24.135845428116703, 6.884873524319901], [43.1184171973414, 21.4565243542508, 4.204820749358301], [45.5366700543574, 18.5687941814097, 3.951268846325701], [46.1317371854166, 15.437283925607499, 1.8934591788822026], [45.87433681906819, 11.8274826809213, 3.0677189563303013], [49.6606510045644, 12.0261722548139, 3.022537752547003], [49.9275326499694, 14.418967068098897, 5.962679444699701], [52.1904167794538, 12.833466417890996, 8.5619424374808], [50.42230316714719, 15.0807933595054, 11.070724187912601], [46.935870035574595, 13.692416568435995, 11.816649052949801], [45.485596029030596, 10.157712383733696, 11.541824322690804], [48.745152415408796, 8.082194195343504, 12.077572783226602], [46.58675145388239, 5.436869749886895, 13.760671671313302], [44.39795795970059, 4.937775404410701, 10.709807879561302], [47.45212754428739, 4.6684451027455935, 8.451933140695601], [48.95431989499039, 2.1122267812582933, 10.844814883467103], [45.810412844306796, -0.010462862396302341, 10.770067756498802], [45.81958817667219, 0.0901348559949966, 6.967699163902801], [49.522572692781196, -0.7033645680370029, 6.688395858250504], [49.4818299250546, -3.5241600327493003, 9.225146136355999], [46.59775120467219, -5.3784923283457005, 7.543414388196205], [48.592093770220394, -5.168761324310701, 4.325304284833203], [52.185401495993986, -5.988918756560608, 5.301999925207801], [51.90680165526239, -7.803360617237502, 8.658944348924098], [53.3127663836806, -11.351129604774904, 8.713756691092701], [54.136755245321595, -12.234814571809608, 12.316952527654202], [54.781928532996595, -10.73426876051331, 15.743279525873806], [56.96347467985699, -12.179246129198603, 18.494250593367404], [58.60297285790279, -10.887386866439208, 21.650102201276], [60.343974422934, -11.367664533200106, 24.9792245954072], [60.43240940110479, -9.118547761786402, 28.027940719330303], [59.954232350508796, -5.525557115543101, 26.915779793946804], [60.938238168523995, -6.1949098908300115, 23.292608750048203], [58.66651032768959, -6.858157014381895, 20.290084999610308], [59.836416588472396, -8.177801052398102, 16.924928100649], [57.45063987207179, -7.460073544365606, 14.062603925256404], [57.91592244050639, -9.131022815321408, 10.679444652253402], [56.4589064805902, -7.588688542524103, 7.526464080662701], [56.470478006502596, -8.042250142539707, 3.7636265906278012], [59.90924810958519, -7.220653967940507, 2.3139887382380016], [60.8696608047198, -3.5629282313400026, 2.4937577304068004], [57.941352640477405, -2.468316984102003, 4.644565837106902], [59.54237444539599, -2.4382634707892024, 8.093674702945604], [60.6841409295066, 1.1801244573564986, 7.847855466823904], [57.22609880048199, 2.4583501757348003, 6.901204186574202], [55.65709853207939, 0.8879087283291938, 10.0051870825163], [58.469400957631194, 2.169624424999398, 12.187490897559499], [57.7875725569026, 5.7299580864875, 11.089946706883403], [54.0573076430888, 5.354845099201697, 11.771867463700104], [54.669167206808396, 3.905362883637899, 15.213354814023006], [57.0978671839124, 6.775059963017796, 15.8519846706481], [54.3786703411476, 9.295094430032897, 14.858850331843605], [51.8922148009032, 7.742466649599301, 17.3139014151015], [54.141452689027, 8.909527723157403, 20.121643991395402], [52.927728698736, 6.236570473828799, 22.575184212923602], [54.4358946581478, 6.416241712247896, 26.028455804740403], [54.79841676805599, 2.6388110385732944, 26.092808154515602], [57.2273842977152, 2.7459363277503, 23.16731932150501], [60.7315052856096, 4.014236736923202, 23.881489591207494], [61.78223624138499, 3.662588468202692, 20.265908178121602], [61.931661280855394, 1.434115763897296, 17.191241437685505], [64.7470362889774, 0.10942992201219681, 14.995927340980003], [63.86850119069959, -1.0705798993178064, 11.502847481534898], [65.6286010648364, -3.269972668447899, 9.017626187566599], [64.1702141115356, -4.262591788726404, 5.728353961329807], [61.41791253548719, -6.6992182026795035, 6.755135706728507], [61.833499191838996, -6.6186760201591, 10.5195228666387], [60.95306404362739, -4.004089837255698, 13.097163850610901], [62.1844159162402, -4.017162952033097, 16.6756335879979], [59.891662277422796, -2.1921702667203036, 19.076264432127605], [61.5450157808464, -1.3073342859845027, 22.379242101105103], [58.907907432845, -0.7398628877409053, 25.0638519000566], [59.312269114667, 0.8822428435603982, 28.5042840863264], [58.4781793956664, -2.3306632097751034, 30.3820412678934], [57.268662431649794, -5.920069482677995, 29.8050349233391], [53.765127087351196, -4.842723666227606, 30.808279700005595], [53.827694766194, -2.1036697895290075, 28.203199442799097], [55.093379917194596, -4.591654561731097, 25.6140034533193], [52.0336004197014, -6.7519123365833025, 26.212035034671803], [49.711084011758196, -3.7673127649199074, 25.909054951276097], [51.282462824121595, -2.718807360314102, 22.602259063260803], [51.144994026989394, -6.268626375797801, 21.2500356027733], [47.42088370392379, -6.619326172320207, 21.8620126884687], [46.811533692939996, -3.1501582852917025, 20.466120237206994], [48.6171118777578, -4.100785972184504, 17.255061156516298], [47.031656416903594, -7.543945290762906, 16.8769092183303], [43.57589364974479, -6.0503423963074, 17.388775499435905], [44.22720845292879, -3.93621275879881, 14.291314780789403], [44.0808421889588, -7.012090834842795, 12.060064687554103]]
anum = ['2', '6', '15', '26', '36', '44', '53', '61', '72', '81', '89', '97', '105', '111', '119', '127', '135', '144', '151', '160', '168', '177', '185', '192', '200', '211', '219', '230', '239', '244', '248', '259', '267', '274', '281', '290', '295', '302', '309', '315', '326', '334', '342', '351', '360', '368', '377', '385', '392', '401', '408', '415', '423', '428', '436', '440', '451', '463', '472', '484', '490', '498', '505', '511', '519', '528', '539', '550', '558', '565', '573', '582', '591', '599', '608', '619', '624', '632', '639', '647', '654', '665', '673', '682', '690', '698', '702', '709', '713', '721', '729', '737', '744', '752', '763', '770', '778', '785', '789', '797', '802', '812', '817', '825', '829', '836', '844', '852', '860', '868', '876', '884', '898', '906', '915', '923', '930', '934', '941', '949', '955', '959', '967', '975', '982', '988', '996', '1004', '1012', '1018', '1029', '1036', '1043', '1052', '1060', '1065', '1074', '1083', '1090', '1096', '1104', '1113', '1121', '1129', '1135', '1143']

io = PDBIO()
p = PDBParser()
structure = p.get_structure("1b4aA", "1b4aA.pdb") # input files
i = 0
for model in structure:
    for chain in model:
        for residue in chain: 
            for atom in residue:
                atom.set_coord(coords_new[i]) # change coordinates
                i += 1
io.set_structure(structure)
io.save("out.pdb", preserve_atom_numbering = True)

The target output should look something like:

ATOM      2  CA  GLY     1      45.406  14.677 -13.328     
ATOM      6  CA  GLN     2      46.505  18.248 -13.979    

....
ATOM   1143  CA  LEU   146      44.081  -7.012  12.060
TER

But right now I can only make it look like:

ATOM      2  CA  GLY     1      45.406  14.677 -13.328        0.00           C  
ATOM      6  CA  GLN     2      46.505  18.248 -13.979        0.00           C  

....
ATOM    1143  CA  LEU   146      44.081  -7.012  12.060        0.00           C 
TER     1143      LEU   146                                                       
END
python PDB output • 5.9k views
ADD COMMENT
0
Entering edit mode
2.7 years ago
Nitin Narwade ★ 1.6k

I guess you are missing atom.set_serial_number(anum[i]) in your for loop, before i += 1.

ADD COMMENT
0
Entering edit mode

I add it to my code, but it doesn't seem to work. The 'out.pdb' does not change whether anum is a list of int or str.

ADD REPLY
0
Entering edit mode

I think it might work to get_serial_number of the input file and set_serial number for structure? So I edit the code a little bit like this:

structure = p.get_structure("1b4aA", "1b4aA.pdb")
for model in structure:
    for chain in model:
        for residue in chain: 
            for atom in residue:
                atom.set_coord(coords_new[i])
                anum = atom.get_serial_number() #code added
                atom.set_serial_number(anum)    #code added
                i += 1

If we print anum, we have a serial of correct atom number over the iteration, but the output.pdb is still not changing for some reason? Did I have something incorrect here?

ADD REPLY
1
Entering edit mode

Yep, got it.

Everything is perfect but when you are exporting the structure by save method it is re-initializing the serial number.

So, you need to pass an additional parameter while saving the updated structure, which is, preserve_atom_numbering = True

So here are the updated lines.

atom.set_serial_number(int(anum[i])) # For now I typecast the anum to int, but better you initialize the anum with integer.
......
io.save("out.pdb", preserve_atom_numbering = True) #Here is the little thing which we were missing.

Cheers!!

ADD REPLY
0
Entering edit mode

Yeah that works! Thanks a lot!!!

ADD REPLY
0
Entering edit mode

Also, could you take a look at my updated version of this question? I want to delete the last two columns and edit the TER line of my output so that it looks the same as the structure of the input. Thanks!

ADD REPLY

Login before adding your answer.

Traffic: 2427 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6