int fai_build(const char *fn) { char *str; RAZF *rz; FILE *fp; faidx_t *fai; str = (char*)calloc(strlen(fn) + 5, 1); sprintf(str, "%s.fai", fn); rz = razf_open(fn, "r"); if (rz == 0) { fprintf(stderr, "[fai_build] fail to open the FASTA file %s\n",fn); free(str); return -1; } fai = fai_build_core(rz); razf_close(rz); fp = fopen(str, "wb"); if (fp == 0) { fprintf(stderr, "[fai_build] fail to write FASTA index %s\n",str); fai_destroy(fai); free(str); return -1; } fai_save(fai, fp); fclose(fp); free(str); fai_destroy(fai); return 0; }
faidx_t *fai_load(const char *fn) { char *str; FILE *fp; faidx_t *fai; str = (char*)calloc(strlen(fn) + 5, 1); sprintf(str, "%s.fai", fn); #ifdef _USE_KNETFILE if (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn) { fp = download_and_open(str); if ( !fp ) { fprintf(stderr, "[fai_load] failed to open remote FASTA index %s\n", str); free(str); return 0; } } else #endif fp = fopen(str, "rb"); if (fp == 0) { fprintf(stderr, "[fai_load] build FASTA index.\n"); fai_build(fn); fp = fopen(str, "rb"); if (fp == 0) { fprintf(stderr, "[fai_load] fail to open FASTA index.\n"); free(str); return 0; } } fai = fai_read(fp); fclose(fp); fai->rz = razf_open(fn, "rb"); free(str); if (fai->rz == 0) { fprintf(stderr, "[fai_load] fail to open FASTA file.\n"); return 0; } return fai; }
int main(int argc, char **argv) { int c, compress, pstdout, is_forced; RAZF *rz; void *buffer; long start, end, size; compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0; while((c = getopt(argc, argv, "cdlhfb:s:")) >= 0){ switch(c){ case 'h': return razf_main_usage(); case 'd': compress = 0; break; case 'c': pstdout = 1; break; case 'l': compress = 2; break; case 'b': start = atol(optarg); break; case 's': size = atol(optarg); break; case 'f': is_forced = 1; break; } } if (size >= 0) end = start + size; if(end >= 0 && end < start){ fprintf(stderr, " -- Illegal region: [%ld, %ld] --\n", start, end); return 1; } if(compress == 1){ int f_src, f_dst = -1; if(argc > optind){ if((f_src = open(argv[optind], O_RDONLY)) < 0){ fprintf(stderr, " -- Cannot open file: %s --\n", argv[optind]); return 1; } if(pstdout){ f_dst = fileno(stdout); } else { char *name = malloc(sizeof(strlen(argv[optind]) + 5)); strcpy(name, argv[optind]); strcat(name, ".rz"); f_dst = write_open(name, is_forced); if (f_dst < 0) return 1; free(name); } } else if(pstdout){ f_src = fileno(stdin); f_dst = fileno(stdout); } else return razf_main_usage(); rz = razf_dopen(f_dst, "w"); buffer = malloc(WINDOW_SIZE); while((c = read(f_src, buffer, WINDOW_SIZE)) > 0) razf_write(rz, buffer, c); razf_close(rz); // f_dst will be closed here if (argc > optind && !pstdout) unlink(argv[optind]); free(buffer); close(f_src); return 0; } else { if(argc <= optind) return razf_main_usage(); if(compress == 2){ rz = razf_open(argv[optind], "r"); if(rz->file_type == FILE_TYPE_RZ) { printf("%20s%20s%7s %s\n", "compressed", "uncompressed", "ratio", "name"); printf("%20lld%20lld%6.1f%% %s\n", (long long)rz->end, (long long)rz->src_end, rz->end * 100.0f / rz->src_end, argv[optind]); } else fprintf(stdout, "%s is not a regular rz file\n", argv[optind]); } else { int f_dst; if (argc > optind && !pstdout) { char *name; if (strstr(argv[optind], ".rz") - argv[optind] != strlen(argv[optind]) - 3) { printf("razip: %s: unknown suffix -- ignored\n", argv[optind]); return 1; } name = strdup(argv[optind]); name[strlen(name) - 3] = '\0'; f_dst = write_open(name, is_forced); free(name); } else f_dst = fileno(stdout); rz = razf_open(argv[optind], "r"); buffer = malloc(WINDOW_SIZE); razf_seek(rz, start, SEEK_SET); while(1){ if(end < 0) c = razf_read(rz, buffer, WINDOW_SIZE); else c = razf_read(rz, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start)); if(c <= 0) break; start += c; write(f_dst, buffer, c); if(end >= 0 && start >= end) break; } free(buffer); if (!pstdout) unlink(argv[optind]); } razf_close(rz); return 0; } }