10 void add_bam_record_to_wiggle(const bam1_t *b, Wiggle& wiggle) {
11 float w = bam_aux2f(bam_aux_get(b, "ZW"));
12 int pos = b->core.pos;
13 uint32_t *p = bam1_cigar(b);
15 for (int i = 0; i < (int)b->core.n_cigar; i++, ++p) {
16 int op = *p & BAM_CIGAR_MASK;
17 int op_len = *p >> BAM_CIGAR_SHIFT;
20 //case BAM_CSOFT_CLIP : pos += op_len; break;
21 case BAM_CINS : pos += op_len; break;
23 for (int j = 0; j < op_len; j++, ++pos) {
24 wiggle.read_depth[pos] += w;
27 case BAM_CREF_SKIP : pos += op_len; break;
28 default : assert(false);
33 void build_wiggles(const std::string& bam_filename,
34 WiggleProcessor& processor) {
35 samfile_t *bam_in = samopen(bam_filename.c_str(), "rb", NULL);
36 if (bam_in == 0) { fprintf(stderr, "Cannot open %s!\n", bam_filename.c_str()); exit(-1); }
37 //assert(bam_in != 0);
39 int cur_tid = -1; //current tid;
41 bam1_t *b = bam_init1();
43 while (samread(bam_in, b) >= 0) {
44 if (b->core.tid != cur_tid) {
45 if (cur_tid >= 0) processor.process(wiggle);
46 cur_tid = b->core.tid;
47 wiggle.name = bam_in->header->target_name[cur_tid];
48 wiggle.read_depth.assign(bam_in->header->target_len[cur_tid], 0.0);
50 add_bam_record_to_wiggle(b, wiggle);
52 if (cnt % 1000000 == 0) fprintf(stderr, "%d FIN\n", cnt);
54 if (cur_tid >= 0) processor.process(wiggle);
60 UCSCWiggleTrackWriter::UCSCWiggleTrackWriter(const std::string& output_filename,
61 const std::string& track_name) {
62 fo = fopen(output_filename.c_str(), "w");
63 fprintf(fo, "track type=wiggle_0 name=\"%s\" description=\"%s\" visibility=full\n",
68 UCSCWiggleTrackWriter::~UCSCWiggleTrackWriter() {
72 void UCSCWiggleTrackWriter::process(const Wiggle& wiggle) {
76 for (size_t i = 0; i < wiggle.read_depth.size(); i++) {
77 if (wiggle.read_depth[i] > 0) {
83 fprintf(fo, "fixedStep chrom=%s start=%d step=1\n", wiggle.name.c_str(), sp + 1);
84 for (int j = sp; j <= ep; j++) fprintf(fo, "%.7g\n", wiggle.read_depth[j]);
91 fprintf(fo, "fixedStep chrom=%s start=%d step=1\n", wiggle.name.c_str(), sp + 1);
92 for (int j = sp; j <= ep; j++) fprintf(fo, "%.7g\n", wiggle.read_depth[j]);
96 ReadDepthWriter::ReadDepthWriter(std::ostream& stream)
100 void ReadDepthWriter::process(const Wiggle& wiggle) {
101 stream_ << wiggle.name << '\t'
102 << wiggle.read_depth.size() << '\t';
103 for (size_t i = 0; i < wiggle.read_depth.size(); ++i) {
104 if (i > 0) stream_ << ' ';
105 stream_ << wiggle.read_depth[i];