Debian Codegraph @

lib/RG/Utils/Conv_hssp2saf.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl
2 ##------------------------------------------------------------------------------#
3 # Copyright 1998 #
4 # Burkhard Rost [email protected] #
5 # Wilckensstr. 15 http://www.embl-heidelberg.de/~rost/ #
6 # D-69120 Heidelberg #
7 # version 0.1 May, 1998 #
8 # version 1.1 Oct, 1998 #
9 #------------------------------------------------------------------------------#
10
11 =pod
12
13 =head1 NAME
14
15 RG::Utils::Conv_hssp2saf - Rost Lab perl utilities
16
17 =head1 SYNOPSIS
18
19 use RG::Utils::Conv_hssp2saf;
20
21 =head1 FUNCTIONS
22
23 See module source for description.
24
25 =over
26
27 =item bynumber
28
29 =item bynumber_high2low
30
31 =item conv_hssp2saf
32
33 =item convHssp2saf
34
35 =item convHssp2saf_hsspRdLoc
36
37 =item errSbr
38
39 =item errSbrMsg
40
41 =item get_range
42
43 =item get_rangeHyphen
44
45 =item hsspGetChain
46
47 =item hsspRdAli
48
49 =item hsspRdHeader
50
51 =item hsspRdSeqSecAccOneLine
52
53 =item is_hssp
54
55 =item is_pdbid
56
57 =item open_file
58
59 =item safWrt
60
61 =back
62
63 =head1 VARIABLES
64
65 =over
66
67 =back
68
69 =head1 AUTHOR
70
71 B. Rost
72
73 =head1 SEE ALSO
74
75 =over
76
77 =item Main website
78
79 L<http://rostlab.org/>
80
81 =back
82
83 =cut
84
85 package RG::Utils::Conv_hssp2saf;
86 no warnings 'deprecated';
87
88 INIT: {
89 $scrName=$0;$scrName=~s/^.*\/|\.pl//g;
90 $scrGoal="converts HSSP 2 SAF\n";
91 }
92
93 #===============================================================================
94 sub conv_hssp2saf {
95 #-------------------------------------------------------------------------------
96 # conv_hssp2saf package version of script
97 #-------------------------------------------------------------------------------
98 $[ =1 ; # sets array count to start at 1, not at 0
99
100
101
102 @PARAMS=@_; # pass from calling
103
104 $par{packName} = __FILE__;
105
106 # ------------------------------
107 foreach $arg(@PARAMS){ # highest priority arguments
108 next if ($arg !~/=/);
109 if ($arg=~/^packName=(.*)/) { $par{"packName"}=$1;
110 shift @PARAMS if ($PARAMS[1] eq $arg); } }
111 # ------------------------------
112 &convHssp2saf("ini"); # sets %par
113
114 # ------------------------------
115 if ($#PARAMS<1){ # help
116 print "goal:\t $scrGoal\n";
117 print "use: \t '$scrName list.hssp (or *.hssp)'\n";
118 print "opt: \t \n";
119 printf " \t %-15s= %-20s %-s\n","fileOut", "x","";
120 printf " \t %-15s= %-20s %-s\n","extr", "p1-p2,p3","(extract proteins p1-p2,p3 only)";
121 printf " \t %-15s= %-20s %-s\n","frag", "n1-n2", "(extract residues n1-n2 only)";
122 printf " \t %-15s %-20s %-s\n","noScreen", "no value","";
123 printf " \t %-15s %-20s %-s\n","expand", "no value","(do expand the insertion list)";
124 # printf " \t %-15s %-20s %-s\n","", "no value","";
125 if (%par){
126 printf " \t %-15s %-20s %-s\n","-" x 15 ,"-" x 20,"-" x 30;
127 printf " \t %-15s %-20s %-s\n","other:","default settings: "," ";
128 foreach $kwd (@kwd){
129 if ($par{"$kwd"}!~/\D/){
130 printf " \t %-15s= %10d %9s %-s\n",$kwd,$par{"$kwd"}," ","(def)";}
131 elsif ($par{"$kwd"}!~/[0-9\.]/){
132 printf " \t %-15s= %10.3f %9s %-s\n",$kwd,$par{"$kwd"}," ","(def)";}
133 else {
134 printf " \t %-15s= %-20s %-s\n",$kwd,$par{"$kwd"},"(def)";} } }
135 exit;}
136
137 ($Lok,$msg)=
138 &convHssp2saf(@PARAMS);
139
140 print "*** $scrName: final msg=\n".$msg."\n" if (! $Lok);
141
142 return(1,"ok $scrName:pack");
143 } # end of conv_hssp2saf
144
145
146 #===============================================================================
147 sub convHssp2saf {
148 local($tmpMode) = @_ ;
149 local($sbrName,$fhinLoc,$tmp,$Lok);
150 $[ =1 ;
151 #-------------------------------------------------------------------------------
152 # convHssp2saf script to convert HSSP 2 SAF
153 # in: file.hssp (or list, or *.hssp)
154 # file.hssp_C for chain!
155 # in: opt fileOut=X -> output file will be X
156 # NOTE: only for single file!
157 # in: opt frag=n1-n2 -> extract residues n1-n2
158 # in: opt extr=p1-p2,p3 -> extract proteins p1-p2,p3
159 # in: opt expand -> write expanded ali
160 # in: opt noScreen -> avoid writing
161 # out: 1|0,msg, implicit: file
162 # err: (1,'ok'), (0,'message')
163 #-------------------------------------------------------------------------------
164 $tmp=$0;$tmp=~s/^.*\/|\.pl//g;$tmp.=":";
165 $sbrName="$tmp:lib-scr::"."convHssp2saf";$fhinLoc="FHIN_"."convHssp2saf";
166 # ------------------------------
167 # defaults
168 @rdHsspHdr=("SEQLENGTH","ID","IDE","LALI","IFIR","ILAS");
169
170 $par{"len1Min"}= 30 if (! defined $par{"len1Min"}); # minimal length
171 $par{"laliMin"}= 30 if (! defined $par{"laliMin"}); # minimal alignment length
172 $par{"pideMax"}= 100 if (! defined $par{"pideMax"}); # maximal percentage sequence identity
173 $par{"expand"}= 0 if (! defined $par{"expand"} ); # expand insertions?
174 $par{"verbose"}= 1 if (! defined $par{"verbose"}); #
175 $par{"verb2"}= 0 if (! defined $par{"verb2"} ); #
176 @kwd=sort (keys %par);
177
178 return if (defined $tmpMode && $tmpMode eq "ini");
179
180 # initialise variables
181 $fhin="FHIN";
182 # ------------------------------
183 $#fileIn=$#chainIn=0; # read command line
184 foreach $arg (@_){
185 if ($arg=~/^fileOut=(.*)$/) { $fileOut=$1;}
186 elsif ($arg=~/^frag=(.*)$/) { $frag=$1;
187 if ($frag !~/\d+\-\d+/){
188 print "*** arg 'frag' must be 'N1-N2'\n";
189 exit;}}
190 elsif ($arg=~/^extr=(.*)$/) { $extr=$1; }
191 elsif ($arg=~/^no[Ss]creen/) { $par{"verbose"}=$par{"verb2"}=0; }
192 elsif ($arg=~/^expand$/) { $par{"expand"}=1; }
193 elsif ($arg=~/^debug$/) { $par{"debug"}=1; }
194 # elsif ($arg=~/^=(.*)$/) { $=$1; }
195 elsif (-e $arg) { push(@fileIn,$arg); push(@chainIn,"*");}
196 elsif ($arg=~/^(.*\.hssp)\_([A-Z0-9])/) { push(@fileIn,$1); push(@chainIn,$2);}
197 elsif (%par && $#kwd>0) {
198 $Lok=0;
199 foreach $kwd (@kwd){
200 if ($arg =~ /^$kwd=(.+)$/){$Lok=1;$par{"$kwd"}=$1;
201 last;}}
202 if (! $Lok){ print "*** wrong command line arg '$arg'\n";
203 die;}}
204 else { print "*** wrong command line arg '$arg'\n";
205 die;}}
206 # ------------------------------
207 # verify 'extr' correct
208 if (defined $extr && length($extr)>=1 && $extr=~/\d/ && $extr ne "0"){
209 @extr=&get_range($extr);
210 return(&errSbr("you gave the argument 'extr=$extr', not valid!\n*** Ought to be of the form:\n".
211 "'extr=n1-n2', or 'extr=n1,n2,n3-n4,n5'\n")) if ($#extr==0);
212 foreach $tmp (@extr) {
213 if ($tmp=~/\D/){
214 return(&errSbr("you gave the argument 'extr=$extr', not valid!\n".
215 "*** Ought to be of the form:\n".
216 "'extr=n1-n2', or 'extr=n1,n2,n3-n4,n5'\n")); }
217 $extr{"$tmp"}=1; }}
218 elsif (defined $extr) {undef $extr ; }
219 # ------------------------------
220 # input file
221 $fileIn=$fileIn[1];
222 die ("missing input $fileIn\n") if (! -e $fileIn);
223 # ------------------------------
224 # (0) read list (if list)
225 if (! &is_hssp($fileIn)){ # ------------------------------
226 print "--- $scrName: read list '$fileIn'\n";
227 $#fileIn=$#chainIn=0;
228 $fhin="FHIN_$scrName";
229 &open_file("$fhin", "$fileIn") || die '*** $scrName ERROR opening file $fileIn';
230 while (<$fhin>) {
231 $_=~s/\n|\s//g;
232 next if (length($_)<5);
233 if ($_=~/^(.*\.hssp)\_([A-Z0-9])/){
234 push(@fileIn,$1); push(@chainIn,$2);}
235 else {
236 push(@fileIn,$_); push(@chainIn,"*");}
237 } close($fhin); $#fileOut=0; } else { @fileOut=($fileOut) if (defined $fileOut);
238 $#fileOut=0 if (! defined $fileOut); }
239 # ------------------------------
240 # (1) loop over file(s)
241 # ------------------------------
242 foreach $itfile(1..$#fileIn){
243 ($Lok,$msg)=
244 &convHssp2saf_hsspRdLoc;
245 return(&errSbrMsg("failed reading file=$fileIn[$itfile], it=$itfile",$msg))
246 if (! $Lok);
247 }
248 if ($par{"verbose"}) { $tmp="";
249 foreach $file (@fileOut) {
250 next if (! -e $file);
251 $tmp.="$file,";}
252 $tmp=~s/,*$//g;
253 if (length($tmp)>2 && $tmp=~/,/){
254 print "--- output files:"; }
255 elsif (length($tmp)>2){
256 print "--- output file:"; }
257 print "$tmp\n";}
258
259
260 return(1,"ok $sbrName");
261 } # end of convHssp2saf
262
263 #===============================================================================
264 sub convHssp2saf_hsspRdLoc {
265 # local($fileInLoc,$fhErrSbr) = @_ ;
266 local($sbrName,$fhinLoc,$tmp,$Lok);
267 $[ =1 ;
268 #-------------------------------------------------------------------------------
269 # convHssp2saf_hsspRdLoc reads stuff
270 # in/out GLOBAL: all
271 # in: $fileInLoc
272 # out: 1|0,msg, implicit:
273 # err: (1,'ok'), (0,'message')
274 #-------------------------------------------------------------------------------
275 $sbrName="lib-scr::"."convHssp2saf_hsspRdLoc";
276 $fhinLoc="FHIN_"."convHssp2saf_hsspRdLoc";
277
278 $fileIn=$fileIn[$itfile]; $chainIn=$chainIn[$itfile];
279 if (! -e $fileIn){
280 print "-*- WARN $scrName: no fileIn=$fileIn\n";
281 next;}
282 print "--- $scrName: working on $itfile =$fileIn $chainIn\n" if ($par{"verbose"});
283 $pdbid= $fileIn;$pdbid=~s/^.*\/|\.hssp//g;
284 $pdbid.="_".$chainIn if ($chainIn ne "*");
285 undef %tmp; # ------------------------------
286 if ($chainIn ne "*"){ # get chain positions
287 ($Lok,%tmp)=
288 &hsspGetChain($fileIn);
289 return(&errSbr("failed on getchain($fileIn)")) if (! $Lok);
290 foreach $it (1..$tmp{"NROWS"}){
291 next if ($chainIn ne $tmp{"$it","chain"});
292 $ifir=$tmp{"$it","ifir"}; $ilas=$tmp{"$it","ilas"}; }}
293 else {
294 $ifir=$ilas=0;}
295 # ------------------------------
296 undef %tmp; # read header of HSSP
297 ($Lok,%tmp)=
298 &hsspRdHeader($fileIn,@rdHsspHdr);
299 return(&errSbr("failed on $fileIn")) if (! $Lok);
300 # ------------------------------
301 # too short -> skip
302 return(1,"too short=".$tmp{"SEQLENGTH"}) if ($tmp{"SEQLENGTH"} < $par{"len1Min"});
303
304 $#numTake=0; # ------------------------------
305 # process data
306 foreach $it (1..$tmp{"NROWS"}){
307 # not chain -> skip
308 next if ($ifir && $ilas && ( ($tmp{"IFIR","$it"} > $ilas) || ($tmp{"ILAS","$it"} < $ifir) ));
309 next if ($par{"laliMin"} > $tmp{"LALI","$it"} ); # lali too short
310 next if ($par{"pideMax"} < 100*$tmp{"IDE","$it"} ); # pide too high
311 next if (defined $extr && ! defined $extr{"$it"} ); # select only some proteins
312 push(@numTake,$it); # ok -> take
313 }
314 # ------------------------------
315 undef %tmp; # read alignments
316 if (defined $par{"expand"} && $par{"expand"}){
317 $kwdSeq="seqAli";} # read ali with insertions
318 else {
319 $kwdSeq="seqNoins";} # read ali without insertions
320
321 ($Lok,%tmp)=
322 &hsspRdAli($fileIn,@numTake,$kwdSeq);
323 return(&errSbrMsg("failed reading alis for $fileIn, num=".
324 join(',',@numTake),$msg)) if (! $Lok);
325
326 $nali=$tmp{"NROWS"};
327 undef %tmp2;
328
329 # ------------------------------
330 if (defined $frag){ # adjust for extraction (arg: frag=N1-n2)
331 ($ibeg,$iend)=split(/\-/,$frag);
332 # additional complication if expand: change numbers
333 if ($kwdSeq eq "seqAli"){ $seq=$tmp{"$kwdSeq","0"};
334 @tmp=split(//,$seq); $ct=0;
335 foreach $it (1..$#tmp){
336 next if ($tmp[$it] eq "."); # skip insertions
337 ++$ct; # count no-insertions
338 next if ($ct > $iend); # outside of range to read
339 next if ($ct < $ibeg); # outside of range to read
340 $ibeg=$it if ($ct == $ibeg); # change begin
341 $iend=$it if ($ct == $iend);}}} # change end
342 else {
343 $ibeg=$iend=0;}
344 # ----------------------------------------
345 # cut out non-chain, and not to read parts
346 # ----------------------------------------
347 foreach $it (0..$nali){
348 if ($ifir && $ilas){
349 $tmp{"$kwdSeq","$it"}=substr($tmp{"$kwdSeq","$it"},$ifir,($ilas-$ifir+1));}
350 if ($ibeg && $iend){
351 $tmp{"$kwdSeq","$it"}=substr($tmp{"$kwdSeq","$it"},$ibeg,($iend-$ibeg+1));}
352 $ct=$it+1;
353 $tmp2{"seq","$ct"}=$tmp{"$kwdSeq","$it"};
354 undef $tmp{"$kwdSeq","$it"};
355 $tmp2{"id","$ct"}= $tmp{"$it"};}
356 $tmp2{"NROWS"}=$nali+1;
357 undef %tmp; # slick-is-in!
358 # ------------------------------
359 undef %tmp; # slick-is-in !
360 undef @numTake; # slick-is-in !
361 # ------------------------------
362 # write output file
363 if (defined $fileOut && $#fileIn==1){
364 $fileOutLoc=$fileOut;}
365 else {
366 $fileOutLoc="$pdbid".".saf";
367 $fileOut.=$fileOutLoc."," if (defined $fileOut); }
368
369 ($Lok,$msg)=
370 &safWrt($fileOutLoc,%tmp2);
371 return(&errSbrMsg("failed writing out=$fileOutLoc, for in=$fileIn",$msg)) if (! $Lok);
372
373 push(@fileOut,$fileOutLoc);
374
375 undef %tmp2; # slick-is-in !
376
377 return(1,"ok $sbrName");
378 } # end of convHssp2saf_hsspRdLoc
379
380
381 #==============================================================================
382 # library collected (begin)
383 #==============================================================================
384
385 #===============================================================================
386 sub bynumber {
387 #-------------------------------------------------------------------------------
388 # bynumber function sorting list by number
389 #-------------------------------------------------------------------------------
390 $a<=>$b;
391 } # end of bynumber
392
393 #===============================================================================
394 sub bynumber_high2low {
395 #-------------------------------------------------------------------------------
396 # bynumber_high2low function sorting list by number (start with high)
397 #-------------------------------------------------------------------------------
398 $b<=>$a;
399 } # end of bynumber_high2low
400
401 #==============================================================================
402 sub errSbr {local($txtInLoc,$sbrNameLocy) = @_ ;
403 #-------------------------------------------------------------------------------
404 # errSbr simply writes '*** ERROR $sbrName: $txtInLoc'
405 #-------------------------------------------------------------------------------
406 $sbrNameLocy=$sbrName if (! defined $sbrNameLocy);
407 $txtInLoc.="\n";
408 $txtInLoc=~s/\n\n+/\n/g;
409 return(0,"*** ERROR $sbrNameLocy: $txtInLoc");
410 } # end of errSbr
411
412 #==============================================================================
413 sub errSbrMsg {local($txtInLoc,$msgInLoc,$sbrNameLocy) = @_ ;
414 #-------------------------------------------------------------------------------
415 # errSbrMsg simply writes '*** ERROR $sbrName: $txtInLoc\n'.$msg
416 #-------------------------------------------------------------------------------
417 $sbrNameLocy=$sbrName if (! defined $sbrNameLocy);
418 $txtInLoc.="\n";
419 $txtInLoc=~s/\n\n+/\n/g;
420 $msgInLoc.="\n";
421 $msgInLoc=~s/\n\n+/\n/g;
422 return(0,"*** ERROR $sbrNameLocy: $txtInLoc".$msgInLoc);
423 } # end of errSbrMsg
424
425 #==============================================================================
426 sub get_range {
427 local ($range_txt,$nall) = @_;
428 local (@range,@rangeLoc,$it,$range1,$range2);
429 $[ =1 ;
430 #--------------------------------------------------------------------------------
431 # get_range converts range=n1-n2 into @range (1,2)
432 # in: 'n1-n2' NALL: e.g. incl=1-5,9,15
433 # n1= begin, n2 = end, * for wild card
434 # NALL = number of last position
435 # out: @takeLoc: begin,begin+1,...,end-1,end
436 #--------------------------------------------------------------------------------
437 $#range=0;
438 if (! defined $range_txt || length($range_txt)<1 || $range_txt eq "unk"
439 || $range_txt !~/\d/ ) {
440 print "*** ERROR in get_range: argument: range=$range_txt, nall=$nall, not digestable\n";
441 return(0);}
442 $range_txt=~s/\s//g; # purge blanks
443 $nall=0 if (! defined $nall);
444 # already only a number
445 return($range_txt) if ($range_txt !~/[^0-9]/);
446
447 if ($range_txt !~/[\-,]/) { # no range given
448 print "*** ERROR in get_range: argument: '$range_txt,$nall' not digestable\n";
449 return(0);}
450 # ------------------------------
451 # dissect commata
452 if ($range_txt =~ /\,/) {
453 @range=split(/,/,$range_txt);}
454 # ------------------------------
455 # dissect hyphens
456 elsif ($range_txt =~ /(\d*|\*)-(\d*|\*)/) {
457 @range=&get_rangeHyphen($range_txt,$nall);}
458
459 # ------------------------------
460 # process further elements with hyphens
461 $#range2=0;
462 foreach $range (@range){
463 if ($range =~ /(\d*|\*)-(\d*|\*)/) {
464 push(@range2,&get_rangeHyphen($range,$nall));}
465 else {
466 push(@range2,$range);}}
467 @range=@range2; $#range2=0;
468 # ------------------------------
469 if ($#range>1){ # sort
470 @range=sort {$a<=>$b} @range;}
471 return (@range);
472 } # end of get_range
473
474 #==============================================================================
475 sub get_rangeHyphen {
476 local ($range_txt,$nall) = @_ ;
477 local (@rangeLoc,$it,$range1,$range2);
478 $[ =1 ;
479 #--------------------------------------------------------------------------------
480 # get_rangeHyphen reads 'n1-n2'
481 # in: 'n1-n2', NALL (n1= begin, n2 = end, * for wild card)
482 # NALL = number of last position
483 # out: begin,begin+1,...,end-1,end
484 #--------------------------------------------------------------------------------
485 if ($range_txt =~ /(\d*|\*)-(\d*|\*)/) {
486 ($range1,$range2)=split(/-/,$range_txt);
487 if ($range1=~/\*/) {$range1=1;}
488 if ($range2=~/\*/) {$range2=$nall;}
489 for($it=$range1;$it<=$range2;++$it) {push(@rangeLoc,$it);} }
490 else { @rangeLoc=($range_txt);}
491 return(@rangeLoc);
492 } # end of get_rangeHyphen
493
494 #==============================================================================
495 sub hsspGetChain {
496 local ($fileIn) = @_ ;
497 local ($fhin,$ifirLoc,$ilasLoc,$tmp1,$tmp2,
498 $chainLoc,$posLoc,$posRd,$chainRd,@cLoc,@pLoc,%rdLoc);
499 $[ =1 ;
500 #--------------------------------------------------------------------------------
501 # hsspGetChain extracts all chain identifiers in HSSP file
502 # in: $file
503 # out: $chains (ABC) ,%rdLoc
504 # no chain -> $chains=' '
505 # out $rdLoc{"NROWS"},$rdLoc{"$ct","chain"},
506 # out $rdLoc{"$ct","ifir"},$rdLoc{"$ct","ilas"}
507 #--------------------------------------------------------------------------------
508 $fhin="FhInHssp";
509 return(0,"no file") if (! -e $fileIn);
510 &open_file("$fhin","$fileIn");
511 while(<$fhin>){ # until start of data
512 last if ($_=~/^ SeqNo/);}
513 $chainLoc=$posLoc="";
514 while(<$fhin>){
515 if ($_=~/^\#/ && (length($chainLoc)>1) ) {
516 $posLoc.="$ifirLoc-$ilasLoc".",";
517 last;}
518 $chainRd=substr($_,13,1);
519 $aaRd= substr($_,15,1);
520 $posRd= substr($_,1,6);$posRd=~s/\s//g;
521 next if ($aaRd eq "!") ; # skip over chain break
522 if ($chainLoc !~/$chainRd/){ # new chain?
523 $posLoc.= "$ifirLoc-$ilasLoc"."," if (length($chainLoc)>1);
524 $chainLoc.= "$chainRd".",";
525 $ifirLoc=$ilasLoc=$posRd;}
526 else {
527 $ilasLoc=$posRd;}
528 }close($fhin);
529 $chainLoc=~s/^,|,$//g;
530 $posLoc=~s/\s//g;$posLoc=~s/^,|,$//g; # purge leading ','
531 # now split chains read
532 undef %rdLoc; $ctLoc=0;
533 @cLoc=split(/,/,$chainLoc);
534 @pLoc=split(/,/,$posLoc);
535
536 foreach $itLoc(1..$#cLoc){
537 ($tmp1,$tmp2)=split(/-/,$pLoc[$itLoc]);
538 next if ($tmp2 == $tmp1); # exclude chains of length 1
539 ++$ctLoc;
540 $rdLoc{"NROWS"}= $ctLoc;
541 $rdLoc{"$ctLoc","chain"}=$cLoc[$itLoc];
542 $rdLoc{"$ctLoc","ifir"}= $tmp1;
543 $rdLoc{"$ctLoc","ilas"}= $tmp2;}
544 $chainLoc=~s/,//g;
545 return($chainLoc,%rdLoc);
546 } # end of hsspGetChain
547
548 #==============================================================================
549 sub hsspRdAli {
550 local ($fileInLoc,@want) = @_ ;
551 local ($sbrName,$fhinLoc,$tmp);
552 $[ =1 ;
553 #-------------------------------------------------------------------------------
554 # hsspRdAli reads and writes the sequence of HSSP + 70 alis
555 # in: $fileHssp (must exist),
556 # @des (1) =1, 2, ..., i.e. number of sequence to be read
557 # (2) = swiss_id1, swiss_id2, i.e. identifiers to read
558 # (3) = all (or undefined)
559 # NOTE: you can give ids AND numbers ('1','paho_chick','2') ..
560 # furthermore:
561 # if @want = 'seq|seqAli|seqNoins'
562 # only those will be returned (e.g. $tmp{"seq","$ct"})
563 # default: all 3!
564 # out: 1|0,$rd{} with:
565 # err: (0,$msg)
566 # overall:
567 # $rd{"NROWS"}= : number of alis, i.e. $#want
568 # $rd{"NRES"}=N : number of residues in guide
569 # $rd{"SWISS"}='sw1,sw2' : list of swiss-ids read
570 # $rd{"0"}='pdbid' : id of guide sequence (in file header)
571 # $rd{"$it"}='sw$ct' : swiss id of the it-th alignment
572 # $rd{"$id"}='$it' : position of $id in final list
573 # $rd{"sec","$itres"} : secondary structure for residue itres
574 # $rd{"acc","$itres"} : accessibility for residue itres
575 # $rd{"chn","$itres"} : chain for residue itres
576 # per prot:
577 # $rd{"seqNoins","$ct"}=sequences without insertions
578 # $rd{"seqNoins","0"}= GUIDE sequence
579 # $rd{"seq","$ct"}=SEQW : sequences, with all insertions
580 # but NOT aligned!!!
581 # $rd{"seqAli","$ct"} : sequences, with all insertions,
582 # AND aligned (all, including guide
583 # filled up with '.' !!
584 #-------------------------------------------------------------------------------
585 $sbrName="lib-br:hsspRdAli"; $fhinLoc="FHIN"."$sbrName"; $fhinLoc=~tr/[a-z]/[A-Z]/;
586 # file existing?
587 if ((! -e $fileInLoc) || (! &is_hssp($fileInLoc))){
588 print "*** ERROR $sbrName no HSSP file '$fileInLoc'\n";
589 return(0);}
590 # ------------------------------
591 # HSSP file format settings
592 $regexpBegAli= "^\#\# ALIGNMENTS"; # begin of reading
593 $regexpEndAli= "^\#\# SEQUENCE PROFILE"; # end of reading
594 $regexpSkip= "^ SeqNo"; # skip lines with pattern
595 $nmaxBlocks= 100; # maximal number of blocks considered (=7000 alis!)
596 $regexpBegIns= "^\#\# INSERTION LIST"; # begin of reading insertion list
597
598 undef %tmp; undef @seqNo; undef %seqNo;
599 # ------------------------------
600 # pointers
601 undef %ptr_id2num; # $ptr{xyz}= N : id=xyz is the Nth ali
602 undef @ptr_num2id; # $ptr[N]= xyz : Nth ali has id= xyz
603 undef @ptr_numWant2numFin; # $ptr[N]= M : the Nth ali is the Mth one in the list
604 # of all numbers wanted (i.e. = $want[M])
605 undef @ptr_numFin2numWant; # $ptr[M]= N : see previous, the other way around!
606
607 $#want=0 if (! defined @want);
608 $LreadAll=0;
609 # ------------------------------
610 # digest input
611 $#tmp=0; undef %kwd; # (1) detect keywords
612 foreach $tmp (@want){
613 if ($tmp=~/^(seq|seqAli|seqNoins)$/){
614 $kwd{$tmp}=1;
615 next;}
616 push(@tmp,$tmp);}
617
618 if (($#want>0) && ($#want == $#tmp) ){ # default keyworkds
619 foreach $des ("seq","seqAli","seqNoins"){
620 $kwd{"$des"}=1;}}
621 @want=@tmp;
622 # (2) all?
623 $LreadAll=1 if ( ! @want || ! $want[1] || ($want[1] eq "all"));
624 if (! $LreadAll){ # (3) read some
625 $#wantNum=$#wantId=0;
626 foreach $want (@want) {
627 if ($want !~ /[^0-9]/){push(@wantNum,$want);} # is number
628 else {push(@wantId,$want);}}} # is id
629 # ------------------------------
630 # get numbers/ids to read
631 ($Lok,%rdHeader)=
632 &hsspRdHeader($fileInLoc,"SEQLENGTH","PDBID","NR","ID");
633 if (! $Lok){
634 print "*** ERROR $sbrName reading header of HSSP file '$fileInLoc'\n";
635 return(0);}
636 $tmp{"NRES"}= $rdHeader{"SEQLENGTH"};$tmp{"NRES"}=~s/\s//g;
637 $tmp{"0"}= $rdHeader{"PDBID"}; $tmp{"0"}=~s/\s//g;
638 $idGuide= $tmp{"0"};
639
640 $#locNum=$#locId=0; # store the translation name/number
641 foreach $it (1..$rdHeader{"NROWS"}){
642 $num=$rdHeader{"NR","$it"}; $id=$rdHeader{"ID","$it"};
643 push(@locNum,$num);push(@locId,$id);
644 $ptr_id2num{"$id"}=$num;
645 $ptr_num2id[$num]=$id;}
646 push(@locNum,"1") if ($#locNum==0); # nali=1
647 # ------------------------------
648 foreach $want (@wantId){ # CASE: input=list of names
649 $Lok=0; # -> add to @wantNum
650 foreach $loc (@locId){
651 if ($want eq $loc){$Lok=1;push(@wantNum,$ptr_id2num{"$loc"});
652 last;}}
653 if (! $Lok){
654 print "-*- WARNING $sbrName wanted id '$want' not in '$fileInLoc'\n";}}
655 # ------------------------------
656 # NOW we have all numbers to get
657 # sort the array
658 @wantNum= sort bynumber (@wantNum);
659 # too many wanted
660 if (defined @wantNum && ($wantNum[$#wantNum] > $locNum[$#locNum])){
661 $#tmp=0;
662 foreach $want (@wantNum){
663 if ($want <= $locNum[$#locNum]){
664 push(@tmp,$want)}
665 else {
666 print "-*- WARNING $sbrName no $want not in '$fileInLoc'\n";
667 exit;
668 }}
669 @wantNum=@tmp;}
670
671 @wantNum=@locNum if ($LreadAll);
672 if ($#wantNum==0){
673 print "*** ERROR $sbrName nothing to read ???\n";
674 return(0);}
675 # sort the array, again
676 @wantNum= sort bynumber (@wantNum);
677 # ------------------------------
678 # assign pointers to final output
679 foreach $it (1..$#wantNum){
680 $numWant=$wantNum[$it];
681 $ptr_numWant2numFin[$numWant]=$it;
682 $ptr_numFin2numWant[$it]= $numWant;}
683
684 # ------------------------------
685 # get blocks to take
686 $wantLast=$wantNum[$#wantNum];$#wantBlock=0;
687 foreach $ctBlock (1..$nmaxBlocks){
688 $beg=1+($ctBlock-1)*70;
689 $end=$ctBlock*70;
690 last if ($wantLast < $beg);
691 $Ltake=0;
692 foreach $num(@wantNum){
693 if ( ($beg<=$num)&&($num<=$end) ){
694 $Ltake=1;
695 last;}}
696 if ($Ltake){
697 $wantBlock[$ctBlock]=1;}
698 else{
699 $wantBlock[$ctBlock]=0;}}
700 # writes ids read
701 $tmp{"SWISS"}="";
702 foreach $it (1..$#wantNum){ $num=$wantNum[$it];
703 $tmp{"$it"}= $ptr_num2id[$num];
704 $tmp{"SWISS"}.="$ptr_num2id[$num]".",";}
705 $tmp{"SWISS"}=~s/,*$//g;
706 $tmp{"NROWS"}=$#wantNum;
707
708 # ------------------------------------------------------------
709 #
710 # NOTATION:
711 # $tmp{"0",$it}= $it-th residue of guide sequnec
712 # $tmp{$itali,$it}= $it-th residue of of ali $itali
713 # note: itali= same numbering as in 1..$#want
714 # i.e. NOT the position in the file
715 # $ptr_numFin2numWant[$itali]=5 may reveal that
716 # the itali-th ali was actually the fifth in the
717 # HSSP file!!
718 #
719 # ------------------------------------------------------------
720
721 # --------------------------------------------------
722 # read the file finally
723 # --------------------------------------------------
724 $Lok=&open_file("$fhinLoc","$fileInLoc");
725 if (! $Lok){print "*** ERROR $sbrName opening hssp file '$fileInLoc'\n";
726 return(0);}
727 # ------------------------------
728 # move until first alis
729 # ------------------------------
730 $ctBlock=$Lread=$#takeTmp=0;
731 while (<$fhinLoc>){
732 last if ($_=~/$regexpEndAli/); # ending
733 if ($_=~/$regexpBegAli/){ # this block to take?
734 ++$ctBlock;$Lread=0;
735 if ($wantBlock[$ctBlock]){
736 $_=~s/^[^0-9]+(\d+) -\s+(\d+).*$//;
737 $beg=$1;$end=$2;$Lread=1;
738 $#wantTmp=0; # local numbers
739 foreach $num (@wantNum){
740 if ( ($beg<=$num) && ($num<=$end) ){
741 $tmp=($num-$beg)+1;
742 print "*** $sbrName negative number $tmp,$beg,$end,\n" x 3 if ($tmp<1);
743 push(@wantTmp,$tmp);}}
744 next;}}
745 next if (! $Lread); # move on
746 next if ($_=~/$regexpSkip/); # skip line
747 $line=$_;
748 # --------------------
749 if (length($line)<52){ # no alis in line
750 $seqNo=substr($line,1,6);$seqNo=~s/\s//g;
751 if (! defined $seqNo{$seqNo}){
752 $seqNo{$seqNo}=1;
753 push(@seqNo,$seqNo);}
754 # NOTE: $chn,$sec,$acc are returned per residue!
755 if (! defined $tmp{"0","$seqNo"}){
756 ($seqNo,$pdbNo,
757 $tmp{"chn","$seqNo"},$tmp{"0","$seqNo"},
758 $tmp{"sec","$seqNo"},$tmp{"acc","$seqNo"})=
759 &hsspRdSeqSecAccOneLine($line);}
760
761 foreach $num(@wantTmp){ # add insertions if no alis
762 $pos= $num+$beg-1;
763 $posFin= $ptr_numWant2numFin[$pos];
764 $tmp{"$posFin","$seqNo"}="."; }
765 next;}
766 # ------------------------------
767 # everything fine, so read !
768 # ------------------------------
769 # --------------------
770 # first the HSSP stuff
771 $seqNo=substr($line,1,6);$seqNo=~s/\s//g;
772 if (! defined $seqNo{$seqNo}){
773 $seqNo{$seqNo}=1;
774 push(@seqNo,$seqNo);}
775 # NOTE: $chn,$sec,$acc are returned per residue!
776 if (! defined $tmp{"0","$seqNo"}){
777 ($seqNo,$pdbNo,
778 $tmp{"chn","$seqNo"},$tmp{"0","$seqNo"},
779 $tmp{"sec","$seqNo"},$tmp{"acc","$seqNo"})=
780 &hsspRdSeqSecAccOneLine($line);}
781 # --------------------
782 # now the alignments
783 $alis=substr($line,52); $alis=~s/\n//g;
784
785 # NOTE: @wantTmp has the positions to read in current block,
786 # e.g. want no 75, block=71-90, => 75->4
787 foreach $num (@wantTmp){
788 $pos= $num+$beg-1; # note: beg=71 in the example above
789 $id= $ptr_num2id[$pos];
790 $posFin= $ptr_numWant2numFin[$pos];
791 $tmp{"$posFin"}= $id;
792 $takeTmp[$pos]= 1;
793 print "*** $sbrName neg number $pos,$beg,$num,\n" x 3 if ($pos<1);
794 $tmp{"seq","$posFin"}= "" if (! defined $tmp{"seq","$posFin"});
795 if (length($alis) < $num){
796 $tmp{"seq","$posFin"}.= ".";
797 $tmp{"$posFin","$seqNo"}=".";}
798 else {
799 $tmp{"seq","$posFin"}.= substr($alis,$num,1);
800 $tmp{"$posFin","$seqNo"}=substr($alis,$num,1);}}}
801 # ------------------------------
802 while (<$fhinLoc>){ # skip over profiles
803 last if ($_=~/$regexpBegIns/); } # begin reading insertion list
804
805 # ----------------------------------------
806 # store sequences without insertions!!
807 # ----------------------------------------
808 if (defined $kwd{"seqNoins"} && $kwd{"seqNoins"}){
809 # --------------------
810 $seq=""; # guide sequence
811 foreach $seqNo(@seqNo){
812 $seq.=$tmp{"0","$seqNo"};}
813 $seq=~s/[a-z]/C/g; # small caps to 'C'
814 $tmp{"seqNoins","0"}=$seq;
815 # --------------------
816 # all others (by final count!)
817 foreach $it (1..$#wantNum){
818 $seq="";
819 foreach $seqNo(@seqNo){
820 $seq.=$tmp{"$it","$seqNo"};}
821 $seq=~s/\s/\./g; # fill up insertions
822 $seq=~tr/[a-z]/[A-Z]/; # small caps to large
823 $tmp{"seqNoins","$it"}=$seq;}
824 }
825 # --------------------------------------------------
826 # read insertion list
827 #
828 # syntax of insertion list:
829 # ....,....1....,....2....,....3....,....4
830 # AliNo IPOS JPOS Len Sequence
831 # 9 58 59 5 kQLGAEi
832 #
833 # --------------------------------------------------
834 undef @insMax; # note: $insMax[$seqNo]=5 means at residue 'seqNo'
835 foreach $seqNo (@seqNo){ # the longest insertion was 5 residues
836 $insMax[$seqNo]=0;}
837 while (<$fhinLoc>){
838 $rd=$_;
839 last if ((! defined $kwd{"seqAli"} || ! $kwd{"seqAli"}) &&
840 (! defined $kwd{"seq"} || ! $kwd{"seq"}) );
841 next if ($rd =~ /AliNo\s+IPOS/); # skip key
842 last if ($rd =~ /^\//); # end
843 next if ($rd !~ /^\s*\d+/); # should not happen (see syntax)
844 $rd=~s/\n//g; $line=$rd;
845 $posIns=$rd; # current insertion from ali $pos
846 $posIns=~s/^\s*(\d+).*$/$1/;
847 # takeTmp[$pos]=1 if $pos to be read
848 next if (! defined $takeTmp[$posIns] || ! $takeTmp[$posIns]);
849 # ok -> take
850 $line=~s/^\s*|\s*$//g; # purge leading blanks
851 @tmp=split(/\s+/,$line);
852 $iposIns=$tmp[2]; # residue position of insertion
853 $seqIns= $tmp[5]; # sequence at insertion 'kQLGAEi'
854 $nresIns=(length($seqIns) - 2); # number of residues inserted
855 $posFin= $ptr_numWant2numFin[$posIns];
856 # --------------------------------------------------
857 # NOTE: here $tmp{"$it","$seqNo"} gets more than
858 # one residue assigned (ref=11)
859 # --------------------------------------------------
860 # change 'ACinK' -> 'ACINEWNK'
861 $tmp{"$posFin","$iposIns"}=substr($seqIns,1,(length($seqIns)-1));
862 # maximal number of insertions
863 $insMax[$iposIns]=$nresIns if ($nresIns > $insMax[$iposIns]);
864 } close($fhinLoc);
865 # end of reading file
866 # --------------------------------------------------
867
868 # ------------------------------
869 # final sequences (not aligned)
870 # ------------------------------
871 if (defined $kwd{"seq"} && $kwd{"seq"}){
872 foreach $it (0..$tmp{"NROWS"}){
873 $seq="";
874 foreach $seqNo(@seqNo){
875 $seq.=$tmp{"$it","$seqNo"};}
876 $seq=~s/[\s\.!]//g; # replace insertions
877 $seq=~tr/[a-z]/[A-Z]/; # all capitals
878 $tmp{"seq","$it"}=$seq; }}
879 # ------------------------------
880 # fill up insertions
881 # ------------------------------
882 if (defined $kwd{"seqAli"} && $kwd{"seqAli"}){
883 undef %ali; # temporary for storing sequences
884 foreach $it (0..$tmp{"NROWS"}){
885 $ali{$it}="";} # set for all alis
886 # ------------------------------
887 foreach $seqNo(@seqNo){ # loop over residues
888 $insMax=$insMax[$seqNo];
889 # loop over all alis
890 foreach $it (0..$tmp{"NROWS"}){
891 # (1) CASE: no insertion
892 if ($insMax==0){
893 $ali{$it}.=$tmp{"$it","$seqNo"};
894 next;}
895 # (2) CASE: insertions
896 $seqHere=$tmp{"$it","$seqNo"};
897 $insHere=(1+$insMax-length($seqHere));
898 # NOTE: dirty fill them in 'somewhere'
899 # take first residue
900 $ali{$it}.=substr($seqHere,1,1);
901 # fill up with dots
902 $ali{$it}.="." x $insHere ;
903 # take remaining residues (if any)
904 $ali{$it}.=substr($seqHere,2) if (length($seqHere)>1); }}
905 # ------------------------------
906 # now assign to final
907 foreach $it (0..$tmp{"NROWS"}){
908 $ali{$it}=~s/\s/\./g; # replace ' ' -> '.'
909 $ali{$it}=~tr/[a-z]/[A-Z]/; # all capital
910 $tmp{"seqAli","$it"}=$ali{$it};}
911 undef %ali; # slim-is-in!
912 }
913 # ------------------------------
914 # save memory
915 foreach $it (0..$tmp{"NROWS"}){
916 if ($it == 0){ # guide
917 $id= $idGuide; }
918 else { # pairs
919 $posOriginal=$ptr_numFin2numWant[$it];
920 $id= $ptr_num2id[$posOriginal]; }
921 $tmp{"$id"}= $id;
922 foreach $seqNo (@seqNo){
923 undef $tmp{"$it","$seqNo"};}}
924 undef @seqNo; undef %seqNo; undef @takeTmp; undef @idLoc;
925 undef @want; undef @wantNum; undef @wantId; undef @wantBlock;
926 undef %rdHeader; undef %ptr_id2num; undef @ptr_num2id;
927 undef @ptr_numWant2numFin; undef @ptr_numFin2numWant;
928 return(1,%tmp);
929 } # end of hsspRdAli
930
931 #==============================================================================
932 sub hsspRdHeader {
933 local ($fileInLoc,@kwdInLoc) = @_ ;
934 local ($sbrName,$fhinLoc,$tmp,
935 @kwdDefHsspTopLoc,@kwdDefHsspHdrLoc,@kwdHsspTopLoc,@kwdHsspHdrLoc,@tmp,
936 $regexpBegHeader,$regexpEndHeader,$regexpLongId,$lenStrid,$LisLongId,
937 %ptr,$kwd,$Lok,$Lpdb,$des,$beg,$end,$mid,$ct,$id,$strid,$ptr,$tmp);
938 $[ =1 ;
939 #-------------------------------------------------------------------------------
940 # hsspRdHeader reads a HSSP header
941 # in: $fileHssp (must exist),
942 # @kwdInLoc = one of the default keywords
943 # (@kwdLocHsspTop,@kwdLocHsspHeader)
944 # in: 'nopair' surpresses reading of pair information
945 # out: for top (e.g. NALIGN): $rd{"kwd"}
946 # for prot (e.g. LALI): $rd{"kwd","$it"}
947 # note: for the later number of elements (it) in
948 # $rd{"NROWS"}
949 # special ID=ID1, $rd{"kwd","$it"} existes for ID1 and ID2
950 #-------------------------------------------------------------------------------
951 $sbrName="lib-br:hsspRdHeader";$fhinLoc="FHIN"."$sbrName";$fhinLoc=~tr/[a-z]/[A-Z]/;
952 # file existing?
953 if (! -e $fileInLoc || ! &is_hssp($fileInLoc) ){
954 print "*** ERROR $sbrName no HSSP file '$fileInLoc'\n";
955 return(0);}
956 # ------------------------------
957 # settings describing format
958 @kwdDefHsspTopLoc= ("PDBID","DATE","SEQBASE","PARAMETER","THRESHOLD",
959 "REFERENCE","HEADER","COMPND","SOURCE","AUTHOR",
960 "SEQLENGTH","NCHAIN","KCHAIN","NALIGN");
961 @kwdDefHsspHdrLoc= ("NR","ID","STRID","IDE","WSIM","IFIR","ILAS",
962 "JFIR","JLAS","LALI","NGAP","LGAP","LSEQ2","ACCNUM","PROTEIN");
963 $regexpBegHeader= "^\#\# PROTEINS"; # begin of reading
964 $regexpEndHeader= "^\#\# ALIGNMENTS"; # end of reading
965 $regexpLongId= "^PARAMETER LONG-ID :YES"; # identification of long id
966
967 $lenStrid= 4; # minimal length to identify PDB identifiers
968 $LisLongId= 0; # long identifier names
969
970 $ptr{"IDE"}=1;$ptr{"WSIM"}=2;$ptr{"IFIR"}=3;$ptr{"ILAS"}=4;$ptr{"JFIR"}=5;$ptr{"JLAS"}=6;
971 $ptr{"LALI"}=7;$ptr{"NGAP"}=8;$ptr{"LGAP"}=9;$ptr{"LSEQ2"}=10; $ptr{"ACCNUM"}=11;
972
973 # ------------------------------
974 # check input arguments
975 $#kwdHsspTopLoc=$#kwdHsspHdrLoc=$Lpdb=$LnoPair=0;
976 foreach $kwd (@kwdInLoc){
977 $Lok=0;
978 if ($kwd eq "nopair"){
979 $LnoPair=1;
980 next;}
981 $Lpdb=1 if (! $Lpdb && ($kwd =~/^PDBID/));
982 foreach $des (@kwdDefHsspTopLoc) {
983 if ($kwd eq $des){ $Lok=1; push(@kwdHsspTopLoc,$kwd);
984 last;}}
985 next if ($Lok || $LnoPair);
986 foreach $des (@kwdDefHsspHdrLoc){
987 if ($kwd eq $des){ $Lok=1; push(@kwdHsspHdrLoc,$kwd);
988 last;} }
989 print "-*- WARNING $sbrName input kwd=$kwd, makes no sense??\n" if (! $Lok);}
990
991 # force reading of NALI
992 push(@kwdHsspTopLoc,"PDBID") if (! $Lpdb);
993
994 # get column numbers to read
995 # ------------------------------
996 # now start to read
997 $Lok=&open_file("$fhinLoc","$fileInLoc");
998 if (! $Lok){print "*** ERROR $sbrName HSSP file could not be opened '$fileInLoc'\n";
999 return(0);}
1000 undef %tmp; # save space
1001 # ------------------------------
1002 while ( <$fhinLoc> ) { # read top
1003 last if ($_ =~ /$regexpBegHeader/);
1004 if ($_ =~ /$regexpLongId/) {
1005 $LisLongId=1;}
1006 else{$_=~s/\n//g;$arg=$_;
1007 foreach $des (@kwdHsspTopLoc){
1008 if ($arg =~ /^$des\s+(.+)$/){
1009 if (defined $ok{"$des"}){ # multiple lines!
1010 $tmp="$1"."\t";
1011 if (defined $tmp{"$des"}){
1012 $tmp{"$des"}.=$tmp;}
1013 else{$tmp{"$des"}=$tmp;}}
1014 else {$ok{"$des"}=1;$tmp{"$des"}=$1;}
1015 if ($des=~/SEQLENGTH|NCHAIN|KCHAIN|NALIGN/){
1016 $tmp{"$des"}=~s/^(\d+)[^0-9]+.*$/$1/;} # purge blanks
1017 last;}}}}
1018 # ------------------------------
1019 $ct=0; # read header
1020 while ( <$fhinLoc> ) {
1021 last if ($LnoPair); # supress reading pair info
1022 last if ($_ =~ /$regexpEndHeader/);
1023 next if ($_ =~ /^ NR\./); # skip descriptors
1024 $_=~s/\n//g;
1025 $lenLine=length($_);
1026 if ($LisLongId){
1027 $maxMid=115; $maxMid=($lenLine-56) if ($lenLine < 115);
1028 $maxEnd=109; $maxEnd=$lenLine if ($lenLine < 109);
1029 $beg=substr($_,1,56);
1030 $end=0; $end=substr($_,109) if ($lenLine >=109);
1031 $mid=substr($_,57,115); }
1032 else {
1033 $maxMid= 62; $maxMid=($lenLine-28) if ($lenLine < 90);
1034 $beg=substr($_,1,28);
1035 $end=0; $end=substr($_,90) if ($lenLine >=90);
1036 $mid=substr($_,29,90); }
1037 $end=~s/^\s*|\s*$//g; # purge leading blanks
1038 $mid=~s/^\s*|\s*$//g; # purge leading blanks
1039 # SWISS accession: hack because it may be empty!
1040 if ($lenLine > 86) {
1041 $accnum=substr($_,81,6); $accnum=~s/(\s)\s+/$1/g ; }
1042 else {
1043 $accnum=0;}
1044 # begin: counter and id
1045 $beg=~s/.+ \:\s*|\s*$//g;
1046 if (! $LisLongId) {$id=$beg;$id=~s/([^\s]+).*$/$1/;
1047 $strid=$beg;$strid=~s/$id|\s//g; }
1048 else {$id=$beg;$id=~s/(.+_\S+).*/$1/;
1049 $strid=substr($_,49,6);$strid=~s/\s//g; }
1050 $tmp=$lenStrid-1;
1051 if ( (length($strid)<$lenStrid) && ($id=~/^[0-9][A-Z0-9]{$tmp,$tmp}\s*/)){
1052 $strid=substr($id,1,$lenStrid); }
1053 ++$ct;
1054
1055 $tmp{"ID","$ct"}= $id;
1056 $tmp{"NR","$ct"}= $ct;
1057 $tmp{"STRID","$ct"}= $strid;
1058 # correct for ID = PDBid
1059 $tmp{"STRID","$ct"}= $id if ($strid=~/^\s*$/ && &is_pdbid($id));
1060
1061 $tmp{"PROTEIN","$ct"}=$end;
1062 $tmp{"ID1","$ct"}=$tmp{"PDBID"};
1063 $tmp{"ACCNUM","$ct"}=$accnum;
1064 # middle all info
1065 $#tmp=0;@tmp=split(/\s+/,$mid);
1066 foreach $_ (@tmp) {$_=~s/\s//g;}
1067
1068 foreach $des (@kwdHsspHdrLoc){
1069 next if ( ! defined $ptr{"$des"});
1070 next if ( $des =~/^ACCNUM/);
1071 $ptr=$ptr{"$des"};
1072 $tmp{"$des","$ct"}=$tmp[$ptr]; }}close($fhinLoc);
1073 $tmp{"NROWS"}=$ct;
1074 # clean up
1075 $#kwdInLoc=$#kwdDefHsspHdrLoc=$#kwdHsspTopLoc=$#tmp=
1076 $#kwdDefHsspTopLoc=$#kwdHsspHdrLoc=0;
1077 undef %ptr;
1078 return(1,%tmp);
1079 } # end of hsspRdHeader
1080
1081 #==============================================================================
1082 sub hsspRdSeqSecAccOneLine {
1083 local ($inLine) = @_ ;
1084 local ($sbrName,$fhinLoc,$seqNo,$pdbNo,$chn,$seq,$sec,$acc,$tmp);
1085 $[ =1 ;
1086 #-------------------------------------------------------------------------------
1087 # hsspRdSeqSecAccOneLine reads begin of one HSSP line
1088 #-------------------------------------------------------------------------------
1089 $sbrName="hsspRdSeqSecAccOneLine";
1090
1091 $seqNo=substr($inLine,1,6);$seqNo=~s/\s//g;
1092 $pdbNo=substr($inLine,7,5);$pdbNo=~s/\s//g;
1093 $chn= substr($inLine,13,1);
1094 $seq= substr($inLine,15,1);
1095 $sec= substr($inLine,18,1);
1096 $acc= substr($inLine,36,4);$acc=~s/\s//g;
1097 return($seqNo,$pdbNo,$chn,$seq,$sec,$acc)
1098 } # end of hsspRdSeqSecAccOneLine
1099
1100 #==============================================================================
1101 sub is_hssp {
1102 local ($fileInLoc) = @_ ;local ($fh,$Lis);
1103 #--------------------------------------------------------------------------------
1104 # is_hssp checks whether or not file is in HSSP format
1105 # in: $file
1106 # out: 1 if is hssp; 0 else
1107 #--------------------------------------------------------------------------------
1108 return (0) if (! defined $fileInLoc || ! -e $fileInLoc) ;
1109 $fh="FHIN_CHECK_HSSP";
1110 &open_file("$fh", "$fileInLoc") || return(0);
1111 $Lis=0;
1112 while ( <$fh> ) {$Lis=1 if (/^HSSP/) ;
1113 last; }close($fh);
1114 return $Lis;
1115 } # end of is_hssp
1116
1117 #==============================================================================
1118 sub is_pdbid {
1119 local ($id) = @_ ;
1120 #--------------------------------------------------------------------------------
1121 # is_pdbid checks whether or not id is a valid PDBid (number 3 char)
1122 # in: $file
1123 # out: 1 if is DBid, 0 else
1124 #--------------------------------------------------------------------------------
1125 return 1
1126 if ((length($id) <= 6) &&
1127 ($id=~/^[0-9][0-9a-z]{3,3}[^0-9a-z]?/));
1128 return 0;
1129 } # end of is_pdbid
1130
1131 #==============================================================================
1132 sub open_file {
1133 local ($file_handle,$file_name,$log_file) = @_ ;
1134 local ($temp_name) ;
1135 #-------------------------------------------------------------------------------
1136 # open_file opens file, writes warning asf
1137 #-------------------------------------------------------------------------------
1138 $temp_name = $file_name ;
1139 $temp_name =~ s/^>>|^>//g ;
1140 if ( ($file_name =~ /^>>/ ) && ( ! -e $temp_name ) ) {
1141 print "*** INFO (open_file): append file=$temp_name, does not exist-> create it\n" ;
1142 open ($file_handle, ">$temp_name") || ( do {
1143 warn "***\t Cannot create new file: $temp_name\n" ;
1144 if ( $log_file ) {
1145 print $log_file "***\t Cannot create new file: $temp_name\n" ;}
1146 return (0);
1147 } );
1148 close ("$file_handle") ;}
1149
1150 open ($file_handle, "$file_name") || ( do {
1151 warn "*** ERROR lib-br:open_file: Cannot open file '$file_name'\n" ;
1152 if ( $log_file ) {
1153 print $log_file "*** lib-br:open_file: Cannot create new file '$file_name'\n" ;}
1154 return(0);
1155 } );
1156 return(1);
1157 } # end of open_file
1158
1159 #==============================================================================
1160 sub safWrt {
1161 local($fileOutLoc,%tmp) = @_ ;
1162 local(@nameLoc,@stringLoc,$tmp,$sbrName,$Lok);
1163 $[ =1 ;
1164 #--------------------------------------------------------------------------------
1165 # safWrt writing an SAF formatted file of aligned strings
1166 # in: $fileOutLoc output file
1167 # = "STDOUT" -> write to screen
1168 # in: $tmp{"NROWS"} number of alignments
1169 # in: $tmp{"id", "$it"} name for $it
1170 # in: $tmp{"seq","$it"} sequence for $it
1171 # in: $tmp{"PER_LINE"} number of res per line (def=50)
1172 # in: $tmp{"HEADER"} 'line1\n,line2\n'..
1173 # with line1= '# NOTATION ..'
1174 # out: 1|0,msg implicit: file
1175 # err: ok-> 1,ok | error -> 0,message
1176 #--------------------------------------------------------------------------------
1177 $sbrName="lib-br:safWrt"; $fhoutLoc="FHOUT_safWrt";
1178 # check input
1179 return(0,"*** ERROR $sbrName: no acceptable output file ($fileOutLoc) defined\n")
1180 if (! defined $fileOutLoc || length($fileOutLoc)<1 || $fileOutLoc !~/\w/);
1181 return(0,"*** ERROR $sbrName: no input given (or not input{NROWS})\n")
1182 if (! %tmp || ! defined $tmp{"NROWS"} );
1183 return(0,"*** ERROR $sbrName: tmp{NROWS} < 1\n")
1184 if ($tmp{"NROWS"} < 1);
1185 $tmp{"PER_LINE"}=50 if (! defined $tmp{"PER_LINE"});
1186 $fhoutLoc="STDOUT" if ($fileOutLoc eq "STDOUT");
1187 # ------------------------------
1188 # open new file
1189 if ($fhoutLoc ne "STDOUT") {
1190 &open_file("$fhoutLoc",">$fileOutLoc") ||
1191 return(0,"*** ERROR $sbrName: failed opening fileOut=$fileOutLoc\n"); }
1192 # ------------------------------
1193 # write header
1194 # ------------------------------
1195 print $fhoutLoc "# SAF (Simple Alignment Format)\n","\# \n";
1196 print $fhoutLoc $tmp{"HEADER"} if (defined $tmp{"HEADER"});
1197
1198 # ------------------------------
1199 # write data into file
1200 # ------------------------------
1201 for($itres=1; $itres<=length($tmp{"seq","1"}); $itres+=$tmp{"PER_LINE"}){
1202 foreach $itpair (1..$tmp{"NROWS"}){
1203 printf $fhoutLoc "%-20s",$tmp{"id","$itpair"};
1204 # chunks of $tmp{"PER_LINE"}
1205 $chunkEnd=$itres + ($tmp{"PER_LINE"} - 1);
1206 foreach $itchunk ($itres .. $chunkEnd){
1207 last if (length($tmp{"seq","$itpair"}) < $itchunk);
1208 print $fhoutLoc substr($tmp{"seq","$itpair"},$itchunk,1);
1209 # add blank every 10
1210 print $fhoutLoc " "
1211 if ($itchunk != $itres && (int($itchunk/10)==($itchunk/10)));
1212 }
1213 print $fhoutLoc "\n"; }
1214 print $fhoutLoc "\n"; }
1215
1216 print $fhoutLoc "\n";
1217 close($fhoutLoc);
1218 $#nameLoc=$#stringLoc=0; # save space
1219
1220 return(0,"*** ERROR $sbrName: failed to write file $fileOutLoc\n") if (! -e $fileOutLoc);
1221 return(1,"ok $sbrName");
1222 } # end of safWrt
1223
1224
1225 #==============================================================================
1226 # library collected (end)
1227 #==============================================================================
1228
1229 1;