Skip to content

Commit

Permalink
stdout fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredo committed Feb 28, 2016
1 parent cdbe798 commit 3891d10
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
37 changes: 24 additions & 13 deletions fastqlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,36 @@ fastqWriter::fastqWriter(string fname){
}

int fastqWriter::open(string fname){
fp = gzopen(fname.c_str(), "wb");
if(!fp ) {
cerr << "Problem reading "<<fname<<endl;
exit(1);
if(fname=="-") {//STDOUT
_stdout=true;
}
else{
_stdout=false;
fp = gzopen(fname.c_str(), "wb");
if(!fp ) {
cerr << "Problem reading "<<fname<<endl;
exit(1);
}
}
return(0);
}

int fastqWriter::write(fqread & read) {
if(read.l>0) {
if(gzwrite(fp,"@",1)==0) die("problem writing output");
if(gzwrite(fp,(char *)read.h.c_str(),read.h.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.s.c_str(),read.s.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.l3.c_str(),read.l3.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.q.c_str(),read.q.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if(_stdout) {
read.print();
}
else{
if(gzwrite(fp,"@",1)==0) die("problem writing output");
if(gzwrite(fp,(char *)read.h.c_str(),read.h.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.s.c_str(),read.s.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.l3.c_str(),read.l3.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
if( gzwrite(fp,(char *)read.q.c_str(),read.q.size())==0) die("problem writing output");
if( gzwrite(fp,"\n",1)==0) die("problem writing output");
}
return(1);
}
else return(0);
Expand Down
3 changes: 2 additions & 1 deletion fastqlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class fastqWriter {
int write(readPair & read);
private:
gzFile fp;
bool _stdout;//true if writing to stdout
};

class pairReader {
Expand All @@ -78,7 +79,7 @@ class pairWriter {

//interleaved
pairWriter(string fname);
int open(string fname);
int open(string fname);
//separate files
pairWriter(string fname1,string fname2);
int open(string fname1,string fname2);
Expand Down
45 changes: 21 additions & 24 deletions matepair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,24 +516,29 @@ nxtrimWriter::nxtrimWriter(string prefix,bool jmp,bool separate) {
}

int nxtrimWriter::open(string prefix,bool jmp,bool separate) {
if(prefix=="-")
return(open(jmp,separate));
n_mp=0;
n_pe=0;
n_se=0;
n_unk=0;
justmp = jmp;

if(separate) mp_out.open(prefix+"_R1.mp.fastq.gz", prefix+"_R2.mp.fastq.gz");
else mp_out.open(prefix+".mp.fastq.gz");
if(separate)
mp_out.open(prefix+"_R1.mp.fastq.gz", prefix+"_R2.mp.fastq.gz");
else
mp_out.open(prefix+".mp.fastq.gz");

if(separate) unknown_out.open(prefix+"_R1.unknown.fastq.gz",prefix+"_R2.unknown.fastq.gz");
else unknown_out.open(prefix+".unknown.fastq.gz");
if(separate)
unknown_out.open(prefix+"_R1.unknown.fastq.gz",prefix+"_R2.unknown.fastq.gz");
else
unknown_out.open(prefix+".unknown.fastq.gz");

if(!justmp) {
if(separate) pe_out.open(prefix+"_R1.pe.fastq.gz",prefix+"_R2.pe.fastq.gz");
else pe_out.open(prefix+".pe.fastq.gz");
se_out.open(prefix+".se.fastq.gz");
}
print_to_stdout=false;
return(0);
}

Expand All @@ -549,29 +554,21 @@ int nxtrimWriter::open(bool jmp,bool separate) {
n_se=0;
n_unk=0;
justmp = jmp;

print_to_stdout=true;
mp_out.open("-");
pe_out.open("-");
unknown_out.open("-");
se_out.open("-");
return(0);
}

int nxtrimWriter::write(matePair m) {
if(!print_to_stdout) {
n_mp+=mp_out.write(m.mp);
n_unk+=unknown_out.write(m.unknown);
if(!justmp) {
n_pe+=pe_out.write(m.pe);
n_se+=se_out.write(m.se);
}
if(DEBUG>0)
cerr << "Wrote: n_mp="<<n_mp<<" n_unk="<<n_unk<<" n_pe="<<n_pe<<" n_se="<<n_se<<endl;
}
else{
m.mp.print();
m.unknown.print();
if(!justmp) {
m.pe.print();
m.se.print();
}
n_mp+=mp_out.write(m.mp);
n_unk+=unknown_out.write(m.unknown);
if(!justmp) {
n_pe+=pe_out.write(m.pe);
n_se+=se_out.write(m.se);
}
if(DEBUG>0) cerr << "Wrote: n_mp="<<n_mp<<" n_unk="<<n_unk<<" n_pe="<<n_pe<<" n_se="<<n_se<<endl;

return(0);
}
2 changes: 1 addition & 1 deletion nxtrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main(int argc,char **argv) {

nxtrimWriter out;
if(write_stdout)
out.open(justmp,separate);
out.open("-",justmp,separate);
else
out.open(prefix,justmp,separate);

Expand Down

0 comments on commit 3891d10

Please sign in to comment.