Exemple #1
0
int main(int argc, char *argv[]) {
	int i;
	int index;
	int format = 0;
	int judge_flag = 0;
	char command_at[2 * MAXCHAR];
	char command_bt[2 * MAXCHAR];
	double sum = 0.0;
	double coordx, coordy, coordz;
	if (strcmp(COLORTEXT, "YES") == 0 || strcmp(COLORTEXT, "yes") == 0) {
		if (argc == 2
			&& (strcmp(argv[1], "-h") == 0
				|| strcmp(argv[1], "-H") == 0)) {
			printf
				("Usage: match  -i input file name in pdb format \n"
				 "              -r ref file name in pdb format\n"
				 "              -o output file name in pdb format\n"
				 "              -m matrix file\n"
				 "              -t type:\n"
				 "                 	1: lsfit, need -i, -r, -o, -m;\n"    
				 "                	2: translation, need -i, -o, -m;\n"); 
			exit(1);
		}
		if (argc != 9 && argc != 11) {
			printf
				("Usage: match  -i input file name in pdb format \n"
				 "              -r ref file name in pdb format\n"
				 "              -o output file name in pdb format\n"
				 "              -m matrix file\n"
				 "              -t type:\n"
				 "                 	1: lsfit, need -i, -r, -o, -m;\n"    
				 "                	2: translation, need -i, -o, -m;\n"); 
			exit(1);
		}
	} else {
		if (argc == 2
			&& (strcmp(argv[1], "-h") == 0
				|| strcmp(argv[1], "-H") == 0)) {
			printf("Usage: match  -i input file name in pdb format \n"
				   "              -r ref file name in pdb format\n"
				   "              -o output file name in pdb format\n"
				   "              -m matrix file \n"
				   "              -t type \n"
				   "                 	1: lsfit, need -i, -r, -o, -m;\n"
				   "                 	2: translation, need -i, -o, -m;\n");
			exit(1);
		}
		if (argc != 9 && argc != 11) {
			printf("Usage: match  -i input file name in pdb format \n"
				   "              -r ref file name in pdb format\n"
				   "              -o output file name in pdb format\n"
				   "              -m matrix file \n"
				   "              -t type \n"
				   "                 	1: lsfit, need -i, -r, -o, -m;\n"
				   "                 	2: translation, need -i, -o, -m;\n");
			exit(1);
		}

	}

	for (i = 1; i < argc; i += 2) {
		if (strcmp(argv[i], "-i") == 0) {
			strcpy(ifilename, argv[i + 1]);
			numarg++;
		}
		if (strcmp(argv[i], "-o") == 0) {
			strcpy(ofilename, argv[i + 1]);
			numarg++;
		}
		if (strcmp(argv[i], "-r") == 0) {
			strcpy(rfilename, argv[i + 1]);
			numarg++;
		}
		if (strcmp(argv[i], "-m") == 0) {
			strcpy(mfilename, argv[i + 1]);
			numarg++;
		}
		if (strcmp(argv[i], "-t") == 0)
			jobtype = atoi(argv[i+1]);
	}
if(jobtype == 1 && numarg != 4) {
	printf("The number of arguments is wrong, exit\n");
	exit(1);
}
if(jobtype == 2 && numarg != 3) {
	printf("The number of arguments is wrong, exit\n");
	exit(1);
}
if(jobtype == 1) {
	rpdb(ifilename, &atomnum1, atom1);
	rpdb(rfilename, &atomnum2, atom2);
	if(atomnum1 != atomnum2 || atomnum1 <=0) {
		printf("\nThe numumber of atoms in input file (%d) is different from the number of atoms in ref file (%d)", atomnum1, atomnum2);
		exit(1);
	}
	atomnum = atomnum1;
	for(i=0;i<atomnum;i++) {
		sum+=(atom1[i].x-atom2[i].x) * (atom1[i].x-atom2[i].x); 
		sum+=(atom1[i].y-atom2[i].y) * (atom1[i].y-atom2[i].y); 
		sum+=(atom1[i].z-atom2[i].z) * (atom1[i].z-atom2[i].z); 
	}
	rms = sqrt(sum/atomnum);
	printf("\nThe rmsd before least-square fitting is %9.4lf", rms);
	if ((fpout = fopen(mfilename, "w")) == NULL) {
		fprintf(stdout, "Cannot open file %s to write in main(), exit\n", mfilename);
		exit(1);
	}
printf("\nwjm %5d\n", atomnum);
for(i=0;i<atomnum;i++) 
	printf("ATOM %5d %5s %9.2lf %9.2lf %9.2lf\n", i+1, atom1[i].name, atom1[i].x, atom1[i].y, atom1[i].z);

for(i=0;i<atomnum;i++) 
	printf("ATOM %5d %5s %9.2lf %9.2lf %9.2lf\n", i+1, atom2[i].name, atom2[i].x, atom2[i].y, atom2[i].z);

for(i=0;i<atomnum;i++) {
	sum = (atom1[i].x-atom2[i].x) * (atom1[i].x-atom2[i].x);
	sum+=(atom1[i].y-atom2[i].y) * (atom1[i].y-atom2[i].y);
	sum+=(atom1[i].z-atom2[i].z) * (atom1[i].z-atom2[i].z);
	sum = sqrt(sum);
	printf("ATOM %5d %5s %9.2lf\n", i+1, atom2[i].name, sum);
}
	rms = lsfit(atom2, atom1, atom3, atomnum) ;
	printf("\nThe rmsd after least-square fitting is %9.4lf\n", rms);
	wpdb(ifilename, ofilename, atom3);
}
if(jobtype == 2) {
	rpdb(ifilename, &atomnum1, atom1);
	rmatrix(mfilename);
	for(i=0;i<atomnum1;i++) {
		coordx = atom1[i].x;
		coordy = atom1[i].y;
		coordz = atom1[i].z;
		atom1[i].x = CG0[0] + ROT[0][0] * (coordx - CG1[0]) + 
				      ROT[0][1] * (coordy - CG1[1]) +
				      ROT[0][2] * (coordz - CG1[2]) ;
		atom1[i].y = CG0[1] + ROT[1][0] * (coordx - CG1[0]) + 
				      ROT[1][1] * (coordy - CG1[1]) +
				      ROT[1][2] * (coordz - CG1[2]) ;
		atom1[i].z = CG0[2] + ROT[2][0] * (coordx - CG1[0]) + 
				      ROT[2][1] * (coordy - CG1[1]) +
				      ROT[2][2] * (coordz - CG1[2]) ;
	}
	wpdb(ifilename, ofilename, atom1);
}
return 0;
}
Exemple #2
0
int main(int argc, char *argv[]) {
	int i,j,k;
	int overflow_flag = 0;
	int count;
	int atid1, atid2;
	int resid1, resid2;
	int oldresid;
	int suc;
	double sum;
	double x,y,z;
	char line[MAXCHAR];
	char defstr[MAXCHAR];
	char atomname[MAXCHAR];

    	amberhome = (char *) getenv("AMBERHOME");
   	if( amberhome == NULL ){
       		fprintf( stdout, "AMBERHOME is not set!\n" );
       		exit(1);
    	}

	if (strcmp(COLORTEXT, "YES") == 0 || strcmp(COLORTEXT, "yes") == 0) {
		if (argc == 2
			&& (strcmp(argv[1], "-h") == 0
				|| strcmp(argv[1], "-H") == 0)) {
			printf
				("Usage: match  -i  input file name \n"
				 "              -r  reference file name \n"
				 "              -f  format: 1-pdb (the default), 2-ac, 3-mol2, 4-sdf, 5-crd/rst\n"
				 "              -o  output file name\n"
				 "              -l  run log file name, default is \"match.log\"\n"
				 "              -s  selection mode\n"
				 "                  0: use all atoms (the default)\n"
				 "                  1: specify atom names\n"
				 "                  2: use atom defination file\n"
				 "                  3: use residue defination file - original residue IDs\n"
				 "                  4: use residue defination file - renumbered residue IDs\n"
				 "              -ds definition string if selection modes of '1' or '3' or '4'\n"
				 "                  e.g. 'C,N,O,CA', or 'HET' which stands for heavy atoms for '-ds 1')\n"
				 "              -df definition file if selection mode of '2' or '3' or '4'\n"
				 "                  records take a form of 'ATOM atom_id_input atom_id_reference'\n"
				 "                  or 'RES res_id_input res_id_reference'\n"
				 "              -n  number of atoms participating ls-fitting,\n"
				 "                  default is -1, which implies to use all the selected atoms\n"
				 "              -m  matrix file, default is \"match.matrix\"\n"
				 "              -t  job type:\n"
				 "                  0: calculate rms only, need -i and -r\n"    
				 "                  1: lsfit, need -i, -r and -o the default\n"    
				 "                  2: translation/rotation, need -i, -o and -m\n"); 
			exit(0);
		}
		if (argc != 23 && argc != 21 && argc != 19 && argc != 17 && argc != 15 && argc != 13 && argc != 11 && argc !=9 && argc !=7 && argc !=5) {
			printf
				("Usage: match  -i  input file name \n"
				 "              -r  reference file name \n"
				 "              -f  format: 1-pdb (the default), 2-ac, 3-mol2, 4-sdf, 5-crd/rst\n"
				 "              -o  output file name\n"
				 "              -l  run log file name, default is \"match.log\"\n"
				 "              -s  selection mode\n"
				 "                  0: use all atoms (the default)\n"
				 "                  1: specify atom names\n"
				 "                  2: use atom defination file\n"
				 "                  3: use residue defination file - original residue IDs\n"
				 "                  4: use residue defination file - renumbered residue IDs\n"
				 "              -ds definition string if selection modes of '1' or '3' or '4'\n"
				 "                  e.g. 'C,N,O,CA', or 'HET' which stands for heavy atoms for '-ds 1')\n"
				 "              -df definition file if selection mode of '2' or '3' or '4'\n"
				 "                  records take a form of 'ATOM atom_id_input atom_id_reference'\n"
				 "                  or 'RES res_id_input res_id_reference'\n"
				 "              -n  number of atoms participating ls-fitting,\n"
				 "                  default is -1, which implies to use all the selected atoms\n"
				 "              -m  matrix file, default is \"match.matrix\"\n"
				 "              -t  job type:\n"
				 "                  0: calculate rms only, need -i and -r\n"    
				 "                  1: lsfit, need -i, -r and -o the default\n"    
				 "                  2: translation/rotation, need -i, -o and -m\n"); 
			exit(0);
		}
	} else {
		if (argc == 2
			&& (strcmp(argv[1], "-h") == 0
				|| strcmp(argv[1], "-H") == 0)) {
			printf
				("Usage: match  -i  input file name \n"
				 "              -r  reference file name \n"
				 "              -f  format: 1-pdb (the default), 2-ac, 3-mol2, 4-sdf, 5-crd/rst\n"
				 "              -o  output file name\n"
				 "              -l  run log file name, default is \"match.log\"\n"
				 "              -s  selection mode\n"
				 "                  0: use all atoms (the default)\n"
				 "                  1: specify atom names\n"
				 "                  2: use atom defination file\n"
				 "                  3: use residue defination file - original residue IDs\n"
				 "                  4: use residue defination file - renumbered residue IDs\n"
				 "              -ds definition string if selection modes of '1' or '3' or '4'\n"
				 "                  e.g. 'C,N,O,CA', or 'HET' which stands for heavy atoms for '-ds 1')\n"
				 "              -df definition file if selection mode of '2' or '3' or '4'\n"
				 "                  records take a form of 'ATOM atom_id_input atom_id_reference'\n"
				 "                  or 'RES res_id_input res_id_reference'\n"
				 "              -n  number of atoms participating ls-fitting,\n"
				 "                  default is -1, which implies to use all the selected atoms\n"
				 "              -m  matrix file, default is \"match.matrix\"\n"
				 "              -t  job type:\n"
				 "                  0: calculate rms only, need -i and -r\n"    
				 "                  1: lsfit, need -i, -r and -o the default\n"    
				 "                  2: translation/rotation, need -i, -o and -m\n"); 
			exit(0);
		}
		if (argc != 23 && argc != 21 && argc != 19 && argc != 17 && argc != 15 && argc != 13 && argc != 11 && argc !=9 && argc !=7 && argc !=5) {
			printf
				("Usage: match  -i  input file name \n"
				 "              -r  reference file name \n"
				 "              -f  format: 1-pdb (the default), 2-ac, 3-mol2, 4-sdf, 5-crd/rst\n"
				 "              -o  output file name\n"
				 "              -l  run log file name, default is \"match.log\"\n"
				 "              -s  selection mode\n"
				 "                  0: use all atoms (the default)\n"
				 "                  1: specify atom names\n"
				 "                  2: use atom defination file\n"
				 "                  3: use residue defination file - original residue IDs\n"
				 "                  4: use residue defination file - renumbered residue IDs\n"
				 "              -ds definition string if selection modes of '1' or '3' or '4'\n"
				 "                  e.g. 'C,N,O,CA', or 'HET' which stands for heavy atoms for '-ds 1')\n"
				 "              -df definition file if selection mode of '2' or '3' or '4'\n"
				 "                  records take a form of 'ATOM atom_id_input atom_id_reference'\n"
				 "                  or 'RES res_id_input res_id_reference'\n"
				 "              -n  number of atoms participating ls-fitting,\n"
				 "                  default is -1, which implies to use all the selected atoms\n"
				 "              -m  matrix file, default is \"match.matrix\"\n"
				 "              -t  job type:\n"
				 "                  0: calculate rms only, need -i and -r\n"    
				 "                  1: lsfit, need -i, -r and -o the default\n"    
				 "                  2: translation/rotation, need -i, -o and -m\n"); 
			exit(0);
		}

	}

	for (i = 1; i < argc; i += 2) {
		if (strcmp(argv[i], "-i") == 0) {
			strcpy(ifilename, argv[i + 1]);
			iinput = 1;
		}
		if (strcmp(argv[i], "-f") == 0) 
			format=atoi(argv[i+1]);
		if (strcmp(argv[i], "-o") == 0) {
			strcpy(ofilename, argv[i + 1]);
			ioutput = 1;
		}
		if (strcmp(argv[i], "-r") == 0) {
			strcpy(rfilename, argv[i + 1]);
			iref = 1;
		}
		if (strcmp(argv[i], "-s") == 0) 
			stype = atoi(argv[i+1]);
		if (strcmp(argv[i], "-ds") == 0) 
			strcpy(def_atomname_str, argv[i + 1]);
		if (strcmp(argv[i], "-df") == 0) 
			strcpy(def_filename, argv[i + 1]);
		if (strcmp(argv[i], "-m") == 0) {
			strcpy(mfilename, argv[i + 1]);
			imatrix = 1;
		}
		if (strcmp(argv[i], "-t") == 0)
			jobtype = atoi(argv[i+1]);
		if (strcmp(argv[i], "-n") == 0)
			nsel = atoi(argv[i+1]);
		if (strcmp(argv[i], "-l") == 0) 
			strcpy(lfilename, argv[i + 1]);
	}

	if(jobtype !=0 && jobtype != 1 && jobtype != 2) jobtype = 1;
	if(format != 1 && format != 2 && format != 3 && format != 4 && format != 5) format = 1;
	if(jobtype == 0) 
		if(iinput == 0 || iref == 0) {
			fprintf(stderr, "RMS calculation needs an input and a reference files, exit\n");
			exit(0);
		}
	if(jobtype == 1) 
		if(iinput == 0 || ioutput == 0 || iref == 0) {
			fprintf(stderr, "The least-square fitting calculation needs an input, an output and a reference files, exit\n");
			exit(0);
		}

	if(jobtype == 2) 
		if(iinput == 0 || ioutput == 0) {
			fprintf(stderr, "The translation/rotation job needs an input and an output files, exit\n");
			exit(0);
		}
	if(jobtype == 0 || jobtype == 1) {
		if ((fplog = fopen(lfilename, "w")) == NULL) {
			fprintf(stderr, "Cannot open log file %s to write, exit\n", lfilename);
			exit(1);
		}
	}
/*	read in input files*/
        memory(0, MAXATOM, MAXBOND, MAXRING);
	if(format == 1) {
                overflow_flag = rpdb(ifilename, &atomnum, atom, cinfo, minfo, 0);
                if (overflow_flag) {
                        cinfo.maxatom = atomnum + 10;
                        cinfo.maxbond = bondnum + 10;
                        memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rpdb(ifilename, &atomnum, atom, cinfo, minfo, 0);
                }
	}
	if(format == 2) {
                overflow_flag = rac(ifilename, &atomnum, atom, &bondnum, bond, &cinfo, &minfo);
                if (overflow_flag) {
                        cinfo.maxatom = atomnum + 10;
                        cinfo.maxbond = bondnum + 10;
                        memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                	overflow_flag = rac(ifilename, &atomnum, atom, &bondnum, bond, &cinfo, &minfo);
                }
	}
	if(format == 3) {
                overflow_flag = rmol2(ifilename, &atomnum, atom, &bondnum, bond, &cinfo, &minfo, 0);
                if (overflow_flag) {
                        cinfo.maxatom = atomnum + 10;
                        cinfo.maxbond = bondnum + 10;
                        memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag =
                		rmol2(ifilename, &atomnum, atom, &bondnum, bond, &cinfo, &minfo, 0);
                }
	}
	if(format == 4) {
                overflow_flag = rmdl(ifilename, &atomnum, atom, &bondnum, bond, cinfo, minfo);
                if (overflow_flag) {
                        cinfo.maxatom = atomnum + 10;
                        cinfo.maxbond = bondnum + 10;
                        memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rmdl(ifilename, &atomnum, atom, &bondnum, bond, cinfo, minfo);
                }
	}
	if(format == 5) {
                overflow_flag = rrst(ifilename, &atomnum, atom, cinfo);
                if (overflow_flag) {
                        cinfo.maxatom = atomnum + 10;
                        memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rrst(ifilename, &atomnum, atom, cinfo);
                }
	}
	free(bond);
	free(ring);
	free(arom);
	cinfo.maxatom = MAXATOM;
	cinfo.maxbond = MAXBOND;
	cinfo.maxring = MAXRING;
        ref_memory(0, MAXATOM, MAXBOND, MAXRING);
	if(format == 1) {
                overflow_flag = rpdb(rfilename, &refatomnum, refatom, cinfo, minfo, 0);
                if (overflow_flag) {
                        cinfo.maxatom = refatomnum + 10;
                        cinfo.maxbond = refbondnum + 10;
                        ref_memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rpdb(rfilename, &refatomnum, refatom, cinfo, minfo, 0);
                }
	}
	if(format == 2) {
               	overflow_flag = rac(rfilename, &refatomnum, refatom, &refbondnum, refbond, &cinfo, &minfo);
                if (overflow_flag) {
                        cinfo.maxatom = refatomnum + 10;
                        cinfo.maxbond = refbondnum + 10;
                        ref_memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
               		overflow_flag = rac(rfilename, &refatomnum, refatom, &refbondnum, refbond, &cinfo, &minfo);
                }
	}
	if(format == 3) {
                overflow_flag = rmol2(rfilename, &refatomnum, refatom, &refbondnum, refbond, &cinfo, &minfo, 0);
                if (overflow_flag) {
                        cinfo.maxatom = refatomnum + 10;
                        cinfo.maxbond = refbondnum + 10;
                        ref_memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rmol2(rfilename, &refatomnum, refatom, &refbondnum, refbond, &cinfo, &minfo, 0);
                }
	}
	if(format == 4) {
                overflow_flag = rmdl(rfilename, &refatomnum, refatom, &refbondnum, refbond, cinfo, minfo);
                if (overflow_flag) {
                        cinfo.maxatom = refatomnum + 10;
                        cinfo.maxbond = refbondnum + 10;
                        ref_memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rmdl(rfilename, &refatomnum, refatom, &refbondnum, refbond, cinfo, minfo);
                }
	}
	if(format == 5) {
                overflow_flag = rrst(rfilename, &refatomnum, refatom, cinfo);
                if (overflow_flag) {
                        cinfo.maxatom = refatomnum + 10;
                        ref_memory(7, cinfo.maxatom, cinfo.maxbond, cinfo.maxring);
                        overflow_flag = rrst(rfilename, &refatomnum, refatom, cinfo);
                }
	}
	free(refbond);
	free(refring);
	free(refarom);
	if(debug == 1) {
		for(i=0;i<atomnum;i++)
			printf("ATOM %5d %5s %5s %5d %8.3lf %8.3lf %8.3lf\n", i+1, atom[i].name, atom[i].aa, atom[i].resno, atom[i].x, atom[i].y, atom[i].z);
		for(i=0;i<refatomnum;i++)
			printf("ATOM %5d %5s %5s %5d %8.3lf %8.3lf %8.3lf\n", i+1, refatom[i].name, refatom[i].aa, refatom[i].resno, refatom[i].x, refatom[i].y, refatom[i].z);
	}
	
if(jobtype == 0 || jobtype == 1) {
        fit_atom = (FITATOM *) malloc(sizeof(FITATOM) * atomnum);
        if (fit_atom == NULL) {
        	fprintf(stderr, "memory allocation error for *fit_atom\n");
                exit(1);
        }
        fit_refatom = (FITATOM *) malloc(sizeof(FITATOM) * refatomnum);
        if (fit_refatom == NULL) {
        	fprintf(stderr, "memory allocation error for *fit_refatom\n");
                exit(1);
        }
	if(stype == 0) {
		for(i=0;i<atomnum;i++) {
			fit_atom[i].x=atom[i].x;
			fit_atom[i].y=atom[i].y;
			fit_atom[i].z=atom[i].z;
		}
		for(i=0;i<refatomnum;i++) {
			fit_refatom[i].x=refatom[i].x;
			fit_refatom[i].y=refatom[i].y;
			fit_refatom[i].z=refatom[i].z;
		}
		fit_atomnum = atomnum;
		fit_refatomnum = refatomnum;
	}
	if(stype == 1) {
		count = 0;
		for(i=0;i <= strlen(def_atomname_str);i++) {
			if(def_atomname_str[i]==' ') continue;
			if(def_atomname_str[i]==',') def_atomname_str[i] = ' ';
			defstr[count] = def_atomname_str[i];
			count++;
		}
		sscanf(defstr, "%s", atomname);	
		strcpy(def_atomname[ndef_atomname].str, atomname);
		ndef_atomname++;
		for(i=1;i<strlen(defstr);i++) 
			if(defstr[i] == ' ') {
				sscanf(&defstr[i], "%s", atomname);
				strcpy(def_atomname[ndef_atomname].str, atomname);
				ndef_atomname++;
			}
		for(i=0;i<atomnum;i++) {
			suc = 0;
			for(j=0;j<ndef_atomname;j++) {
				if(strcmp(def_atomname[j].str, atom[i].name) == 0) {
					suc = 1;
					break;
				}
				if(strcmp(def_atomname[j].str, "HET") == 0) 
					if(atom[i].name[0] != 'H') {
						suc = 1;
						break;
					}
			}
			if(suc == 1) {
				fit_atom[fit_atomnum].x=atom[i].x;
				fit_atom[fit_atomnum].y=atom[i].y;
				fit_atom[fit_atomnum].z=atom[i].z;
				fit_atomnum++;
			}
				
		}
		for(i=0;i<refatomnum;i++) {
			suc = 0;
			for(j=0;j<ndef_atomname;j++) {
				if(strcmp(def_atomname[j].str, refatom[i].name) == 0) {
					suc = 1;
					break;
				}
				if(strcmp(def_atomname[j].str, "HET") == 0) 
					if(refatom[i].name[0] != 'H') {
						suc = 1;
						break;
					}
			}
			if(suc == 1) {
				fit_refatom[fit_refatomnum].x=refatom[i].x;
				fit_refatom[fit_refatomnum].y=refatom[i].y;
				fit_refatom[fit_refatomnum].z=refatom[i].z;
				fit_refatomnum++;
			}
		}
	}
	if(stype == 2) {
		if ((fpdef = fopen(def_filename, "r")) == NULL) {
			fprintf(stderr, "Cannot open file %s, exit\n", def_filename);
			exit(1);
		}
		count = 0;
		for(;;) {
			if (fgets(line, MAXCHAR, fpdef) == NULL) break;
			if(strncmp(line, "ATOM", 4) == 0) {
				sscanf(&line[4], "%d%d", &atid1, &atid2);
				atid1--;
				atid2--;
				fit_atom[count].x=atom[atid1].x;
				fit_atom[count].y=atom[atid1].y;
				fit_atom[count].z=atom[atid1].z;
				fit_refatom[count].x=refatom[atid2].x;
				fit_refatom[count].y=refatom[atid2].y;
				fit_refatom[count].z=refatom[atid2].z;
				count++;
			}
		}
		fit_atomnum = count;
		fit_refatomnum = count;
		fclose(fpdef);
	}

	if(stype == 3 || stype == 4) {
/*first of all, read in atom name definition strings*/
		count = 0;
		for(i=0;i <= strlen(def_atomname_str);i++) {
			if(def_atomname_str[i]==' ') continue;
			if(def_atomname_str[i]==',') def_atomname_str[i] = ' ';
			defstr[count] = def_atomname_str[i];
			count++;
		}
		sscanf(defstr, "%s", atomname);	
		strcpy(def_atomname[ndef_atomname].str, atomname);
		ndef_atomname++;
		for(i=1;i<strlen(defstr);i++) 
			if(defstr[i] == ' ') {
				sscanf(&defstr[i], "%s", atomname);
				if(strcmp(atomname, "HET") == 0) {
					fprintf(stderr, "Warning: HET cannot be defined with -s of 3\n");
					continue;
				}
				strcpy(def_atomname[ndef_atomname].str, atomname);
				ndef_atomname++;
			}
		if(debug == 1) {
			for(i=0;i<ndef_atomname;i++)
				printf("DEF %5d %5s\n", i+1, def_atomname[i].str);

		}
		if(stype == 4) {
			count = 0;
			oldresid = -99999;
			for(i=0;i<atomnum;i++) {
				if(atom[i].resno != oldresid) {
					oldresid = atom[i].resno;
					count++;
				}
				atom[i].resno = count;
			}

			count = 0;
			oldresid = -99999;
			for(i=0;i<refatomnum;i++) {
				if(refatom[i].resno != oldresid) {
					oldresid = refatom[i].resno;
					count++;
				}
				refatom[i].resno = count;
			}
		}
/* now working on residue definition file */
		if ((fpdef = fopen(def_filename, "r")) == NULL) {
			fprintf(stderr, "Cannot open file %s, exit\n", def_filename);
			exit(1);
		}
		count = 0;
		for(;;) {
			if (fgets(line, MAXCHAR, fpdef) == NULL) break;
			if(strncmp(line, "RES", 3) == 0) {
				sscanf(&line[3], "%d%d", &resid1, &resid2);
				if(debug == 1) 
					printf("RES %5d %5d\n", resid1, resid2);	
				for(i=0; i< ndef_atomname; i++) {
					atid1 = -1;
					atid2 = -1;
					for(j=0;j<atomnum;j++) 
						if(strcmp(def_atomname[i].str, atom[j].name) == 0 && atom[j].resno == resid1) {
							atid1 = j;	
							break;
						}
					for(j=0;j<refatomnum;j++) 
						if(strcmp(def_atomname[i].str, refatom[j].name) == 0 && refatom[j].resno == resid2) {
							atid2 = j;	
							break;
						}
					
					if(atid1 >= 0 && atid2 >= 0) {	
						fit_atom[count].x=atom[atid1].x;
						fit_atom[count].y=atom[atid1].y;
						fit_atom[count].z=atom[atid1].z;
						fit_refatom[count].x=refatom[atid2].x;
						fit_refatom[count].y=refatom[atid2].y;
						fit_refatom[count].z=refatom[atid2].z;
						count++;
					}
				}
			}
		}

		fit_atomnum = count;
		fit_refatomnum = count;
		fclose(fpdef);
	}


	if(fit_atomnum != fit_refatomnum || fit_atomnum <=0) {
		fprintf(stderr, "\nThe numumber of fitting atoms in input file (%d) is different from the number of fitting atoms in ref file (%d)", fit_atomnum, fit_refatomnum);
		exit(0);
	}
	if(nsel > 0 && fit_atomnum > nsel) fit_atomnum = nsel;	
	if(fit_atomnum <= 0) {
		fprintf(stderr, "\nThe number of fitting atoms is smaller than or equal to zero, exit!");
		exit(1);
	}
	sum = 0;
	for(i=0;i<fit_atomnum;i++) {
		sum+=(fit_atom[i].x-fit_refatom[i].x) * (fit_atom[i].x-fit_refatom[i].x); 
		sum+=(fit_atom[i].y-fit_refatom[i].y) * (fit_atom[i].y-fit_refatom[i].y); 
		sum+=(fit_atom[i].z-fit_refatom[i].z) * (fit_atom[i].z-fit_refatom[i].z); 
	}
	rms = sqrt(sum/fit_atomnum);
	fprintf(fplog, "The rmsd before least-square fitting is %9.4lf\n", rms);
	fprintf(fplog, "Number of atoms participate least-square fitting is %9d\n", fit_atomnum);
	fprintf(stdout, "\nThe rmsd before least-square fitting is %9.4lf", rms);
	fprintf(stdout, "\nNumber of atoms participate least-square fitting is %9d", fit_atomnum);
	if(jobtype == 0) {
		fclose(fplog);
		return 0;
	}
	if ((fpmatrix = fopen(mfilename, "w")) == NULL) {
		fprintf(stderr, "Cannot open file %s, exit\n", mfilename);
		exit(1);
	}
	rms = lsfit(fit_refatom, fit_atom, fit_atomnum) ;
	for(i=0;i<fit_atomnum;i++) {
               	x=CG0[0]+ROT[0][0]*(fit_atom[i].x-CG1[0])+ ROT[0][1]*(fit_atom[i].y-CG1[1])+ ROT[0][2]*(fit_atom[i].z-CG1[2]);
               	y=CG0[1]+ROT[1][0]*(fit_atom[i].x-CG1[0])+ ROT[1][1]*(fit_atom[i].y-CG1[1])+ ROT[1][2]*(fit_atom[i].z-CG1[2]);
               	z=CG0[2]+ROT[2][0]*(fit_atom[i].x-CG1[0])+ ROT[2][1]*(fit_atom[i].y-CG1[1])+ ROT[2][2]*(fit_atom[i].z-CG1[2]);

		sum =(x-fit_refatom[i].x) * (x-fit_refatom[i].x);
		sum+=(y-fit_refatom[i].y) * (y-fit_refatom[i].y);
		sum+=(z-fit_refatom[i].z) * (z-fit_refatom[i].z);
		sum = sqrt(sum);
		fprintf(fplog, "ATOM    %5d %9.3lf %9.3lf %9.3lf ", i+1,  fit_atom[i].x, fit_atom[i].y, fit_atom[i].z);
		fprintf(fplog, "REFATOM %9.3lf %9.3lf %9.3lf %9.3lf\n", fit_refatom[i].x, fit_refatom[i].y, fit_refatom[i].z, sum);
	}
	fclose(fpmatrix);
	fprintf(fplog, "\nThe rmsd after least-square fitting is %9.4lf\n", rms);
	fprintf(stdout, "\nThe rmsd after least-square fitting is %9.4lf\n", rms);
	fclose(fplog);
}
if(jobtype == 2) 
	rmatrix(mfilename);
if(jobtype == 1 || jobtype == 2) {
	for(i=0;i<atomnum;i++) {
                x=CG0[0]+ROT[0][0]*(atom[i].x-CG1[0])+ ROT[0][1]*(atom[i].y-CG1[1])+ ROT[0][2]*(atom[i].z-CG1[2]);
                y=CG0[1]+ROT[1][0]*(atom[i].x-CG1[0])+ ROT[1][1]*(atom[i].y-CG1[1])+ ROT[1][2]*(atom[i].z-CG1[2]);
                z=CG0[2]+ROT[2][0]*(atom[i].x-CG1[0])+ ROT[2][1]*(atom[i].y-CG1[1])+ ROT[2][2]*(atom[i].z-CG1[2]);
		atom[i].x = x;
		atom[i].y = y;
		atom[i].z = z;
	}
	if(format == 1) wpdb(ofilename, atomnum, atom);
	if(format == 2) wac(ofilename, atomnum, atom, bondnum, bond, cinfo, minfo);
	if(format == 3) wmol2(ofilename, atomnum, atom, bondnum, bond, arom, cinfo, minfo);
	if(format == 4) wmdl(ofilename, atomnum, atom, bondnum, bond, cinfo);
	if(format == 5) wrst(ofilename, atomnum, atom);
}
return 0;
}