Skip to content

Commit

Permalink
[chore/fix] add metadata and formula require func
Browse files Browse the repository at this point in the history
add function to check whether metadata and formula is needed for given
arguments

fix error due to metadata formula requirement checks

remove deprecated code
  • Loading branch information
isinaltinkaya committed Jul 1, 2023
1 parent ad03dc8 commit 66d36a2
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 169 deletions.
30 changes: 20 additions & 10 deletions Makefile
Expand Up @@ -8,11 +8,16 @@
# `make` - compile in release mode
#
# `make dev` - compile in developer mode
# -O0 (no optimization)
# -g (debugging info)
# -Wall (turn on compiler warnings)
# -save-temps (save intermediate files; useful for seeing macro expansions)
# -v (verbose)
# - O0 (no optimization)
# - g (debugging info)
# - Wall (turn on compiler warnings)
#
# `make dev ext`- compile in extended developer mode
# - O0 (no optimization)
# - g (debugging info)
# - Wall (turn on compiler warnings)
# - save-temps (save intermediate files; useful for seeing macro expansions)
# - v (verbose)
#
# `make clean` - clean up the director
#
Expand Down Expand Up @@ -51,7 +56,13 @@ $(info _________________________________________________________________________

DEV_MODE=1
OPTIM_FLAGS := -O0

ifeq (ext,$(filter ext,$(MAKECMDGOALS)))
CXXFLAGS += -g -Wall $(OPTIM_FLAGS) -save-temps -v
else
CXXFLAGS += -g -Wall $(OPTIM_FLAGS)
endif

$(info )
$(info [INFO] Make updated CXXFLAGS="$(CXXFLAGS)")
$(info )
Expand Down Expand Up @@ -177,15 +188,14 @@ OBJ := $(CXXSRC:.cpp=.o)
# Dependency files
DEP := $(OBJ:.o=.d)


LIBS := $(LIBHTS) $(LIBS)

$(PROGRAM): $(OBJ)
$(CXX) -o $(PROGRAM) *.o $(LIBS)


-include $(DEP)
FLAGS := $(CPPFLAGS) $(CXXFLAGS)

$(PROGRAM): $(OBJ)
$(CXX) -o $(PROGRAM) *.o $(LIBS)

%.o: %.cpp
$(CXX) -c $(FLAGS) $*.cpp
$(CXX) -MM $(FLAGS) $*.cpp >$*.d
Expand Down
1 change: 1 addition & 0 deletions dataStructs.cpp
Expand Up @@ -905,6 +905,7 @@ distanceMatrixStruct *distanceMatrixStruct_read(paramStruct *pars) {

int n_vals = 0;

// TODO
if (IO::isGzFile(args->in_dm_fn) == 1) {
size_t buf_size = FGETS_BUF_SIZE;
size_t *buf_size_ptr = &buf_size;
Expand Down
156 changes: 25 additions & 131 deletions io.cpp
Expand Up @@ -9,6 +9,21 @@ const char *IO::FILE_EXTENSIONS[] = {"", ".gz", ".bgz"};

IO::outFilesStruct *outFiles = new IO::outFilesStruct();

kstring_t *kbuf_init() {
kstring_t *kbuf = new kstring_t;
kbuf->l = 0;
kbuf->m = 0;
kbuf->s = NULL;
return kbuf;
}

void kbuf_destroy(kstring_t *kbuf) {
if (NULL != kbuf) {
FREE(kbuf->s);
delete kbuf;
}
}

// int IO::readFile::getNextLine(FILE* fp, char* line, size_t* len)

void IO::append_argsFile(const char *format, ...) {
Expand Down Expand Up @@ -329,30 +344,6 @@ char *IO::readFile::getFirstLine(FILE *fp) {
return full_line;
}

char *IO::readFile::readToBuffer(const char *fn) {
char *buffer = NULL;
size_t buf_size = 0;
FILE *fp = IO::getFile(fn, "r");

// seek to end of file
fseek(fp, 0, SEEK_END);
// offset to the end of the file == size of the file
buf_size = ftell(fp);
ASSERT(buf_size > 0);

buffer = (char *)malloc((buf_size + 1) * sizeof(char));
ASSERT(buffer != NULL);

// seek back to beginning of file
fseek(fp, 0, SEEK_SET);

fread(buffer, sizeof(char), buf_size, fp);
buffer[buf_size] = '\0';

FCLOSE(fp);
return buffer;
}

int IO::readGzFile::readToBuffer(char *fn, char **buffer_p, size_t *buf_size_p) {
gzFile fp = IO::getGzFile(fn, "r");

Expand Down Expand Up @@ -396,64 +387,6 @@ int IO::inspectFile::count_nCols(const char *line, const char *delims) {
return count;
}

/// @brief count_nRows count number of rows in a file
/// @param fn file name
/// @param HAS_COLNAMES 1 if file has header
/// @return integer n number of rows
int IO::inspectFile::count_nRows(char *fn, int HAS_COLNAMES) {
FILE *fp = IO::getFile(fn, "r");

char buf[FREAD_BUF_SIZE];
int n = 0;
for (;;) {
size_t res = fread(buf, 1, FREAD_BUF_SIZE, fp);
ASSERT(ferror(fp) == 0);

size_t i;
for (i = 0; i < res; i++)
if (buf[i] == '\n')
n++;

if (feof(fp))
break;
}

if (HAS_COLNAMES == 1)
n--;

fclose(fp);

return n;
}

/// @brief count_nRows count number of rows in a file
/// @param fp pointer to file
/// @param HAS_COLNAMES 1 if file has header
/// @return integer n number of rows
int IO::inspectFile::count_nRows(FILE *fp, int HAS_COLNAMES) {
// return to the beginning of the file
ASSERT(fseek(fp, 0, SEEK_SET) == 0);

char buf[FREAD_BUF_SIZE];
int n = 0;
for (;;) {
size_t res = fread(buf, 1, FREAD_BUF_SIZE, fp);
ASSERT(ferror(fp) == 0);

size_t i;
for (i = 0; i < res; i++)
if (buf[i] == '\n')
n++;

if (feof(fp))
break;
}

if (HAS_COLNAMES == 1)
n--;
return n;
}

// //TODO DEPRECATED?
// /// @brief IO::validateFile::Metadata validate metadata file (input=BCF)
// /// @param in_mtd_fp pointer to metadata file
Expand Down Expand Up @@ -505,21 +438,6 @@ int IO::inspectFile::count_nRows(FILE *fp, int HAS_COLNAMES) {
// }
// }

kstring_t *kbuf_init() {
kstring_t *kbuf = new kstring_t;
kbuf->l = 0;
kbuf->m = 0;
kbuf->s = NULL;
return kbuf;
}

void kbuf_destroy(kstring_t *kbuf) {
if (NULL != kbuf) {
FREE(kbuf->s);
delete kbuf;
}
}

IO::outputStruct::outputStruct(const char *fn_, const char *suffix, int fc_) {
fc = OUTFC(fc_);
switch (fc) {
Expand All @@ -528,7 +446,6 @@ IO::outputStruct::outputStruct(const char *fn_, const char *suffix, int fc_) {
fp = openFileW(fn);
break;
case OUTFC::GZ:
NEVER;
fn = setFileName(fn_, suffix, FILE_EXTENSIONS[fc]);
gzfp = openGzFileW(fn);
break;
Expand All @@ -537,8 +454,7 @@ IO::outputStruct::outputStruct(const char *fn_, const char *suffix, int fc_) {
bgzfp = bgzf_open(fn, "wb");
break;
default:
fprintf(stderr, "\n[ERROR] Unknown file compression type (%d)\n", fc);
exit(1);
ERROR("Unknown file compression type is specified (%d)", fc);
break;
}
// fprintf(stderr, "\n[INFO] Opening output file: %s with compression type: %d (%s)\n", fn, fc, OUTFC_LUT[(OUTFC)fc]);
Expand Down Expand Up @@ -642,7 +558,6 @@ void IO::outputStruct::kbuf_write() {
ASSERT(fprintf(fp, "%s", kbuf->s) > 0);
break;
case OUTFC::GZ:
fprintf(stderr, "%s", kbuf->s);
ASSERT(gzprintf(gzfp, "%s", kbuf->s) > 0);
break;
case OUTFC::BBGZ:
Expand Down Expand Up @@ -730,39 +645,18 @@ void IO::outFilesStruct_init(IO::outFilesStruct *ofs) {
void IO::outFilesStruct_destroy(IO::outFilesStruct *ofs) {
// flushAll();

IFDEL(ofs->out_args_fs);
IFDEL(ofs->out_dm_fs);
IFDEL(ofs->out_amova_fs);
IFDEL(ofs->out_dev_fs);
IFDEL(ofs->out_jgcd_fs);
IFDEL(ofs->out_dxy_fs);
IFDEL(ofs->out_nj_fs);
IFDEL(ofs->out_blockstab_fs);
IFDEL(ofs->out_v_bootstrapRep_fs);
FDEL(ofs->out_args_fs);
FDEL(ofs->out_dm_fs);
FDEL(ofs->out_amova_fs);
FDEL(ofs->out_dev_fs);
FDEL(ofs->out_jgcd_fs);
FDEL(ofs->out_dxy_fs);
FDEL(ofs->out_nj_fs);
FDEL(ofs->out_blockstab_fs);
FDEL(ofs->out_v_bootstrapRep_fs);

delete ofs;
}

// void flushAll()
// {
// if (out_dm_fs != NULL)
// {
// out_dm_fs->flush();
// }
// if (out_jgcd_fs != NULL)
// {
// out_jgcd_fs->flush();
// }
// if (out_amova_fs != NULL)
// {
// out_amova_fs->flush();
// }
// if (out_dev_fs != NULL)
// {
// out_dev_fs->flush();
// }
// }

int IO::verbose(const int verbose_threshold) {
if (verbose_threshold == 0) {
return 1; // if checking against 0 (i.e. no verbose needed) return 1
Expand Down
32 changes: 21 additions & 11 deletions ngsAMOVA.cpp
Expand Up @@ -36,17 +36,16 @@ void input_VCF(paramStruct *pars) {
char **indNames = NULL;

metadataStruct *metadata = NULL;
// if analyses requiring metadata are requested
if (0 != args->doAMOVA || 0 != args->doDxy || 2 == args->doNJ) {
if (NULL != args->in_mtd_fn) {
if (require_metadata()) {
if (NULL == args->in_mtd_fn) {
ERROR("Requested analyses requiring metadata but no metadata file was provided.");
} else {
if (NULL != args->formula) {
metadata = metadataStruct_get(pars);
indNames = metadata->indNames;
} else {
ERROR("Requested analyses requiring metadata but no formula was provided.");
NEVER; // this should already be checked in require_formula()
}
} else {
ERROR("Requested analyses requiring metadata but no metadata file was provided.");
}
}

Expand Down Expand Up @@ -105,7 +104,7 @@ void input_VCF(paramStruct *pars) {
DEL(njSt);
}

IFDEL(metadata);
FDEL(metadata);
DEL2D(pairSt, pars->nIndCmb);
DEL(distanceMatrix);

Expand All @@ -119,9 +118,19 @@ void input_DM(paramStruct *pars) {

distanceMatrixStruct *distanceMatrix = distanceMatrixStruct_read(pars);

metadataStruct *metadata = metadataStruct_get(pars);

distanceMatrix->set_item_labels(metadata->indNames);
metadataStruct *metadata = NULL;
if (require_metadata()) {
if (NULL == args->in_mtd_fn) {
ERROR("Requested analyses requiring metadata but no metadata file was provided.");
} else {
if (NULL != args->formula) {
metadata = metadataStruct_get(pars);
distanceMatrix->set_item_labels(metadata->indNames);
} else {
NEVER; // this should already be checked in require_formula()
}
}
}

pairStruct **pairSt = new pairStruct *[pars->nIndCmb];

Expand Down Expand Up @@ -159,7 +168,8 @@ void input_DM(paramStruct *pars) {
DEL(njSt);
}

DEL(metadata);
FDEL(metadata);

DEL2D(pairSt, pars->nIndCmb);
DEL(distanceMatrix);
}
Expand Down
28 changes: 25 additions & 3 deletions paramStruct.cpp
Expand Up @@ -6,6 +6,24 @@ void setInputFileType(paramStruct *pars, int inputFileType) {
pars->in_ft = pars->in_ft | inputFileType;
}

// check if any analysis requires formula
bool require_formula(void) {
if (0 != args->doAMOVA) {
return (true);
}

return (false);
}

// check if any analysis requires metadata
bool require_metadata(void) {
if (0 != args->doAMOVA) {
return (true);
}

return (false);
}

paramStruct *paramStruct_init(argStruct *args) {
paramStruct *pars = new paramStruct;

Expand Down Expand Up @@ -33,8 +51,12 @@ paramStruct *paramStruct_init(argStruct *args) {
pars->totSites = 0;
pars->nContigs = 0;

if (NULL != args->formula) {
pars->formula = formulaStruct_get(args->formula);
if (require_formula()) {
if (NULL != args->formula) {
pars->formula = formulaStruct_get(args->formula);
} else {
ERROR("Specified analyses require formula (`--formula/-f`)");
}
}

pars->nIndCmb = 0;
Expand Down Expand Up @@ -66,7 +88,7 @@ void paramStruct_destroy(paramStruct *pars) {
/// @brief check_consistency_args_pars - check consistency between arguments and parameters
/// @param args pointer to argStruct
/// @param pars pointer to paramStruct
void check_consistency_args_pars(argStruct *args, paramStruct *pars) {
void check_consistency_args_pars(paramStruct *pars) {
if (args->minInd == pars->nInd) {
fprintf(stderr, "\n\t-> -minInd %d is equal to the number of individuals found in file: %d. Setting -minInd to 0 (all).\n", args->minInd, pars->nInd);
args->minInd = 0;
Expand Down

0 comments on commit 66d36a2

Please sign in to comment.