Subject: csd_to_pdb renew version

From: <[email-masked]>
Date: Mon, 2 May 1994 19:32:48 +0200

Dear netters,

Sorry to bother you,

I did include a wrong code of csdpdb.c, oops!, the one I send works
but does not skip molecules with no coordinates, that one does.

See you

Francois Savary

--------------------------------------------------------------
Francois Savary

Department of Physical Chemistry
University of Geneva
30, quai Ernest-Ansermet
CH-1211 Geneva 4

phone  : +4122 702 65 32
fax    : +4122 702 65 18
e-mail : [email-masked]
--------------------------------------------------------------

--------------------ultimate version-------------------------
/* converter from .dat file, from CSD to .ent file
   Francois Savary, University of Geneva
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define SQR(a) (a)*(a)

FILE *open_file(char buffer[101],char type[5]);
char* get_name(char text[]);
char buffer[101],dumm[101],refcode[9],txtinfo[101],symetrypos[101],
     radiusvalue[101],nomat[250][6];
long sys,adat,nopr,nrad,nat,nsat,year,cella,cellb,cellc,cellA,cellB,cellC
     ,preca,precb,precc,precA,precB,precC,nline,nbat,nrfac,nrem,ndis,nerr
     ,nsymline,nradline,ntxtline;
float x[250],y[250],z[250],rcov[250];
void ortho(float x[250],float y[250],float z[250],long cella,long cellb,
	      long cellc,long cellA,long cellB,long cellC, long nbat);
void writepdb(char nomat[250][6],float x[250],float y[250],
	            float z[250], char refcode[9]);

static char rcovlab[98][3]={
" H","H1","H2","H3","NE"," F"," O","O1","O2","O3","O4"," N",
" C","C1","C2","C3","C4"," B","BE","HE","AR","CL"," S",
" P","SI","KR","BR","NI","CO","SE","MN","FE","CU","AL","CR","AS","GE",
" V","LI","RU","ZN","RH","GA","OS","IR","TC","RE","PD",
" W","PT","MO","XE","TI"," I","NB","TA","AU","AG","MG","TE","SB","SN",
" U","IN","SC","HF","AT","ZR","BI","PO","PB","TH","CD","HG","NA","LU",
"TM","ER","HO","DY","TB","GD","SM"," Y","PM","ND","TH","CE","PR","LA",
"CA","YB","EU","SR","BA"," K","RB","CS"};

static float rcovval[98]={
0.32,0.32,0.32,0.32,0.71,0.72,0.73,0.73,0.73,0.73,0.73,0.75,0.77,0.77,
0.77,0.77,0.77,0.82,0.90,0.93,0.98,0.99,1.02,1.00,1.11,1.12,1.14,1.15,
1.16,1.16,1.17,1.17,1.17,1.18,1.18,1.20,1.22,1.22,1.23,1.25,1.25,
1.25,1.26,1.26,1.27,1.27,1.28,1.28,1.30,1.30,1.30,1.31,1.32,1.33,
1.34,1.34,1.34,1.34,1.36,1.36,1.40,1.41,1.42,1.44,1.44,1.44,1.45,
1.45,1.46,1.46,1.47,1.48,1.48,1.49,1.54,1.56,1.56,1.57,1.58,1.59,
1.59,1.61,1.62,1.62,1.63,1.64,1.65,1.65,1.65,1.69,1.74,1.74,1.85,
1.91,1.98,2.03,2.16,2.35};

void main(void)
{
  register int i,j,k;
  char blank[21];
  FILE *inp;
  strcpy(buffer,get_name("nom input file : "));
  inp=open_file(buffer,"r");
  strcpy(blank,"                   ");
  while (fgets(buffer,100,inp))
    {
    sscanf(buffer,"%1c",dumm);
    if(!(strncmp(dumm,"#",1)))
      {
    sscanf(buffer+1,"%8c",refcode);
    sscanf(buffer+9,"%1c",dumm);
    sscanf(dumm,"%ld",&sys);strcpy(dumm,blank);
    sscanf(buffer+11,"%6c",dumm);
    sscanf(dumm,"%ld",&adat);strcpy(dumm,blank);
    sscanf(buffer+26,"%3c",dumm);
    sscanf(dumm,"%ld",&nrfac);strcpy(dumm,blank);
    sscanf(buffer+29,"%3c",dumm);
    sscanf(dumm,"%ld",&nrem);strcpy(dumm,blank);
    sscanf(buffer+32,"%3c",dumm);
    sscanf(dumm,"%ld",&ndis);strcpy(dumm,blank);
    sscanf(buffer+35,"%3c",dumm);
    sscanf(dumm,"%ld",&nerr);strcpy(dumm,blank);
    sscanf(buffer+38,"%3c",dumm);
    sscanf(dumm,"%ld",&nopr);strcpy(dumm,blank);
    sscanf(buffer+41,"%3c",dumm);
    sscanf(dumm,"%ld",&nrad);strcpy(dumm,blank);
    sscanf(buffer+44,"%3c",dumm);
    sscanf(dumm,"%ld",&nat);strcpy(dumm,blank);
    sscanf(buffer+47,"%3c",dumm);
    sscanf(dumm,"%ld",&nsat);strcpy(dumm,blank);
    sscanf(buffer+78,"%2c",dumm);
    sscanf(dumm,"%ld",&year);strcpy(dumm,blank);
    fgets(buffer,100,inp);
    sscanf(buffer,"%6c",dumm);
    sscanf(dumm,"%ld",&cella);strcpy(dumm,blank);
    sscanf(buffer+6,"%6c",dumm);
    sscanf(dumm,"%ld",&cellb);strcpy(dumm,blank);
    sscanf(buffer+12,"%6c",dumm);
    sscanf(dumm,"%ld",&cellc);strcpy(dumm,blank);
    sscanf(buffer+18,"%6c",dumm);
    sscanf(dumm,"%ld",&cellA);strcpy(dumm,blank);
    sscanf(buffer+24,"%6c",dumm);
    sscanf(dumm,"%ld",&cellB);strcpy(dumm,blank);
    sscanf(buffer+30,"%6c",dumm);
    sscanf(dumm,"%ld",&cellC);strcpy(dumm,blank);
    sscanf(buffer+36,"%1c",dumm);
    sscanf(dumm,"%d",&preca);strcpy(dumm,blank);
    sscanf(buffer+37,"%1c",dumm);
    sscanf(dumm,"%d",&precb);strcpy(dumm,blank);
    sscanf(buffer+38,"%1c",dumm);
    sscanf(dumm,"%d",&precc);strcpy(dumm,blank);
    sscanf(buffer+39,"%1c",dumm);
    sscanf(dumm,"%d",&precA);strcpy(dumm,blank);
    sscanf(buffer+40,"%1c",dumm);
    sscanf(dumm,"%d",&precB);strcpy(dumm,blank);
    sscanf(buffer+41,"%1c",dumm);
    sscanf(dumm,"%d",&precC);strcpy(dumm,blank);
    nline=(nrfac+nrem+ndis+nerr) %80;
    if (nline !=0) {ntxtline=(nrfac+nrem+ndis+nerr) / 80 +1;}
    else {ntxtline=(nrfac+nrem+ndis+nerr) / 80;};
    for (i=0;i<ntxtline;i++)
    fgets(txtinfo,100,inp);
    nline=nopr %5;
    if (nline !=0) {nsymline=nopr / 5 +1;}
    else {nsymline=nopr / 5;};
    for (i=0;i<nsymline;i++)
    {
     fgets(symetrypos,100,inp);
    }
    nline=nrad%16;
    if (nline !=0) {nradline=nrad / 16 +1;}
    else {nradline=nrad/16;};
    for (i=0;i<nradline;i++)
     fgets(radiusvalue,100,inp);
    nbat = nat + nsat;
    if (nbat!=0)
    {
    nline = nbat % 3;
    if (nline !=0)
      {
      k= (nbat / 3) + 1;
      for (i=0;i < k;i++)
	{
	  j= 3*i;
	  fgets(buffer,100,inp);
	  sscanf(buffer,"%5c",nomat+j);
	  sscanf(buffer+5,"%7c",dumm);
	  sscanf(dumm,"%f",x+j);strcpy(dumm,blank);
	  sscanf(buffer+12,"%7c",dumm);
	  sscanf(dumm,"%f",y+j);strcpy(dumm,blank);
	  sscanf(buffer+19,"%7c",dumm);
	  sscanf(dumm,"%f",z+j);strcpy(dumm,blank);
	  sscanf(buffer+27,"%5c",nomat+j+1);
	  sscanf(buffer+32,"%7c",dumm);
	  sscanf(dumm,"%f",x+j+1);strcpy(dumm,blank);
	  sscanf(buffer+39,"%7c",dumm);
	  sscanf(dumm,"%f",y+j+1);strcpy(dumm,blank);
	  sscanf(buffer+46,"%7c",dumm);
	  sscanf(dumm,"%f",z+j+1);strcpy(dumm,blank);
	  sscanf(buffer+54,"%5c",nomat+j+2);
	  sscanf(buffer+59,"%7c",dumm);
	  sscanf(dumm,"%f",x+j+2);strcpy(dumm,blank);
	  sscanf(buffer+66,"%7c",dumm);
	  sscanf(dumm,"%f",y+j+2);strcpy(dumm,blank);
	  sscanf(buffer+73,"%7c",dumm);
	  sscanf(dumm,"%f",z+j+2);strcpy(dumm,blank);
	}
    }
    else
      {
      k= (nbat / 3);
      for (i=0;i < k;i++)
	{

	  j= 3*i;
	  fgets(buffer,100,inp);
	  sscanf(buffer,"%5c",nomat+j);
	  sscanf(buffer+5,"%7c",dumm);
	  sscanf(dumm,"%f",x+j);strcpy(dumm,blank);
	  sscanf(buffer+12,"%7c",dumm);
	  sscanf(dumm,"%f",y+j);strcpy(dumm,blank);
	  sscanf(buffer+19,"%7c",dumm);
	  sscanf(dumm,"%f",z+j);strcpy(dumm,blank);
	  sscanf(buffer+27,"%5c",nomat+j+1);
	  sscanf(buffer+32,"%7c",dumm);
	  sscanf(dumm,"%f",x+j+1);strcpy(dumm,blank);
	  sscanf(buffer+39,"%7c",dumm);
	  sscanf(dumm,"%f",y+j+1);strcpy(dumm,blank);
	  sscanf(buffer+46,"%7c",dumm);
	  sscanf(dumm,"%f",z+j+1);strcpy(dumm,blank);
	  sscanf(buffer+54,"%5c",nomat+j+2);
	  sscanf(buffer+59,"%7c",dumm);
	  sscanf(dumm,"%f",x+j+2);strcpy(dumm,blank);
	  sscanf(buffer+66,"%7c",dumm);
	  sscanf(dumm,"%f",y+j+2);strcpy(dumm,blank);
	  sscanf(buffer+73,"%7c",dumm);
	  sscanf(dumm,"%f",z+j+2);strcpy(dumm,blank);

	}
    }
    ortho(x,y,z,cella,cellb,cellc,cellA,cellB,cellC,nbat);
    writepdb(nomat ,x,y,z,refcode);
  }
  }
  }
  fclose(inp);
}

void ortho(float x[250],float y[250],float z[250],long cella,long cellb,
	      long cellc,long cellA,long cellB,long cellC, long nbat)
{
  register int i;
  float alp,bet,gam,cosa,cosb,cosg,sing,q,p,degrad,precaf,precbf,preccf;

  degrad = 1/57.29578;
  alp = cellA*exp(precA*log(10)*-1)*degrad;
  bet = cellB*degrad*exp(precB*log(10)*-1);
  gam = cellC*degrad*exp(precC*log(10)*-1);
  cosa = cos(alp);
  cosb = cos(bet);
  cosg = cos(gam);
  sing = sin(gam);
  q = (cosa-cosb*cosg) / sing;
  p = sqrt(1.-cosb*cosb-q*q);
  precaf=exp(preca*(-1*log(10)));
  precbf=exp(precb*(-1*log(10)));
  preccf=exp(precc*(-1*log(10)));
  for (i=0;i<nbat;i++)
    {
    x[i] = x[i]*1e-5*cella*precaf;
    y[i] = y[i]*1e-5*cellb*precbf;
    z[i] = z[i]*1e-5*cellc*preccf;
    x[i] += y[i]*cosg + z[i]*cosb;
    y[i] = y[i]*sing + z[i]*q;
    z[i] *= p;
  }
  return;
}

void writepdb(char nomat[250][6],float x[250],float y[250],
	            float z[250], char refcode[9])
{
  FILE *out;
  char blank[21];
  float rayon;
  char file[12],nom[5],nom1[5];
  register int i,j,inom,bond;
  strcpy(blank,"                   ");
  strncpy(file,blank,11);
  strncpy(file,refcode,8);
  if (!strncmp(file+6," ",1)) 
  {
   strcpy(file+6,"\0");
  }
  else
  {
   if (!strncmp(file+7," ",1))
   {
    strcpy(file+7,"\0");
   }
   else
   {
    strcpy(file+8,"\0");
   }
  }
  strcat(file,".ent");
  for (i=0;i<strlen(file);i++) file[i]=tolower(file[i]);
  out=open_file(file,"w");
  fprintf(out,"AUTHOR Francois Savary, csdpdb 1993\n");
  for (i=0;i<nbat;i++)
    {
   strncpy(nom,blank,5);
   strncpy(nom,nomat[i],4);
   inom = isalpha(nom[1]);
   if (inom==0)
     {
    strcpy(nom1," \0");
    strncat(nom1,nom,3);
    strcpy(nom,nom1);
     }
   if (strlen(nom)>4) strcpy(nom+4,"\0");

   fprintf(out,"ATOM   %4d %4s NUL     0    %8.3f%8.3f%8.3f\n",
	      i+1,nom,x[i],y[i],z[i]);

    j=0;
    while (1)
    {
     if (strstr(nom,rcovlab[j]))
     {
      rcov[i]=rcovval[j];
      break;
     }
     j++;
    }

 }

  for (i=0;i<nbat;i++)
  {
   bond=0;
  for (j=i+1;j<nbat;j++)  
  {
   rayon = sqrt(SQR(x[i]-x[j])+SQR(y[i]-y[j])+SQR(z[i]-z[j]));
   if (rayon <= (1.1*(rcov[i]+rcov[j])))
    {
     if (bond==0) fprintf(out,"CONECT%5d",i+1);
     fprintf(out,"%5d",j+1);
     bond++;
    }
  }
   if (bond!=0) fprintf(out,"\n");
  }

  fclose(out);
}

char* get_name(char text[])
{
  char name[101];
  printf("%s",text);
  gets(name);
  return(name);
}

FILE *open_file(char name[101],char type[5])
{
  FILE *Filename;

  if((Filename=fopen(name,type))==NULL) {
    printf("cannot open the file %s\n",name);
    exit(1);
  }
  return(Filename);
}