hhmakemodel.pl 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. #! /usr/bin/perl
  2. #
  3. # hhmakemodel.pl
  4. # Generate a model from an output alignment of hhsearch.
  5. # Usage: hhmakemodel.pl -i file.out (-ts file.pdb|-al file.al) [-m int|-m name|-m auto] [-pdb pdbdir]
  6. # (C) Johannes Soeding 2012
  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 strict;
  27. use Align;
  28. $|=1; # force flush after each print
  29. # Default parameters
  30. our $d=7; # gap opening penalty for Align.pm; more than 2 mismatches - 2 matches ## previously: 1
  31. our $e=0.01; # gap extension penatlty for Align.pm; allow to leave large gaps bridging uncrystallized regions ## previously: 0.1
  32. our $g=0.1; # endgap penalty for Align.pm; allow to shift SEQRES residues for uncrystallized aas to ends of alignment ## previously: 0.9
  33. my $v=2; # 3: DEBUG
  34. my $formatting="CASP"; # CASP or LIVEBENCH
  35. my $servername="My server"; #
  36. my $MINRES=30; # minumum number of new query residues required for a hit to be used as additional parent
  37. my $infile="";
  38. my $outfile="";
  39. my $outformat="fas";
  40. my $pickhits="1 "; # default: build one model from best hit
  41. my $Pthr=0;
  42. my $Ethr=0;
  43. my $Prob=0;
  44. my $shift=0; # ATTENTION: set to 0 as default!
  45. my $NLEN=14; # length of the name field in alignments of hhsearch-output
  46. my $NUMRES=100; # number of residues per line in FASTA, A2M, PIR format
  47. my $program=$0; # name of perl script
  48. my $usage="
  49. hhmakemodel.pl from HHsuite $VERSION
  50. From the top hits in an hhsearch output file (hhr), you can
  51. * generate a MSA (multiple sequence alignment) containing all representative
  52. template sequences from all selected alignments (options -fas, -a2m, -a3m, -pir)
  53. * generate several concatenated pairwise alignments in AL format (option -al)
  54. * generate several concatenated coarse 3D models in PDB format (option -ts)
  55. In PIR, PDB and AL format, the pdb files are required in order to read the pdb residue numbers
  56. and ATOM records.
  57. The PIR formatted file can be used directly as input to the MODELLER homology modelling package.
  58. Usage: $program [-i] file.hhr [options]
  59. Options:
  60. -i <file.hhr> results file from hhsearch with hit list and alignments
  61. -fas <file.fas> write a FASTA-formatted multiple alignment to file.fas
  62. -a2m <file.a2m> write an A2M-formatted multiple alignment to file.a2m
  63. -a3m <file.a3m> write an A3M-formatted multiple alignment to file.a3m
  64. -m <int> [<int> ...] pick hits with specified indices (default='-m 1')
  65. -p <probability> minimum probability threshold (default=$Pthr)
  66. -e <E-value> maximum E-value threshold (default=$Ethr)
  67. -q <query_ali> use the full-length query sequence in the alignment
  68. (not only the aligned part);
  69. the query alignment file must be in HHM, FASTA, A2M,
  70. or A3M format.
  71. -N use query name from hhr filename (default: use same
  72. name as in hhr file)
  73. -first include only first Q or T sequence of each hit in MSA
  74. -v verbose mode (default=$v)
  75. Options when database matches in hhr file are PDB or SCOP sequences
  76. -pir <file.pir> write a PIR-formatted multiple alignment to file.pir
  77. -ts <file.pdb> write the PDB-formatted models based on *pairwise*
  78. alignments into file.pdb
  79. -al <file.al> write the AL-formatted *pairwise* alignments into file.al
  80. -d <pdbdirs> directories containing the pdb files (for PDB, SCOP, or DALI
  81. sequences) (default=$pdbdir)
  82. -s <int> shift the residue indices up/down by an integer (default=$shift);
  83. -CASP formatting for CASP (for -ts, -al options) (default: LIVEBENCH
  84. formatting)
  85. Options when query is compared to itself (for repeat detection)
  86. -conj include also conjugate alignments in MSA (with query and
  87. template exchanged)
  88. -conjs include conjugate alignments and sort by ascending diagonal
  89. value (i.e. i0-j0)
  90. \n";
  91. # Options to help extract repeats from self-alignments:
  92. # 1. default 2. -conj 3. -conj_diag 4. -conj_compact
  93. # ABCD ABCD ---A ABCD
  94. # BCD- BCD- --AB BCDA
  95. # D--- CD-- -ABC CDAB
  96. # CD-- D--- ABCD DABC
  97. # ---A BCD-
  98. # --AB CD--
  99. # -ABC D---
  100. # Variable declarations
  101. my $line; # input line
  102. my $score=-1; # score of the best model; at the moment: Probability
  103. my $qname=""; # name of query from hhsearch output file (infile)
  104. my $tname=""; # name of template (hit) from hhsearch output file (infile)
  105. my $qnameline=""; # nameline of query
  106. my $tnameline; # nameline of template
  107. my $pdbfile; # name of pdbfile to read
  108. my $pdbcode; # four-letter pdb code in lower case and _A if chain A (e.g. 1h7w_A)
  109. my $aaq; # query amino acids from hhsearch output
  110. my @aaq; # query amino acids from hhsearch output
  111. my @qname; # query names in present alignment as returned from ReadAlignment()
  112. my @qfirst; # indices of first residues in present alignmet as returned from ReadAlignment()
  113. my @qlast; # indices of last residues in present alignmet as returned from ReadAlignment()
  114. my @qseq; # sequences of querys in present alignment as returned from ReadAlignment()
  115. my @tname; # template names in present alignment as returned from ReadAlignment()
  116. my @tfirst; # indices of first residues in present alignmet as returned from ReadAlignment()
  117. my @tlast; # indices of last residues in present alignmet as returned from ReadAlignment()
  118. my @tseq; # sequences of templates in present alignment as returned from ReadAlignment()
  119. my $aat; # template amino acids from hhsearch output
  120. my @aat; # template amino acids from hhsearch output
  121. my $aapdb; # template amino acids from pdb file
  122. my @aapdb; # template amino acids from pdb file
  123. my $qfirst=0; # first residue of query
  124. my $qlast=0; # last residue of query
  125. my $qlength; # length of query sequence
  126. my $tfirst=0; # first residue of template
  127. my $tlast=0; # first residue of template
  128. my $tlength; # length of template sequence
  129. my $l=1; # counts template residues from pdb file (first=1, like for i[col2] and j[col2]
  130. my $col1=0; # counts columns from hhsearch alignment
  131. my $col2=0; # counts columns from alignment (by function &AlignNW) of $aat versus $aapdb
  132. my @i1; # $i1[$col1] = index of query residue in column $col1 of hhsearch-alignment
  133. my @j1; # $j1[$col1] = index of template residue in column $col1 of hhsearch-alignment
  134. my @j2; # $j2[$col2] = index of hhsearch template seq in $col2 of alignment against pdb template sequence
  135. my @l2; # $l2[$col2] = index of pdb template seq in $col2 of alignment against hhsearch template sequence
  136. my @l1; # $l1[$col1] = $l2[$col2]
  137. my $res; # residue name
  138. my $chain; # pdb chain from template name
  139. my $qfile; # name of query sequence file (for -q option)
  140. my $qmatch; # number of match states in alignment
  141. my $hit; # index of hit in hit list
  142. my $k; # index of hit sorted by position in alignment with query (k=1,...,k=@first-2)
  143. my %picked=(); # $picked{$hit} is defined and =$k for hits that will be transformed into model
  144. my @remarks;
  145. my @printblock; # block 0: header block k: k'th hit
  146. my $keyword=""; # either METHOD for CASP format or REMARK for LIVEBENCH format
  147. my $conj=0; # include conjugate sequences? Sort in which way?
  148. my $conjugate=0; # when query is compared to itself: do not include conjugate alignments
  149. my $onlyfirst=0; # include only first representative sequence of each Q/T alignment
  150. my $dummy; # dummy
  151. my $addchain=1; # 1: PDB files contain chain-id as in 1abc_A.pdb (not 1abc.pdb or pdb1abc.pdb etc.)
  152. my $pdbdirs=$pdbdir; # default pdb directory with *.pdb files
  153. my $options="";
  154. # Processing command line options
  155. if (@ARGV<1) {die $usage;}
  156. for (my $i=0; $i<@ARGV; $i++) {$options.=" $ARGV[$i] ";}
  157. # Set options
  158. if ($options=~s/ -i\s+(\S+) / /g) {$infile=$1;}
  159. if ($options=~s/ -q\s+(\S+) / /g) {$qfile=$1;}
  160. if ($options=~s/ -ts\s+(\S+) / /ig) {$outfile=$1; $outformat="TS";}
  161. if ($options=~s/ -pdb\s+(\S+) / /ig) {$outfile=$1; $outformat="TS";}
  162. if ($options=~s/ -al\s+(\S+) / /ig) {$outfile=$1; $outformat="AL";}
  163. if ($options=~s/ -pir\s+(\S+) / /ig) {$outfile=$1; $outformat="PIR";}
  164. if ($options=~s/ -fas\s+(\S+) / /ig) {$outfile=$1; $outformat="FASTA";}
  165. if ($options=~s/ -a2m\s+(\S+) / /ig) {$outfile=$1; $outformat="A2M";}
  166. if ($options=~s/ -a3m\s+(\S+) / /ig) {$outfile=$1; $outformat="A3M";}
  167. if ($options=~s/ -p\s+(\S+) / /g) {$Pthr=$1;}
  168. if ($options=~s/ -e\s+(\S+) / /g) {$Ethr=$1;}
  169. if ($options=~s/ -s\s+(\S+) / /g) {$shift=$1;}
  170. if ($options=~s/ -d\s+(([^-\s]\S*\s+)*)/ /g) {$pdbdirs=$1;}
  171. if ($options=~s/ -m\s+((\d+\s+)+)/ /g) {$pickhits=$1; }
  172. if ($options=~s/ -first\s+/ /ig) {$onlyfirst=1;}
  173. # Self-alignment options
  174. if ($options=~s/ -conj\s+/ /ig) {$conj=1;}
  175. if ($options=~s/ -conjs\s+/ /ig) {$conj=2;}
  176. # Switch formatting and method description
  177. if ($options=~s/ -CASP\s+/ /ig) {$formatting="CASP";}
  178. if ($options=~s/ -LIVEBENCH\s+/ /ig) {$formatting="LIVEBENCH";}
  179. if ($options=~s/ -server\s+(\S+)/ /g) {$servername=$1;}
  180. # Set verbose mode?
  181. if ($options=~s/ -v\s+(\d+) / /g) {$v=$1;}
  182. elsif ($options=~s/ -v\s+/ /g) {$v=1;}
  183. # Read infile and outfile
  184. if (!$infile && $options=~s/^\s*([^-]\S+)\s*/ /) {$infile=$1;}
  185. if (!$outfile && $options=~s/^\s*([^-]\S+)\s*/ /) {$outfile=$1;}
  186. if ($options=~s/ -N / /ig) {
  187. $qname=$infile;
  188. $qname=~s/^.*?([^\/]+)$/$1/; # remove path
  189. $qname=~s/^(.*)\.[^\.]*$/$1/; # remove extension
  190. $qnameline=$qname;
  191. }
  192. # Warn if unknown options found or no infile/outfile
  193. if ($options!~/^\s*$/) {$options=~s/^\s*(.*?)\s*$/$1/g; die("Error: unknown options '$options'\n");}
  194. if ($infile eq "") {die("$usage\nError in $program: input file missing: $!\n");}
  195. if ($outfile eq "") {die("$usage\nError in $program: output file missing: $!\n");}
  196. my @pdbdirs = split(/\s+/,$pdbdirs);
  197. # Find query name in input file
  198. open (INFILE, "<$infile") || die "Error in $program: Couldn't open $infile: $!\n";
  199. while ($line=<INFILE>) {
  200. if ($v>=3) {print("$line");}
  201. if ($qname eq "" && $line=~/^Query:?\s*(\S+)(.*)/) {$qname=$1; $qnameline=$1.$2;}
  202. if ($line=~/^Match_columns:?\s*(\S+)/) {$qmatch=$1; last;}
  203. }
  204. if (!($line=<INFILE>)) {die ("Error in $program: wrong format in $infile: $!\n");}
  205. # Prepare hash %pick with indices of hits that will be transformed into model
  206. # No Hit Prob E-value P-value Score SS Cols Query HMM Template HMM
  207. # 1 153l Lysozyme (E.C.3.2.1.17) 100.0 0 0 381.0 19.4 185 1-185 1-185 (185)
  208. # 2 1qsa_A Soluble lytic transglyc 100.0 2.1E-39 2.5E-43 225.8 8.3 149 21-182 423-600 (618)
  209. # 3 1ltm 36 kDa soluble lytic tr 95.9 3.5E-06 4.1E-10 50.3 11.0 95 28-122 76-221 (320)
  210. # option '-m m1 m2 m3': pick models manually
  211. my @pickhits = split(/\s+/,$pickhits);
  212. $k=1;
  213. foreach $hit (@pickhits) {
  214. if (!defined $picked{$hit}) {$picked{$hit}=$k;}
  215. $k++;
  216. }
  217. if ($outformat eq "AL" || $outformat eq "TS") {
  218. &MakePairwiseAlignments();
  219. } else {
  220. &MakeMultipleAlignment();
  221. }
  222. exit;
  223. ##################################################################################
  224. # Construct AL or TS formatted alignment as a list of pairwise alignments
  225. ##################################################################################
  226. sub MakePairwiseAlignments()
  227. {
  228. # Scan through query-vs-template-alignments from infile and create first (combination) model
  229. $hit=0; # counts hits in hit list
  230. my $models=0;
  231. while ($line=<INFILE>) {
  232. if ($line=~/^>(\S+)/) {
  233. $hit++;
  234. if ($Pthr || $Ethr || defined $picked{$hit}) {
  235. # Found right alignment (hit)
  236. if (defined $picked{$hit}) {$k=$picked{$hit};} else {$k=$hit;}
  237. if ($line=~/^>(.*?)\s+E=.*$/) {
  238. $line=$1; # remove E=1.3E-30 etc. at the end
  239. } else {
  240. $line=~/^>(.*)/;
  241. $line=$1;
  242. }
  243. my $nameline=$line;
  244. my $evalue;
  245. $line=<INFILE>;
  246. if ($line=~/Probab\s*=\s*(\S+).*E-value\s*=\s*(\S+)/) {$score=$1; $evalue=$2}
  247. else {$score=0; warn("WARNING: could not print score $line");}
  248. if ($line=~/Aligned_cols=\s*(\S+)/) {;} else {warn("WARNING: could not find aligned_cols\n");}
  249. if ($Pthr && $score<$Pthr) {last;} # Probability too low -> finished
  250. if ($Ethr && $evalue>$Ethr) {last;} # Evalue too high > finished
  251. # Commented out in CASP format
  252. if ($formatting eq "LIVEBENCH") {
  253. $printblock[$k] ="PFRMAT $outformat\n";
  254. $printblock[$k].="TARGET $qname\n";
  255. }
  256. $remarks[$k]="REMARK $k: $nameline\n";
  257. $remarks[$k].="REMARK $line";
  258. &ReadAlignment();
  259. $qfirst = $qfirst[0];
  260. $qlast = $qlast[0];
  261. $aaq = $qseq[0];
  262. $tfirst = $tfirst[0];
  263. $aat = $tseq[0];
  264. $tname = $tname[0];
  265. if ($v>=3) {
  266. for (my $i=0; $i<@qfirst; $i++) {
  267. printf("Q %-14.14s %s\n",$qname[$i],$qseq[$i]);
  268. }
  269. printf("\n");
  270. for (my $i=0; $i<@tfirst; $i++) {
  271. printf("T %-14.14s %s\n",$tname[$i],$tseq[$i]);
  272. }
  273. printf("\n");
  274. }
  275. # Extract pdbcode and construct name of pdbfile and return in global variables $pdbid and $chain
  276. if (&ExtractPdbcodeAndChain($tname[0])) {next;}
  277. if ($chain eq "[A ]") {$pdbcode.="_A";} elsif ($chain eq ".") {;} else {$pdbcode.="_$chain";}
  278. # Read score (=probability)
  279. $printblock[$k].="REMARK $nameline\n";
  280. $printblock[$k].="REMARK $line";
  281. $printblock[$k].="SCORE $score\n";
  282. $printblock[$k].="PARENT $pdbcode\n";
  283. $printblock[$k].="MODEL $k\n";
  284. &WritePairwiseAlignments();
  285. $printblock[$k].="END\n";
  286. $models++;
  287. }
  288. }
  289. }
  290. $k=$#printblock; # set $k to last index in @printblock
  291. if ($k<0) {
  292. $printblock[1]="PARENT NONE\nTER\n";
  293. $printblock[1].="END\n";
  294. if ($v>=1) {print("WARNING: no hits found for model!\n");}
  295. }
  296. close (INFILE);
  297. if ($v>=2) {
  298. printf("$models models built\n");
  299. }
  300. # Write model file header
  301. #---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
  302. # Print header
  303. my $date = scalar(localtime);
  304. if ($formatting eq "CASP") {
  305. $printblock[0]="PFRMAT $outformat\n";
  306. $printblock[0].="TARGET $qname\n";
  307. }
  308. $printblock[0].="REMARK AUTHOR $servername\n";
  309. $printblock[0].="REMARK $date\n";
  310. # $printblock[0].="REMARK J. Soeding \n";
  311. # Add remarks
  312. for ($k=0; $k<@remarks; $k++) {
  313. if (defined $remarks[$k]) {
  314. $printblock[0].=$remarks[$k];
  315. }
  316. }
  317. $printblock[0].="REMARK \n";
  318. # Print @printblock into outfile
  319. open (OUTFILE, ">$outfile") || die "Error in $program: Couldn't open $outfile: $!\n";
  320. foreach my $printstr (@printblock) {
  321. my @printarr=split(/\n/,$printstr);
  322. if ($outformat eq "TS") {
  323. foreach $printstr (@printarr) {
  324. printf(OUTFILE "%-80.80s\n",$printstr);
  325. }
  326. } else {
  327. foreach $printstr (@printarr) {
  328. printf(OUTFILE "%s\n",$printstr);
  329. }
  330. }
  331. }
  332. close (OUTFILE);
  333. if ($outformat eq "TS") {
  334. # Call MaxSprout to generate sidechains
  335. }
  336. return;
  337. }
  338. ##################################################################################
  339. # Construct multiple alignment in FASTA, A2M, or PIR format
  340. ##################################################################################
  341. sub MakeMultipleAlignment()
  342. {
  343. my @hitnames=(); # $hitnames[$k] is the nameline of the ihit'th hit
  344. my @hitseqs=(); # $hitseqs[$k] contains the residues of the ihit'th hit
  345. my @hitdiag=(); # $hitdiag[$k] = $qfirst[0]-$tfirst[0]
  346. my @conjnames=(); # $hitnames[$k] is the nameline of the ihit'th conjugate hit
  347. my @conjseqs=(); # $hitseqs[$k] contains the residues of the ihit'th conjugate hit
  348. my @conjdiag=(); # $hitdiag[$k] = $qfirst[0]-$tfirst[0] for conjugate alignments
  349. my $new_hit; # residues of new hit
  350. my $i; # residue index
  351. my $j; # residue index
  352. my $k; # sequence index
  353. $hitnames[0]="";
  354. $hitseqs[0]="";
  355. $hitdiag[0]=0;
  356. $conjnames[0]="";
  357. $conjseqs[0]="";
  358. $conjdiag[0]=0;
  359. open (INFILE, "<$infile") || die "Error in $program: Couldn't open $infile: $!\n";
  360. $hit=0; # counts hits in hit list
  361. # Read one alignment after the other
  362. while ($line=<INFILE>) {
  363. # Found new aligment
  364. if ($line=~/^>(\S+)/) {
  365. $hit++;
  366. # Is alignment selected by user?
  367. if ($Pthr || $Ethr || defined $picked{$hit}) {
  368. if ($line=~/^>(\S+)(.*)/) {$tname=$1; $tnameline=$1.$2;}
  369. else {die("\nError: bad format in $infile, line $.: code 1\n");}
  370. $line = <INFILE>;
  371. if ($line=~/Probab\s*=\s*(\S+).*E-value\s*=\s*(\S+)/) {
  372. if ($Pthr && $1<$Pthr) {last;} # Probability too low -> finished
  373. if ($Ethr && $2>$Ethr) {last;} # Evalue too high > finished
  374. } else { die("\nError: bad format in $infile, line $.: code 2\n"); }
  375. # Read next alignment with $aaq, $qfirst, @tseq, @first, and @tname
  376. &ReadAlignment();
  377. chomp($tnameline);
  378. if ($tnameline=~/\S+\s+(.*)/) {$tname[0].=" $1";} # template seed sequence gets its description
  379. # Format sequences into @hitseqs and @hitnames
  380. &FormatSequences(\@hitnames,\@hitseqs,\@hitdiag,\@qname,\@qseq,\@qfirst,\@qlast,\$qlength,\@tname,\@tseq,\@tfirst,\@tlast,\$tlength);
  381. # Use conjugate alignments?
  382. if ($conj>0) {
  383. &FormatSequences(\@conjnames,\@conjseqs,\@conjdiag,\@tname,\@tseq,\@tfirst,\@tlast,\$tlength,\@qname,\@qseq,\@qfirst,\@qlast,\$qlength);
  384. }
  385. } # end: if ($Pthr>0 || defined $picked{$hit})
  386. } # end: if ($line=~/^>(\S+)/) # found new alignment
  387. } # end while
  388. close (INFILE);
  389. # Insert full-length query sequence?
  390. if ($qfile) {
  391. $hitseqs[0]="";
  392. open (QFILE, "<$qfile") || die "Error in $program: Couldn't open $qfile: $!\n";
  393. while ($line=<QFILE>) {
  394. if ($line=~/^>/ && $line!~/^>ss_/ && $line!~/^>sa_/ && $line!~/^>aa_/ && $line!~/^>Consensus/) {last;}
  395. }
  396. while ($line=<QFILE>) {
  397. if ($line=~/^>/ || $line=~/^\#/) {last;}
  398. $line=~tr/\n\.-//d;
  399. $line=~tr/a-z/A-Z/;
  400. $hitseqs[0].=$line;
  401. }
  402. close(QFILE);
  403. if ($v>=2) {printf("\nQ(full) %-14.14s %s\n",$qname,$hitseqs[0]);}
  404. }
  405. # DEBUG
  406. if ($v>=3) {
  407. printf("\nQuery %-14.14s %s\n",$qname,$hitseqs[0]);
  408. for ($k=1; $k<@hitnames; $k++) {
  409. printf("T hit %3i %-14.14s %s\n",$k,$hitnames[$k],$hitseqs[$k]);
  410. }
  411. printf("\n");
  412. printf("\nQuery %-14.14s %s\n",$qname,$conjseqs[0]);
  413. for ($k=1; $k<@conjnames; $k++) {
  414. printf("T conj %3i %-14.14s %s\n",$k,$conjnames[$k],$conjseqs[$k]);
  415. }
  416. printf("\n");
  417. }
  418. # Include conjugate sequences?
  419. if ($conj>0) {
  420. shift(@conjseqs); # delete zeroth ("query") sequence of @conjseqs
  421. shift(@conjnames); #
  422. shift(@conjdiag); #
  423. # Sort by diagonals $hitdiag[], $conjdiag[]
  424. &Sort(\@hitdiag,\@hitseqs,\@hitnames);
  425. &Sort(\@conjdiag,\@conjseqs,\@conjnames);
  426. # Append conjugate sequences to hitseqs
  427. splice(@hitseqs,scalar(@hitseqs),0,@conjseqs);
  428. splice(@hitnames,scalar(@hitnames),0,@conjnames);
  429. if ($v>=3) {
  430. printf("\nQuery %-14.14s %s\n",$qname,$hitseqs[0]);
  431. for ($k=1; $k<@hitnames; $k++) {
  432. chomp($hitnames[$k]);
  433. printf("T tot %3i %-14.14s %s\n",$k,$hitnames[$k],$hitseqs[$k]);
  434. $hitnames[$k].="\n";
  435. }
  436. }
  437. }
  438. # Insert gaps:
  439. my @len_ins; # $len_ins[$j] will count the maximum number of inserted residues after match state $j.
  440. my @inserts; # $inserts[$j] contains the insert (in small case) of sequence $k after the $j'th match state
  441. my $insert;
  442. my $ngap;
  443. # For each match state determine length of LONGEST insert after this match state and store in @len_ins
  444. for ($k=0; $k<@hitnames; $k++) {
  445. # split into list of single match states and variable-length inserts
  446. # ([A-Z]|-) is the split pattern. The parenthesis indicate that split patterns are to be included as list elements
  447. # The '#' symbol is prepended to get rid of a perl bug in split
  448. $j=0;
  449. @inserts = split(/([A-Z]|-)/,"#".$hitseqs[$k]."#");
  450. # printf("Sequence $k: $hitseqs[$k]\n");
  451. # printf("Sequence $k: @inserts\n");
  452. foreach $insert (@inserts) {
  453. if( !defined $len_ins[$j] || length($insert)>$len_ins[$j]) {
  454. $len_ins[$j]=length($insert);
  455. }
  456. $j++;
  457. # printf("$insert|");
  458. }
  459. # printf("\n");
  460. }
  461. # After each match state insert residues and fill up with gaps to $len_ins[$i] characters
  462. for ($k=0; $k<@hitnames; $k++) {
  463. # split into list of single match states and variable-length inserts
  464. @inserts = split(/([A-Z]|-)/,"#".$hitseqs[$k]."#");
  465. $j=0;
  466. # append the missing number of gaps after each match state
  467. foreach $insert (@inserts) {
  468. if($outformat eq "FASTA") {
  469. for ($i=length($insert); $i<$len_ins[$j]; $i++) {$insert.="-";}
  470. }
  471. else {
  472. for ($i=length($insert); $i<$len_ins[$j]; $i++) {$insert.=".";}
  473. }
  474. $j++;
  475. }
  476. $hitseqs[$k] = join("",@inserts);
  477. $hitseqs[$k] =~ tr/\#//d; # remove the '#' symbols inserted at the beginning and end
  478. }
  479. # Remove columns at beginning and end with gaps in all sequences
  480. my $remove_start;
  481. my $remove_end;
  482. my $len;
  483. $hitseqs[0]=~/^(-*)/;
  484. $remove_start=length($1);
  485. $hitseqs[0]=~/(-*)$/;
  486. $remove_end=length($1);
  487. for ($k=0; $k<@hitnames; $k++) {
  488. $hitseqs[$k]=~s/^.{$remove_start}(.*).{$remove_end}/$1/;
  489. }
  490. $len=($hitseqs[0]=~tr/a-zA-Z/a-zA-Z/);
  491. # Prepare name line of query
  492. if ($outformat eq "PIR") {
  493. my $qnametmp=$qname;
  494. $qnametmp=~tr/:/;/;
  495. $qnameline=~/^\S+\s*(.*)/;
  496. my $qnamelinetmp=$1;
  497. $qnamelinetmp=~tr/:/;/;
  498. $hitnames[0] = sprintf(">P1;%s\nsequence:%s:%4i: :%4i: :%s: : 0.00: 0.00\n",$qnametmp,$qnametmp,$remove_start+1,$len+$remove_start,$qnamelinetmp);
  499. } else {
  500. # outformat is "FASTA" or "A2M" or "A3M" or ...
  501. $hitnames[0] = ">$qnameline\n";
  502. }
  503. # If pretty diagonally sorted order is wanted...
  504. if ($conj>0) {
  505. if ($conj==2) {
  506. my $center = 0.5*(scalar(@hitseqs)-1);
  507. @conjseqs = splice(@hitseqs,$center+1,$center);
  508. splice(@hitseqs,0,0,@conjseqs);
  509. @hitseqs = reverse(@hitseqs);
  510. @conjnames = splice(@hitnames,$center+1,$center);
  511. splice(@hitnames,0,0,@conjnames);
  512. @hitnames = reverse(@hitnames);
  513. # Shorten namelines of all but first sequence
  514. my %count;
  515. for ($k=0; $k<@hitnames; $k++) {
  516. if ($k==$center) {$k++;}
  517. $hitnames[$k]=~/(\S{1,14})/;
  518. if (!defined $count{$1}) {$count{$1}=0;}
  519. my $count = ++$count{$1};
  520. # printf("vorher: %s ",$hitnames[$k]);
  521. $hitnames[$k]=~s/^(\S{1,14}).*/$1:$count/;
  522. # printf("nachher: %s\n",$hitnames[$k]);
  523. }
  524. } else {
  525. for ($k=0; $k<@hitnames; $k++) {$hitnames[$k]=">$qname\n";}
  526. }
  527. }
  528. # Remove gaps? Captialize?
  529. if ($outformat eq "PIR") {
  530. for ($k=0; $k<@hitnames; $k++) {
  531. $hitseqs[$k].="*";; # Transform to upper case
  532. $hitseqs[$k]=~tr/a-z./A-Z-/; # Transform to upper case
  533. $hitseqs[$k]=~s/(.{1,$NUMRES})/$1\n/g; # insert newlines every NUMRES positions
  534. }
  535. } elsif ($outformat eq "FASTA") {
  536. for ($k=0; $k<@hitnames; $k++) {
  537. $hitseqs[$k]=~tr/a-z./A-Z-/; # Transform to upper case
  538. $hitseqs[$k]=~s/(.{1,$NUMRES})/$1\n/g; # insert newlines every NUMRES positions
  539. }
  540. } elsif ($outformat eq "A2M") {
  541. for ($k=0; $k<@hitnames; $k++) {$hitseqs[$k]=~s/(.{1,$NUMRES})/$1\n/g;} # insert newlines every NUMRES positions
  542. } elsif ($outformat eq "A3M") {
  543. for ($k=0; $k<@hitnames; $k++) {$hitseqs[$k]=~tr/.//d;$hitseqs[$k].="\n";} # Remove gaps aligned to inserts
  544. }
  545. # Write sequences into output file
  546. open (OUTFILE, ">$outfile") || die ("cannot open $outfile:$!");
  547. for ($k=0; $k<@hitnames; $k++) {
  548. print(OUTFILE "\n$hitnames[$k]$hitseqs[$k]");
  549. }
  550. close OUTFILE;
  551. if ($v>=2) {
  552. printf("%i sequences written to $outfile\n",scalar(@hitnames));
  553. }
  554. }
  555. # Format sequences into @hitseqs and @hitnames
  556. # & Call with FormatSequences(\@hitnames,\@hitseqs,\@qname,\@qseq,\@qfirst,\@qlast,\$qlength,\@tname,\@tseq,\@tfirst,\@tlast,\$tlength);
  557. sub FormatSequences()
  558. {
  559. my $p_hitnames = $_[0]; # typeglob to $hitname
  560. my $p_hitseqs = $_[1]; # ...
  561. my $p_hitdiag = $_[2]; # ...
  562. my $p_qname = $_[3]; #
  563. my $p_qseq = $_[4]; #
  564. my $p_qfirst = $_[5]; #
  565. my $p_qlast = $_[6]; #
  566. my $p_qlength = $_[7]; #
  567. my $p_tname = $_[8]; #
  568. my $p_tseq = $_[9]; #
  569. my $p_tfirst = $_[10]; #
  570. my $p_tlast = $_[11]; #
  571. my $p_tlength = $_[12]; #
  572. my $i;
  573. if ($v>=2) {
  574. if (defined $picked{$hit}) {
  575. print("hit=$hit picked=$picked{$hit} tname=$tname[0]");
  576. } else {
  577. print("hit=$hit picked=evalue<$Ethr tname=$tname[0]");
  578. }
  579. for (my $i=1; $i<@{$p_tname}; $i++) {
  580. print(", $tname[$i]");
  581. }
  582. print("\n");
  583. }
  584. my $qfirst = ${$p_qfirst}[0];
  585. my $qlast = ${$p_qlast}[0];
  586. my $qlength = ${$p_qlength};
  587. my $aaq = ${$p_qseq}[0];
  588. @aaq = unpack("C*",$aaq); # needed for transforming template sequences into a3m based on query residues (NOT HMM match states!)
  589. $aaq=~tr/.-//d; # remove all gaps from query sequence
  590. # For all template sequences in the present alignment
  591. for (my $k=0; $k<@{$p_tname}; $k++) {
  592. $tname =${$p_tname}[$k];
  593. $tfirst=${$p_tfirst}[$k];
  594. $aat =${$p_tseq}[$k];
  595. # Transform template residues into a3m format:
  596. # match states are those where query has residue (NOT where HMM has match state!)
  597. # This makes sense since we want to build a model for the query sequence.
  598. @aat = unpack("C*",$aat);
  599. $aat="";
  600. # Transform all columns with residue in query into match/delete states, all others to inserts
  601. for ($i=0; $i<scalar(@aaq); $i++) {
  602. if ($aaq[$i]!=45 && $aaq[$i]!=46) { # no gap in query
  603. if($aat[$i]==46) {
  604. $aat.="-"; # transform '.' to '-' if aligned with a query residue
  605. } else {
  606. $aat .= uc(chr($aat[$i])); # UPPER case if aligned with a query residue (match state)
  607. }
  608. } else {
  609. if($aat[$i]!=45 && $aat[$i]!=46) { # no gap in template?
  610. $aat.=lc(chr($aat[$i])); # lower case if aligned with a gap in the query (insert state)
  611. }
  612. }
  613. }
  614. if ($v>=2) {
  615. printf("\nQ %-14.14s %s\n",$qname,$aaq);
  616. printf("T %-14.14s %s\n",$tname,$aat);
  617. }
  618. # Outformat is PIR? => read residues and indices from PDB ATOM records
  619. if ($outformat eq "PIR") {
  620. # Extract pdbcode and construct name of pdbfile and return in global variables $pdbid and $chain
  621. if (&ExtractPdbcodeAndChain($tname)) {next;}
  622. # Read sequence from pdb file
  623. if (!open (PDBFILE, "$pdbfile")) {
  624. die ("Error in $program: Couldn't open $pdbfile: $!\n");
  625. }
  626. $aapdb="";
  627. $l=0;
  628. my @nres; # $nres[$l] = pdb residue index for residue $aapdb[$l]
  629. my $nres=-1e6;
  630. my $resolution=-1.00;
  631. my $rvalue=-1.00;
  632. while ($line=<PDBFILE>) {
  633. if ($line=~/^REMARK.*RESOLUTION\.\s+(\d+\.?\d*)/) {$resolution=$1;}
  634. if ($line=~/^REMARK.*R VALUE\s+\(WORKING SET\)\s+:\s+(\d+\.?\d*)/) {$rvalue=$1;}
  635. if ($line=~/^ENDMDL/) {last;} # if file contains NMR models read only first one
  636. if (($line=~/^ATOM\s+\d+ .. [ A](\w{3}) $chain\s*(-?\d+.)/ ||
  637. ($line=~/^HETATM\s+\d+ .. [ A](\w{3}) $chain\s*(-?\d+.)/ && &Three2OneLetter($1) ne "X") ) &&
  638. $2 ne $nres ) {
  639. $res=$1;
  640. $nres=$2;
  641. $nres[$l]=$2;
  642. $res=&Three2OneLetter($res);
  643. $aapdb[$l++]=$res;
  644. $aapdb.=$res;
  645. }
  646. }
  647. close (PDBFILE);
  648. if (length($aapdb)<=0) {die("Error: chain $chain not found in pdb file $pdbfile\n");}
  649. # Align template in hh-alignment ($aat) with template sequence in pdb ($aapdb)
  650. my $xseq=$aat;
  651. $xseq=~tr/-/~/; # transform Deletes to '~' to distinguish them from gaps '-' inserted by Align.pm
  652. my $yseq=$aapdb;
  653. my ($jmin,$jmax,$lmin,$lmax);
  654. my $Sstr;
  655. my $score;
  656. # The aligned characters are returend in $j2[$col2] and $l2[$col2]
  657. $score=&AlignNW(\$xseq,\$yseq,\@j2,\@l2,\$jmin,\$jmax,\$lmin,\$lmax,\$Sstr);
  658. # DEBUG
  659. if ($v>=3) {
  660. printf("Template (hh) $xseq\n");
  661. printf("Identities $Sstr\n");
  662. printf("Template (pdb) $yseq\n");
  663. printf("\n");
  664. if ($v>=4) {
  665. for ($col2=0; $col2<@l2 && $col2<1000; $col2++) {
  666. printf("%3i %3i:%s %3i:%s -> %i\n",$col2,$j2[$col2],substr($aat,$j2[$col2]-1,1),$l2[$col2],substr($aapdb,$l2[$col2]-1,1),$nres[$l2[$col2]-1]);
  667. }
  668. }
  669. }
  670. # check for reasonable alignment
  671. my $num_match = 0;
  672. for ($i=0; $i<@l2; $i++) {
  673. if ($j2[$i] > 0 && $l2[$i] > 0) {
  674. $num_match++;
  675. }
  676. }
  677. if (($score/$num_match) < 1) {
  678. print "WARNING! Match score with PDBfile (score: $score num: $num_match score/num:".($score/$num_match).") to low => $pdbfile not included!\n";
  679. next;
  680. }
  681. # Assign a3m-formatted amino acid sequence from pdb file to $aapdb
  682. $aapdb="";
  683. my @xseq=unpack("C*",$xseq);
  684. my @yseq=unpack("C*",$yseq);
  685. for ($i=0; $i<@yseq; $i++) {
  686. if(($xseq[$i]>=65 && $xseq[$i]<=90) || $xseq[$i]==ord('~')) { # if $aat has upper case residue or Delete state
  687. # Match state
  688. $aapdb.=uc(chr($yseq[$i]));
  689. } else {
  690. # Insert state
  691. if ($yseq[$i]!=45) {$aapdb.=lc(chr($yseq[$i]));} # add only if not a gap '-'
  692. }
  693. }
  694. # Remove overlapping ends of $aapdb
  695. $aapdb=~s/^[a-z]*(.*?)[a-z]*$/$1/;
  696. # Glue gaps at beginning and end of aligned pdb sequence and add sequence to alignment
  697. push (@{$p_hitseqs}, ("-" x ($qfirst-1)).$aapdb.("-" x ($qlength-$qlast)) ); # use ATOM record residues $aapdb!
  698. # Write hitname in PIR format into @hitnames
  699. my $descr;
  700. my $organism;
  701. my $struc=$pdbcode;
  702. if ($tnameline=~/^(\S+)\s+(.*)/) {$descr=$2; $descr=~tr/://d;} else {$descr=" ";}
  703. if ($tnameline=~/^(\S+)\s+.*\s+\{(.*)\}/) {$organism=$2;} else {$organism=" ";}
  704. if (length($chain)>1 || $chain eq ".") { # MODELLER's special symbol for 'chain unspecified'
  705. $chain=".";
  706. } elsif ($addchain && $chain ne " ") {
  707. $struc.="_$chain";
  708. }
  709. # push (@{$p_hitnames}, sprintf(">P1;%s\nstructureX:%4s:%4i:%1s:%4i:%1s:%s:%s:%-.2f:%-.2f\n",$struc,$struc,$nres[$lmin-1],$chain,$nres[$lmax-1],$chain,$descr,$organism,$resolution,$rvalue) );
  710. my $descrtmp=$descr;
  711. $descrtmp=~tr/:/;/;
  712. $organism=~tr/://d;
  713. push (@{$p_hitnames}, sprintf(">P1;%s\nstructureX:%4s: :%1s: :%1s:%s:%s:%-.2f:%-.2f\n",$struc,$struc,$chain,$chain,$descrtmp,$organism,$resolution,$rvalue) );
  714. push (@{$p_hitdiag}, $tfirst-$qfirst);
  715. } else {
  716. # outformat is "FASTA" or "A2M" or "A3M" or ...
  717. # Write hitname in FASTA format into @hitnames
  718. push (@{$p_hitseqs}, ("-" x ($qfirst-1)).$aat.("-" x ($qlength-$qlast)) );
  719. push (@{$p_hitnames}, ">$tname\n" );
  720. push (@{$p_hitdiag}, $tfirst-$qfirst);
  721. }
  722. if ($onlyfirst>0) {last;} # extract only first (seed?) sequence in each alignment
  723. } # end: for (my $k=0; $k<@{$tname}; $k++)
  724. # Paste aligned subsequence of query over $hitseqs[0]
  725. if (${$p_hitseqs}[0] eq "") {${$p_hitseqs}[0] = "-" x $qlength;}
  726. if (!$qfile) {substr(${$p_hitseqs}[0],$qfirst-1,length($aaq),$aaq);}
  727. return;
  728. }
  729. ##################################################################################
  730. # Read Alignment from infile (*.hhr file)
  731. # Results:
  732. # $aaq: query residues in present alignment
  733. # $qfirst: index of first query residue in present alignment
  734. # @tname: template names in present alignmen
  735. # @tfirst: indices of first residues in present alignmet
  736. # @tseq: sequences of templates in present alignment
  737. ##################################################################################
  738. sub ReadAlignment() {
  739. @qname=(); # name of $it'th query in this alignment
  740. @qfirst=(); # index of first residue in $it'th query in this alignment
  741. @qlast=(); # index of last residue in $it'th query in this alignment
  742. @qseq=(); # residues of $it'th query in this alignment
  743. @tname=(); # name of $it'th template in this alignment
  744. @tfirst=(); # index of first residue in $it'th template in this alignment
  745. @tlast=(); # index of last residue in $it'th template in this alignment
  746. @tseq=(); # residues of $it'th template in this alignment
  747. if ($v>=3) {printf("Searching for Q $qname vs T $tname\n");}
  748. $line=<INFILE>;
  749. # Search for first line beginning with Q ot T and not followed by aa_, ss_pred, ss_conf, or Consensus
  750. while (1) {
  751. my $i; # index for query sequence in this alignment
  752. # Scan up to first line starting with Q; stop when line 'No\s+\d+' or 'Done' is found
  753. while (defined $line && $line!~/^Q\s(\S+)/) {
  754. if ($line=~/^No\s+\d/ || $line=~/^Done/) {last;}
  755. $line=<INFILE>; next;
  756. }
  757. if (!defined $line || $line=~/^No\s+\d/ || $line=~/^Done/) {last;}
  758. # Scan up to first line that is not secondary structure line or consensus line
  759. while (defined $line && $line=~/^Q\s+(ss_|sa_|aa_|Consens|Cons-)/) {$line=<INFILE>;}
  760. # Read next block of query sequences
  761. $i=0;
  762. while ($line=~/^Q\s+/) {
  763. if ($line!~/^Q\s+(ss_|sa_|aa_|Consens|Cons-)/ && $line=~/^Q\s*(\S+)\s+(\d+)\s+(\S+)\s+(\d+)\s+\((\d+)/) {
  764. $qname[$i]=$1;
  765. if (!$qfirst[$i]) {$qfirst[$i]=$2;} # if $qfirst is undefined then this is the first alignment block -> set $qfirst to $1
  766. if (!$qseq[$i]) {$qseq[$i]=$3;} else {$qseq[$i].=$3;}
  767. $qlast[$i]=$4;
  768. if ($i==0) {$qlength=$5}
  769. $i++;
  770. }
  771. $line=<INFILE>;
  772. }
  773. if ($i==0) {
  774. die("\nError in $program: bad format in $infile, line $.: query block\n");
  775. }
  776. # Scan up to first line starting with T
  777. while (defined $line && $line!~/^T\s+(\S+)/) {$line=<INFILE>;}
  778. # Scan up to first line that is not secondary structure line or consensus line
  779. while (defined $line && $line=~/^T\s+(ss_|sa_|aa_|Consens|Cons-)/) {$line=<INFILE>;}
  780. # Read next block of template sequences
  781. $i=0;
  782. while ($line=~/^T\s+/) {
  783. if ($line!~/^T\s+(ss_|sa_|aa_|Consens|Cons-)/ && $line=~/^T\s*(\S+)\s+(\d+)\s+(\S+)\s+(\d+)\s+\((\d+)/){
  784. $tname[$i]=$1;
  785. if (!$tfirst[$i]) {$tfirst[$i]=$2;} # if $tfirst is undefined then this is the first alignment block -> set $tfirst to $1
  786. if (!$tseq[$i]) {$tseq[$i]=$3;} else {$tseq[$i].=$3;}
  787. $tlast[$i]=$4;
  788. if ($i==0) {$tlength=$5}
  789. $i++;
  790. }
  791. $line=<INFILE>;
  792. }
  793. if ($i==0) {
  794. die("\nError in $program: bad format in $infile, line $.: template block\n");
  795. }
  796. } # end while ($line=<INFILE>)
  797. # if (!$qfirst) {$qfirst=1;} # if still $qfirst==0 then set $qfirst to 1
  798. # for (my $i=0; $i<@tfirst; $i++) {
  799. # if (!$tfirst[$i]) {$tfirst[$i]=1;} # if still $tfirst[$i]==0 then set $tfirst to 1
  800. # }
  801. # Check lengths
  802. if (length($qseq[0])!=length($tseq[0])) {
  803. print("\nError: query and template lines do not have the same length in $infile, line $.\n");
  804. for (my $i=0; $i<@qfirst; $i++) {
  805. printf("Q %-14.14s %s\n",$qname[$i],$qseq[$i]);
  806. }
  807. printf("\n");
  808. for (my $i=0; $i<@tfirst; $i++) {
  809. printf("T %-14.14s %s\n",$tname[$i],$tseq[$i]);
  810. }
  811. printf("\n");
  812. exit 1;
  813. }
  814. if ($v>=3) {
  815. for (my $i=0; $i<@qfirst; $i++) {
  816. printf("Q %-14.14s %s\n",$qname[$i],$qseq[$i]);
  817. }
  818. printf("\n");
  819. for (my $i=0; $i<@tfirst; $i++) {
  820. printf("T %-14.14s %s\n",$tname[$i],$tseq[$i]);
  821. }
  822. printf("\n");
  823. }
  824. return;
  825. }
  826. ##################################################################################
  827. # Write Alignment to $printblock[$k]
  828. ##################################################################################
  829. sub WritePairwiseAlignments() {
  830. #Delete columns with gaps in both sequences
  831. $aaq=uc($aaq);
  832. $aat=uc($aat);
  833. @aaq=split(//,$aaq);
  834. @aat=split(//,$aat);
  835. my $col=0;
  836. for ($col1=0; $col1<@aaq; $col1++) {
  837. if ($aaq[$col1]=~tr/a-zA-Z/a-zA-Z/ || $aat[$col1]=~tr/a-zA-Z/a-zA-Z/) {
  838. $aaq[$col]=$aaq[$col1];
  839. $aat[$col]=$aat[$col1];
  840. $col++;
  841. }
  842. }
  843. splice(@aaq,$col); # delete end of @aaq;
  844. splice(@aat,$col);
  845. $aaq=join("",@aaq);
  846. $aat=join("",@aat);
  847. # Count query and template residues into @i1 and @j1
  848. for ($col1=0; $col1<@aaq; $col1++) {
  849. if ($aaq[$col1]=~tr/a-zA-Z/a-zA-Z/) {
  850. $i1[$col1]=$qfirst++; #found query residue in $col1
  851. } else {
  852. $i1[$col1]=0; #found gap in $col1
  853. }
  854. if ($aat[$col1]=~tr/a-zA-Z/a-zA-Z/) {
  855. $j1[$col1]=$tfirst++; #found template residue in $col1
  856. } else {
  857. $j1[$col1]=0; #found gap in $col1
  858. }
  859. }
  860. # DEBUG
  861. if ($v>=3) {
  862. printf ("col Q i1 T j1\n");
  863. for ($col1=0; $col1<@aaq; $col1++) {
  864. printf ("%3i %s %3i %s %3i\n",$col1,$aaq[$col1],$i1[$col1],$aat[$col1],$j1[$col1]);
  865. }
  866. printf ("\n");
  867. }
  868. # Read protein chain from pdb file
  869. # ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
  870. # ATOM 1 N SER A 27 38.637 79.034 59.693 1.00 79.70 # ATOM 2083 CD1 LEU A 22S 15.343 -12.020 43.761 1.00 5.00 C
  871. # Extract pdbcode and construct name of pdbfile and return in global variables $pdbid and $chain
  872. if (&ExtractPdbcodeAndChain($tname)) {next;}
  873. # Read sequence from pdb file
  874. if (! defined $pdbfile) {die ("Error in $program: Couldn't find pdb code in $tname\n");}
  875. open (PDBFILE, "$pdbfile") || die ("Error in $program: Couldn't open $pdbfile: $!\n");
  876. if ($chain eq "[A ]") {$pdbcode.="_A";} elsif ($chain eq ".") {;} else {$pdbcode.="_$chain";}
  877. $aapdb=""; $l=1;
  878. $line=<PDBFILE>;
  879. while ($line) {if ($line=~/^ATOM/) {last;} $line=<PDBFILE>;} # advance to ATOM records
  880. my @nres; # $nres[$l] = pdb residue index for residue $aapdb[$l]
  881. my @coord; # $coord[$l] = coordinates of CA atom of residue $aapdb[$l]
  882. while ($line) {
  883. if ($line=~/^ATOM\s+\d+ CA [ AB](\w{3}) $chain\s*(-?\d+.) (\s*\S+\s+\S+\s+\S+)/ ||
  884. ($line=~/^HETATM\s+\d+ CA [ AB](\w{3}) $chain\s*(-?\d+.) (\s*\S+\s+\S+\s+\S+)/ && &Three2OneLetter($1) ne "X") ) {
  885. $res=$1;
  886. $nres[$l]=$2;
  887. $coord[$l]=$3." 1.00";
  888. $res=&Three2OneLetter($res);
  889. $aapdb[$l]=$res;
  890. $aapdb.=$res;
  891. $l++;
  892. }
  893. elsif ($l>10 && $line=~/^ATOM\s+\d+ CA/) {last;}
  894. elsif ($line=~/^ENDMDL/) {last;} # if file contains NMR models read only first one
  895. $line=<PDBFILE>;
  896. }
  897. close (PDBFILE);
  898. # Align template in hh-alignment ($aat) with template sequence in pdb ($aapdb)
  899. my $xseq=$aat;
  900. my $yseq=$aapdb;
  901. my ($jmin,$jmax,$lmin,$lmax);
  902. my $Sstr;
  903. my $score;
  904. $xseq=~tr/-/~/d; # transform Deletes to '~' to distinguish them from gaps inserted by Align.pm
  905. #the aligned characters are returend in $j2[$col2] and $l2[$col2]
  906. if ($v>=3) {
  907. printf("Template (hh) $xseq\n");
  908. printf("Identities $Sstr\n");
  909. printf("Template (pdb) $yseq\n");
  910. printf("\n");
  911. }
  912. $score=&AlignNW(\$xseq,\$yseq,\@j2,\@l2,\$jmin,\$jmax,\$lmin,\$lmax,\$Sstr);
  913. # DEBUG
  914. if ($v>=3) {
  915. printf("Template (hh) $xseq\n");
  916. printf("Identities $Sstr\n");
  917. printf("Template (pdb) $yseq\n");
  918. printf("\n");
  919. if ($v>=4) {
  920. for ($col2=0; $col2<@l2 && $col2<200; $col2++) {
  921. printf("%3i %3i %3i\n",$col2,$j2[$col2],$l2[$col2]);
  922. }
  923. }
  924. }
  925. # DEBUG
  926. # Construct alignment of $aaq <-> $aapdb via alignments $aaq <-> $aat and $aat <-> $aapdb:
  927. # Find $l1[$col1] = line of pdb file corresponding to residue $aat[$col1] and $aaq[$col1]
  928. $col2=0;
  929. for ($col1=0; $col1<@aaq; $col1++) {
  930. if ($j1[$col1]==0 || $i1[$col1]==0) {$l1[$col1]=0; next;} # skip gaps in query and gaps in template
  931. while ($j2[$col2]<$col1+1) {$col2++;} # in $j2[col2] first index is 1, in $col1 first column is 0
  932. $l1[$col1] = $l2[$col2];
  933. if ($v>=4) {printf("l1[%i]=%i l2[%i]=%i\n",$col1,$l1[$col1],$col2,$l2[$col2]);}
  934. }
  935. if ($pdbcode ne "NONE") {
  936. if ($outformat eq "TS") {
  937. for ($col1=0; $col1<@aat; $col1++) {
  938. if ($i1[$col1]==0) {next;} # skip gaps in query
  939. if ($j1[$col1]==0) {next;} # skip gaps in template sequence
  940. if ($l1[$col1]==0) {next;} # skip if corresponding residue was skipped in pdb file
  941. $printblock[$k].=sprintf("ATOM %5i CA %3s %4i %-50.50s\n",$i1[$col1],&One2ThreeLetter($aaq[$col1]),$i1[$col1]+$shift,$coord[$l1[$col1]]);
  942. if ($v>=4) {
  943. printf("ATOM %5i CA %3s %4i %-50.50s\n",$i1[$col1],&One2ThreeLetter($aaq[$col1]),$i1[$col1]+$shift,$coord[$l1[$col1]]);
  944. }
  945. }
  946. } else {
  947. for ($col1=0; $col1<@aat; $col1++) {
  948. if ($i1[$col1]==0) {next;} # skip gaps in query
  949. if ($j1[$col1]==0) {next;} # skip gaps in template sequence
  950. if ($l1[$col1]==0) {next;} # skip if corresponding residue was skipped in pdb file
  951. $printblock[$k].=sprintf("%1s %3i %1s %s\n",$aaq[$col1],$i1[$col1],$aat[$col1],$nres[$l1[$col1]]);
  952. if ($v>=4) {printf("%1s %3i %1s %s\n",$aaq[$col1],$i1[$col1],$aat[$col1],$nres[$l1[$col1]]);}
  953. }
  954. }
  955. }
  956. $printblock[$k].=sprintf("TER\n");
  957. return;
  958. }
  959. # Extract pdbcode and construct name of pdbfile and return in global variables $pdbid and $chain
  960. sub ExtractPdbcodeAndChain()
  961. {
  962. my $name=$_[0];
  963. $name=~/^(\S+)/;
  964. $name=$1;
  965. # SCOP ID? (d3lkfa_,d3grs_3,d3pmga1,g1m26.1)
  966. if ($name=~/^[defgh](\d[a-z0-9]{3})([a-z0-9_.])[a-z0-9_]$/) {
  967. $pdbcode=$1;
  968. if ($2 eq "_") {$chain="[A ]";} else {$chain=uc($2);}
  969. }
  970. # PDB ID? (8fab, 1a0i)
  971. elsif ($name=~/^(\d[a-z0-9]{3})$/) {
  972. $pdbcode=$1;
  973. $chain="[A ]";
  974. }
  975. # PDB ID? (8fab_A)
  976. elsif ($name=~/^(\d[a-z0-9]{3})_(\S)$/) {
  977. $pdbcode=$1;
  978. $chain=$2;
  979. }
  980. # PDB ID? (1u1z_ABC)
  981. elsif ($name=~/^(\d[a-z0-9]{3})_(\S\S+)$/) {
  982. $pdbcode=$1;
  983. $chain="[$2]";
  984. }
  985. # DALI ID? (8fabA_0,1a0i_2)
  986. elsif ($name=~/^(\d[a-z0-9]{3})([A-Za-z0-9]?)_\d+$/) {
  987. $pdbcode=$1;
  988. $chain=$2;
  989. }
  990. else {
  991. $pdbcode=$name;
  992. $chain="A";
  993. # return 1; # no SCOP/DALI/pdb sequence
  994. }
  995. $pdbfile = &FindPDBfile($pdbcode, $chain);
  996. if ($pdbfile eq "") {
  997. if ($v>=2) {print("Warning: no pdb file found for sequence name '$name'\n");}
  998. return 1;
  999. }
  1000. return 0;
  1001. }
  1002. # Resort arrays according to sorting array0:
  1003. # Resort(\@array0,\@array1,...,\@arrayN)
  1004. sub Sort()
  1005. {
  1006. my $p_array0 = $_[0];
  1007. my @index=();
  1008. for (my $i=0; $i<@{$p_array0}; $i++) {$index[$i]=$i;}
  1009. @index = sort { ${$p_array0}[$a] <=> ${$p_array0}[$b] } @index;
  1010. foreach my $p_array (@_) {
  1011. my @dummy = @{$p_array};
  1012. @{$p_array}=();
  1013. foreach my $i (@index) {
  1014. push(@{$p_array}, $dummy[$i]);
  1015. }
  1016. }
  1017. }
  1018. ##################################################################################
  1019. # Convert three-letter amino acid code into one-letter code
  1020. ##################################################################################
  1021. sub Three2OneLetter {
  1022. my $res=uc($_[0]);
  1023. if ($res eq "GLY") {return "G";}
  1024. elsif ($res eq "ALA") {return "A";}
  1025. elsif ($res eq "VAL") {return "V";}
  1026. elsif ($res eq "LEU") {return "L";}
  1027. elsif ($res eq "ILE") {return "I";}
  1028. elsif ($res eq "MET") {return "M";}
  1029. elsif ($res eq "PHE") {return "F";}
  1030. elsif ($res eq "TYR") {return "Y";}
  1031. elsif ($res eq "TRP") {return "W";}
  1032. elsif ($res eq "ASN") {return "N";}
  1033. elsif ($res eq "ASP") {return "D";}
  1034. elsif ($res eq "GLN") {return "Q";}
  1035. elsif ($res eq "GLU") {return "E";}
  1036. elsif ($res eq "CYS") {return "C";}
  1037. elsif ($res eq "PRO") {return "P";}
  1038. elsif ($res eq "SER") {return "S";}
  1039. elsif ($res eq "THR") {return "T";}
  1040. elsif ($res eq "LYS") {return "K";}
  1041. elsif ($res eq "HIS") {return "H";}
  1042. elsif ($res eq "ARG") {return "R";}
  1043. # The HETATM selenomethionine is read by MODELLER like a normal MET in both its HETATM_IO=off and on mode!
  1044. elsif ($res eq "MSE") {return "M";} # SELENOMETHIONINE
  1045. elsif ($res eq "ASX") {return "B";}
  1046. elsif ($res eq "GLX") {return "Z";}
  1047. else {return "X";}
  1048. # The following post-translationally modified residues are ignored by MODELLER in its default SET HETATM_IO=off mode
  1049. # elsif ($res eq "SEC") {return "C";} # SELENOCYSTEINE
  1050. # elsif ($res eq "SEP") {return "S";} # PHOSPHOSERINE
  1051. # elsif ($res eq "TPO") {return "T";} # PHOSPHOTHREONINE
  1052. # elsif ($res eq "TYS") {return "Y";} # SULFONATED TYROSINE
  1053. # elsif ($res eq "KCX") {return "K";} # LYSINE NZ-CARBOXYLIC ACID
  1054. }
  1055. ##################################################################################
  1056. # Convert one-letter amino acid code into three-letter code
  1057. ##################################################################################
  1058. sub One2ThreeLetter {
  1059. my $res=uc($_[0]);
  1060. if ($res eq "G") {return "GLY";}
  1061. elsif ($res eq "A") {return "ALA";}
  1062. elsif ($res eq "V") {return "VAL";}
  1063. elsif ($res eq "L") {return "LEU";}
  1064. elsif ($res eq "I") {return "ILE";}
  1065. elsif ($res eq "M") {return "MET";}
  1066. elsif ($res eq "F") {return "PHE";}
  1067. elsif ($res eq "Y") {return "TYR";}
  1068. elsif ($res eq "W") {return "TRP";}
  1069. elsif ($res eq "N") {return "ASN";}
  1070. elsif ($res eq "D") {return "ASP";}
  1071. elsif ($res eq "Q") {return "GLN";}
  1072. elsif ($res eq "E") {return "GLU";}
  1073. elsif ($res eq "C") {return "CYS";}
  1074. elsif ($res eq "P") {return "PRO";}
  1075. elsif ($res eq "S") {return "SER";}
  1076. elsif ($res eq "T") {return "THR";}
  1077. elsif ($res eq "K") {return "LYS";}
  1078. elsif ($res eq "H") {return "HIS";}
  1079. elsif ($res eq "R") {return "ARG";}
  1080. elsif ($res eq "U") {return "SEC";}
  1081. elsif ($res eq "B") {return "ASX";}
  1082. elsif ($res eq "Z") {return "GLX";}
  1083. else {return "UNK";}
  1084. }
  1085. # Find the pdb file with $pdbcode in pdb directory
  1086. sub FindPDBfile() {
  1087. my $pdbcode=lc($_[0]);
  1088. foreach $pdbdir (@pdbdirs) {
  1089. if (! -e "$pdbdir") {warn("Warning in $program: pdb directory '$pdbdir' does not exist!\n"); next;}
  1090. if (-e "$pdbdir/all") {$pdbfile="$pdbdir/all/";}
  1091. elsif (-e "$pdbdir/divided") {$pdbfile="$pdbdir/divided/".substr($pdbcode,1,2)."/";}
  1092. else {$pdbfile="$pdbdir/";}
  1093. if ($pdbdir=~/divided.?$/) {$pdbfile.=substr($pdbcode,1,2)."/";}
  1094. if (-e $pdbfile."pdb$pdbcode.ent") {$pdbfile.="pdb$pdbcode.ent";}
  1095. elsif (-e $pdbfile."pdb$pdbcode.ent.gz") {$pdbfile="gunzip -c $pdbfile"."pdb$pdbcode.ent.gz |";}
  1096. elsif (-e $pdbfile."pdb$pdbcode.ent.Z") {$pdbfile="gunzip -c $pdbfile"."pdb$pdbcode.ent.Z |";}
  1097. elsif (-e $pdbfile."$pdbcode.pdb") {$pdbfile.="$pdbcode.pdb";}
  1098. elsif (scalar(@_) == 2 && -e $pdbfile.$_[0]."\_".$_[1].".pdb") {$pdbfile.=$_[0]."\_".$_[1].".pdb"}
  1099. else {next;}
  1100. return $pdbfile;
  1101. }
  1102. printf(STDERR "Warning in $program: Cannot find pdb file $pdbfile"."pdb$pdbcode.ent!\n");
  1103. return "";
  1104. }