Hi guys,
I have an input file as below (it is a family tree):
1 0 0
2 0 0
3 0 0
4 1 2
5 1 2
6 0 0
7 3 4
8 5 6
9 7 8
10 7 8
where column 1 = individual number
column 2 = mother number
column 3 = father number
so for example, individual 7's mom is individual 3, and his father is individual 4.
A further 2 columns are added. The program places a 0 or 1 in there depending on probabilities I have set out myself. An example of the output is as follows:
1 0 0 0 1
2 0 0 1 1
3 0 0 1 0
4 1 2 0 1
5 1 2 0 1
6 0 0 0 0
7 3 4 0 0
8 5 6 1 0
9 7 8 0 0
10 7 8 0 0
What I want to do is set a counter on how many 00's there are, how many 01's there are, and how many 11's there are. So for the above example, it would say:
00 = 4
01 = 4
11 = 2
My code is as below.
#include <fstream.h>
#include <stdlib.h>
int i, j;
int matrix[10000][5] = {0}; //Define a matrix with a maximum of 100 rows and 6 columns
int counters[2][2] = {0}; // Define the genotype counter
int repeats = 0;
int norepeats;
float p, q;
float rand1, rand2, rand3, rand4;
void SetSeed();
int main () {
FILE *output_file = fopen("output.txt", "w");
printf("Enter the frequency of allele A: ");
scanf("%f", &p);
printf("\n");
printf("Frequency of allele A (p) is: %.3f\n", p); // States the frequency of the A allele
q = 1 - p; // The value of q is 1 - (the probability entered for p)
printf("Frequency of allele B (q) is: %.3f\n", q); // States the frequency of the B allele
printf("\n");
printf("Please enter the desired number of repeats: ");
scanf("%d", &norepeats); // Desired number of repeats of the program stored in 'norepeats'
printf("\n");
while (repeats != norepeats){ // A while loop to loop the program 'norepeats' amount of times
const float RANGE = 1.0; // Random number generated is below 1
SetSeed(); // Seed file for RNG set here
//Open the input file//
ifstream inFile;
inFile.open("input.txt"); // Genealogy input file opened
if (!inFile)
{
printf ("Unable to open input file"); // Error message reported if input.txt cannot be opened
exit(1);
}
//Read the input file into the array
while (inFile >> matrix[i][0])
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < 3; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}
int counter;
if (matrix[i][1] == 0 && matrix[i][2] == 0){ // If the individual is a founder:
rand1 = (float) rand() * RANGE / RAND_MAX; // Random number 1 generated
rand2 = (float) rand() * RANGE / RAND_MAX; // Random number 2 generated
if (rand1 < p){ // Place a 0 allele down in column 4 if random number 1 is below the set p value
matrix[i][3] = 0;
}
else{
matrix[i][3] = 1; // Place a 1 allele down in column 4 if random number 1 is above the set p value
}
printf("\t%d", matrix[i][3]);
if (rand2 < p){ // Place a 0 allele down in column 5 if random number 2 is below the set p value
matrix[i][4] = 0;
}
else{ // Place a 1 allele down in column 5 if random number 2 is above the set p value
matrix[i][4] = 1;
}
printf("\t%d", matrix[i][4]);
}
else{
int dad = matrix[i][1]; // 1 is "dad" column
int mum = matrix[i][2]; // 2 is "mum" column
int dadRowIndex = dad - 1; // The row of the father is 'dad - 1' as the first row is set at zero
int mumRowIndex = mum -1; // The row of the mother is 'mum - 1' as the first row is set at zero
for (counter = 1;counter <= 1;counter++){
rand3 = (float) rand() * RANGE / RAND_MAX; // Random number 3 generated
rand4 = (float) rand() * RANGE / RAND_MAX; // Random number 4 generated
}
if (rand3 < 0.5){ // If random number 3 is less than a half, receive allele stored in column 4 from dad
matrix[i][3] = matrix[dadRowIndex][3];
}
else{ // If random number 3 is more than a half, receive allele stored in column 5 from dad
matrix[i][3] = matrix[dadRowIndex][4];
}
printf("\t%d", matrix[i][3]);
if (rand4 < 0.5){ // If random number 4 is less than a half, receive allele stored in column 4 from mum
matrix[i][4] = matrix[mumRowIndex][3];
}
else{ // If random number 4 is more than a half, receive allele stored in column 5 from mum
matrix[i][4] = matrix[mumRowIndex][4];
}
printf("\t%d", matrix[i][4]);
}
i++;
printf ("\n");
}
inFile.close (); // Close the infile
printf("\n");
repeats++; // End loop here
}
fclose(output_file);
}
void SetSeed()
{
// Determining the seed file
FILE *seed_file;
long int seed_value;
seed_file = fopen("seed.txt","r"); // Open seed file
fscanf(seed_file,"%d",&seed_value); // Exctract seed from file
fclose(seed_file); // Close seed file
srand(seed_value); // Randomise seed value
seed_file = fopen("seed.txt","w"); // Create temporary output
fprintf(seed_file,"%d",rand()); // Print random number to output
fclose(seed_file); // Close temporary seed output file
}
Any ideas guys?