SEXP bamfile_isopen(SEXP ext) { int ans = FALSE; if (NULL != BAMFILE(ext)) { _checkext(ext, BAMFILE_TAG, "isOpen"); ans = NULL != BAMFILE(ext)->file; } return ScalarLogical(ans); }
BAM_DATA _init_BAM_DATA(SEXP ext, SEXP space, SEXP flag, SEXP isSimpleCigar, SEXP tagFilter, SEXP mapqFilter, int reverseComplement, int yieldSize, int obeyQname, int asMates, char qnamePrefixEnd, char qnameSuffixStart, void *extra) { int nrange = R_NilValue == space ? 1 : LENGTH(VECTOR_ELT(space, 0)); BAM_DATA bd = _Calloc_BAM_DATA(1 == nrange ? 5 * BAM_INIT_SIZE : BAM_INIT_SIZE, 32768); bd->parse_status = BAM_PARSE_STATUS_OK; bd->bfile = BAMFILE(ext); bd->irange = bd->bfile->irange0; bd->nrange = nrange; bd->irec = bd->iparsed = 0; bd->keep_flag[0] = INTEGER(flag)[0]; bd->keep_flag[1] = INTEGER(flag)[1]; bd->cigar_flag = LOGICAL(isSimpleCigar)[0]; bd->tagfilter = _tagFilter_as_C_types(tagFilter); int mapqfilter = INTEGER(mapqFilter)[0]; bd->mapqfilter = mapqfilter == NA_INTEGER ? 0 : mapqfilter; /* uint32_t */ bd->reverseComplement = reverseComplement; bd->yieldSize = yieldSize; bd->obeyQname = obeyQname; bd->asMates = asMates; bd->qnamePrefixEnd = qnamePrefixEnd; bd->qnameSuffixStart = qnameSuffixStart; bd->extra = extra; return bd; }
static void _bamfile_finalizer(SEXP ext) { if (NULL == R_ExternalPtrAddr(ext)) return; _bamfile_close(ext); BAM_FILE bfile = BAMFILE(ext); Free(bfile); R_SetExternalPtrAddr(ext, NULL); }
SEXP bamfile_isincomplete(SEXP ext) { int ans = FALSE; BAM_FILE bfile; if (NULL != BAMFILE(ext)) { _checkext(ext, BAMFILE_TAG, "isIncomplete"); bfile = BAMFILE(ext); if (NULL != bfile && NULL != bfile->file) { /* heuristic: can we read a record? bam_seek does not * support SEEK_END */ off_t offset = bam_tell(bfile->file->x.bam); char buf; ans = bam_read(bfile->file->x.bam, &buf, 1) > 0; bam_seek(bfile->file->x.bam, offset, SEEK_SET); } } return ScalarLogical(ans); }
static void _bamfile_close(SEXP ext) { BAM_FILE bfile = BAMFILE(ext); if (NULL != bfile->file) samclose(bfile->file); if (NULL != bfile->index) bam_index_destroy(bfile->index); if (NULL != bfile->iter) bam_mate_iter_destroy(bfile->iter); if (NULL != bfile->pbuffer) pileup_pbuffer_destroy(bfile->pbuffer); bfile->file = NULL; bfile->index = NULL; bfile->iter = NULL; }
SEXP _read_bam_header(SEXP ext, SEXP what) { samfile_t *sfile = BAMFILE(ext)->file; bam_header_t *header = sfile->header; SEXP ans = PROTECT(NEW_LIST(2)); SEXP nms = NEW_CHARACTER(2); setAttrib(ans, R_NamesSymbol, nms); SET_STRING_ELT(nms, 0, mkChar("targets")); SET_STRING_ELT(nms, 1, mkChar("text")); if (LOGICAL(what)[0] == TRUE) { /* 'targets' */ int n_elts = header->n_targets; SET_VECTOR_ELT(ans, 0, NEW_INTEGER(n_elts)); SEXP tlen = VECTOR_ELT(ans, 0); /* target length */ SEXP tnm = NEW_CHARACTER(n_elts); /* target name */ setAttrib(tlen, R_NamesSymbol, tnm); for (int j = 0; j < n_elts; ++j) { INTEGER(tlen)[j] = header->target_len[j]; SET_STRING_ELT(tnm, j, mkChar(header->target_name[j])); } } if (LOGICAL(what)[1] == TRUE) { /* 'text' */ int n_text_elts = 0; for (int i = 0; i < header->l_text; ++i) if (header->text[i] == '\n') n_text_elts += 1; SET_VECTOR_ELT(ans, 1, NEW_LIST(n_text_elts)); SEXP text = VECTOR_ELT(ans, 1); SEXP tag = NEW_CHARACTER(n_text_elts); setAttrib(text, R_NamesSymbol, tag); int start = 0, end; for (int i = 0; i < n_text_elts; ++i) { int n_elts = header->text[start] == '\n' ? 0 : 1; end = start; while (header->text[end] != '\n') { if (header->text[end] == '\t') ++n_elts; ++end; } if (n_elts == 0) { SET_VECTOR_ELT(text, i, NEW_CHARACTER(0)); /* SET_STRING_ELT(tag, i, mkChar("")); */ start = end + 1; continue; } SET_VECTOR_ELT(text, i, NEW_CHARACTER(n_elts - 1)); SEXP elts = VECTOR_ELT(text, i); for (int j = 0; j < n_elts; ++j) { end = start; while (header->text[end] != '\t' && header->text[end] != '\n') ++end; SEXP elt = mkCharLen(&header->text[start], end - start); if (j == 0) /* tag */ SET_STRING_ELT(tag, i, elt); else SET_STRING_ELT(elts, j - 1, elt); start = end + 1; } } } UNPROTECT(1); return ans; }