/******************************************************************************/
/*Program: renumb                                                             */
/*Author: Stefan Henrich                                                      */ 
/*Email:  stefan.henrich@eml-r.villa-bosch.de                                 */
/*Date: 23.05.2005  version 1.4                                               */
/* 15.09.2005 v 1.5: cyxout is not written anymore                            */
/*                                                                            */
/*Purpose: renumbering of residues because of problems in sander              */
/* for distinguish between residues, e.g. Arg37A,Gly37B,Gly37C                */
/*                                                                            */
/*COMPILATION: g++ -o renumb renumb.C                                         */
/*USAGE: >renumb protein.modif.pdb protein.renumb.pdb                         */
/******************************************************************************/

// v1.1: stop with '\n' after TER
// v1.4: if TER than change resiold strncpy(resiold,"TER     ",5)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (
	int argc,         /* Number of args */
	char ** argv)     /* Arg list       */
{
	FILE *finput,*foutput,*fcyxout;
    char line[120],newline[120],cyxtmp[200];
	char resinew[]="      ",resiold[]="      ",fill[]="      ",buffer[]="      ";
	int resinumb=0,length=0;

	if((finput = fopen(argv[1] , "r"))==NULL)
	  {printf("Input file can not be opened .\n");exit(0);}
	if((foutput = fopen(argv[2], "w"))==NULL)
	  {printf("Output file can not be written .\n");exit(0);}
//	if((fcyxout = fopen("CYX-RESIDUES.TMP", "w"))==NULL)
//	  {printf("Output file can not be written .\n");exit(0);}

////////////////////////////////////////////////////////
	while(fgets(line,100,finput)){
		strncpy (newline,line,99);
		if(strncmp(line,"ATOM  ",6)==0 || strncmp(line,"HETATM",6)==0)	strncpy(resinew,line+22,5); //get string of residue number and extension

		if((strncmp(line,"ATOM  ",6)==0 || strncmp(line,"HETATM",6)==0) && strcmp(resinew,resiold)!=0)	{  
			resinumb=resinumb+1;} //for new residue add 1 to residue number
		if(strncmp(line,"ATOM  ",6)==0 || strncmp(line,"HETATM",6)==0)	{  
			sprintf(buffer,"%i",resinumb); //integer to string
			length=strlen(buffer); //length of string
			strncpy(newline,line,22);newline[22]='\0';
			strncat(newline,fill,4-length); //add blank to buffer from left side of residue number
			strcat(newline,buffer);
			strcat(newline," "); //overwrite former extension of residue number
			strncat(newline+27,line+27,72);
			strcpy(resiold,resinew); //store residue number for next cycle
		}
		if(strncmp(line,"TER",3)==0)	{
			strcpy(newline,"TER\n");
			//newline[3]='\n';
			strncpy(resiold,"TER     ",5);
		}

// write residue numbers of CYX into file CYX-RESIDUES.TMP
// renumb.C (vers. 1.2) writes file CYX-RESIDUES.TMP with residue numbers of CYX residues
// the CYX-RESIDUES.TMP will be later used (after whatif) for renaming CYS bach into CYX
/*		if(strncmp(line,"ATOM  ",6)==0 && strncmp(line+17,"CYX",3)==0 && strncmp(line+13,"N",1)==0)	{ 
			strncpy(cyxtmp,newline+22,4);
			cyxtmp[4]='\0';
			fprintf(fcyxout,"%s",cyxtmp); //print into cyxout file	 
		}
*/
  
		fprintf(foutput,"%s",newline); //print into output file
	} /* end of while */

	fclose(finput);
	fclose(foutput);
//	fclose(fcyxout);
 return(1);
}
