Skip to content

Commit

Permalink
Adds error message for missing headers
Browse files Browse the repository at this point in the history
Prevents addition of dummy group or removal of valid group

#779 #769
  • Loading branch information
mothur-westcott committed Jul 23, 2021
1 parent f1b4624 commit c55e885
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 69 deletions.
2 changes: 2 additions & 0 deletions source/commands/amovacommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ int AmovaCommand::execute(){

//read design file
designMap = new DesignMap(designFileName);

if (m->getControl_pressed()) { delete designMap; return 0; }

if (outputdir == "") { outputdir = util.hasPath(phylipFileName); }

Expand Down
2 changes: 1 addition & 1 deletion source/commands/anosimcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int AnosimCommand::execute(){
if (abort) { if (calledHelp) { return 0; } return 2; }

//read design file
designMap = new DesignMap(designFileName);
designMap = new DesignMap(designFileName); if (m->getControl_pressed()) { delete designMap; return 0; }

if (outputdir == "") { outputdir = util.hasPath(phylipFileName); }

Expand Down
2 changes: 1 addition & 1 deletion source/commands/classifysvmsharedcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ void ClassifySvmSharedCommand::readSharedAndDesignFiles(const string& sharedFile
Groups = lookup->getNamesGroups();

DesignMap designMap;
designMap.read(designFilePath);
designMap.read(designFilePath); if (m->getControl_pressed()) { return ; }

while ( lookup != NULL ) {
vector<SharedRAbundVector*> data = lookup->getSharedRAbundVectors();
Expand Down
4 changes: 2 additions & 2 deletions source/commands/getgroupscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ void GetGroupsCommand::readDesign(){
variables["[extension]"] = util.getExtension(designfile);
string outputFileName = getOutputFileName("design", variables);

DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return ; }

bool wroteSomething = false;

Expand Down Expand Up @@ -1025,7 +1025,7 @@ void GetGroupsCommand::fillNames(){
//**********************************************************************************************************************
void GetGroupsCommand::fillGroupsFromDesign(){
try {
DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return ; }
Groups = designMap.getNamesGroups(Sets);
}
catch(exception& e) {
Expand Down
2 changes: 1 addition & 1 deletion source/commands/homovacommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int HomovaCommand::execute(){
if (abort) { if (calledHelp) { return 0; } return 2; }

//read design file
DesignMap* designMap = new DesignMap(designFileName);
DesignMap* designMap = new DesignMap(designFileName); if (m->getControl_pressed()) { delete designMap; return 0; }

//read in distance matrix and square it
ReadPhylipVector readMatrix(phylipFileName);
Expand Down
2 changes: 1 addition & 1 deletion source/commands/indicatorcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ int IndicatorCommand::execute(){
//read designfile if given and set up groups for read of sharedfiles
vector<string> allGroups;
if (designfile != "") {
designMap = new DesignMap(designfile);
designMap = new DesignMap(designfile); if (m->getControl_pressed()) { delete designMap; return 0; }

if (Groups.size() == 0) { Groups = designMap->getCategory(); }
allGroups = designMap->getCategory();
Expand Down
2 changes: 1 addition & 1 deletion source/commands/kruskalwalliscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int KruskalWallisCommand::execute(){

if (abort) { if (calledHelp) { return 0; } return 2; }

DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return 0; }

//if user did not select class use first column
if (mclass == "") { mclass = designMap.getDefaultClass(); m->mothurOut("\nYou did not provide a class, using " + mclass +".\n\n"); }
Expand Down
2 changes: 1 addition & 1 deletion source/commands/lefsecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ int LefseCommand::execute(){
m->setRandomSeed(1982);
if (abort) { if (calledHelp) { return 0; } return 2; }

DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return 0; }

//if user did not select class use first column
if (mclass == "") { mclass = designMap.getDefaultClass(); m->mothurOut("\nYou did not provide a class, using " + mclass +".\n\n"); if (subclass == "") { subclass = mclass; } }
Expand Down
2 changes: 1 addition & 1 deletion source/commands/makelefsecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ int MakeLefseCommand::runRelabund(map<int, consTax2>& consTax, SharedRAbundFloat
DesignMap* designMap = NULL;
vector<string> namesOfGroups = lookup->getNamesGroups();
if (designfile != "") {
designMap = new DesignMap(designfile);
designMap = new DesignMap(designfile); if (m->getControl_pressed()) { out.close(); delete designMap; return 0; }
vector<string> categories = designMap->getNamesOfCategories();

if (categories.size() > 3) { m->mothurOut("\n[NOTE]: LEfSe input files allow for a class, subclass and subject. More than 3 categories can cause formatting errors.\n\n"); }
Expand Down
2 changes: 1 addition & 1 deletion source/commands/mergegroupscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int MergeGroupsCommand::execute(){

if (abort) { if (calledHelp) { return 0; } return 2; }

designMap = new DesignMap(designfile);
designMap = new DesignMap(designfile); if (m->getControl_pressed()) { delete designMap; return 0; }

if (method != "sum") {
string defaultClass = designMap->getDefaultClass();
Expand Down
2 changes: 1 addition & 1 deletion source/commands/metastatscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ int MetaStatsCommand::execute(){

if (abort) { if (calledHelp) { return 0; } return 2; }

DesignMap* designMap = new DesignMap(designfile);
DesignMap* designMap = new DesignMap(designfile); if (m->getControl_pressed()) { delete designMap; return 0; }

InputData input(inputfile, format, Groups);
set<string> processedLabels;
Expand Down
4 changes: 2 additions & 2 deletions source/commands/removegroupscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ void RemoveGroupsCommand::readDesign(){
variables["[extension]"] = util.getExtension(designfile);
string outputFileName = getOutputFileName("design", variables);

DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return ; }

vector<string> groupsToKeep;
vector<string> allGroups = designMap.getNamesGroups();
Expand Down Expand Up @@ -1091,7 +1091,7 @@ void RemoveGroupsCommand::fillNames(){
//**********************************************************************************************************************
void RemoveGroupsCommand::fillGroupsFromDesign(){
try {
DesignMap designMap(designfile);
DesignMap designMap(designfile); if (m->getControl_pressed()) { return ; }
Groups = designMap.getNamesGroups(Sets);
}
catch(exception& e) {
Expand Down
73 changes: 17 additions & 56 deletions source/datastructures/designmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,33 @@ DesignMap::DesignMap(string file) {
/************************************************************/
int DesignMap::read(string file) {
try {
ifstream in;
util.openInputFile(file, in);
namesOfCategories.clear();
indexCategoryMap.clear();
indexGroupNameMap.clear();
designMap.clear();

ifstream in; util.openInputFile(file, in);

string temp = "";
in >> temp; util.gobble(in);
string headers = util.getline(in); util.gobble(in);
vector<string> tempColumnHeaders = util.splitWhiteSpace(headers);
if (tempColumnHeaders.size() != 0) { temp = tempColumnHeaders[0]; }
else { m->setControl_pressed(true); return 0; }

vector<string> columnHeaders;
vector<string> tempColumnHeaders;
if ((temp == "group") || (temp == "Group")) {
string headers = util.getline(in); util.gobble(in);
columnHeaders = util.splitWhiteSpace(headers);
columnHeaders.insert(columnHeaders.begin(), "group");
}else {
string headers = util.getline(in); util.gobble(in);
tempColumnHeaders = util.splitWhiteSpace(headers);
int num = tempColumnHeaders.size();
columnHeaders.push_back("group");
for (int i = 0; i < num; i++) { columnHeaders.push_back("value" + toString(i)); }
for (int i = 1; i < tempColumnHeaders.size(); i++) { columnHeaders.push_back(tempColumnHeaders[i]); }
}else {
m->mothurOut("\n[ERROR]: Expected 'group' and found '" + temp + "'. Mothur expects the design file to have column headers. The first column header should be 'group'.\n\nSomething like: group\ttreatment\tmetadata.\n\nWithout the headers, mothur is unable to determine if the first row is group information or a header of the wrong name. This can result in a dummy group or missing group which will results in errors. Quitting, please correct.\n\n");
m->setControl_pressed(true); in.close(); return 0;
}

namesOfCategories.clear();
indexCategoryMap.clear();
indexGroupNameMap.clear();
designMap.clear();

map<int, string> originalGroupIndexes;
for (int i = 1; i < columnHeaders.size(); i++) { namesOfCategories.push_back(columnHeaders[i]); originalGroupIndexes[i-1] = columnHeaders[i]; }
if (columnHeaders.size() > 1) { defaultClass = columnHeaders[1]; }
else {
m->mothurOut("[ERROR]: Your design file contains only one column. Please correct.\n"); m->setControl_pressed(true);
}
else { m->mothurOut("[ERROR]: Your design file contains only one column. Please correct.\n"); m->setControl_pressed(true); }

//sort groups to keep consistent with how we store the groups in groupmap
sort(namesOfCategories.begin(), namesOfCategories.end());
Expand All @@ -64,48 +61,12 @@ int DesignMap::read(string file) {
totalCategories.resize(numCategories);
int count = 0;

//file without headers, fix it
if ((temp != "group") && (temp != "Group")){
group = temp;
util.checkGroupName(group);
if (m->getDebug()) { m->mothurOut("[DEBUG]: group = " + group + "\n"); }

//if group info, then read it
vector<string> categoryValues; categoryValues.resize(numCategories, "not found");
for (int i = 0; i < numCategories; i++) {
int thisIndex = indexCategoryMap[originalGroupIndexes[i]]; //find index of this category because we sort the values.
string temp = tempColumnHeaders[i];
util.checkGroupName(temp);
categoryValues[thisIndex] = temp;

if (m->getDebug()) { m->mothurOut("[DEBUG]: value = " + temp + "\n"); }

//do we have this value for this category already
map<string, int>::iterator it = totalCategories[thisIndex].find(temp);
if (it == totalCategories[thisIndex].end()) { totalCategories[thisIndex][temp] = 1; }
else { totalCategories[thisIndex][temp]++; }
}


map<string, int>::iterator it = indexGroupNameMap.find(group);
if (it == indexGroupNameMap.end()) {
groups.push_back(group);
indexGroupNameMap[group] = count;
designMap.push_back(categoryValues);
count++;
}else {
error = true;
m->mothurOut("[ERROR]: Your design file contains more than 1 group named " + group + ", group names must be unique. Please correct.\n");
}
}

while (!in.eof()) {

if (m->getControl_pressed()) { break; }

in >> group; util.gobble(in);
util.checkGroupName(group);
if (m->getDebug()) { m->mothurOut("[DEBUG]: group = " + group + "\n"); }
util.checkGroupName(group); if (m->getDebug()) { m->mothurOut("[DEBUG]: group = " + group + "\n"); }

//if group info, then read it
vector<string> categoryValues; categoryValues.resize(numCategories, "not found");
Expand Down

0 comments on commit c55e885

Please sign in to comment.