renumberpdb.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #!/usr/bin/perl
  2. # renumberpdb.pl - generate a PDB file with renumbered indices that match residue indices in input sequence
  3. # Usage: renumberpdb.pl [options] infile [outfile]
  4. # Example: renumberpdb.pl d1hz4a_.a3m d1hz4a_.pdb
  5. # (C) Johannes Soeding, 2012
  6. #
  7. # HHsuite version 3.0.0 (15-03-2015)
  8. #
  9. # Reference:
  10. # Remmert M., Biegert A., Hauser A., and Soding J.
  11. # HHblits: Lightning-fast iterative protein sequence searching by HMM-HMM alignment.
  12. # Nat. Methods, epub Dec 25, doi: 10.1038/NMETH.1818 (2011).
  13. # This program is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation, either version 3 of the License, or
  16. # (at your option) any later version.
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. # We are very grateful for bug reports! Please contact us at [email protected]
  24. use lib $ENV{"HHLIB"}."/scripts";
  25. use HHPaths; # config file with path variables for nr, blast, psipred, pdb, dssp etc.
  26. use Align; # Needleman-Wunsch and Smith-Waterman alignment functions
  27. use File::Temp qw/ tempfile /;
  28. use strict;
  29. $|=1; # autoflush on
  30. # Default parameters for Align.pm
  31. our $d=3; # gap opening penalty for Align.pm
  32. our $dx=20; # gap opening penalty for sequence x Align.pm
  33. our $e=0.1; # gap extension penalty for Align.pm
  34. our $g=0.09; # endgap penalty for Align.pm
  35. our $v=2; # verbose mode
  36. our $matrix="identity";
  37. my $TEMPDIR = $ENV{'TEMP'};
  38. # Global variables
  39. my $infile; # input file
  40. my $outfile; # output file
  41. my $line; # input line
  42. my $nameline=""; # characters following '>' in input file
  43. my $aaq=""; # amino acid sequence read from input file
  44. my $outdir; # directory path of input file
  45. my $program=$0; # name of perl script
  46. my $replaceMSE=1; # replace HETATM MSE records by ATOM MET
  47. my $dopt=0;
  48. my $altloc="A";
  49. my $pdbfile;
  50. $program=~s/.*\///; # remove path
  51. if (scalar(@ARGV)<1) {
  52. print("
  53. $program - generate a PDB file with renumbered indices that match residue indices in
  54. input sequence
  55. The program reads an input sequence in FASTA/A3M which must have a SCOP domain, PDB chain, or DALI
  56. domain identifier (e.g. d1hz4a_, 1hz4_A, or 1hz4A_1). It reads the corresponding PDB file from the
  57. given pdb directory and generates a new PDB file by aligning the input sequence to the sequence
  58. extracted from the ATOM records of the corresponding pdb chain and renumbering the residues in
  59. columns 23-26 according to the position in the input sequence.
  60. (HETATM records for MSE (selenomethionine) will be interpreted as ATOM records for MET in the
  61. alignment. MSE will be changed to MET in the output file.)
  62. Usage: $program [options] infile [outfile]
  63. Example: $program d1hz4a_.a3m /cluster/tmp/d1hz4a_.pdb
  64. Options:
  65. -o <file> output file (default: <infile_wo_extension>.pdb
  66. -d <pdbdir> give directory of pdb files (default=$pdbdir)
  67. -a [A|B] filter alternative location identifier (e.g. A or B)
  68. \n");
  69. exit;
  70. }
  71. my $options="";
  72. for (my $i=0; $i<@ARGV; $i++) {$options.=" $ARGV[$i] ";}
  73. # General options
  74. if ($options=~s/ -d\s*(\S+) / /) {$pdbdir=$1; $dopt=1;}
  75. if ($options=~s/ -a\s*(\S+) / /) {$altloc=$1;}
  76. if ($options=~s/ -i\s*(\S*) //) {$infile=$1;}
  77. if ($options=~s/ -o\s*(\S*) //) {$outfile=$1;}
  78. if ($options=~s/ -v\s+(\d+) / /g) {$v=$1;}
  79. if (!$infile && $options=~s/^\s*([^-]\S*)\s* / /) {$infile=$1;}
  80. if (!$outfile && $options=~s/^\s*([^-]\S*)\s* / /) {$outdir=$1;}
  81. if (!$outfile) {
  82. $outfile=$infile;
  83. $outfile=~s/^(.*)\..*?$/$1.pdb/;
  84. }
  85. # Read infile
  86. if ($v>=3) {print("Reading $infile...\n");}
  87. my @infile_lines;
  88. if($infile ne "stdin") {
  89. open(INFILE,"<$infile") or die("Error: can't open $infile: $!\n");
  90. @infile_lines = <INFILE>;
  91. close(INFILE);
  92. }
  93. else {
  94. @infile_lines = <STDIN>;
  95. }
  96. # Read query sequence in infile
  97. my $line_index = 0;
  98. for ($line_index = 0; $line_index < scalar(@infile_lines); $line_index++) {
  99. $line = $infile_lines[$line_index];
  100. if ($line =~ /^>/ && $line !~ /^>(aa|ss)_/) {
  101. last;
  102. }
  103. }
  104. $line=~/^>(.*)/;
  105. $nameline=$1;
  106. # Read query sequence
  107. for ($line_index += 1; $line_index < scalar(@infile_lines); $line_index++) {
  108. $line = $infile_lines[$line_index];
  109. if($line =~ /^>/) {
  110. last;
  111. }
  112. chomp($line);
  113. $aaq .= $line;
  114. }
  115. # Prepare output file if stdout
  116. my $is_stdout_output = 0;
  117. if ($outfile eq "stdout") {
  118. $is_stdout_output = 1;
  119. (undef, $outfile) = tempfile(UNLINK => 1, OPEN => 0, DIR => $TEMPDIR);
  120. }
  121. if (&MakePdbFile($nameline,$aaq,$outfile) !=0) {exit(1);}
  122. # Print outfile to stdout if specified by the user
  123. if ($is_stdout_output == 1) {
  124. open(FH, "<$outfile") or die("Error: can't open temporary $outfile: $!\n");
  125. while( my $line = <FH>) {
  126. print($line);
  127. }
  128. close(FH);
  129. }
  130. if ($v>=2) {print("Done\n");}
  131. exit(0);
  132. ##################################################################################
  133. # Make a pdb file $base.pdb with correct resdiue numbers from query sequence in $base.hhm
  134. ##################################################################################
  135. sub MakePdbFile()
  136. {
  137. # >g1avo.1 a.24.8.1 (A:,B:) Proteasome activator reg(alpha) {Human (Homo sapiens)}
  138. my $nameline=$_[0]; # everything in line following '>' in infile
  139. my $aaq=$_[1]; # query amino acids
  140. my $outfile=$_[2]; # query amino acids
  141. my $pdbcode; # e.g. 1hz4
  142. my $chain; # chain id of query sequence for hmmfile
  143. my $line; # currently read input line
  144. my $header; # PDB file header
  145. my @remarks; # PDB file remarks
  146. my $date=`date`;
  147. chomp($date);
  148. $nameline=~/^(\S+)/;
  149. my $name=$1;
  150. # SCOP ID? (d3lkfa_,d3grs_3,d3pmga1,g1m26.1)
  151. if ($nameline=~/^[d-u](\d[a-z0-9]{3})([a-z0-9_.])[a-z0-9_]\b/) {
  152. $pdbcode=lc($1);
  153. $chain=uc($2);
  154. if ($chain eq "_") {$chain="[A ]";} else {$chain=uc($2);}
  155. $header="HEADER SCOP domain $name $date";
  156. }
  157. # DALI ID? (8fabA_0,1a0i_2)
  158. elsif ($nameline=~/^(\d[a-z0-9]{3})([A-Za-z0-9])?_\d+\s+\d+\.\d+.\d+.\d+.\d+.\d+/) {
  159. $pdbcode=lc($1);
  160. $chain=$2;
  161. if ($chain eq "") {$chain="A";}
  162. $header="HEADER DALI domain $name $date";
  163. }
  164. # PDB ID? (8fab_A, 1a0i, 1a0i_2)
  165. elsif ($nameline=~/^(\d[a-z0-9]{3})_?(\S?)\b/) {
  166. $pdbcode=lc($1);
  167. $chain=$2;
  168. if ($chain eq "") {$chain="[A ]";}
  169. $header="HEADER PDB chain $name $date";
  170. }
  171. # T0289_B or similar
  172. elsif ($dopt && $nameline=~/^([^_\s]+)/) {
  173. $pdbcode=$1;
  174. if ($nameline=~/^[^_\s]+_(\S)/) {$chain=$1;} else {$chain=".";}
  175. $header="HEADER $pdbcode $date";
  176. }
  177. else {
  178. die("Error: no pdb code found in sequence '$nameline'\n");
  179. }
  180. my $i; # index for residues in scop sequence
  181. my $l; # index for residue record in pdb file: l-1'st character in $aapdb <=> l'th record of $pdbline
  182. my @pdbline; # $pdbline[$l][$natom] = line in pdb file for l'th residue in $aapdb and atom N, CB, or O
  183. my $natom; # runs from 0 up to 2 (N, CB, O)
  184. my $res; # residue in pdb line
  185. my $atom; # atom code in pdb file (N, CA, CB, O, ...)
  186. my $aapdb=""; # template amino acids from ATOM record of pdb file
  187. my $col; # column of alignment query (from hhm file) versus pdb-residues
  188. my $nres; # residue number in pdb file
  189. if ($v>=3) {print("Looking for pdb file with pdb code $pdbcode ...\n");}
  190. $pdbfile = &OpenPDBfile($pdbcode);
  191. if ($v>=3) {print("Opening pdb file $pdbfile ...\n");}
  192. if ($pdbfile eq "") {return 1;}
  193. $l=0;
  194. $nres=-1e6;
  195. while ($line=<PDBFILE>) {
  196. # ATOM 1 N GLY A 1 -19.559 8.872 4.925 1.00 16.44 N
  197. # ATOM 2 CA GLY A 1 -19.004 8.179 6.112 1.00 14.30 C
  198. if ($line=~/^ENDMDL/) {last;} # if file contains NMR models read only first
  199. if ($line=~/^ATOM \s*\d+ (....)[ $altloc](\w{3}) $chain\s*(-?\d+\w?).*/ ||
  200. $line=~/^HETATM\s*\d+ (....)[ $altloc](MSE) $chain\s*(-?\d+\w?).*/) {
  201. $atom=$1;
  202. $res=$2;
  203. # New residue?
  204. if ($3 ne $nres) { # $3<$nres if new chain (A:,B:)
  205. $nres=$3;
  206. $l++;
  207. $res=&Three2OneLetter($res);
  208. $aapdb.=$res;
  209. $natom=0;
  210. }
  211. if ($replaceMSE) {
  212. $line=~s/^(HETATM\s*\d+ )SE(...MSE)/$1 S$2/ && $line=~s/SE(\s*)$/ S$1/;
  213. $line=~s/^HETATM(\s*\d+ .....)MSE/ATOM $1MET/;
  214. }
  215. $pdbline[$l][$natom++]=$line;
  216. }
  217. }
  218. close (PDBFILE);
  219. # Align scop query sequence ($aaq) with query sequence in pdb ($aapdb)
  220. my $xseq=$aaq;
  221. my $yseq=$aapdb;
  222. my ($imin,$imax,$lmin,$lmax);
  223. my $Sstr;
  224. my $score;
  225. my (@i,@l); # The aligned characters are returend in $j[$col] and $l[$col]
  226. if ($v>=3) {print("Aligning query sequence with sequence from pdb ATOM records \n");}
  227. $score=&AlignNW(\$xseq,\$yseq,\@i,\@l,\$imin,\$imax,\$lmin,\$lmax,\$Sstr);
  228. # DEBUG
  229. if ($v>=3) {print("Generating renumbered pdbfile \n");}
  230. # Set remarks etc.
  231. my $author ="AUTHOR J. Soeding johannes\@soeding.com";
  232. push(@remarks,"REMARK This file was generated by $program. Its residue numbers refer");
  233. push(@remarks,"REMARK to the following SEQRES sequence, for which $program was called:");
  234. push(@remarks,"REMARK >$nameline");
  235. $line=uc($aaq);
  236. while ($line) {
  237. $line=~s/^(\S{1,60})//;
  238. push(@remarks,"REMARK $1");
  239. }
  240. # Print scop-pdb file
  241. if (!open (OUTFILE,">$outfile")) {warn("Error: can't open $outfile: $!\n"); return 3;}
  242. printf(OUTFILE "%-80.80s\n",$header);
  243. printf(OUTFILE "%-80.80s\n",$author);
  244. foreach my $remark (@remarks) {printf(OUTFILE "%-80.80s\n",$remark);}
  245. my $match=0;
  246. my $len=length($aaq);
  247. for ($col=0; $col<@i; $col++) {
  248. if ($i[$col] && $l[$col]) {
  249. $match++;
  250. for ($natom=0; $natom<scalar(@{$pdbline[$l[$col]]}); $natom++) {
  251. $pdbline[$l[$col]][$natom]=~/(^......\s*\d+ .....\w{3} $chain).{5}(.*)/;
  252. if ($v>=4) {printf("%s%4i %s\n",$1,$i[$col],$2);}
  253. $line=sprintf("%s%4i %s\n",$1,$i[$col],$2);
  254. # $pdbline[$l[$col]][$natom]=~/(^......\s*\d+ .....\w{3} $chain).{4}(.*)/;
  255. # if ($v>=4) {printf("%s%4i%s\n",$1,$i[$col],$2);}
  256. # $line=sprintf("%s%4i%s\n",$1,$i[$col],$2);
  257. print(OUTFILE $line);
  258. }
  259. }
  260. }
  261. $line=~/^ATOM\s+(\d+)......(.........)/;
  262. printf(OUTFILE "TER %5i %s \n",1+$1,$2);
  263. print(OUTFILE "END \n");
  264. close (OUTFILE);
  265. # Warn?
  266. if ($v>=2 || ($v>=1 && $len-$match>5)) {
  267. if ($v>=1 && $len-$match>1) {
  268. printf("\nWARNING: could not find coordinates for %i query residues:\n",$len-$match);
  269. } else { printf("\n"); }
  270. $nameline=~/^(\S+)/;
  271. printf("%-14.14s $xseq\n","Q $1:");
  272. printf("%-14.14s $Sstr\n","Identities:");
  273. printf("%-14.14s $yseq\n","T $pdbcode"."_$chain:");
  274. printf("\n");
  275. if ($v>=4) {
  276. for ($col=0; $col<@l && $col<200; $col++) {
  277. printf("%3i %3i %3i\n",$col,$i[$col],$l[$col]);
  278. }
  279. }
  280. }
  281. return 0;
  282. }
  283. # End MakePdbFile()
  284. ##################################################################################
  285. # Convert three-letter amino acid code into one-letter code
  286. ##################################################################################
  287. sub Three2OneLetter {
  288. my $res=uc($_[0]);
  289. if ($res eq "GLY") {return "G";}
  290. elsif ($res eq "ALA") {return "A";}
  291. elsif ($res eq "VAL") {return "V";}
  292. elsif ($res eq "LEU") {return "L";}
  293. elsif ($res eq "ILE") {return "I";}
  294. elsif ($res eq "MET") {return "M";}
  295. elsif ($res eq "PHE") {return "F";}
  296. elsif ($res eq "TYR") {return "Y";}
  297. elsif ($res eq "TRP") {return "W";}
  298. elsif ($res eq "ASN") {return "N";}
  299. elsif ($res eq "ASP") {return "D";}
  300. elsif ($res eq "GLN") {return "Q";}
  301. elsif ($res eq "GLU") {return "E";}
  302. elsif ($res eq "CYS") {return "C";}
  303. elsif ($res eq "PRO") {return "P";}
  304. elsif ($res eq "SER") {return "S";}
  305. elsif ($res eq "THR") {return "T";}
  306. elsif ($res eq "LYS") {return "K";}
  307. elsif ($res eq "HIS") {return "H";}
  308. elsif ($res eq "ARG") {return "R";}
  309. elsif ($res eq "SEC") {return "U";}
  310. elsif ($res eq "ASX") {return "B";}
  311. elsif ($res eq "GLX") {return "Z";}
  312. elsif ($res eq "KCX") {return "K";}
  313. elsif ($res eq "MSE") {return "M";} # SELENOMETHIONINE
  314. elsif ($res eq "SEP") {return "S";} # PHOSPHOSERINE
  315. else {return "X";}
  316. }
  317. # Find the pdb file with $pdbcode in pdb directory
  318. sub OpenPDBfile() {
  319. my $pdbcode=lc($_[0]);
  320. if (! -e "$pdbdir") {
  321. print(STDERR "Error in $program: pdb directory '$pdbdir' does not exist!\n");
  322. return 1;
  323. }
  324. if (-e "$pdbdir/all") {$pdbfile="$pdbdir/all/";}
  325. elsif (-e "$pdbdir/divided") {$pdbfile="$pdbdir/divided/".substr($pdbcode,1,2)."/";}
  326. else {$pdbfile="$pdbdir/";}
  327. if ($pdbdir=~/divided.?$/) {$pdbfile.=substr($pdbcode,1,2)."/";}
  328. if (-e $pdbfile."pdb$pdbcode.ent") {$pdbfile.="pdb$pdbcode.ent";}
  329. elsif (-e $pdbfile."pdb$pdbcode.ent.gz") {$pdbfile="gunzip -c $pdbfile"."pdb$pdbcode.ent.gz |";}
  330. elsif (-e $pdbfile."pdb$pdbcode.ent.Z") {$pdbfile="gunzip -c $pdbfile"."pdb$pdbcode.ent.Z |";}
  331. elsif (-e $pdbfile."$pdbcode.pdb") {$pdbfile.="$pdbcode.pdb";}
  332. else {
  333. printf(STDERR "Error in $program: Cannot find pdb file $pdbfile"."pdb$pdbcode.ent!\n");
  334. return "";
  335. }
  336. if (!open (PDBFILE, "$pdbfile")) {
  337. printf(STDERR "Error in $program: Cannot open pdb file: $!\n");
  338. return "";
  339. }
  340. return $pdbfile;
  341. }