#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "nabcode.h"
































































INT_T fix_angles( m1, i, nr, omega, psi, phi )
MOLECULE_T *  * m1;
INT_T * i;
INT_T * nr;
FLOAT_T * omega;
FLOAT_T * psi;
FLOAT_T * phi;
{


STRING_T * omega_string,  * psi_string,  * phi_string;



STRING_T * npos,  * cpos,  * capos,  * nm1pos,  * cm1pos,  * cam1pos;


POINT_T n_xyz, ca_xyz, c_xyz;

POINT_T cm1_xyz;

POINT_T u, v, zax, p_head, p_tail;

POINT_T va, vb, vc;

FLOAT_T a0, rot_angle, phi0, psi0, omega0;

ATOM_T * a;

INT_T ii;

MATRIX_T mat;


STRING_T * __st0001__;
POINT_T __pt0001__;
if(  * i >  * nr ) * nr =  * i;
NAB_strcpy(  &omega_string, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d-%d:",  * i,  * nr ), __st0001__ ) );
NAB_strcpy(  &psi_string, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:O|:%d-%d:",  * i - 1,  * i,  * nr ), __st0001__ ) );
NAB_strcpy(  &phi_string, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:C*,O*,?[A-Z]*|:%d-%d:*",  * i,  * i + 1,  * nr ), __st0001__ ) );
NAB_strcpy(  &npos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:N",  * i ), __st0001__ ) );
NAB_strcpy(  &cpos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:C",  * i ), __st0001__ ) );
NAB_strcpy(  &capos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:CA",  * i ), __st0001__ ) );
NAB_strcpy(  &cm1pos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:C",  * i - 1 ), __st0001__ ) );
NAB_strcpy(  &cam1pos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:CA",  * i - 1 ), __st0001__ ) );
NAB_strcpy(  &nm1pos, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), ":%d:N",  * i - 1 ), __st0001__ ) );




setpoint(  * m1, npos, n_xyz );
setpoint(  * m1, capos, ca_xyz );
setpoint(  * m1, cpos, c_xyz );
setpoint(  * m1, cm1pos, cm1_xyz );

NAB_ptcpy( u, NAB_ptsub( __pt0001__, ca_xyz, n_xyz ) );
NAB_ptcpy( v, NAB_ptsub( __pt0001__, cm1_xyz, n_xyz ) );
NAB_ptcpy( zax, NAB_ptcrs( __pt0001__, u, v ) );

a0 = angle(  * m1, cm1pos, npos, capos );
rot_angle = 1.219000E+02 - a0;

NAB_ptcpy( p_tail, n_xyz );
NAB_ptcpy( p_head, NAB_ptadd( __pt0001__, n_xyz, zax ) );

NAB_matcpy( mat, rot4p( p_head, p_tail, rot_angle ) );
transformmol( mat,  * m1, omega_string );

psi0 = torsion(  * m1, nm1pos, cam1pos, cm1pos, npos );
rot_angle =  * psi - psi0;
NAB_matcpy( mat, rot4(  * m1, cam1pos, cm1pos, rot_angle ) );
transformmol( mat,  * m1, psi_string );

omega0 = torsion(  * m1, cam1pos, cm1pos, npos, capos );
rot_angle =  * omega - omega0;
NAB_matcpy( mat, rot4(  * m1, cm1pos, npos, rot_angle ) );
transformmol( mat,  * m1, omega_string );

phi0 = torsion(  * m1, cm1pos, npos, capos, cpos );
rot_angle =  * phi - phi0;
NAB_matcpy( mat, rot4(  * m1, npos, capos, rot_angle ) );
transformmol( mat,  * m1, phi_string );

return( 0 );
}



INT_T match_template
( f, phi, psi, omega, struct_type, nres )
FILE_T *  * f;
FLOAT_T phi[];
FLOAT_T psi[];
FLOAT_T omega[];
STRING_T *  * struct_type;
INT_T * nres;
{
STRING_T * line;

INT_T ir, template_nres, ntemp, found;

STRING_T * ttype,  * template_name[50];

STRING_T * s1,  * s2,  * s3;

FLOAT_T f1, f2, f3;

STRING_T * ftmp;


found = 0;
ntemp = 0;
while( NAB_strcpy(  &line, getline(  * f ) ) ){
sscanf( line, "%s %d", NAB_readstring(  &ttype ),  &template_nres );
if( EQ( ttype, "" ) )
continue;
if( template_nres < 1 ){
fprintf( stderr, "template has no residues\n" );
exit( 0 );
}
 ++ ntemp;
NAB_strcpy(  &template_name[ntemp - 1], ttype );
if( NE( ttype,  * struct_type ) ){
for( ir = 1;ir <= template_nres;ir ++  )
NAB_strcpy(  &line, getline(  * f ) );
continue;
}
found = 1;
if( template_nres != 1 && template_nres !=  * nres ){

fprintf( stderr, "template has %d atoms and sequence has %d\n", template_nres,  * nres );
exit( 0 );
}
for( ir = 1;ir <= template_nres;ir ++  ){
NAB_strcpy(  &line, getline(  * f ) );
sscanf( line, "%s %lf %s %lf %s %lf", NAB_readstring(  &s1 ),  &f1, NAB_readstring(  &s2 ),  &f2, NAB_readstring(  &s3 ),  &f3 );
if( EQ( s1, "phi" ) )phi[ir - 1] = f1;
else if( EQ( s1, "psi" ) )psi[ir - 1] = f1;
else if( EQ( s1, "omega" ) )omega[ir - 1] = f1;

if( EQ( s2, "phi" ) )phi[ir - 1] = f2;
else if( EQ( s2, "psi" ) )psi[ir - 1] = f2;
else if( EQ( s2, "omega" ) )omega[ir - 1] = f2;

if( EQ( s3, "phi" ) )phi[ir - 1] = f3;
else if( EQ( s3, "psi" ) )psi[ir - 1] = f3;
else if( EQ( s3, "omega" ) )omega[ir - 1] = f3;
}




if( template_nres == 1 ){
for( ir = 2;ir <=  * nres;ir ++  ){
phi[ir - 1] = phi[1 - 1];
psi[ir - 1] = psi[1 - 1];
omega[ir - 1] = omega[1 - 1];
}
}
break;
}
if(  !found ){
fprintf( stderr, "template not found\n" );
fprintf( stderr, "must be one of:" );
for( ir = 1;ir <= ntemp; ++ ir )
fprintf( stderr, " %s", template_name[ir - 1] );
fprintf( stderr, "\n" );
exit( 0 );
}
return( 0 );
}



static INT_T ir, nr;

static STRING_T * seq,  * struct_type;

static MOLECULE_T * m1;

static FLOAT_T omega[500], psi[500], phi[500];

static POINT_T ax, center;

static ATOM_T * a;

static FILE_T * conformation_file;

static STRING_T * outfile;

static INT_T ac;


main( argc, argv )
	int	argc;
	char	*argv[];
{

static STRING_T * __st0001__;
static STRING_T * __st0002__;
if( argc != 4 && argc != 6 ){
fprintf( stderr, "Usage: %s structure_type sequence pdbout <-lib XXX>\n", argv[1 - 1] );
exit( 1 );
}
if( argc > 4 ){
if( NE( argv[5 - 1], "-lib" ) ){
fprintf( stderr, "Usage: %s structure_type sequence pdbout <-lib XXX>\n", argv[1 - 1] );
exit( 1 );
}
conformation_file = fopen( argv[6 - 1], "r" );
if( conformation_file == NULL ){
fprintf( stderr, "conformation file not found %s\n", argv[6 - 1] );
exit( 1 );
}
}else{
conformation_file = fopen( NAB_strcat( getenv( "NABHOME" ), "/reslib/conf.lib" ), "r" );
if( conformation_file == NULL ){

fprintf( stderr, "conformation file not found %s\n", NAB_strcat( getenv( "NABHOME" ), "/reslib/conf.lib" ) );
exit( 1 );
}
}

NAB_strcpy(  &struct_type, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), "%s", argv[2 - 1] ), __st0001__ ) );
NAB_strcpy(  &seq, SPRINTF( sprintf( NAB_readstring(  &__st0001__ ), "%s", argv[3 - 1] ), __st0001__ ) );
nr = length( seq );
NAB_strcpy(  &outfile, argv[4 - 1] );

if( nr > 500 ){
fprintf( stderr, "MAXRES exceeded\n" );
exit( 0 );
}


match_template(  &conformation_file, phi, psi, omega,  &struct_type,  &nr );


m1 = linkprot( STEMP( __st0001__, "new" ),  &seq, STEMP( __st0002__, "" ) );


for( ir = 2;ir <= nr; ++ ir ){
fix_angles(  &m1,  &ir,  &nr,  &omega[ir - 1],  &psi[ir - 1 - 1],  &phi[ir - 1] );
}

putpdb( outfile, m1 );

	exit( 0 );
}
