libPDB: PDB Abstraction library (PHP)
The libPDB library is intended to allow navigation through PDB files, as well as some manipulation. It's written in OO-PHP, hence it's easy to use but not terribly fast. For most projects in our lab, the speed is not a concern. Access to a specific residue or atom is very easy, as is enumeration. Several examples are shown below.
This library was written for a specific project and is by no means comprehensive. Only ATOM coordinates from PDB files are used; all other records are discarded. More functionality will be added as needed. If you need a specific functionality, please contact Luki for tips how to extend this library.
The most current version is available on NFS in ~luki/lib/libpdb.php
Organization of PDB files
The library only uses ATOM coordinates from PDB files and discards everything else.
- PDBFile: A PDB file is a collection of PDBChains; a PDBFile also keeps track of the PDB ID and filename, if available.
- PDBChain: A PDB chain is a collection of PDBResidues; a chain also keeps track of its chain ID letter.
- PDBResidue: A PDB residue is a collection of PDB Atoms; a residue also keeps track of its residue type.
- PDBAtom: A PDB atom corresponds to the ATOM record in the PDB file, specifically the X, Y and Z coordinates and the B factor value.
Caveates / Known Issues
- This library does not deal with alternative conformations; if multiple comformations are present, only the first one (A) will be used.
- Missing residue atoms will not be rebuilt or verified.
How to Use
For fully functional examples, scroll down.
Input/Output and Navigation
Library inclusion in your PHP script:
include('/path/to/libpdb.php');
Load a PDB file:
$pdb = new PDBFile('/path/to/file.pdb');
Get PDB ID (from HEADER record):
echo $pdb->id;
Get the chain IDs:
$pdb->chains;
Get residue 10 of chain A:
$residue = $pdb[A][10];
Get the residue count of chain A:
echo $pdb[A]->count;
Get residue's 1 letter and 3 letter code:
echo "1 Letter: {$pdb[A][10]->a}; 3 Letter: {$pdb[A][10]->aa}\n";
Extract residue 10-20 of chain A:
$pdb[A]->extract(10,20);
Extract residues 10, 12, 15 of chain A:
$pdb[A]->extract(array(10,12,15));
Get the backbone atoms of residue 10 in chain A:
$pdb[A][10]->extract(PDBResidue::$BACKBONE_ATOMS);
or
$pdb[A][10]->extract(array('N','CA','C','O'));
Get the residue sequence of chain A (missing residues are represented with -):
echo $pdb[A]->sequence;
Get the ATOM records (in PDB format) for chain A:
echo $pdb[A];
Export entire PDB back in PDB format:
echo $pdb;
Export the PDB sequences in FASTA format:
echo $pdb->fasta;
Computing Distances, Vectors and Centers
Compute the center of a chain A:
$pdb[A]->center;
Compute the center of residue 10 of chain A:
$pdb[A][10]->center;
Compute the distance in Angstroms between the centers of residue 10 of chain A, and residue 10 of chain B:
echo $pdb[A][10]->center->dist($pdb[B][10]->center);
Compute the vector from center of residue 10 of chain A to residue 10 of chain B:
$v = $pdb[B][10]->center->subtract($pdb[A][10]->center);
Display the vector coordinates:
echo $v;
Display the magnitude (distance) of the vector:
echo $v->magnitude;
Display the unit vector:
echo $v->unit_vector;
Get the reverse vector (change direction by 180 degrees):
echo $v->reverse;
Compute the cosine of the dot product of two vectors (for two parallel vectors, the cosine of the dot product will be 1):
$v->dot_cos($v2);
Compute the cross product:
$v->cross($v2);
Moving Atoms, Residues and Chains
Move chain A 10 Angstroms in the Z direction (in native orientation of the PDB file; the orientation can be computed from the unit vector between two equivalent residues):
$pdb[A]->translate(0, 0, 10);
Note: this modifies the original coordinates and does not make a copy. To work on a copy instead, clone() the chain first:
clone($pdb[A])->translate(0, 0, 10);
Move chain A, residue 5, 10 Angstroms in the Z direction:
$pdb[A][5]->translate(0, 0, 10);
Rotation operations are currently not supported.
Complete & Functional Examples
Find all residues within 5 Angstrom of each other in the first two chains
Code:
<?
include('/data12/users/luki/lib/libpdb.php');
$pdb = new PDBFile('/pdb/pdb2qw7.ent');
list($chain_1, $chain_2) = $pdb->chains;
foreach($pdb[$chain_1] as $resino_1 => $residue_1) {
foreach($pdb[$chain_2] as $resino_2 => $residue_2) {
$d = $residue_1->center->dist($residue_2->center);
if($d <= 5)
echo "$chain_1:$resino_1:$residue_1->aa -> ",
"$chain_2:$resino_2:$residue_2->aa :: ",
round($d, 2), " Angstrom\n";
}
}
?>
Output:
A:1:MET -> B:42:VAL :: 3.98 Angstrom
A:1:MET -> B:75:ASP :: 4.49 Angstrom
A:62:ALA -> B:73:PRO :: 4.96 Angstrom
A:65:LYS -> B:71:ASP :: 4.95 Angstrom
A:81:ILE -> B:14:SER :: 4.39 Angstrom
A:83:ASP -> B:11:VAL :: 4.84 Angstrom
A:84:THR -> B:11:VAL :: 4.34 Angstrom
A:86:ASN -> B:9:THR :: 4.95 Angstrom
Note:
To use the alpha carbon (or any other atom of the residue) rather than the residue center, change:
$d = $residue_1->center->dist($residue_2->center);
to:
$d = $residue_1[CA]->dist($residue_2[CA]);
Output:
A:1:MET -> B:42:VAL :: 4.86 Angstrom
A:62:ALA -> B:73:PRO :: 4.34 Angstrom
A:80:GLY -> B:15:LYS :: 3.98 Angstrom
A:82:ILE -> B:12:SER :: 4.62 Angstrom
A:85:VAL -> B:10:VAL :: 4.85 Angstrom
A:87:VAL -> B:8:GLY :: 4.39 Angstrom
Extract backbone atoms of residues 10-20 of chain B,
renumber to residues 1-11 of chain A,
set origin at residue CA of 10:
Code:
<?
include('/data12/users/luki/lib/libpdb.php');
$in = new PDBFile('/pdb/pdb2qw7.ent');
$out = new PDBFile;
$out[A] = new PDBChain;
foreach($in[B]->extract(10,20) as $residue)
$out[A][++$n] = $residue->extract(PDBResidue::$BACKBONE_ATOMS);
$out[A]->translate($in[B][10][CA]->reverse);
echo $out;
?>
Output:
ATOM 1 N VAL A 1 1.097 -0.022 -0.978 1.00 30.62 N
ATOM 2 CA VAL A 1 0.000 0.000 0.000 1.00 28.45 C
ATOM 3 C VAL A 1 -1.199 -0.781 -0.520 1.00 28.65 C
ATOM 4 O VAL A 1 -1.756 -0.428 -1.558 1.00 32.54 O
ATOM 5 N VAL A 2 -1.598 -1.817 0.209 1.00 26.72 N
ATOM 6 CA VAL A 2 -2.735 -2.607 -0.155 1.00 25.57 C
ATOM 7 C VAL A 2 -3.910 -2.160 0.714 1.00 29.22 C
ATOM 8 O VAL A 2 -3.792 -2.040 1.930 1.00 31.82 O
...



