You are here:

C/Find Cutoff value

Advertisement


Question
QUESTION: Hi Zlatko,
This is saran, I come up with same program with another doubt.
My code:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <math.h>

char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
 int count = stop - start;
  if ( count >= --size )
  {
     count = size;
  }
  sprintf(dst, "%.*s", count, src + start);
  return dst;
}

int main()
{
 const char filename[] = "3IL8.pdb";
 char temp[200], buffer[2000];
 FILE *file,*file1;
 char X[10],Y[10],Z[10],s1[50],s2[1000][25];
 int i,j,k,t,c=0,count=0,count1=0;
 double a,x[600],y[600],z[600],d[1000],s[1000];


 file = fopen(filename, "r");
 file1 = fopen("grep.txt","w");

 while(fgets(temp,200,file)!=NULL)
 {
  if (strncmp(temp, "ATOM ", 5) == 0)
  {
   fprintf(file1, "%s", temp);
  }
 }
 fclose(file);
 fclose(file1);
 file1 = fopen("grep.txt", "r");
 if ( file1 )
  {
     printf("
---------------------------------------------------------------------------------------------
");
     printf("       Atom details    X Coordiante       Y Coordiante       Z Coordiante
");
     printf("---------------------------------------------------------------------------------------------
");
     for ( i = 1; fgets(buffer, sizeof buffer, file1); i++ )
     {
       printf("%s   ", substring(7, 27, buffer, s1, sizeof s1));
       printf("X[%d] = %s   ", i,substring(30, 8, buffer, X, sizeof X));
       printf("Y[%d] = %s   ", i,substring(38, 8, buffer, Y, sizeof Y));
       printf("Z[%d] = %s
", i,substring(46, 8, buffer, Z, sizeof Z));
       count+=1;
       x[i]=atof(X);
       y[i]=atof(Y);
       z[i]=atof(Z);
       strcpy(s2[i],s1);
     }
   }
  fclose(file1);
  for(i=1;i<=count;i++)
  {
   for(j=i+1;j<=count;j++)
   {
     d[i]=sqrt(pow((x[j] - x[i]),2)+pow((y[j] - y[i]),2)+pow((z[j] - z[i]),2));
   printf("%s   %s   %lf
",s2[i],s2[j],d[i]);
   }
  }
  printf("

Enter the interaction cut-off value : ");
  scanf("%lf",&a);
  for(k=1,t=k+1;k<count;k++,t++)
  {
   if(d[k]<=a)
   {
       printf("%s   %s   %lf
",s2[k],s2[t],d[k]);
    }
   }
}


This program find the distance of all atoms in a pdb file. Now i want find the particular set of value i.e. The user enter the cutoff value, then it display the values under that cutoff and print it in the same format "printf("%s   %s   %lf
",s2[i],s2[j],d[i]);". I tried it at end of the program but it doesnt display all values which come under that cutoff value. Reply ASAP. Thanks in advance.

ANSWER: Hello Saran
The problem is in this code
for(i=1;i<=count;i++)
   {
       for(j=i+1;j<=count;j++)
       {
         d[i]=sqrt(pow((x[j] - x[i]),2)+pow((y[j] - y[i]),2)+pow((z[j] - z[i]),2));
         printf("%s %s %lf",s2[i],s2[j],d[i]);
       }
   }

Notice that for a given value of i, the inner loop goes through a number of calculations, but only the last calculation of the inner loop is saved in d[i]. If you are trying to find the distance between all pairs of atoms, then you need a two dimensional array for d to save all calculations.

You can make the dimensions of d the same as the dimensions of x, y, and z, which are each 600, but make d a two dimensional array.

double d[600][600];

   for(i=1;i<=count;i++)
   {
       for(j=i+1;j<=count;j++)
       {
         d[i][j]=sqrt(pow((x[j] - x[i]),2)+pow((y[j] - y[i]),2)+pow((z[j] - z[i]),2));
         printf("%s %s %lf",s2[i],s2[j],d[i][j]);
       }
   }
   printf("Enter the interaction cut-off value : ");
   scanf("%lf",&a);
   for(i=1;i<=count;i++)
   {
       for(j=i+1;j<=count;j++)
       {
         if(d[i][j]<=a)
         {
         printf("%s %s %lf",s2[i],s2[j],d[i][j]);
         }
       }
   }


In the loop where you are reading data from the file, you should make sure that count never reaches 600 because the maximum valid index for x, y, and z is 599. Stop reading in lines and print a warning message when count reaches 600.

I think that will work for you.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Zlatko ur answers are very helpful to me... First of all thanks for that...

My question is, In the same program, First I get each line from the file and split it by using "substring" function and print the splited value. Actually I dont want to print it. But without print statement it doesnt work. How can i do this. Reply ASAP.

Thanks Zlatko...

Answer
Hello Saran.

I think you are not using your substring function correctly in all cases. Notice that the first two parameters of the substring function are start and stop positions, so the second parameter must always be greater than the first. Your first use of substring seems correct:
printf("%s ", substring(7, 27, buffer, s1, sizeof s1));

But the following uses do not look correct. In the following uses, you seem to be specifying a start position and a length instead of a start position and an end position.
printf("X[%d] = %s ", i,substring(30, 8, buffer, X, sizeof X));
printf("Y[%d] = %s ", i,substring(38, 8, buffer, Y, sizeof Y));
printf("Z[%d] = %s ", i,substring(46, 8, buffer, Z, sizeof Z));

I suggest you make those corrections, and show me the latest version of your code, and send me a copy of the code and the input file 3IL8.pdb to zlatko.c.help at gmail.com. It is difficult for me to help you if I cannot actually run the program with your data.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.