Project: s41467-024-48303-3 | Function: r_calculator (cpp)

system: You are an expert C++ programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow C++ best practices and maintain consistent code style.
Return **only** the complete C++ function code.

user: Complete this C++ function:

**DESCRIPTION:** Calculates Pearson correlation coefficient between transcription factor and gene expression profiles across time points

**KEY TERMS:** {
  "TF_exp_table": "transcription factor expression table",
  "gene_exp_table": "gene expression table",
  "LD_exp": "lateral down expression data",
  "TD_exp": "temporal down expression data",
  "N_LD": "number of lateral down time points",
  "N_TD": "number of temporal down time points"
}

**CONTEXT CODE:**     	for(int k=0; k<10; k++) {
    		gene_exp_table[i].network[k] = 0;
    	}
        for(int j=0; j<num_of_point_LD; j++) {
            gene_exp_table[i].LD_exp[j] = 0;
        }
        for(int j=0; j<num_of_point_TD; j++) {
            gene_exp_table[i].TD_exp[j] = 0;
        }
    }	

	int index = 0;
    while(fscanf(fptr,"%s", GID) != EOF) {
		strcpy(gene_exp_table[index].gene_ID, GID);
		for(int i=0; i<num_of_point_LD; i++) {
			fscanf(fptr,"\t%lf", &LDE[i]);
			gene_exp_table[index].LD_exp[i] = LDE[i];
		}
		for(int j=0; j<num_of_point_TD; j++) {
			fscanf(fptr,"\t%lf", &TDE[j]);
			gene_exp_table[index].TD_exp[j] = TDE[j];
		}
		index++;
	}		
	
    fclose(fptr);
}

void Read_seed_genes (char *input) {
    FILE *fptr = fopen(input, "r");
    char GID[20];
    
    num_of_seeds = 0;
    while(fscanf(fptr,"%s", GID) != EOF) {
        num_of_seeds++;
    }

    rewind(fptr);
    //table initialization
    gene_seed_table = new Time_Course[num_of_seeds];

    int index = 0;
    while(fscanf(fptr,"%s", GID) != EOF) {
        strcpy(gene_seed_table[index].gene_ID, GID);
        index++;
    }
    
    fclose(fptr);
}

double r_calculator (int x, int y, int z) {
    fprintf(stderr, "Function r_calculator has been masked\n");
    abort();
    return -1.0;
}

void node_pair_generator_LD_or_TD(int opt) {
    fprintf(stderr, "Function node_pair_generator_LD_or_TD has been masked\n");
    abort();
}

void set_neighbor_pos(char *check_ID, int L) {
    
    for (int i=0; i<num_of_pos_edge; i++) {
        if (Pos_Coexp[i].checked == 0) {
            if (strcmp(check_ID, Pos_Coexp[i].query_gene_ID) == 0) {
                Pos_Coexp[i].checked = 1;
                for (int j=0; j<num_of_TFs; j++) {
                    if(strcmp(Pos_Coexp[i].target_gene_ID, TF_exp_table[j].gene_ID) == 0) {
                        if (TF_exp_table[j].level == -1) {
                            TF_exp_table[j].level = L;
                            done = 0;
                        }
                    }
                }
            }
        }
    }
    
    for (int i=0; i<num_of_pos_edge; i++) {
        if (Pos_Coexp[i].checked == 0) {
            if (strcmp(check_ID, Pos_Coexp[i].target_gene_ID) == 0) {
                Pos_Coexp[i].checked = 1;
                for (int j=0; j<num_of_TFs; j++) {
                    if(strcmp(Pos_Coexp[i].query_gene_ID, TF_exp_table[j].gene_ID) == 0) {
                        if (TF_exp_table[j].level == -1) {
                            TF_exp_table[j].level = L;
                            done = 0;
                        }
                    }
                }
            }
        }
    }
}

void level_assignment() {
    
    int level = 1;
    int num_of_found_seeds = 0;
    int seed_found_in_GCN = 0;

    for (int i=0; i<num_of_TFs; i++) {
        for (int j=0; j<num_of_seeds; j++) {
            if(strcmp(gene_seed_table[j].gene_ID, TF_exp_table[i].gene_ID) == 0) {
                    TF_exp_table[i].level = level;
                num_of_found_seeds++;
            }
        }
    }
    
    if(num_of_found_seeds == num_of_seeds) {
        seed_found_in_GCN = 1;
    }
    
    if (seed_found_in_GCN == 0) {
        printf("\nSome ID cannot be found in the GCN. Please check the see ID list again!\n\n");
    } else {

        for (int i=0; i<num_of_TFs; i++) {
            for (int j=0; j<num_of_seeds; j++) {
                if(strcmp(gene_seed_table[j].gene_ID, TF_exp_table[i].gene_ID) == 0) {
                    set_neighbor_pos(TF_exp_table[i].gene_ID, level+1);
                }
            }
        }
        
        while (done == 0) {
            level++;
            done = 1;
            for (int i=0; i<num_of_TFs; i++) {
                if(TF_exp_table[i].level == level) {
                    set_neighbor_pos(TF_exp_table[i].gene_ID, level+1);
                }
            }
        }
    }
}

void function_three () {
    FILE *fout3;
    
    fout3 = fopen("TF_level.csv","w");
    
    fprintf(fout3, "TF gene ID,level in GCN\n");
    for(int i=0; i<num_of_TFs; i++) {
        if(TF_exp_table[i].level >= 0) {
            fprintf(fout3, "%s, %d\n", TF_exp_table[i].gene_ID, TF_exp_table[i].level);
        }
    }


Complete the function implementation replacing "throw std::runtime_error("Not implemented");".