示例#1
0
/**
 * DATE: 2010-7-29
 * FUNCTION: open sam/bam file in pattern mode.
 * PARAMETER: path: the file's path. mode: the pattern.
 * RETURN: successful: true. failed: flase.
 */
bool SamCtrl::open(const char *path, const char *mode) 
{
	if (mode[0] == 'r') {
		// judge if opened a file before
		if (m_in != 0) {
			return false;
		}

		m_in_mode = mode;
		m_in_path = path;

		// open sam/bam file
		if ((m_in = samopen(m_in_path.c_str(), m_in_mode.c_str(), m_fn_list)) == 0) 
		{
			return false;		
		}

		if (m_in->header == 0) 
		{
			return false;
		}

		if ((m_out = samopen(m_out_path.c_str(), m_out_mode.c_str(), m_in->header)) == 0) 
		{
			return false;
		}		
		return true;
	} 
	return false;
}
示例#2
0
void SAM_istream::open(const char * _filename, bam_header_t * header) throw (SAM_IO_Error) {
	close();
	if (_filename != NULL)
		filename.assign(_filename);

	if (filename.size() == 0)
		throw SAM_IO_Error(SAM_IO_Error::empty_file_name);

	switch (detect_file_type_by_name(filename.c_str())) {
	case unknown_sam_format:
		throw SAM_IO_Error(SAM_IO_Error::incorrect_filename_format,filename.c_str());
		break;
	case bam_format:
		sam_file = samopen(filename.c_str(),"rb",NULL);
		break;
	case tam_format:
		sam_file = samopen(filename.c_str(),"r",NULL);
		break;
	case tam_compressed_format:
		throw SAM_IO_Error(SAM_IO_Error::not_supported_method,"tam compressed file");
		break;
	}
	if (sam_file == NULL) {
		throw SAM_IO_Error(SAM_IO_Error::cannot_open_file_r,filename.c_str());

	}
	popolate_name_list(sam_file->header);
	end_of_file = false;
	samfile_is_open = true;
}
示例#3
0
static inline tmap_sam_io_t *
tmap_sam_io_init_helper(const char *fn, int32_t is_bam)
{
  tmap_sam_io_t *samio = NULL;

  // initialize memory
  samio = tmap_calloc(1, sizeof(tmap_sam_io_t), "samio");
  if(0 == is_bam) {
      samio->fp = samopen(fn, "r", NULL);
  }
  else {
      samio->fp = samopen(fn, "rb", NULL);
  }
  if(NULL == samio->fp) {
      tmap_error(fn, Exit, OpenFileError);
  }
  samio->bam_end_vfo = 0;

  // check if there are sequences in the header
  /*
  if(samio->fp->header->n_targets == 0) {
      tmap_error("Found no @SQ lines in the SAM header", Exit, OutOfRange);
  }
  */

  return samio;
}
示例#4
0
文件: SAM.cpp 项目: vezzi/ERNE
/*static*/ bam_header_t * SAM::update_header_from_list(bam_header_t *header, names_list_t & list) {
	Temporary_File samfile;
	samfile.close_file();
	samfile_t * sf = samopen(samfile.get_filename().c_str(),"wh",header);
	samclose(sf);

	Temporary_File tempfile;
	ofstream &output = tempfile.get_stream();

	ifstream input(samfile.get_filename().c_str());
	string temp;
	while (not input.eof()) {
		getline(input,temp);
		if ((temp.size() >= 3) and (temp[0] != '@' or temp[1] != 'S' or temp[2] != 'Q'))
			output << temp << '\n';
	}

	for (names_list_t::iterator iter = list.begin(); iter != list.end(); iter++)
		output << "@SQ\tSN:" << iter->first << "\tLN:" << iter->second << '\n';
	tempfile.close_file();

	tamFile fp = sam_open(tempfile.get_filename().c_str());

	bam_header_t * newheader = sam_header_read(fp);
	sam_close(fp);

	return newheader;
}
BamMerge::BamMerge(const vector<string>& bam_fnames,
		   vector<int64_t> file_offsets) :
  _bam_fnames(bam_fnames),
  _lines(less_bam(true)),
  _last_id(0)
{
  if (bam_fnames.size() <= 0)
    return;
  
  for (size_t i = 0; i < _bam_fnames.size(); ++i) {
    const char* fname = _bam_fnames[i].c_str();
    samfile_t* fp = samopen(fname, "rb", 0);
    if (fp==0) {
      warn_msg(ERR_BAM_OPEN, fname);
      exit(1);
    }

    if (bam_fnames.size() == file_offsets.size() &&
	file_offsets[i] > 0)
      bgzf_seek(fp->x.bam, file_offsets[i], SEEK_SET);

    bam1_t* b = bam_init1();
    if (samread(fp, b) > 0) {
      _src_files.push_back(fp);
      CBamLine brec(_lines.size(), b, fp->header);
      _lines.push(brec);
    }
    else { bam_destroy1(b); }
  }

  if (_lines.size() == 0) {
    warn_msg("Warning: no input BAM records found.\n");
    exit(1);
  }
}
示例#6
0
boolean bamFileExists(char *fileOrUrl)
/* Return TRUE if we can successfully open the bam file and its index file. */
{
    char *bamFileName = fileOrUrl;
    samfile_t *fh = samopen(bamFileName, "rb", NULL);
    boolean usingUrl = TRUE;
    usingUrl = (strstr(fileOrUrl, "tp://") || strstr(fileOrUrl, "https://"));
    if (fh != NULL)
    {
#ifndef KNETFILE_HOOKS
        // When file is an URL, this caches the index file in addition to validating:
        // Since samtools's url-handling code saves the .bai file to the current directory,
        // chdir to a trash directory before calling bam_index_load, then chdir back.
        char *runDir = getCurrentDir();
        char *samDir = getSamDir();
        if (usingUrl)
            setCurrentDir(samDir);
#endif//ndef KNETFILE_HOOKS
        bam_index_t *idx = bam_index_load(bamFileName);
#ifndef KNETFILE_HOOKS
        if (usingUrl)
            setCurrentDir(runDir);
#endif//ndef KNETFILE_HOOKS
        samclose(fh);
        if (idx == NULL)
        {
            warn("bamFileExists: failed to read index corresponding to %s", bamFileName);
            return FALSE;
        }
        free(idx); // Not freeMem, freez etc -- sam just uses malloc/calloc.
        return TRUE;
    }
    return FALSE;
}
示例#7
0
void samToOpenBed(char *samIn, FILE *f)
/* Like samToOpenBed, but the output is the already open file f. */
{
    samfile_t *sf = samopen(samIn, "r", NULL);
    bam_header_t *bamHeader = sf->header;
    bam1_t one;
    ZeroVar(&one);
    int err;
    while ((err = samread(sf, &one)) >= 0)
    {
        int32_t tid = one.core.tid;
        if (tid < 0)
            continue;
        char *chrom = bamHeader->target_name[tid];
        // Approximate here... can do better if parse cigar.
        int start = one.core.pos;
        int size = one.core.l_qseq;
        int end = start + size;
        boolean isRc = (one.core.flag & BAM_FREVERSE);
        char strand = '+';
        if (isRc)
        {
            strand = '-';
            reverseIntRange(&start, &end, bamHeader->target_len[tid]);
        }
        fprintf(f, "%s\t%d\t%d\t.\t0\t%c\n", chrom, start, end, strand);
    }
    if (err < 0 && err != -1)
        errnoAbort("samread err %d", err);
    samclose(sf);
}
示例#8
0
samfile_t *bamOpen(char *fileOrUrl, char **retBamFileName)
/* Return an open bam file, dealing with FUSE caching if need be.
 * Return parameter if NON-null will return the file name after FUSing */
{
    char *bamFileName = fileOrUrl;
    if (retBamFileName != NULL)
        *retBamFileName = bamFileName;

#ifdef BAM_VERSION
// suppress too verbose messages in samtools >= 0.1.18; see redmine #6491
// This variable didn't exist in older versions of samtools (where BAM_VERSION wasn't defined).
    bam_verbose = 1;
#endif

    samfile_t *fh = samopen(bamFileName, "rb", NULL);
    if (fh == NULL)
    {
        boolean usingUrl = (strstr(fileOrUrl, "tp://") || strstr(fileOrUrl, "https://"));
        struct dyString *urlWarning = dyStringNew(0);
        if (usingUrl)
        {
            dyStringAppend(urlWarning,
                           ". If you are able to access the URL with your web browser, "
                           "please try reloading this page.");
        }
        errAbort("Failed to open %s%s", fileOrUrl, urlWarning->string);
    }
    return fh;
}
示例#9
0
文件: bam-unique.c 项目: hjanime/cbgb
hash_table* hash_ids(const char* fn)
{
    fprintf(stderr, "hashing ... \n");

    hash_table* T = create_hash_table();

    samfile_t* f = samopen(fn, "rb", NULL);
    if (f == NULL) {
        fprintf(stderr, "can't open bam file %s\n", fn);
        exit(1);
    }

    bam1_t* b = bam_init1();

    uint32_t n = 0;

    while (samread(f, b) >= 0) {
        if (++n % 1000000 == 0) {
            fprintf(stderr, "\t%d reads\n", n);
        }

        inc_hash_table(T, bam1_qname(b), b->core.l_qname);
    }

    bam_destroy1(b);
    samclose(f);

    fprintf(stderr, "done.\n");
    return T;
}
示例#10
0
bool YTranscriptFetcher::fetchBAMTranscripts(const char* filename, const char *refName, unsigned int start, unsigned int end, std::vector<YTranscript*> *transcripts,std::set<std::string> *transcriptNames) {
    //Open the region in the bam file

    fetch_data_t data;
    fetch_data_t *d = &data;
    d->beg = start-1-buffer;
    d->end = end+buffer;

    d->transcripts = transcripts;
    d->requestedTranscripts = transcriptNames;
    d->in = samopen(filename, "rb", 0);

    if (d->in == 0) {
        fprintf(stderr, "Failed to open BAM file %s\n", filename);
        return 0;
    }
    bam_index_t *idx;
    idx = bam_index_load(filename); // load BAM index
    if (idx == 0) {
        fprintf(stderr, "BAM indexing file is not available.\n");
        return 0;
    }
    bam_init_header_hash(d->in->header);
    d->tid = bam_get_tid(d->in->header, refName);
    if(d->tid == -1) {
        fprintf(stderr, "Reference id %s not found in BAM file",refName);
        return 0;
    }
    bam_fetch(d->in->x.bam, idx, d->tid, d->beg, d->end, d, fetch_func);
    bam_index_destroy(idx);
    samclose(d->in);
    return 1;
}
int main(int argc, char* argv[]) {
    samfile_t *ifile = NULL, *ofile = NULL;
    bam1_t *read = bam_init1();
    int keep = 0;
    char *p = NULL;

    //Open input file, either SAM or BAM
    p = strrchr(argv[1], '.');
    if(strcmp(p, ".bam") == 0) {
        ifile = samopen(argv[1], "rb", NULL);
    } else {
        ifile = samopen(argv[1], "r", NULL);
    }

    bam_header_t *head = ifile->header;

    //Open output file
    // ofile = samopen("AND_type.bam", "wb", ifile->header);
    ofile = samopen(argv[2], "wb", ifile->header);


    //Iterate through the lines
    while(samread(ifile, read) > 1) {
        keep = 0;
        //Is the read's mate on the same chromosome/contig?
        if(read->core.tid == read->core.mtid) {
            //Are the mates on opposite strands?
            if(read->core.flag & BAM_FREVERSE && !(read->core.flag & BAM_FMREVERSE)) {
                if(read->core.pos < read->core.mpos) {
                    // Are mates 500 bp or less from the ends?
                    if (read-> core.pos <= 500 && read->core.mpos > head->target_len[read->core.tid] - 500)
                        keep=1;
                }
            } else if(!(read->core.flag & BAM_FREVERSE) && read->core.flag & BAM_FMREVERSE) {
                if(read->core.mpos < read->core.pos) {
                    if (read-> core.mpos <= 500 && read->core.pos > head->target_len[read->core.tid] - 500)
                        keep=1;
                }
            }
        }
        if(keep) samwrite(ofile, read);
    }
    bam_destroy1(read);
    samclose(ifile);
    samclose(ofile);
    return 0;
}
示例#12
0
void edwBamToWig(char *input, char *output)
/* edwBamToWig - Convert a bam file to a wig file by measuring depth of coverage, optionally adjusting hit size to average for library.. */
{
FILE *f = mustOpen(output, "w");
/* Open file and get header for it. */
samfile_t *sf = samopen(input, "rb", NULL);
if (sf == NULL)
    errnoAbort("Couldn't open %s.\n", input);
bam_header_t *head = sf->header;
if (head == NULL)
    errAbort("Aborting ... Bad BAM header in file: %s", input);


/* Scan through input populating genome range trees */
struct genomeRangeTree *grt = genomeRangeTreeNew();
bam1_t one = {};
for (;;)
    {
    /* Read next record. */
    if (bam_read1(sf->x.bam, &one) < 0)
	break;
    if (one.core.tid >= 0 && one.core.n_cigar > 0)
	{
	char *chrom = head->target_name[one.core.tid];
	int start = one.core.pos;
	int end = start + one.core.l_qseq;
	if (one.core.flag & BAM_FREVERSE)
	    {
	    start -= clPad;
	    }
	else
	    {
	    end += clPad;
	    }
	struct rbTree *rt = genomeRangeTreeFindOrAddRangeTree(grt,chrom);
	rangeTreeAddToCoverageDepth(rt, start, end);
	}
    }


/* Convert genome range tree into output wig */

/* Get list of chromosomes. */
struct hashEl *hel, *helList = hashElListHash(grt->hash);
for (hel = helList; hel != NULL; hel = hel->next)
    {
    char *chrom = hel->name;
    struct rbTree *rt = hel->val;
    struct range *range, *rangeList = rangeTreeList(rt);
    for (range = rangeList; range != NULL; range = range->next)
         {
	 fprintf(f, "%s\t%d\t%d\t%d\n",  chrom, range->start, range->end, ptToInt(range->val));
	 }
    }

carefulClose(&f);
}
示例#13
0
boolean bamIsSortedByTarget(char *fileName, int maxToCheck)
/* Return TRUE if bam is sorted by target for at least the first bits. */
{
int leftToCheck = maxToCheck;
struct hash *targetHash = hashNew(0);
boolean result = TRUE;

/* Open bam/sam file and set up basic I/O vars on it. */
samfile_t *sf = samopen(fileName, "rb", NULL);
bam_header_t *bamHeader = sf->header;
bam1_t one;
ZeroVar(&one);
int err;

char lastTarget[PATH_LEN] = "";
int lastPos = 0;

/* Loop through while still haven't hit our max and file still has data */
while ((err = bam_read1(sf->x.bam, &one)) >= 0)
    {
    if (--leftToCheck < 0)
        {
	break;
	}
    /* Get target,  skipping read if it's not aligned well enough to have a target. */
    int32_t tid = one.core.tid;
    if (tid < 0)
        continue;
    char *target = bamHeader->target_name[tid];

    int pos = one.core.pos;

    /* If we are on same target then make sure we are in ascending order. */
    if (sameString(target, lastTarget))
        {
	if (pos < lastPos)
	    {
	    result = FALSE;
	    break;
	    }
	}
    else
	{
	/* If sorted should not go back to a new chromosome. Use hash to check this */
	if (hashLookup(targetHash, target))
	    {
	    result = FALSE;
	    break;
	    }
	hashAdd(targetHash, target, NULL);
	safef(lastTarget, sizeof(lastTarget), "%s", target);
	}
    lastPos = pos;
    }
hashFree(&targetHash);
return result;
}
示例#14
0
int sam_fetch(char *ifn, char *ofn, char *reg, void *data, sam_fetch_f func) {
	int ret = 0;
	samfile_t *in = samopen(ifn, "rb", 0);
	samfile_t *out = 0;
	if (ofn) out = samopen(ofn, "wb", in->header);

	if (reg) {
		bam_index_t *idx = bam_index_load(ifn);
		if (idx == 0) {
			fprintf(stderr, "[%s:%d] Random alignment retrieval only works for indexed BAM files.\n",
							__func__, __LINE__);
			exit(1);
		}
		int tid, beg, end;
		bam_parse_region(in->header, reg, &tid, &beg, &end);
		if (tid < 0) {
			fprintf(stderr, "[%s:%d] Region \"%s\" specifies an unknown reference name. \n",
							__func__, __LINE__, reg);
			exit(1);
		}
		bam_iter_t iter;
		bam1_t *b = bam_init1();
		iter = bam_iter_query(idx, tid, beg, end);
		while ((ret = bam_iter_read(in->x.bam, iter, b)) >= 0) func(b, in, out, data);
		bam_iter_destroy(iter);
		bam_destroy1(b);
		bam_index_destroy(idx);
	} else {
		bam1_t *b = bam_init1();
		while ((ret = samread(in, b)) >= 0) func(b, in, out, data);
		bam_destroy1(b);
	}
	if (out) samclose(out);
	samclose(in);
			
	if (ret != -1) {					/* truncated is -2 */
		fprintf(stderr, "[%s:%d] Alignment retrieval failed due to truncated file\n",
						__func__, __LINE__);
		exit(1);
	}

	return ret;
}
示例#15
0
int MyBamWrap::mySamOpen(char* fileName)
{
	in=samopen(fileName, "rb", 0);
	if(in==0)
	{
		cerr<<"Fail to open Bam file: "<<fileName<<endl;
		exit(1);
	}
	return 0;
}
示例#16
0
int bam_rmdupse(int argc, char *argv[])
{
	samfile_t *in, *out;
	buffer_t *buf;
	if (argc < 3) {
		fprintf(stderr, "Usage: samtools rmdupse <in.bam> <out.bam>\n\n");
		fprintf(stderr, "Note: Picard is recommended for this task.\n");
		return 1;
	}
	buf = calloc(1, sizeof(buffer_t));
	in = samopen(argv[1], "rb", 0);
	out = samopen(argv[2], "wb", in->header);
	while (fill_buf(in, buf)) {
		rmdupse_buf(buf);
		dump_buf(buf, out);
	}
	samclose(in); samclose(out);
	free(buf->buf); free(buf);
	return 0;
}
示例#17
0
文件: bam-unique.c 项目: dcjones/cbgb
hash_table* hash_ids(const char* fn)
{
    fprintf(stderr, "hashing ... \n");

    hash_table* T = create_hash_table();

    samfile_t* f = samopen(fn, "rb", NULL);
    if (f == NULL) {
        fprintf(stderr, "can't open bam file %s\n", fn);
        exit(1);
    }

    bam1_t* b = bam_init1();

    uint32_t n = 0;

    char* qname = NULL;
    size_t qname_size = 0;

    while (samread(f, b) >= 0) {
        if (++n % 1000000 == 0) {
            fprintf(stderr, "\t%d reads\n", n);
        }

        if (qname_size < b->core.l_qname + 3) {
            qname_size = b->core.l_qname + 3;
            qname = realloc(qname, qname_size);
        }

        memcpy(qname, bam1_qname(b), b->core.l_qname);

        if (b->core.flag & BAM_FREAD2) {
            qname[b->core.l_qname]     = '/';
            qname[b->core.l_qname + 1] = '2';
            qname[b->core.l_qname + 2] = '\0';
        }
        else {
            qname[b->core.l_qname]     = '/';
            qname[b->core.l_qname + 1] = '1';
            qname[b->core.l_qname + 2] = '\0';
        }


        inc_hash_table(T, qname, b->core.l_qname + 2);
    }

    free(qname);

    bam_destroy1(b);
    samclose(f);

    fprintf(stderr, "done.\n");
    return T;
}
示例#18
0
samfile_t *_bam_tryopen(const char *filename, const char *filemode, void *aux)
{
    samfile_t *sfile = samopen(filename, filemode, aux);
    if (sfile == 0)
        Rf_error("failed to open SAM/BAM file\n  file: '%s'", filename);
    if (sfile->header == 0 || sfile->header->n_targets == 0) {
        samclose(sfile);
        Rf_error("SAM/BAM header missing or empty\n  file: '%s'", filename);
    }
    return sfile;
}
示例#19
0
void test_cigar_to_spans() {
  char *sam_filename = "testdata/RUM.sam";
  samfile_t *samfile = samopen(sam_filename, "r", NULL);  
  bam1_t *rec = bam_init1();

  struct SpanAssertion {
    int read_num;
    int num_spans;
    struct Span spans[10];
  };

  struct SpanAssertion cases[] = {
    { 102, 1, { { 12465667, 12465724 } } },
    { 104, 1, { { 2095233, 2095289 } } },
    { 128, 1, { { 152316, 152373 } } },
    { 162, 1, { { 14232813, 14232886 } } },
    { 172, 2, { { 3619619, 3619627 },
                { 3619984, 3620048  } } },
    { 642, 1, { { 15291546, 15291622 } } },
    { 670, 2, { { 3950665, 3950724 },
                { 3951436, 3951453 } } }
  };

  int num_cases = sizeof(cases) / sizeof(struct SpanAssertion);
  int read_num = 0;
  int case_num = 0;
  CigarCursor curs;
  while (case_num < num_cases &&
         samread(samfile, rec) > 0) {

    if (cases[case_num].read_num == read_num) {

      int num_spans = cases[case_num].num_spans;
      Span *span;

      init_cigar_cursor(&curs, rec);

      for (span = cases[case_num].spans; span < cases[case_num].spans + num_spans; span++) {

        assert_equals(1, next_span(&curs), "Should have found a span");
        assert_equals(span->start, curs.start, "Start");
        assert_equals(span->end, curs.end, "End");
      }

      assert_equals(0, next_span(&curs), "No more spans");

      case_num++;
    }
    read_num++;
  }

  if (case_num < num_cases)
    assert_equals(0, 1, "Ran out of records in sam file");
}
示例#20
0
/**
* @brief Open the file handler
*
* @param name Filename
* @return void
* @details sets the global bam_file or file variable to the start of the file
* @todo nothing
*/
samfile_t * open_samtools(const char * filename) {
    const char *dot = strrchr(filename, '.'),*mode;
    if(dot && !strcmp(dot,".bam")){
    	mode="rb";
    }else if(dot && !strcmp(dot,".sam")){
    	mode="r";
    }else{
    	warning("File ending not '.sam' or '.bam'");
    	return(0);
    }
	return(samopen(filename,mode,0));
}
示例#21
0
GtSamfileIterator* gt_samfile_iterator_new(const char *samfilename,
                                           const char *mode,
                                           void *aux,
                                           GtAlphabet *alphabet)
{
  GtSamfileIterator *s_iter;
  s_iter = gt_malloc(sizeof (GtSamfileIterator));
  s_iter->samfile = samopen(samfilename, mode, aux);
  s_iter->current_alignment = NULL;
  s_iter->alphabet = gt_alphabet_ref(alphabet);
  return s_iter;
}
示例#22
0
void hash_reads( table* T, const char* reads_fn, interval_stack* is )
{
    samfile_t* reads_f = samopen( reads_fn, "rb", NULL );
    if( reads_f == NULL ) {
        failf( "Can't open bam file '%s'.", reads_fn );
    }

    bam_index_t* reads_index = bam_index_load( reads_fn );
    if( reads_index == NULL ) {
        failf( "Can't open bam index '%s.bai'.", reads_fn );
    }

    bam_init_header_hash( reads_f->header );

    table_create( T, reads_f->header->n_targets );
    T->seq_names = (char**)malloc( sizeof(char*) * reads_f->header->n_targets );
    size_t k;
    for( k = 0; k < reads_f->header->n_targets; k++ ) {
        T->seq_names[k] = strdup(reads_f->header->target_name[k]);
    }

    log_puts( LOG_MSG, "hashing reads ... \n" );
    log_indent();
    bam_iter_t read_iter;
    bam1_t* read = bam_init1();
    int tid;

    interval_stack::iterator i;
    for( i = is->begin(); i != is->end(); i++ ) {
        tid = bam_get_tid( reads_f->header, i->seqname );
        if( tid < 0 ) continue;

        read_iter = bam_iter_query( reads_index, tid,
                                    i->start, i->end );

        while( bam_iter_read( reads_f->x.bam, read_iter, read ) >= 0 ) {
            if( bam1_strand(read) == i->strand ) {
                table_inc( T, read );
            }
        }

        bam_iter_destroy(read_iter);
    }

    bam_destroy1(read);

    log_unindent();
    log_printf( LOG_MSG, "done. (%zu unique reads hashed)\n", T->m );


    bam_index_destroy(reads_index);
    samclose(reads_f);
}
示例#23
0
int main(int argc, char *argv[])
{
     char *progname;

     char *bamfilename;
     int32_t tid;

     samfile_t *bamin;
     bam_index_t *bamidx;
     bam_plbuf_t *buf;
     bam1_t *bam_read;
     uint32_t next_pos = 1;

     progname = *argv;
     argv++; argc--;
     if (argc < 2) {
          printf("Usage: %s bam_file tid\n", progname);
          exit(1);
     }
     else {
          bamfilename = argv[0];
          tid = strtol(argv[1], NULL, 10);
     }

     /* try to open bam file */
     bamin = samopen(bamfilename, "rb", NULL);
     if (!bamin) {
          fprintf(stderr, "Error opening bamfile %s\n", bamfilename);
          exit(1);
     }
     /* try to open index */
     bamidx = bam_index_load(bamfilename);
     if (!bamidx) {
          fprintf(stderr, "Error opening index for %s\n", bamfilename);
          exit(1);
     }
     bam_read = bam_init1();

     buf = bam_plbuf_init(&pileup_func, &next_pos);
     /* disable maximum pileup depth */
     bam_plp_set_maxcnt(buf->iter, INT_MAX);
     bam_fetch(bamin->x.bam, bamidx,
               tid, 0, INT_MAX,
               buf, &fetch_func);
     bam_plbuf_push(0, buf);    /* finish pileup */

     bam_plbuf_destroy(buf);
     bam_destroy1(bam_read);
     bam_index_destroy(bamidx);
     samclose(bamin);
     return 0;
}
示例#24
0
inline tmap_sam_io_t *
tmap_sam_io_init2(const char *fn, const char *mode,
                  bam_header_t *header)
{
  tmap_sam_io_t *io = NULL;

  io = tmap_calloc(1, sizeof(tmap_sam_io_t), "io");

  // Open the file for writing
  io->fp = samopen(fn, mode, header);

  return io;
}
示例#25
0
int main(int argc, char *argv[])
{
    samfile_t *fp;
    if ((fp = samopen(argv[1], "rb", 0)) == 0) {
        fprintf(stderr, "showbam: Fail to open BAM file %s\n", argv[1]);
        return 1;
    }
    bam1_t *b = bam_init1();
    while (samread(fp, b) >= 0) fetch_func(b);
    bam_destroy1(b);
    samclose(fp);
    return 0;
}
  void init()
  {
    fp = samopen(bam_file_path.c_str(),"rb",0);
    if(fp == NULL)
    {
      throw std::runtime_error("samopen() error with " + bam_file_path);
    }

    bamidx = bam_index_load(bam_file_path.c_str());
    if (bamidx == NULL)
    {
      throw std::runtime_error("bam_index_load() error with " + bam_file_path);
    }
  }
void edwSamRepeatAnalysis(char *inSam, char *outRa)
/* edwSamRepeatAnalysis - Analyze result of alignment vs. RepeatMasker type libraries.. */
{
/* Go through sam file, filling in hiLevelHash with count of each hi level repeat class we see. */
struct hash *hiLevelHash = hashNew(0);
samfile_t *sf = samopen(inSam, "r", NULL);
bam_header_t *bamHeader = sf->header;
bam1_t one;
ZeroVar(&one);
int err;
long long hit = 0, miss = 0;
while ((err = samread(sf, &one)) >= 0)
    {
    int32_t tid = one.core.tid;
    if (tid < 0)
	{
	++miss;
        continue;
	}
    ++hit;

    /* Parse out hiLevel classification from target,  which is something like 7SLRNA#SINE/Alu 
     * from which we'd want to extract SINE.  The '/' is not present in all input. */
    char *target = bamHeader->target_name[tid];
    char *hashPos = strchr(target, '#');
    if (hashPos == NULL)
        errAbort("# not found in target %s", target);
    char *hiLevel = cloneString(hashPos + 1);
    char *slashPos = strchr(hiLevel, '/');
    if (slashPos != NULL)
        *slashPos = 0;

    hashIncInt(hiLevelHash, hiLevel);
    }
samclose(sf);

/* Output some basic stats as well as contents of hash */
FILE *f = mustOpen(outRa, "w");
double invTotal = 1.0 / (hit + miss);
double mapRatio = (double)hit * invTotal;
struct hashEl *hel, *helList = hashElListHash(hiLevelHash);
slSort(&helList, hashElCmp);
for (hel = helList; hel != NULL; hel = hel->next)
    {
    double hitRatio = ptToInt(hel->val) * invTotal;
    fprintf(f, "%s %g\n", hel->name, hitRatio);
    }
fprintf(f, "total %g\n", mapRatio);
carefulClose(&f);
}
示例#28
0
文件: test.c 项目: BGIHENG/RED-ML
int main()
{	
	samfile_t *in = 0, *out = 0;
        int slx2sngr = 0;
        char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;
        strcpy(in_mode, "r"); strcpy(out_mode, "w");
        strcat(in_mode, "b");
                printf("start******************************************************\n");
        char *filename = "/ifs1/RD/shaohaojing/DAI/1000306/1000306_HUMgqbRLJDIAAPE_091015_I58_FC42UC6AAXX_L2_HUMgqbRLJDIAAPE_1.fq.bam.sort.bam";
        if ((in = samopen(filename, in_mode, fn_list)) == 0)
        {
                fprintf(stderr, "[main_samview] fail to open file for reading.\n");
                exit(0);
        }
        if (in->header == 0) {
                fprintf(stderr, "[main_samview] fail to read the header.\n");
                exit(0);
        }
        if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) {
                fprintf(stderr, "[main_samview] fail to open file for writing.\n");
                exit(0);
        }

        bam1_t *b = bam_init1();
        int r;
        char *s;
        while((r = samread(in, b)) >= 0)
        {
                if (!__g_skip_aln(in->header, b)) {
                        s = bam_format1_core(out->header, b, out->type>>2&3);
                }
                printf("%s\n", s);
                free(s);
//                printf("%s", b->data);
//                printf("\n++++++++++%d+++++++++++", r);
//              printf("********************\n");
        }
示例#29
0
文件: bam-unique.c 项目: hjanime/cbgb
void filter_by_id(const char* fn, hash_table* T)
{
    fprintf(stderr, "filtering ... \n");

    samfile_t* fin = samopen(fn, "rb", NULL);
    if (fin == NULL) {
        fprintf(stderr, "can't open bam file %s\n", fn);
        exit(1);
    }

    samfile_t* fout = samopen("-", "w", (void*)fin->header);
    if (fout == NULL) {
        fprintf(stderr, "can't open stdout, for some reason.\n");
        exit(1);
    }

    fputs(fin->header->text, stdout);

    bam1_t* b = bam_init1();
    uint32_t n = 0;

    while (samread(fin, b) >= 0) {
        if (++n % 1000000 == 0) {
            fprintf(stderr, "\t%d reads\n", n);
        }

        if (get_hash_table(T, bam1_qname(b), b->core.l_qname) == 1) {
            samwrite(fout, b);
        }
    }

    bam_destroy1(b);
    samclose(fout);
    samclose(fin);

    fprintf(stderr, "done.\n");
}
示例#30
0
void test_cigar_to_spans2() {

  char *sam_filename = "testdata/cigar_bug.sam";
  samfile_t *samfile = samopen(sam_filename, "r", NULL);  
  bam1_t *rec = bam_init1();

  struct SpanAssertion {
    int read_num;
    int num_spans;
    struct Span spans[10];
  };

  struct SpanAssertion cases[] = {
    { 0, 2, { { 46383142, 46383163 },
              { 46384677, 46384749 } } }
  };

  int num_cases = sizeof(cases) / sizeof(struct SpanAssertion);
  int read_num = 0;
  int case_num = 0;
  CigarCursor curs;
  while (case_num < num_cases &&
         samread(samfile, rec) > 0) {

    if (cases[case_num].read_num == read_num) {

      int num_spans = cases[case_num].num_spans;
      Span *span;

      init_cigar_cursor(&curs, rec);

      for (span = cases[case_num].spans; span < cases[case_num].spans + num_spans; span++) {

        if (next_span(&curs)) {
          assert_equals(span->start, curs.start, "Start");
          assert_equals(span->end, curs.end, "End");
        }
        
      }

      assert_equals(0, next_span(&curs), "No more spans");

      case_num++;
    }
    read_num++;
  }

}