addss.pl 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. #!/usr/bin/perl
  2. #
  3. # addss.pl
  4. # Add PSIPRED secondary structure prediction (and DSSP annotation) to an MSA or HMMER file.
  5. # Output format is A3M (for input alignments) or HMMER (see User Guide).
  6. # HHsuite version 3.0.0 (15-03-2015)
  7. #
  8. # Reference:
  9. # Remmert M., Biegert A., Hauser A., and Soding J.
  10. # HHblits: Lightning-fast iterative protein sequence searching by HMM-HMM alignment.
  11. # Nat. Methods, epub Dec 25, doi: 10.1038/NMETH.1818 (2011).
  12. # (C) Johannes Soeding and Michael Remmert, 2012
  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 tempdir /;
  28. use File::Copy;
  29. use strict;
  30. my $ss_cit =
  31. "PSIPRED: Jones DT. (1999) Protein secondary structure prediction based on position-specific scoring matrices. JMB 292:195-202.";
  32. # Module needed for aligning DSSP-sequence
  33. $| = 1; # Activate autoflushing on STDOUT
  34. # Default values:
  35. our $v = 2; # verbose mode
  36. my $numres = 0; # number of residues per line for secondary structure
  37. my $informat = "a3m"; # input format
  38. my $neff = 7; # use alignment with this diversity for PSIPRED prediction
  39. my $program = $0; # name of perl script
  40. my $pdbfile;
  41. my $help = "
  42. addss.pl from HHsuite $VERSION
  43. Add PSIPRED secondary structure prediction (and DSSP annotation) to a multiple sequence alignment (MSA)
  44. or HMMER (multi-)model file.
  45. If the input file is an MSA, the predicted secondary structure and confidence values are added as
  46. special annotation sequences with names >ss_pred, >ss_conf, and >ss_dssp to the top of the output
  47. A3M alignment. If no output file is given, the output file will have the same name as the input file,
  48. except for the extension being replaced by '.a3m'. Allowed input formats are A3M (default),
  49. A2M/FASTA (-fas, -a2m), CLUSTAL (-clu), STOCKHOLM (-sto), HMMER (-hmm).
  50. If the input file contains HMMER models, records SSPRD and SSCON containing predicted secondary
  51. structure and confidence values are added to each model. In this case the output file name is
  52. obligatory and must be different from the input file name.
  53. Usage: perl addss.pl <ali_file> [<outfile>] [-fas|-a3m|-clu|-sto]
  54. or perl addss.pl <hhm_file> <outfile> -hmm
  55. \n";
  56. # Variable declarations
  57. my $line;
  58. my @seqs; # sequences from infile (except >aa_ and >ss_pred sequences)
  59. my $query_length;
  60. my $header; # header of MSA: everything before first '>'
  61. my $name; # query in fasta format: '>$name [^\n]*\n$qseq\n'
  62. my $qseq; # residues of query sequence
  63. my $infile;
  64. my $outfile;
  65. my $ss_pred = ""; # psipred ss states
  66. my $ss_conf = ""; # psipred confidence values
  67. my $ss_dssp; # dssp states as string
  68. my $sa_dssp; # relative solvent accessibility from dssp as string {A,B,C,D,E} A:absolutely buried, B:buried, E:exposed
  69. my $aa_dssp; # residues from dssp file as string
  70. my $aa_astr; # residues from infile as string
  71. my $q_match; # number of match states in query sequence
  72. my $xseq; # sequence x returned from Align.pm
  73. my $yseq; # sequence y returned from Align.pm
  74. my $Sstr; # match sequence returned from Align.pm
  75. ###############################################################################################
  76. # Processing command line input
  77. ###############################################################################################
  78. if ( @ARGV < 1 ) { die($help); }
  79. my $options = "";
  80. for ( my $i = 0 ; $i < @ARGV ; $i++ ) { $options .= " $ARGV[$i] "; }
  81. #Input format fasta?
  82. if ( $options =~ s/ -fas\s/ /g ) { $informat = "fas"; }
  83. elsif ( $options =~ s/ -a2m\s/ /g ) { $informat = "a2m"; }
  84. elsif ( $options =~ s/ -a3m\s/ /g ) { $informat = "a3m"; }
  85. elsif ( $options =~ s/ -clu\s/ /g ) { $informat = "clu"; }
  86. elsif ( $options =~ s/ -sto\s/ /g ) { $informat = "sto"; }
  87. elsif ( $options =~ s/ -hmm\s/ /g ) { $informat = "hmm"; }
  88. if ( $options =~ s/ -v\s+(\d+) / /g ) { $v = $1; }
  89. # Set input and output file
  90. if ( $options =~ s/ -i\s+(\S+) // ) { $infile = $1; }
  91. if ( $options =~ s/ -o\s+(\S+) // ) { $outfile = $1; }
  92. if ( $options =~ s/^\s*([^-]\S*) // ) { $infile = $1; }
  93. if ( $options =~ s/^\s*([^-]\S*) // ) { $outfile = $1; }
  94. # Warn if unknown options found or no infile/outfile
  95. if ( $options !~ /^\s*$/ ) {
  96. $options =~ s/^\s*(.*?)\s*$/$1/g;
  97. die("Error: unknown options '$options'\n");
  98. }
  99. if ( !$infile ) { print($help); exit(1); }
  100. my $v2 = $v - 1;
  101. if ( $v2 > 2 ) { $v2--; }
  102. if ( $v2 < 0 ) { $v2 = 0; }
  103. if ( $informat eq "hmm" && !$outfile ) {
  104. print(
  105. "Error: no output file given. With the -hmm option an output file is obligatory\n"
  106. );
  107. exit(1);
  108. }
  109. ###############################################################################################
  110. # Reformat input alignment to a3m and psiblast-readable format and generate file with query sequence
  111. ###############################################################################################
  112. my $inbase; # $inbasename of infile: remove extension
  113. my $inroot; # $inbasename of infile: remove path and extension
  114. if ( $infile =~ /(.*)\..*/ ) { $inbase = $1; }
  115. else { $inbase = $infile; } # remove extension
  116. if ( $inbase =~ /.*\/(.*)/ ) { $inroot = $1; }
  117. else { $inroot = $inbase; } # remove path
  118. # Create tmpfile
  119. my $tmpdir;
  120. if ( $v <= 3 ) { $tmpdir = tempdir( CLEANUP => 1 ); }
  121. else { $tmpdir = tempdir( CLEANUP => 0 ); }
  122. my ( $tmpf, $tmpfile ) = tempfile( DIR => $tmpdir );
  123. my $tmpfile_no_dir;
  124. if ( $tmpfile =~ /.*\/(.*)/ ) {
  125. $tmpfile_no_dir = $1;
  126. }
  127. else {
  128. $tmpfile_no_dir = $tmpfile;
  129. }
  130. my $tmp_outfile = "$tmpfile.out";
  131. if ( $infile eq "stdin" ) {
  132. my @stdin = <STDIN>;
  133. $infile = "$tmpfile.stdin";
  134. open(OUT, ">$infile");
  135. foreach my $line (@stdin) {
  136. print OUT $line;
  137. }
  138. close(OUT);
  139. }
  140. ############################################################################################
  141. if ( $informat ne "hmm" ) {
  142. if ( !$outfile ) { $outfile = "$inbase.a3m"; }
  143. # Use first sequence to define match states and reformat input file to a3m and psi
  144. if ( $informat ne "a3m" ) {
  145. &HHPaths::System(
  146. "$hhscripts/reformat.pl -v $v2 -M first $informat a3m $infile $tmpfile.1.in.a3m"
  147. );
  148. }
  149. else {
  150. &HHPaths::System("cp $infile $tmpfile.1.in.a3m");
  151. }
  152. # Sanitise the input file - remove all '>' characters, except the one at the start of the string
  153. # Note that this will corrupt the file if the "header" is not separated by a newline from the
  154. # first line that starts with '>'
  155. open( INFILE, "<$tmpfile.1.in.a3m" );
  156. open( OUTFILE, ">$tmpfile.in.a3m" );
  157. while ( $line = <INFILE> ) {
  158. $line =~ s/([^>]+)>/$1/g;
  159. print OUTFILE $line;
  160. }
  161. close(INFILE);
  162. close(OUTFILE);
  163. # Read query sequence
  164. open( INFILE, "<$tmpfile.in.a3m" )
  165. or die("ERROR: cannot open $tmpfile.in.a3m!\n");
  166. $/ = ">"; # set input field separator
  167. my $i = 0;
  168. $qseq = "";
  169. $header = <INFILE>;
  170. $header =~ s />$//;
  171. while ( $line = <INFILE> ) {
  172. $line =~ s/>$//;
  173. if ( $line =~ /^ss_/ || $line =~ /^aa_/ ) { next; }
  174. $seqs[ $i++ ] = ">$line";
  175. if ( !$qseq ) {
  176. $line =~ s/^(.*)[^\n]*//;
  177. $name = $1;
  178. $qseq = $line;
  179. $qseq =~ s/\n//g;
  180. }
  181. }
  182. close(INFILE);
  183. $/ = "\n"; # set input field separator
  184. if ( $qseq =~ /\-/ ) {
  185. # First sequence contains gaps => calculate consensus sequence
  186. &HHPaths::System(
  187. "hhconsensus -i $tmpfile.in.a3m -s $tmpfile.sq -o $tmpfile.in.a3m > /dev/null"
  188. );
  189. }
  190. else {
  191. $query_length = ( $qseq =~ tr/A-Z/A-Z/ );
  192. $qseq =~ tr/A-Z//cd; # remove everything except capital letters
  193. # Write query sequence file in FASTA format
  194. open( QFILE, ">$tmpfile.sq" )
  195. or die("ERROR: can't open $tmpfile.sq: $!\n");
  196. printf( QFILE ">%s\n%s\n", $name, $qseq );
  197. close(QFILE);
  198. }
  199. # Filter alignment to diversity $neff
  200. if ( $v >= 1 ) { printf(STDERR "Filtering alignment to diversity $neff ...\n"); }
  201. &HHPaths::System(
  202. "hhfilter -v $v2 -neff $neff -i $tmpfile.in.a3m -o $tmpfile.in.a3m");
  203. # Reformat into PSI-BLAST readable file for jumpstarting
  204. &HHPaths::System(
  205. "$hhscripts/reformat.pl -v $v2 -r -noss a3m psi $tmpfile.in.a3m $tmpfile.in.psi"
  206. );
  207. open( ALIFILE, ">$tmp_outfile" )
  208. || die("ERROR: cannot open $tmp_outfile: $!\n");
  209. printf( ALIFILE "%s", $header );
  210. # Add DSSP sequence (if available)
  211. if ( $dssp ne "" ) {
  212. if ( !&AppendDsspSequences("$tmpfile.sq") ) {
  213. if ($numres) {
  214. # insert a line break every $numres residues
  215. $ss_dssp =~ s/(\S{$numres})/$1\n/g;
  216. }
  217. printf( ALIFILE ">ss_dssp\n%s\n", $ss_dssp );
  218. if ( $v >= 1 ) { print(STDERR "\nAdding DSSP state sequence ...\n"); }
  219. }
  220. }
  221. # Secondary structure prediction with psipred
  222. if ( $v >= 2 ) {
  223. print(STDERR "Predicting secondary structure with PSIPRED ... ");
  224. }
  225. &RunPsipred("$tmpfile.sq");
  226. if ( open( PSIPREDFILE, "<$tmpfile.horiz" ) ) {
  227. $ss_conf = "";
  228. $ss_pred = "";
  229. # Read Psipred file
  230. while ( $line = <PSIPREDFILE> ) {
  231. if ( $line =~ /^Conf:\s+(\S+)/ ) { $ss_conf .= $1; }
  232. elsif ( $line =~ /^Pred:\s+(\S+)/ ) { $ss_pred .= $1; }
  233. }
  234. close(PSIPREDFILE);
  235. $ss_conf =~ tr/0-9/0/c; # replace all non-numerical symbols with a 0
  236. if ($numres) {
  237. $ss_pred =~ s/(\S{$numres})/$1\n/g
  238. ; # insert a line break every $numres residues
  239. $ss_conf =~ s/(\S{$numres})/$1\n/g
  240. ; # insert a line break every $numres residues
  241. }
  242. if (length($ss_pred) > 0 && length($ss_conf) > 0) {
  243. printf( ALIFILE ">ss_pred PSIPRED predicted secondary structure\n%s\n", $ss_pred);
  244. printf( ALIFILE ">ss_conf PSIPRED confidence values\n%s\n", $ss_conf );
  245. }
  246. }
  247. # Append alignment sequences to psipred sequences
  248. for ( $i = 0 ; $i < @seqs ; $i++ ) {
  249. printf( ALIFILE "%s", $seqs[$i] );
  250. }
  251. close(ALIFILE);
  252. if ( !$outfile ) {
  253. $outfile = "$inbase.a3m";
  254. }
  255. if($outfile ne "stdout") {
  256. move( $tmp_outfile, $outfile );
  257. }
  258. else {
  259. open(IN, "<$tmp_outfile");
  260. foreach $line(<IN>) {
  261. print $line;
  262. }
  263. close(IN)
  264. }
  265. if ( $v >= 2 ) { print(STDERR "done \n"); }
  266. }
  267. ##############################################################
  268. # HMMER format
  269. else {
  270. if ( !$outfile ) {
  271. $outfile = "$inbase.hmm";
  272. }
  273. my $log2 = log(2);
  274. my @logoddsmat;
  275. my @lines;
  276. my $length;
  277. my $query;
  278. # empirically determined scale factor between HMMER bit score and PSI-BLAST score, 0.3 for HMMER3
  279. my $scale = 0.13;
  280. my $acc;
  281. my $name;
  282. my $desc;
  283. my $nmodels = 0;
  284. open( INFILE, "<$infile" ) || die("ERROR: cannot open $infile: $!\n");
  285. open( OUTFILE, ">$tmp_outfile" ) || die("ERROR: cannot open $tmp_outfile: $!\n");
  286. # Read HMMER file model by model
  287. while ( $line = <INFILE> ) {
  288. # Search for start of next model
  289. while ( $line && $line !~ /^HMMER/ && $line !~ /^NAME / ) {
  290. $line = <INFILE>;
  291. }
  292. if ( $line =~ /^HMMER3/ ) {
  293. $scale = 0.3;
  294. @logoddsmat = ();
  295. @lines = ($line);
  296. while ( $line = <INFILE> ) {
  297. push( @lines, $line );
  298. if ( $line =~ /^LENG/ ) { last; }
  299. }
  300. $line =~ /^LENG\s+(\d+)/;
  301. $length = $1; # number of match states in HMM
  302. $query = ""; # query residues from NULL emission lines
  303. while ( $line = <INFILE> ) {
  304. push( @lines, $line );
  305. if ( $line =~ /^\s*m->m/ ) { last; }
  306. }
  307. push( @lines, $line = <INFILE> );
  308. if ( $line !~ /^\s*COMPO/ ) {
  309. die("Error: need null-model probablities (Parameter COMPO)!\n");
  310. }
  311. $line =~ s/^\s*COMPO\s+(\S.*\S)\s*$/$1/;
  312. my @nullmodel = split( /\s+/, $line );
  313. @nullmodel =
  314. map { $_ = exp( -1 * $_ ) }
  315. @nullmodel; # Transform to probabilities
  316. push( @lines, $line = <INFILE> ); # state 0 insert emission
  317. push( @lines, $line = <INFILE> ); # transisitions from begin state
  318. while ( $line = <INFILE> ) {
  319. push( @lines, $line );
  320. if ( $line =~ /^\/\// ) { last; }
  321. $line =~ s/^\s*\d+\s+(\S.*\S)\s+\d+\s+(\S)\s+\S\s*$/$1/;
  322. $query .= $2;
  323. my @probs = split( /\s+/, $line );
  324. @probs =
  325. map { $_ = exp( -1 * $_ ) }
  326. @probs; # Transform to probabilities
  327. # calculate log-odds
  328. my @logodds = ();
  329. for ( my $a = 0 ; $a < scalar(@probs) ; $a++ ) {
  330. my $logodd =
  331. ( log( $probs[$a] / $nullmodel[$a] ) / $log2 ) * 1000;
  332. push( @logodds, $logodd );
  333. }
  334. push( @logoddsmat, \@logodds );
  335. push( @lines, $line = <INFILE> );
  336. push( @lines, $line = <INFILE> );
  337. }
  338. }
  339. else {
  340. $scale = 0.13;
  341. if ( $line !~ /^HMMER/ && $line !~ /^NAME / ) {
  342. last;
  343. } # first line in each model must begin with 'HMMER...'
  344. @logoddsmat = ();
  345. @lines = ($line);
  346. while ( $line = <INFILE> ) {
  347. push( @lines, $line );
  348. if ( $line =~ /^LENG/ ) { last; }
  349. }
  350. $line =~ /^LENG\s+(\d+)/;
  351. $length = $1; # number of match states in HMM
  352. $query = ""; # query residues from NULL emission lines
  353. while ( $line = <INFILE> ) {
  354. push( @lines, $line );
  355. if ( $line =~ /^\s*m->m/ ) { last; }
  356. }
  357. push( @lines, $line = <INFILE> );
  358. while ( $line = <INFILE> ) {
  359. push( @lines, $line );
  360. if ( $line =~ /^\/\// ) { last; }
  361. $line =~ s/^\s*\d+\s+(\S.*\S)\s*$/$1/;
  362. my @logodds = split( /\s+/, $line );
  363. push( @logoddsmat, \@logodds );
  364. push( @lines, $line = <INFILE> );
  365. $line =~ /^\s*(\S)/;
  366. $query .= $1;
  367. push( @lines, $line = <INFILE> );
  368. }
  369. }
  370. # Write mtx matrix
  371. open( MTXFILE, ">$tmpfile.mtx" ) || die("ERROR: cannot open $tmpfile.mtx: $!\n");
  372. printf( MTXFILE "%i\n", $length );
  373. printf( MTXFILE "%s\n", $query );
  374. printf( MTXFILE
  375. "2.670000e-03\n4.100000e-02\n-3.194183e+00\n1.400000e-01\n2.670000e-03\n4.420198e-02\n-3.118986e+00\n1.400000e-01\n3.176060e-03\n1.339561e-01\n-2.010243e+00\n4.012145e-01\n"
  376. );
  377. while (@logoddsmat) {
  378. my @logodds = @{ shift(@logoddsmat) };
  379. print( MTXFILE "-32768 " );
  380. splice( @logodds, 1, 0, -32768 / $scale )
  381. ; # insert logodds value for B
  382. splice( @logodds, 20, 0, -100 / $scale )
  383. ; # insert logodds value for X
  384. splice( @logodds, 22, 0, -32768 / $scale )
  385. ; # insert logodds value for Z
  386. for ( my $i = 0 ; $i < 23 ; $i++ ) {
  387. printf( MTXFILE "%4.0f ", $scale * $logodds[$i] );
  388. }
  389. print( MTXFILE "-32768 -400\n" );
  390. }
  391. close(MTXFILE);
  392. # Start Psiblast from checkpoint file tmp.chk that was generated to build the profile
  393. # Psipred version < 3.0
  394. if ( -e "$datadir/weights.dat4" ) {
  395. &HHPaths::System(
  396. "$execdir/psipred $tmpfile.mtx $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3 $datadir/weights.dat4 > $tmpfile.ss"
  397. );
  398. }
  399. else {
  400. &HHPaths::System(
  401. "$execdir/psipred $tmpfile.mtx $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3 > $tmpfile.ss"
  402. );
  403. }
  404. # READ PSIPRED file
  405. if (open( PSIPRED, "$execdir/psipass2 $datadir/weights_p2.dat 1 0.98 1.09 $tmpfile.ss2 $tmpfile.ss |"))
  406. {
  407. $ss_conf = "";
  408. $ss_pred = "";
  409. # Read Psipred file
  410. while ( $line = <PSIPRED> ) {
  411. if ( $line =~ /^Conf:\s+(\d+)/ ) { $ss_conf .= $1; }
  412. elsif ( $line =~ /^Pred:\s+(\S+)/ ) { $ss_pred .= $1; }
  413. }
  414. close(PSIPREDFILE);
  415. }
  416. # Add secondary structure to HMMER output file and print
  417. foreach $line (@lines) {
  418. if ( $line =~ /^SSPRD/ || $line =~ /^SSCON/ || $line =~ /^SSCIT/ ) {
  419. next;
  420. }
  421. if ( $line =~ /^HMM / ) {
  422. # insert a line break every 80 residues
  423. $ss_pred =~ s/(\S{80})/$1\nSSPRD /g;
  424. # insert a line break every 80 residues
  425. $ss_conf =~ s/(\S{80})/$1\nSSCON /g;
  426. printf( OUTFILE
  427. "SSCIT HHsearch-readable PSIPRED secondary structure prediction:\n"
  428. );
  429. printf( OUTFILE "SSPRD %s\n", $ss_pred );
  430. printf( OUTFILE "SSCON %s\n", $ss_conf );
  431. printf( OUTFILE "SSCIT %s\n", $ss_cit );
  432. }
  433. printf( OUTFILE $line );
  434. }
  435. $nmodels++;
  436. }
  437. close(OUTFILE);
  438. close(INFILE);
  439. if ( !$outfile ) {
  440. $outfile = "$inbase.a3m";
  441. }
  442. move( $tmp_outfile, $outfile );
  443. &HHPaths::System("rm $tmpfile.mtx $tmpfile.ss $tmpfile.ss2");
  444. if ( $v >= 2 ) {
  445. printf( STDERR "Added PSIPRED secondary structure to %i models\n", $nmodels );
  446. }
  447. }
  448. if ( $v <= 3 ) {
  449. unlink("$tmpfile.in.a3m");
  450. unlink("$tmpfile.in.psi");
  451. unlink("$tmpfile.horiz");
  452. unlink("$tmpfile.dssp");
  453. }
  454. exit;
  455. ##############################################################################################
  456. # Run SS prediction starting from alignment in $tmpfile.in.psi (called by BuildAlignment)
  457. ##############################################################################################
  458. sub RunPsipred() {
  459. # This is a simple script which will carry out all of the basic steps
  460. # required to make a PSIPRED V2 prediction. Note that it assumes that the
  461. # following programs are in the appropriate directories:
  462. # blastpgp - PSIBLAST executable (from NCBI toolkit)
  463. # makemat - IMPALA utility (from NCBI toolkit)
  464. # psipred - PSIPRED V2 program
  465. # psipass2 - PSIPRED V2 program
  466. my $infile = $_[0];
  467. my $basename; #file name without extension
  468. my $rootname; #basename without directory path
  469. if ( $infile =~ /^(.*)\..*?$/ ) { $basename = $1; }
  470. else { $basename = $infile; }
  471. if ( $basename =~ /^.*\/(.*?)$/ ) { $rootname = $1; }
  472. else { $rootname = $basename; }
  473. # Does dummy database exist?
  474. if ( !-e "$dummydb.phr" ) {
  475. if ( !-e "$dummydb" ) {
  476. die "Error in addss.pl: Could not find $dummydb\n";
  477. }
  478. &HHPaths::System("cp $infile $dummydb");
  479. &HHPaths::System("$ncbidir/formatdb -i $dummydb");
  480. if ( !-e "$dummydb.phr" ) {
  481. die "Error in addss.pl: Could not find nor create index files for $dummydb\n";
  482. }
  483. }
  484. # Start Psiblast from checkpoint file tmp.chk that was generated to build the profile
  485. &HHPaths::System(
  486. "$ncbidir/blastpgp -b 1 -j 1 -h 0.001 -d $dummydb -i $infile -B $tmpfile.in.psi -C $tmpfile.chk 1> $tmpfile.blalog 2> $tmpfile.blalog"
  487. );
  488. #print("Predicting secondary structure...\n");
  489. &HHPaths::System( "echo " . "$tmpfile_no_dir" . ".chk > $tmpfile.pn\n" );
  490. &HHPaths::System( "echo " . "$tmpfile_no_dir" . ".sq > $tmpfile.sn\n" );
  491. &HHPaths::System("$ncbidir/makemat -P $tmpfile");
  492. # Start Psiblast from checkpoint file tmp.chk that was generated to build the profile
  493. if ( -e "$datadir/weights.dat4" ) { # Psipred version < 3.0
  494. &HHPaths::System(
  495. "$execdir/psipred $tmpfile.mtx $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3 $datadir/weights.dat4 > $tmpfile.ss"
  496. );
  497. }
  498. else {
  499. &HHPaths::System(
  500. "$execdir/psipred $tmpfile.mtx $datadir/weights.dat $datadir/weights.dat2 $datadir/weights.dat3 > $tmpfile.ss"
  501. );
  502. }
  503. &HHPaths::System(
  504. "$execdir/psipass2 $datadir/weights_p2.dat 1 0.98 1.09 $tmpfile.ss2 $tmpfile.ss > $tmpfile.horiz"
  505. );
  506. # Remove temporary files
  507. if ( $v <= 3 ) {
  508. unlink(
  509. split ' ',
  510. "$tmpfile.pn $tmpfile.sn $tmpfile.mn $tmpfile.chk $tmpfile.blalog $tmpfile.mtx $tmpfile.aux $tmpfile.ss $tmpfile.ss2 $tmpfile.sq"
  511. );
  512. }
  513. return;
  514. }
  515. ##############################################################################################
  516. # Read query sequence and extract dssp sequence
  517. ##############################################################################################
  518. sub AppendDsspSequences() {
  519. my $qfile = $_[0];
  520. my $line; #input line
  521. my $name; #name of sequence in in file, e.g. d1g8ma1
  522. my $qrange; #chain and residue range of query sequence
  523. my $aas = ""; #amino acids from in file for each $name
  524. my $dsspfile;
  525. my $pdbfile;
  526. my $pdbcode
  527. ; #pdb code for accessing dssp file; shortened from in code, e.g. 1g8m
  528. my @ss_dssp = (); #dssp states for residues (H,E,L)
  529. my @sa_dssp = (); #dssp states for residues (H,E,L)
  530. my @aa_dssp = (); #residues in dssp file
  531. my @aa_astr = (); #residues from infile
  532. my $length; #length of sequence
  533. # Default parameters for Align.pm
  534. our $d = 3; # gap opening penatlty for Align.pm
  535. our $e = 0.1; # gap extension penatlty for Align.pm
  536. our $g = 0.09; # endgap penatlty for Align.pm
  537. our $matrix = "identity";
  538. # Read query sequence -> $name, $nameline, $range, $aas
  539. open( QFILE, "<$qfile" ) || die("cannot open $qfile: $!");
  540. while ( $line = <QFILE> ) {
  541. if ( $line =~ />(\S+)/ ) {
  542. $name = $1;
  543. # SCOPe ID? (d3lkfa_,d3grs_3,d3pmg.1)
  544. if ( $line =~ /^>(d[a-z0-9]{4}[a-z0-9_.][a-z0-9_])/ )
  545. {
  546. $pdbcode = $1;
  547. $qrange = "";
  548. }
  549. # SCOP ID? (d3lkfa_,d3grs_3,d3pmga1,g1m26.1)
  550. elsif ( $line =~
  551. /^>[defgh](\d[a-z0-9]{3})[a-z0-9_.][a-z0-9_]\s+[a-z]\.\d+\.\d+\.\d+\s+\((\S+)\)/
  552. )
  553. {
  554. $pdbcode = $1;
  555. $qrange = $2;
  556. }
  557. # PDB ID? (8fab_A, 1a0i)
  558. elsif ( $line =~ /^>(\d[A-Za-z0-9]{3})_?(\S?)\s/ ) {
  559. $pdbcode = $1;
  560. if ( $2 ne "" ) { $qrange = "$2:"; }
  561. else { $qrange = "-"; }
  562. }
  563. # DALI ID? (8fabA_0,1a0i_2)
  564. elsif ( $line =~
  565. /^>(\d[a-z0-9]{3})[A-Za-z0-9]?_\d+\s+\d+\.\d+.\d+.\d+.\d+.\d+\s+\((\S+)\)/
  566. )
  567. {
  568. $pdbcode = $1;
  569. $qrange = $2;
  570. }
  571. else {
  572. if ( $v >= 3 ) {
  573. print( STDERR
  574. "Warning: no pdb code found in sequence name '$name'\n"
  575. );
  576. }
  577. close(QFILE);
  578. return
  579. 1; # no astral/DALI/pdb sequence => no dssp states available
  580. }
  581. $aas = "";
  582. }
  583. else {
  584. chomp($line);
  585. $line =~ tr/a-z \t/A-Z/d;
  586. $aas .= $line;
  587. }
  588. }
  589. close(QFILE);
  590. if ( $v >= 3 ) {
  591. printf( STDERR "Searching DSSP state assignments: name=%s range=%s\n",
  592. $name, $qrange);
  593. }
  594. # Try to open dssp file
  595. $pdbcode =~ tr/[A-Z]/[a-z]/;
  596. my $dsspfile = "$dsspdir/$pdbcode.dssp";
  597. if ( -e $dsspfile ) {
  598. ( $aa_dssp, $ss_dssp, $sa_dssp ) = &readDSSP( $dsspfile, $qrange );
  599. if ( length($aa_dssp) <= 20 ) {
  600. #dssp file is erroneous or chain identifiers in $qrange are erroneous
  601. if ( $v >= 3 ) {
  602. printf( STDERR "Warning in $program: Found only "
  603. . length($aa_dssp)
  604. . " residues in $dsspfile matching $qrange!\n" );
  605. }
  606. $aa_dssp = "";
  607. }
  608. }
  609. else {
  610. if ( $v >= 3 ) {
  611. printf( STDERR "Warning in $program: Cannot open $dsspfile!\n" );
  612. }
  613. }
  614. if ( $aa_dssp eq "" ) {
  615. $pdbfile = &OpenPDBfile($pdbcode);
  616. if ( $pdbfile eq "" ) { return 1; }
  617. $dsspfile = $tmpfile . ".dssp";
  618. # Thanks to Stefan Bienert for this patch
  619. &HHPaths::System("$dssp -i $pdbfile -o $dsspfile 2> /dev/null");
  620. if ( !-e $dsspfile ) {
  621. if ( $v >= 3 ) {
  622. printf( STDERR "Warning in $program: dssp couldn't generate file from $pdbfile. Skipping $name\n");
  623. }
  624. return 1;
  625. }
  626. else {
  627. ( $aa_dssp, $ss_dssp, $sa_dssp ) = &readDSSP( $dsspfile, $qrange );
  628. if ( length($aa_dssp) <= 20 ) {
  629. if ( $v >= 3 ) {
  630. printf( STDERR
  631. "Warning in $program: Found only %i residues in newly calculated $dsspfile matching $qrange!\n",
  632. length($aa_dssp)
  633. );
  634. }
  635. # Read in whole DSSP file
  636. ( $aa_dssp, $ss_dssp, $sa_dssp ) = &readDSSP( $dsspfile, "" );
  637. }
  638. }
  639. }
  640. if ( length($aa_dssp) == 0 ) {
  641. print(STDERR "WARNING: no residues found in $dsspfile\n");
  642. return 1;
  643. }
  644. if ( length($aa_dssp) <= 20 ) {
  645. printf(STDERR "WARNING: only %i residues found in $dsspfile\n", length($aa_dssp) );
  646. return 1;
  647. }
  648. # Postprocess $aa_dssp etc
  649. $aa_dssp =~ tr/a-z/CCCCCCCCCCCCCCCCCCCCCCCCCC/;
  650. $ss_dssp =~ tr/ I/CC/;
  651. $ss_dssp =~ s/ \S / /g;
  652. $ss_dssp =~ s/ \S\S / /g;
  653. # Align query with dssp sequence
  654. $aa_astr = $aas;
  655. $xseq = $aas;
  656. $yseq = $aa_dssp;
  657. my ( $imax, $imin, $jmax, $jmin );
  658. my ( @i, @j );
  659. # For long sequences extremely slow... should be replaced
  660. my $score = &AlignNW( \$xseq, \$yseq, \@i, \@j, \$imin, \$imax, \$jmin, \$jmax, \$Sstr );
  661. # Initialize strings (=arrays) for dssp states with "----...-"
  662. my @ss_dssp_ali = (); # $ss_dssp_ali[$i] is dssp state aligned to $aa_astr[$i]
  663. my @sa_dssp_ali = (); # $sa_dssp_ali[$i] is solvent accessibility string
  664. my @aa_dssp_ali = (); # $aa_dssp_ali[$i] is dssp residue aligned to $aa_astr[$i]
  665. # sum up to len+1 because 0'th element in @ss_dssp and @aa_dssp is dummy "-"
  666. for ( my $i = 0 ; $i <= length($aa_astr) ; $i++ ) {
  667. $ss_dssp_ali[$i] = "-";
  668. $sa_dssp_ali[$i] = "-";
  669. $aa_dssp_ali[$i] = "-";
  670. }
  671. # To each residue (from i=0 to len-1) of input sequence $aa_astr assign aligned dssp state
  672. @ss_dssp = split( //, $ss_dssp );
  673. @sa_dssp = split( //, $sa_dssp );
  674. @aa_dssp = split( //, $aa_dssp );
  675. @aa_astr = split( //, $aa_astr );
  676. my $len = 0;
  677. #add a gap symbol at beginning -> first residue is at 1!
  678. unshift( @aa_dssp, "-" );
  679. #add a gap symbol at beginning -> first residue is at 1!
  680. unshift( @ss_dssp, "-" );
  681. #add a gap symbol at beginning -> first residue is at 1!
  682. unshift( @sa_dssp, "-" );
  683. #add a gap symbol at beginning -> first residue is at 1!
  684. unshift( @aa_astr, "-" );
  685. for ( my $col = 0 ; $col < @i ; $col++ ) {
  686. if ( $i[$col] > 0 ) {
  687. # count match states (for score/len calculation)
  688. if ( $j[$col] > 0 ) {
  689. $len++;
  690. }
  691. $ss_dssp_ali[ $i[$col] ] = $ss_dssp[ $j[$col] ];
  692. $sa_dssp_ali[ $i[$col] ] = $sa_dssp[ $j[$col] ];
  693. $aa_dssp_ali[ $i[$col] ] = $aa_dssp[ $j[$col] ];
  694. }
  695. if ( $v >= 4 ) {
  696. printf(
  697. "%s %3i %s %3i\n",
  698. $aa_astr[ $i[$col] ], $i[$col],
  699. $aa_dssp[ $j[$col] ], $j[$col]
  700. );
  701. }
  702. }
  703. shift(@ss_dssp_ali); # throw out first "-"
  704. shift(@sa_dssp_ali); # throw out first "-"
  705. shift(@aa_dssp_ali); # throw out first "-"
  706. $aa_dssp = join( "", @aa_dssp_ali );
  707. $ss_dssp = join( "", @ss_dssp_ali );
  708. $sa_dssp = join( "", @sa_dssp_ali );
  709. # Debugging output
  710. if ( $v >= 4 ) {
  711. printf( STDOUT "DSSP: %s: length=%-3i score/len:%-5.3f\n",
  712. $name, $len, $score / $len );
  713. }
  714. if ( $v >= 4 ) {
  715. printf( "IN: %s\n", $xseq );
  716. printf( "MATCH: %s\n", $Sstr );
  717. printf( "DSSP: %s\n", $yseq );
  718. printf("\n");
  719. printf(">ss_dssp $name\n$ss_dssp\n");
  720. printf(">sa_dssp $name\n$sa_dssp\n");
  721. printf(">aa_dssp $name\n$aa_dssp\n");
  722. printf(">aa_astra $name\n$aa_astr\n\n");
  723. }
  724. if ( $score / $len < 0.5 ) {
  725. printf( STDERR "\nWARNING: in $name: alignment score with dssp residues too low: Score/len=%f.\n\n",
  726. $score / $len
  727. );
  728. printf( STDERR "IN: %s\n", $xseq );
  729. printf( STDERR "MATCH: %s\n", $Sstr );
  730. printf( STDERR "DSSP: %s\n", $yseq );
  731. return 1;
  732. }
  733. return 0;
  734. }
  735. ################################################################################################
  736. ### Return solvent accessibility code
  737. ################################################################################################
  738. sub sa2c () {
  739. my %maxsa = (
  740. A => 106,
  741. B => 160,
  742. C => 135,
  743. D => 163,
  744. E => 194,
  745. F => 197,
  746. G => 84,
  747. H => 184,
  748. I => 169,
  749. K => 205,
  750. L => 164,
  751. M => 188,
  752. N => 157,
  753. P => 136,
  754. Q => 198,
  755. R => 248,
  756. S => 130,
  757. T => 142,
  758. V => 142,
  759. W => 227,
  760. X => 180,
  761. Y => 222,
  762. Z => 196
  763. ); # maximum solvent accessiblity
  764. if ( $_[1] =~ /[a-z]/ ) { return "F"; } # disulphide bridge
  765. if ( !defined $maxsa{ $_[1] } ) { return "-"; } # no amino acid
  766. my $rsa = $_[0] / $maxsa{ $_[1] };
  767. #printf("aa=%s sa=%5.1f max_sa=%5.1f rsa=%5.3f\n",$_[1],$_[0],$maxsa{$_[1]},$rsa);
  768. if ( $rsa <= 0.02 ) { return "A"; }
  769. elsif ( $rsa <= 0.14 ) { return "B"; }
  770. elsif ( $rsa <= 0.33 ) { return "C"; }
  771. elsif ( $rsa <= 0.55 ) { return "D"; }
  772. else { return "E"; }
  773. }
  774. # Find the pdb file with $pdbcode in pdb directory
  775. sub OpenPDBfile() {
  776. my $pdbcode = lc( $_[0] );
  777. if ( !-e "$pdbdir" ) {
  778. if ( $v >= 3 ) {
  779. print( STDERR
  780. "Warning in $program: pdb directory '$pdbdir' does not exist!\n"
  781. );
  782. }
  783. return 1;
  784. }
  785. if ( -e "$pdbdir/all" ) { $pdbfile = "$pdbdir/all/"; }
  786. elsif ( -e "$pdbdir/divided" ) {
  787. $pdbfile = "$pdbdir/divided/" . substr( $pdbcode, 1, 2 ) . "/";
  788. }
  789. else { $pdbfile = "$pdbdir/"; }
  790. if ( $pdbdir =~ /divided.?$/ ) {
  791. $pdbfile .= substr( $pdbcode, 1, 2 ) . "/";
  792. }
  793. if ( -e $pdbfile . "$pdbcode.ent" ) { $pdbfile .= "$pdbcode.ent"; }
  794. elsif ( -e $pdbfile . "pdb$pdbcode.ent" ) { $pdbfile .= "pdb$pdbcode.ent"; }
  795. elsif ( -e $pdbfile . "pdb$pdbcode.ent.gz" ) {
  796. $pdbfile .= "pdb$pdbcode.ent.gz";
  797. }
  798. elsif ( -e $pdbfile . "pdb$pdbcode.ent.Z" ) {
  799. $pdbfile .= "pdb$pdbcode.ent.Z";
  800. }
  801. elsif ( -e $pdbfile . "$pdbcode.pdb" ) { $pdbfile .= "$pdbcode.pdb"; }
  802. elsif ( -e $pdbfile . "$pdbcode.cif" ) { $pdbfile .= "$pdbcode.cif"; }
  803. else {
  804. if ( $v >= 3 ) {
  805. printf( STDERR "Warning in $program: Cannot find pdb file $pdbfile"
  806. . "pdb$pdbcode.ent!\n" );
  807. }
  808. return "";
  809. }
  810. if ( !open( PDBFILE, "$pdbfile" ) ) {
  811. if ( $v >= 3 ) {
  812. printf( STDERR "Error in $program: Cannot open pdb file: $!\n" );
  813. }
  814. return "";
  815. }
  816. if ( $pdbfile =~ /\.(Z|gz)$/i ) {
  817. my $tmp_pdb_filename;
  818. (undef, $tmp_pdb_filename) = tempfile(UNLINK => 1, OPEN => 0);
  819. &HHPaths::System("gunzip -c $pdbfile > $tmp_pdb_filename");
  820. return "$tmp_pdb_filename";
  821. }
  822. else {
  823. return "$pdbfile";
  824. }
  825. }
  826. #....+....1....+....2....+....3....+....4
  827. # # RESIDUE AA STRUCTURE BP1 BP2 ACC etc.
  828. # 623 630 A R < 0 0 280 etc.
  829. # 624 !* 0 0 0 etc.
  830. # 625 8 B A 0 0 105 etc.
  831. # 626 9 B P >> - 0 0 71 etc.
  832. # 292 28SA K H 4 S+ 0 0 71 etc. (1qdm.dssp)
  833. # 293 29SA K H > S+ 0 0 28 etc.
  834. sub readDSSP() {
  835. my $dsspfile = $_[0];
  836. my $qrange = $_[1];
  837. my $aa_dssp = "";
  838. my $sa_dssp = "";
  839. my $ss_dssp = "";
  840. open( DSSPFILE, "<$dsspfile" );
  841. my $line;
  842. while ( $line = <DSSPFILE> ) {
  843. if ( $line =~ /^\s*\#\s*RESIDUE\s+AA/ ) { last; }
  844. }
  845. while ( $line = <DSSPFILE> ) {
  846. if ( $line =~ /^.{5}(.{5})(.)(.)\s(.).\s(.).{18}(...)/ ) {
  847. my $thisres = $1;
  848. my $icode = $2;
  849. my $chain = $3;
  850. my $aa = $4;
  851. my $ss = $5;
  852. my $sa = $6;
  853. my $range = $qrange;
  854. if ( $aa eq "!" ) { next; } # missing residues!
  855. $thisres =~ tr/ //d;
  856. $chain =~ tr/ //d;
  857. $icode =~ tr/ //d;
  858. $sa =~ tr/ //d;
  859. my $contained = 0;
  860. if ( $qrange ne "" ) {
  861. do {
  862. #syntax (A:56S-135S)
  863. if ( $range =~ s/^(\S):(-?\d+)[A-Z]-(\d+)([A-Z])//
  864. && $chain eq $1
  865. && $icode eq $4
  866. && $2 <= $thisres
  867. && $thisres <= $3 )
  868. {
  869. $contained = 1;
  870. }
  871. #syntax (R:56-135)
  872. elsif ($range =~ s/^(\S):(-?\d+)[A-Z]?-(\d+)[A-Z]?//
  873. && $chain eq $1
  874. && $2 <= $thisres
  875. && $thisres <= $3 )
  876. {
  877. $contained = 1;
  878. }
  879. #syntax (56-135)
  880. elsif ($range =~ s/^(-?\d+)[A-Z]-(\d+)([A-Z])//
  881. && $chain eq ""
  882. && $icode eq $3
  883. && $1 <= $thisres
  884. && $thisres <= $2 )
  885. {
  886. $contained = 1;
  887. }
  888. #syntax (56-135)
  889. elsif ($range =~ s/^(-?\d+)[A-Z]?-(\d+)[A-Z]?//
  890. && $chain eq ""
  891. && $1 <= $thisres
  892. && $thisres <= $2 )
  893. {
  894. $contained = 1;
  895. }
  896. #syntax (A:) or (A:,2:)
  897. elsif ( $range =~ s/^(\S):// && $chain eq $1 ) {
  898. $contained = 1;
  899. }
  900. #syntax (-)
  901. elsif ( $range =~ s/^-$// && $chain eq "" ) {
  902. $contained = 1;
  903. }
  904. $range =~ s/^,//;
  905. } while ( $contained == 0 && $range ne "" );
  906. if ( $contained == 0 ) {
  907. next;
  908. }
  909. }
  910. $aa_dssp .= $aa;
  911. $ss_dssp .= $ss;
  912. $sa_dssp .= &sa2c( $sa, $aa );
  913. }
  914. }
  915. close DSSPFILE;
  916. return ( $aa_dssp, $ss_dssp, $sa_dssp );
  917. }