#!/usr/local/bin/perl # # Reads a sequence file and makes data file for density net. # # this came from /nfs/ind2/usr1/people/th/dir/mackay/nn # and beta/d/make_aa.p # # p=0/1 states whether the 3,4 data is for a # parallel net or an antiparallel. # # For efficiency of data storage, I will make p=0 cause *only* # the short range dudes to be printed out. So to get data to train a # parallel net, concatenate p=1 and p=0 together. # p=2 prints both # usage: # seq2density.p f=3 p=2 i=in.sh3.seq cp=cp.sh3.seq < sh3.seq sub aa_number { # returns a number in 0-19 or 20 for "-" #usage ( $c ) local($c)=@_; local($ans)=0; if ( ord($c) == ord('-') ) { $ans = 20 ; } else { $ans = ord($c) - ord('A') ; # 0 - 24 with gaps at 1,9,14,20,23. if( $ans <= 8 && $ans >=2 ){ $ans -= 1 ; } elsif( $ans <= 13 && $ans >= 10 ){ $ans -= 2 ; } elsif( $ans <= 19 && $ans >= 15 ){ $ans -= 3 ; } elsif( $ans == 21 || $ans == 22 ){ $ans -= 4 ; } elsif( $ans == 24 ){ $ans -= 5; } elsif( $ans != 0 ) { print STDERR "Not an amino acid: " , $c, "\n" ; $ans = - 1 ; } } return($ans); } sub target { # 1,2-> "1" 3,4,5,6-> "0" #usage ( $c ) local($c)=@_; if( $c == 1 || $c ==2 ){ $c = 1 ; } elsif( $c <= 6 && $c >= 3 ){ $c = 0 ; } else { print STDERR "Not a target: " , $c, "\n" ; $c = 0 ; } return($c); } sub distance { # 0 -> 100; # 1,2 .. 99 -> divide by 100.0 #usage ( $c ) local($c)=@_; if( $c == 0 || $c== -1 || $c > 100 ){ $c = 100 ; } return(substr(($c+0.1)/100.0,0,4)); } # # START HERE # $i=inputs; $t=targets; $f=1; $p=0; # p only relevant to 3 & 4. $min=11; $smallest=100; $count= 0; $naa = 6 ; $ntot = 28 ; $cp=dcopy ; # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # calculate start and end of the two aa sequences if ( $naa % 2 ) { print STDERR "Warning, naa is odd\n" ; } $hnaa = $naa / 2 ; $firstlo = $ntot / 4 - $hnaa ; $firsthi = $ntot / 4 + $hnaa - 1; $secndlo = 3 * $ntot / 4 - $hnaa ; $secndhi = 3 * $ntot / 4 + $hnaa - 1 ; printf STDERR "amino acids %d - %d , %d - %d \n" , $firstlo , $firsthi, $secndlo , $secndhi ; printf STDERR "number of inputs is %d\n", 2 * $naa * 21 + 2 ; if($f>=3){ $doreverse = 1; } else { $doreverse = 0; } open(OUT,">$i"); open(TA,">$t"); open(CP,">$cp"); # read a sequence file and add end blank sequence $aaseq='------'; while(<>){ chop; $aaseq.=$_; } $aaseq.='------'; $nres=length($aaseq); $nr1=$nres-12+1; $nre=$nres-7; print "$nr1 aa were read\n"; print $aaseq,"\n"; # loop over sequence for($i=7;$i<$nre;$i++){ for($j=$i+2;$j<$nre;$j++){ $ii=$i-6; $jj=$j-6; # get sequence $seqpair=substr($aaseq,$ii,14).substr($aaseq,$jj,14); # extract separation of 12,34 $oa = ($j-$i)-2; $ds = $oa; if ( $f<=2 || # ( all distances are reported ) ( $p == 0 && $oa < $min ) || # antiparallel ( $p == 1 && $oa >= $min ) || # parallel ( $p == 2 ) ) # both { # $di = &distance( $oa ) ; # 0-1 variable # # keep track of min separation. Zero doesn't count. # if ( $oa < $smallest && $oa > 0 ) { # $smallest = $oa; # } $out=""; # Turn the first 28 amino acids into unary codes $first = ""; for ( $a = $firstlo ; $a<= $firsthi ; $a++ ) { $v = &aa_number(substr($seqpair,$a,1)) + 1 ; @count[$v] ++ ; $first .= sprintf ( "%2d " , $v ) ; } $out .= $first." "; $secnd = ""; for ( $a = $secndlo ; $a<= $secndhi ; $a++ ) { $v = &aa_number(substr($seqpair,$a,1)) + 1 ; @count[$v] ++ ; $secnd .= sprintf ( "%2d " , $v ) ; } $out .= $secnd."\n"; print OUT $out ; # write out file listing i,j,nc,sep # nc=0 -> NC $i6=$i-6; $j6=$j-6; $out = $i6." ".$j6." 0 ".$ds."\n"; print CP $out; # opposite order if ( $doreverse ) { $rev = $secnd." ".$first."\n" ; print OUT $rev ; $out = $i6." ".$j6." 1 ".$ds."\n"; # why make these two entries? Isn't it just doubling the labour? print CP $out; } } else { $skippedcount++ ; } } } for ( $v=0; $v<=20; $v++ ) {print $v, " ", @count[$v], "\n"; } print STDERR " count = ",$count ,": smallest distance = ", $smallest, "\n"; print STDERR " skipped count = ",$skippedcount ,"\n";