pio_status_t pio_open_ex(struct pio_file_t *plink_file, const char *fam_path, const char *bim_path, const char *bed_path) { int error = 0; int num_samples = 0; int num_loci = 0; if( fam_open( &plink_file->fam_file, fam_path ) == PIO_OK ) { num_samples = fam_num_samples( &plink_file->fam_file ); } else { error = 1; } if( bim_open( &plink_file->bim_file, bim_path ) == PIO_OK ) { num_loci = bim_num_loci( &plink_file->bim_file ); } else { error = 1; } if( bed_open( &plink_file->bed_file, bed_path, num_loci, num_samples ) != PIO_OK ) { error = 1; } if( error == 0 ) { return PIO_OK; } else { fam_close( &plink_file->fam_file ); bim_close( &plink_file->bim_file ); bed_close( &plink_file->bed_file ); return PIO_ERROR; } }
region_table_t *parse_regions_from_bed_file(char *filename, const char *url, const char *species, const char *version) { bed_file_t *file = bed_open(filename); if (file == NULL) { return NULL; } region_table_t *regions_table = new_region_table_from_ws(url, species, version); int ret_code = 0; size_t max_batches = 20, batch_size = 2000; list_t *read_list = (list_t*) malloc (sizeof(list_t)); list_init("batches", 1, max_batches, read_list); #pragma omp parallel sections { // The producer reads the bed file #pragma omp section { LOG_DEBUG_F("Thread %d reads the BED file\n", omp_get_thread_num()); ret_code = bed_read_batches(read_list, batch_size, file); list_decr_writers(read_list); if (ret_code) { LOG_FATAL_F("Error while reading BED file %s (%d)\n", filename, ret_code); } } // The consumer inserts regions in the structure #pragma omp section { list_item_t *item = NULL; bed_batch_t *batch; bed_record_t *record; region_t *regions_batch[REGIONS_CHUNKSIZE]; int avail_regions = 0; while ( item = list_remove_item(read_list) ) { batch = item->data_p; // For each record in the batch, generate a new region for (int i = 0; i < batch->records->size; i++) { record = batch->records->items[i]; region_t *region = region_new(strndup(record->sequence, record->sequence_len), record->start, record->end, strndup(&record->strand, 1), NULL); LOG_DEBUG_F("region '%s:%u-%u'\n", region->chromosome, region->start_position, region->end_position); regions_batch[avail_regions++] = region; // Save when the recommended size is reached if (avail_regions == REGIONS_CHUNKSIZE) { insert_regions(regions_batch, avail_regions, regions_table); for (int i = 0; i < avail_regions; i++) { free(regions_batch[i]); } avail_regions = 0; } } bed_batch_free(batch); list_item_free(item); } // Save the remaining regions that did not fill a batch if (avail_regions > 0) { insert_regions(regions_batch, avail_regions, regions_table); for (int i = 0; i < avail_regions; i++) { free(regions_batch[i]); } avail_regions = 0; } } } list_free_deep(read_list, NULL); bed_close(file, 1); return regions_table; }