Skip to content

Commit

Permalink
add a command -log to allow logging to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mcshane committed May 12, 2015
1 parent fe0a530 commit f477744
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 84 deletions.
1 change: 1 addition & 0 deletions pbwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef struct { /* data structure for moving forwards - doesn't know PBWT */
/* pbwtMain.c */

extern char *commandLine ; /* a copy of the command line */
extern FILE *logFilePtr ; /* log file pointer */

/* pbwtCore.c */

Expand Down
8 changes: 4 additions & 4 deletions pbwtCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ PBWT *pbwtSubSites (PBWT *pOld, double fmin, double frac)
}
pbwtCursorToAFend (uNew, pNew) ;

fprintf (stderr, "subsites with fmin %f, frac %f leaves %d sites\n", fmin, frac, pNew->N) ;
fprintf (logFilePtr, "subsites with fmin %f, frac %f leaves %d sites\n", fmin, frac, pNew->N) ;

pNew->chrom = pOld->chrom ; pOld->chrom = 0 ;
pNew->samples = pOld->samples ; pOld->samples = 0 ;
Expand Down Expand Up @@ -176,7 +176,7 @@ void pbwtBuildReverse (PBWT *p)
/* save uR->a, which is the lexicographic order of the sequences */
if (!p->aRend) p->aRend = myalloc (M, int) ; memcpy (p->aRend, uR->a, M * sizeof(int)) ;

fprintf (stderr, "built reverse PBWT - size %ld\n", arrayMax(p->zz)) ;
fprintf (logFilePtr, "built reverse PBWT - size %ld\n", arrayMax(p->zz)) ;

if (isCheck) /* print out the reversed haplotypes */
{ FILE *fp = fopen ("rev.haps","w") ;
Expand Down Expand Up @@ -622,7 +622,7 @@ PBWT *pbwtSelectSites (PBWT *pOld, Array sites, BOOL isKeepOld)
}
pbwtCursorToAFend (uNew, pNew) ;

fprintf (stderr, "%d sites selected from %d, pbwt size for %d haplotypes is %ld\n",
fprintf (logFilePtr, "%d sites selected from %d, pbwt size for %d haplotypes is %ld\n",
pNew->N, pOld->N, pNew->M, arrayMax(pNew->yz)) ;

if (isKeepOld)
Expand Down Expand Up @@ -682,7 +682,7 @@ PBWT *pbwtRemoveSites (PBWT *pOld, Array sites, BOOL isKeepOld)
}
pbwtCursorToAFend (uNew, pNew) ;

fprintf (stderr, "%d sites selected from %d, pbwt size for %d haplotypes is %ld\n",
fprintf (logFilePtr, "%d sites selected from %d, pbwt size for %d haplotypes is %ld\n",
pNew->N, pOld->N, pNew->M, arrayMax(pNew->yz)) ;

if (isKeepOld)
Expand Down
4 changes: 2 additions & 2 deletions pbwtGeneticMap.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void readGeneticMap (FILE *fp)

buildMap () ;

fprintf (stderr, "read %d genetic map entries from %d, %f to %d, %f\n",
fprintf (logFilePtr, "read %d genetic map entries from %d, %f to %d, %f\n",
n, arr(map.x, 0, int), arr(map.g, 0, double),
arr(map.x, n-1, int), arr(map.g, n-1, double)) ;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ void pbwt4hapsStats (PBWT *p)
{
if (!p || !p->sites) die ("hap4stats called without a PBWT with sites") ;
if (!map.x)
{ fprintf (stderr, "hap4stats called without a map - using a linear 1cM/Mb map\n") ;
{ fprintf (logFilePtr, "hap4stats called without a map - using a linear 1cM/Mb map\n") ;
map.x = arrayCreate (2, int) ;
array(map.x,0,int) = arrp(p->sites,0,Site)->x ;
array(map.x,1,int) = arrp(p->sites,arrayMax(p->sites)-1,Site)->x ;
Expand Down
9 changes: 6 additions & 3 deletions pbwtHtslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ PBWT *pbwtReadVcfGT (char *filename) /* read GTs from vcf/bcf using htslib */
free (x) ; pbwtCursorDestroy (u) ;
free (xMissing) ;

fprintf (stderr, "read genotypes from %s\n", filename) ;
if (p->missingOffset) fprintf (stderr, "%ld missing values at %d sites\n",
fprintf (logFilePtr, "read genotypes from %s with %ld sample names and %ld sites on chromosome %s: M, N are %d, %d\n",
filename, arrayMax(p->samples)/2, arrayMax(p->sites), p->chrom, p->M, p->N) ;
if (p->missingOffset) fprintf (logFilePtr, "%ld missing values at %d sites\n",
nMissing, nMissingSites) ;

return p ;
Expand Down Expand Up @@ -241,7 +242,7 @@ void pbwtWriteVcf (PBWT *p, char *filename, char *referenceFasta, char *mode)
if (!fp) die ("could not open file for writing: %s", filename) ;
if (!p) die ("pbwtWriteVcf called without a valid pbwt") ;
if (!p->sites) die ("pbwtWriteVcf called without sites") ;
if (!p->samples) fprintf (stderr, "Warning: pbwtWriteVcf called without samples... using fake sample names PBWT0, PBWT1 etc...\n") ;
if (!p->samples) fprintf (logFilePtr, "Warning: pbwtWriteVcf called without samples... using fake sample names PBWT0, PBWT1 etc...\n") ;
BOOL isDosage = p->dosageOffset ? TRUE : FALSE ;

// write header
Expand Down Expand Up @@ -389,6 +390,8 @@ void pbwtWriteVcf (PBWT *p, char *filename, char *referenceFasta, char *mode)
bcf_hdr_destroy(bcfHeader) ;
bcf_destroy1(bcfRecord);
hts_close(fp) ;

fprintf (logFilePtr, "written vcf file: %d records and %d samples\n", p->N, p->M/2) ;
}

/******* end of file ********/
38 changes: 19 additions & 19 deletions pbwtIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void pbwtWrite (PBWT *p, FILE *fp) /* just writes compressed pbwt in yz */
if (fwrite (arrp(p->yz, 0, uchar), sizeof(uchar), arrayMax(p->yz), fp) != arrayMax(p->yz))
die ("error writing data in pbwtWrite") ;

fprintf (stderr, "written %ld chars pbwt: M, N are %d, %d\n", arrayMax(p->yz), p->M, p->N) ;
fprintf (logFilePtr, "written %ld chars pbwt: M, N are %d, %d\n", arrayMax(p->yz), p->M, p->N) ;
}

void pbwtWriteSites (PBWT *p, FILE *fp)
Expand All @@ -72,7 +72,7 @@ void pbwtWriteSites (PBWT *p, FILE *fp)
}
if (ferror (fp)) die ("error writing sites file") ;

fprintf (stderr, "written %d sites from %d to %d\n", p->N,
fprintf (logFilePtr, "written %d sites from %d to %d\n", p->N,
arrp(p->sites, 0, Site)->x, arrp(p->sites, p->N-1, Site)->x) ;
}

Expand All @@ -91,7 +91,7 @@ void pbwtWriteSamples (PBWT *p, FILE *fp)
}
if (ferror (fp)) die ("error writing samples file") ;

fprintf (stderr, "written %d samples\n", p->M/2) ;
fprintf (logFilePtr, "written %d samples\n", p->M/2) ;
}

void writeDataOffset (FILE *fp, char *name, Array offset, Array data, int N)
Expand All @@ -108,7 +108,7 @@ void writeDataOffset (FILE *fp, char *name, Array offset, Array data, int N)
if (fwrite (arrp(offset, 0, long), sizeof(long), N, fp) != N)
die ("error writing offsets in write %s", name) ;

fprintf (stderr, "written %ld chars compressed %s data\n", n, name) ;
fprintf (logFilePtr, "written %ld chars compressed %s data\n", n, name) ;
}

void pbwtWriteMissing (PBWT *p, FILE *fp)
Expand All @@ -125,7 +125,7 @@ void pbwtWriteReverse (PBWT *p, FILE *fp)
int* tstart = p->aFstart ; p->aFstart = p->aRstart ;
int* tend = p->aFend ; p->aFend = p->aRend ;

fprintf (stderr, "reverse: ") ; pbwtWrite (p, fp) ;
fprintf (logFilePtr, "reverse: ") ; pbwtWrite (p, fp) ;

p->yz = tz ; p->aFstart = tstart ; p->aFend = tend ;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ PBWT *pbwtRead (FILE *fp)
if (fread (arrp(p->yz, 0, uchar), sizeof(uchar), nz, fp) != nz)
die ("error reading data in pbwt file") ;

fprintf (stderr, "read pbwt %s file with %ld bytes: M, N are %d, %d\n", tag, nz, p->M, p->N) ;
fprintf (logFilePtr, "read pbwt %s file with %ld bytes: M, N are %d, %d\n", tag, nz, p->M, p->N) ;
return p ;
}

Expand Down Expand Up @@ -248,7 +248,7 @@ Array pbwtReadSitesFile (FILE *fp, char **chrom)

if (ferror (fp)) die ("error reading sites file") ;

fprintf (stderr, "read %ld sites on chromosome %s from file\n", arrayMax(sites), *chrom) ;
fprintf (logFilePtr, "read %ld sites on chromosome %s from file\n", arrayMax(sites), *chrom) ;

arrayDestroy (varTextArray) ;
return sites ;
Expand Down Expand Up @@ -324,7 +324,7 @@ Array pbwtReadSamplesFile (FILE *fp) /* for now assume all samples diploid */
}
arrayDestroy (nameArray) ;

fprintf (stderr, "read %ld sample names\n", arrayMax(samples)) ;
fprintf (logFilePtr, "read %ld sample names\n", arrayMax(samples)) ;

return samples ;
}
Expand Down Expand Up @@ -357,7 +357,7 @@ static void readDataOffset (FILE *fp, char *name, Array *offset, Array *data, in
if (fread (arrp(*data, 0, uchar), sizeof(uchar), n, fp) != n)
die ("error reading zMissing in pbwtReadMissing") ;
arrayMax(*data) = n ;
fprintf (stderr, "read %ld chars compressed missing data\n", n) ;
fprintf (logFilePtr, "read %ld chars compressed missing data\n", n) ;

*offset = arrayReCreate (*offset, N, long) ;
if (dummy != -1) /* old version with ints not longs */
Expand Down Expand Up @@ -469,9 +469,9 @@ PBWT *pbwtReadMacs (FILE *fp)
}
pbwtCursorToAFend (u, p) ;

fprintf (stderr, "read MaCS file: M, N are\t%d\t%d\n", M, p->N) ;
fprintf (logFilePtr, "read MaCS file: M, N are\t%d\t%d\n", M, p->N) ;
if (isStats)
fprintf (stderr, " xtot, ytot are\t%d\t%d\n", nxTot, nyTot) ;
fprintf (logFilePtr, " xtot, ytot are\t%d\t%d\n", nxTot, nyTot) ;

free(x) ; pbwtCursorDestroy (u) ;

Expand Down Expand Up @@ -563,9 +563,9 @@ static PBWT *pbwtReadLineFile (FILE *fp, char* type, ParseLineFunc parseLine)
}
pbwtCursorToAFend (u, p) ;

fprintf (stderr, "read %s file", type) ;
if (p->chrom) fprintf (stderr, " for chromosome %s", p->chrom) ;
fprintf (stderr, ": M, N are\t%d\t%d; yz length is %ld\n", p->M, p->N, arrayMax(p->yz)) ;
fprintf (logFilePtr, "read %s file", type) ;
if (p->chrom) fprintf (logFilePtr, " for chromosome %s", p->chrom) ;
fprintf (logFilePtr, ": M, N are\t%d\t%d; yz length is %ld\n", p->M, p->N, arrayMax(p->yz)) ;

arrayDestroy(xArray) ; pbwtCursorDestroy (u) ;

Expand Down Expand Up @@ -665,7 +665,7 @@ PBWT *pbwtReadGen (FILE *fp, char *chrom)
nGenMissing = 0 ;
PBWT *p = pbwtReadLineFile (fp, "gen", parseGenLine) ;
p->chrom = strdup (chrom) ;
if (nGenMissing) fprintf (stderr, "%ld missing genotypes set to 00\n", nGenMissing) ;
if (nGenMissing) fprintf (logFilePtr, "%ld missing genotypes set to 00\n", nGenMissing) ;
return p ;
}

Expand Down Expand Up @@ -722,9 +722,9 @@ PBWT *pbwtReadPhase (FILE *fp) /* Li and Stephens PHASE format */
}
pbwtCursorToAFend (u, p) ;

fprintf (stderr, "read phase file") ;
if (p->chrom) fprintf (stderr, " for chromosome %s", p->chrom) ;
fprintf (stderr, ": M, N are\t%d\t%d; yz length is %ld\n", p->M, p->N, arrayMax(p->yz)) ;
fprintf (logFilePtr, "read phase file") ;
if (p->chrom) fprintf (logFilePtr, " for chromosome %s", p->chrom) ;
fprintf (logFilePtr, ": M, N are\t%d\t%d; yz length is %ld\n", p->M, p->N, arrayMax(p->yz)) ;

for (i = 0 ; i < p->N ; ++i) free(data[i]) ;
free (data) ; pbwtCursorDestroy (u) ;
Expand All @@ -750,7 +750,7 @@ void pbwtWriteHaplotypes (FILE *fp, PBWT *p)
}
free (hap) ; pbwtCursorDestroy (u) ;

fprintf (stderr, "written haplotype file: %d rows of %d\n", p->N, M) ;
fprintf (logFilePtr, "written haplotype file: %d rows of %d\n", p->N, M) ;
}

/*************** write IMPUTE files ********************/
Expand Down
Loading

0 comments on commit f477744

Please sign in to comment.