void read_ancestrymap (char* input_file, int N, int M, int* data) { FILE *File = NULL; int i, j, allele; char tmp[512] = ""; char ref[512] = ""; int max_char_per_line = 1000; char szbuff[max_char_per_line]; int warning = 0; // open input file File = fopen_read(input_file); j = 0; i = 0; // while not the end of the file // or not too many SNPs (j < M-1) // or last SNP but not too many ind (j == (M-1) && i< N)) while (fgets(szbuff,max_char_per_line, File) && (j < M-1 || (j == (M-1) && i< N))) { // read line read_line_ancestrymap(szbuff, &allele, tmp, i+1, j+1, input_file, &warning); // first SNP, save SNP name if (j == 0 && i == 0) strcpy(ref, tmp); // new SNP if (strcmp(ref, tmp) != 0) { // test if the number indiviual for the line is ok test_column (input_file, File, i, j+1, N, NULL); // new line i = 0; j++; // save new SNP name strcpy(ref, tmp); } // write genotype data[i * M + j] = allele; i++; } test_column (input_file, File, i, j+1, N, NULL); // test the number of lines test_line(input_file, File, j+1, M); fclose(File); }
int main (int argc, char *argv[]) { // parameters int i1, i2, N; char *token = NULL; FILE *m_File; // command line args configuration i1 = (int) atoi(argv[1]); i2 = (int) atoi(argv[2]); N = (int) atoi(argv[3]); if (argc > 4) { token = (char *) calloc(4, sizeof(char)); token = "tmp"; } // test test_column m_File = fopen("files/test.txt","r"); test_column("files/test.txt",m_File, i1, 10, N, token); // test test_line test_line("files/test.txt", m_File, i2, N); return 0; }
void read_data_int(char *file_data, int N, int M, int *dat) { FILE *m_File = NULL; int i = 0; int j = 0; int max_char_per_line = MAX_LENGTH_NB_INT * M + 20; char *szbuff; char *token; // allocate memory szbuff = (char *) malloc(max_char_per_line * sizeof(char)); // open file m_File = fopen_read(file_data); i = 0; while (fgets(szbuff, max_char_per_line, m_File) && (i < N)) { j = 0; // cut line with split character SEP (" ") token = strtok(szbuff, SEP); // read elements and register them in dat while (token && j < M) { //printf("%s\n",token); dat[i * M + j] = (int)atof(token); // next elements token = strtok(NULL, SEP); j++; } i++; // check the number of columns test_column(file_data, m_File, j, i, M, token); } // check the number of lines test_line(file_data, m_File, i, N); // close file fclose(m_File); // free memory free(szbuff); }