]> git.donarmstrong.com Git - samtools.git/blobdiff - sam.c
* samtools-0.1.5-16 (r421)
[samtools.git] / sam.c
diff --git a/sam.c b/sam.c
index e8e742c80649f583b16de8fb7b1519eca9a5af10..b8e7350f3bebd70cd043bf54de02c77315092ee9 100644 (file)
--- a/sam.c
+++ b/sam.c
@@ -1,4 +1,6 @@
 #include <string.h>
+#include <unistd.h>
+#include "faidx.h"
 #include "sam.h"
 
 #define TYPE_BAM  1
@@ -152,3 +154,22 @@ int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
        bam_destroy1(b);
        return 0;
 }
+
+char *samfaipath(const char *fn_ref)
+{
+       char *fn_list = 0;
+       fn_list = calloc(strlen(fn_ref) + 5, 1);
+       strcat(strcpy(fn_list, fn_ref), ".fai");
+       if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
+               if (access(fn_ref, R_OK) == -1) {
+                       fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
+               } else {
+                       fprintf(stderr, "[samfaipath] build FASTA index...\n");
+                       if (fai_build(fn_ref) == -1) {
+                               fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
+                               free(fn_list); fn_list = 0;
+                       }
+               }
+       }
+       return fn_list;
+}