open multiple BamWriters

32 views
Skip to first unread message

tlx...@gmail.com

unread,
Jan 7, 2014, 10:52:17 AM1/7/14
to bamtoo...@googlegroups.com
Hi everyone,

I want to split one bam file into several bam files and I am trying to use std::vector<BamTools::BamWriter> to open multiple BamWriters:

std::vector<BamTools::BamWriter> vBW(10);
for(int i=0;i<10;i++){
    if (!vBW[i].Open(filename[i], header, references)) {
        cerr << "Could not open output BAM file "<<filename[i] << endl;
        ......
    }
}

The source code can be compiled correctly but there is segmentation fault in this part while running. Anyone knows how to correctly open multiple BamWriters at the same time?

Best,

Lin

Derek Barnett

unread,
Jan 7, 2014, 11:52:55 AM1/7/14
to bamtoo...@googlegroups.com
Hi Lin,
I've verified & reproduced the bug you mentioned. Thanks for the heads-up.

The current workaround for this is to use a pointer to your BamWriter:

std::vector<BamTools::BamWriter*> writers(numInput, 0);
for ( int i = 0; i < numInput; ++i ) {
  writers[i] = new BamWriter;
  if ( !writers[i]->Open(....) {
     cerr << .....
  }
}

// do stuff with writers, and then
// later on, cleanup

for ( int i = 0; i < numInput; ++i ) {
    if ( writers[i] ) {
       writers[i]->Close();
       delete writers[i];
       writers[i] = 0;
    }
}

I know it's a little extra effort to manage the memory manually, but it will work. In fact several command-line utilities (e.g. 'bamtools split', and API classes (e.g. BamMultiReader) use this approach for readers & writers. There are other reasons I had used the pointer approach, but that at least explains why I haven't seen this behavior so far. I'm sure it has to do with the pimpl not playing well with STL copy behavior for containers. I'll make a note to fix this in the next major release.

Please let me know this helps or not.
  - Derek
--
You received this message because you are subscribed to the Google Groups "bamtools-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bamtools-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Lin Song

unread,
Jan 7, 2014, 12:18:04 PM1/7/14
to bamtoo...@googlegroups.com
Hi Derek,

I tried the pointer approach in my program and it works exactly as what I wish. Thank you very much! 

Best,

Lin


You received this message because you are subscribed to a topic in the Google Groups "bamtools-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bamtools-user/-CIuYwgoBNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bamtools-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages