1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
|
/*
* countseqscommand.cpp
* Mothur
*
* Created by westcott on 6/1/11.
* Copyright 2011 Schloss Lab. All rights reserved.
*
*/
#include "countseqscommand.h"
#include "sharedutilities.h"
#include "counttable.h"
//**********************************************************************************************************************
vector<string> CountSeqsCommand::setParameters(){
try {
CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","count",false,true,true); parameters.push_back(pname);
CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(pgroup);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
CommandParameter plarge("large", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(plarge);
CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string CountSeqsCommand::getHelpString(){
try {
string helpString = "";
helpString += "The count.seqs aka. make.table command reads a name file and outputs a .count_table file. You may also provide a group file to get the counts broken down by group.\n";
helpString += "The groups parameter allows you to indicate which groups you want to include in the counts, by default all groups in your groupfile are used.\n";
helpString += "The large parameter indicates the name and group files are too large to fit in RAM.\n";
helpString += "When you use the groups parameter and a sequence does not represent any sequences from the groups you specify it is not included in the .count.summary file.\n";
helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
helpString += "The count.seqs command should be in the following format: count.seqs(name=yourNameFile).\n";
helpString += "Example count.seqs(name=amazon.names) or make.table(name=amazon.names).\n";
helpString += "Note: No spaces between parameter labels (i.e. name), '=' and parameters (i.e.yourNameFile).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string CountSeqsCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "count") { pattern = "[filename],count_table"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
CountSeqsCommand::CountSeqsCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["count"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "CountSeqsCommand");
exit(1);
}
}
//**********************************************************************************************************************
CountSeqsCommand::CountSeqsCommand(string option) {
try {
abort = false; calledHelp = false;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//initialize outputTypes
vector<string> tempOutNames;
outputTypes["count"] = tempOutNames;
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("name");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["name"] = inputDir + it->second; }
}
it = parameters.find("group");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["group"] = inputDir + it->second; }
}
}
//check for required parameters
namefile = validParameter.validFile(parameters, "name", true);
if (namefile == "not open") { namefile = ""; abort = true; }
else if (namefile == "not found"){
namefile = m->getNameFile();
if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
}else { m->setNameFile(namefile); }
groupfile = validParameter.validFile(parameters, "group", true);
if (groupfile == "not open") { abort = true; }
else if (groupfile == "not found") { groupfile = ""; }
else { m->setGroupFile(groupfile); }
groups = validParameter.validFile(parameters, "groups", false);
if (groups == "not found") { groups = "all"; }
m->splitAtDash(groups, Groups);
string temp = validParameter.validFile(parameters, "large", false); if (temp == "not found") { temp = "F"; }
large = m->isTrue(temp);
temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
m->setProcessors(temp);
m->mothurConvert(temp, processors);
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(namefile); }
}
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "CountSeqsCommand");
exit(1);
}
}
//**********************************************************************************************************************
int CountSeqsCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
map<string, string> variables;
variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(namefile));
string outputFileName = getOutputFileName("count", variables);
int total = 0;
int start = time(NULL);
if (!large) { total = processSmall(outputFileName); }
else { total = processLarge(outputFileName); }
if (m->control_pressed) { m->mothurRemove(outputFileName); return 0; }
m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create a table for " + toString(total) + " sequences.");
m->mothurOutEndLine();
m->mothurOutEndLine();
//set rabund file as new current rabundfile
itTypes = outputTypes.find("count");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { string current = (itTypes->second)[0]; m->setCountTableFile(current); }
}
m->mothurOutEndLine();
m->mothurOut("Total number of sequences: " + toString(total)); m->mothurOutEndLine();
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
m->mothurOut(outputFileName); m->mothurOutEndLine();
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int CountSeqsCommand::processSmall(string outputFileName){
try {
ofstream out;
m->openOutputFile(outputFileName, out); outputTypes["count"].push_back(outputFileName);
outputNames.push_back(outputFileName); outputTypes["count"].push_back(outputFileName);
out << "Representative_Sequence\ttotal\t";
GroupMap* groupMap;
if (groupfile != "") {
groupMap = new GroupMap(groupfile); groupMap->readMap();
//make sure groups are valid. takes care of user setting groupNames that are invalid or setting groups=all
SharedUtil* util = new SharedUtil();
vector<string> nameGroups = groupMap->getNamesOfGroups();
util->setGroups(Groups, nameGroups);
delete util;
//sort groupNames so that the group title match the counts below, this is needed because the map object automatically sorts
sort(Groups.begin(), Groups.end());
//print groupNames
for (int i = 0; i < Groups.size(); i++) {
out << Groups[i] << '\t';
}
}
out << endl;
out.close();
int total = createProcesses(groupMap, outputFileName);
if (groupfile != "") { delete groupMap; }
return total;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "processSmall");
exit(1);
}
}
/**************************************************************************************************/
int CountSeqsCommand::createProcesses(GroupMap*& groupMap, string outputFileName) {
try {
vector<int> processIDS;
int process = 0;
vector<unsigned long long> positions;
vector<linePair> lines;
int numSeqs = 0;
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
positions = m->divideFilePerLine(namefile, processors);
for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); }
#else
if(processors == 1){ lines.push_back(linePair(0, 1000)); }
else {
int numSeqs = 0;
positions = m->setFilePosEachLine(namefile, numSeqs);
if (positions.size() < processors) { processors = positions.size(); }
//figure out how many sequences you have to process
int numSeqsPerProcessor = numSeqs / processors;
for (int i = 0; i < processors; i++) {
int startIndex = i * numSeqsPerProcessor;
if(i == (processors - 1)){ numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor; }
lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor));
}
}
#endif
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
//loop through and create all the processes you want
while (process != processors-1) {
int pid = fork();
if (pid > 0) {
processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
process++;
}else if (pid == 0){
string filename = toString(getpid()) + ".temp";
numSeqs = driver(lines[process].start, lines[process].end, filename, groupMap);
string tempFile = toString(getpid()) + ".num.temp";
ofstream outTemp;
m->openOutputFile(tempFile, outTemp);
outTemp << numSeqs << endl;
outTemp.close();
exit(0);
}else {
m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
exit(0);
}
}
string filename = toString(getpid()) + ".temp";
numSeqs = driver(lines[processors-1].start, lines[processors-1].end, filename, groupMap);
//force parent to wait until all the processes are done
for (int i=0;i<processIDS.size();i++) {
int temp = processIDS[i];
wait(&temp);
}
for (int i = 0; i < processIDS.size(); i++) {
string tempFile = toString(processIDS[i]) + ".num.temp";
ifstream intemp;
m->openInputFile(tempFile, intemp);
int num;
intemp >> num; intemp.close();
numSeqs += num;
m->mothurRemove(tempFile);
}
#else
vector<countData*> pDataArray;
DWORD dwThreadIdArray[processors-1];
HANDLE hThreadArray[processors-1];
vector<GroupMap*> copies;
//Create processor worker threads.
for( int i=0; i<processors-1; i++ ){
string filename = toString(i) + ".temp";
GroupMap* copyGroup = new GroupMap();
copyGroup->getCopy(groupMap);
copies.push_back(copyGroup);
vector<string> cGroups = Groups;
countData* temp = new countData(filename, copyGroup, m, lines[i].start, lines[i].end, groupfile, namefile, cGroups);
pDataArray.push_back(temp);
processIDS.push_back(i);
hThreadArray[i] = CreateThread(NULL, 0, MyCountThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
}
string filename = toString(processors-1) + ".temp";
numSeqs = driver(lines[processors-1].start, lines[processors-1].end, filename, groupMap);
//Wait until all threads have terminated.
WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
//Close all thread handles and free memory allocations.
for(int i=0; i < pDataArray.size(); i++){
numSeqs += pDataArray[i]->total;
delete copies[i];
CloseHandle(hThreadArray[i]);
delete pDataArray[i];
}
#endif
//append output files
for(int i=0;i<processIDS.size();i++){
m->appendFiles((toString(processIDS[i]) + ".temp"), outputFileName);
m->mothurRemove((toString(processIDS[i]) + ".temp"));
}
m->appendFiles(filename, outputFileName);
m->mothurRemove(filename);
//sanity check
if (groupfile != "") {
if (numSeqs != groupMap->getNumSeqs()) {
m->mothurOut("[ERROR]: processes reported processing " + toString(numSeqs) + " sequences, but group file indicates you have " + toString(groupMap->getNumSeqs()) + " sequences.");
if (processors == 1) { m->mothurOut(" Could you have a file mismatch?\n"); }
else { m->mothurOut(" Either you have a file mismatch or a process failed to complete the task assigned to it.\n"); m->control_pressed = true; }
}
}
return numSeqs;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "createProcesses");
exit(1);
}
}
/**************************************************************************************************/
int CountSeqsCommand::driver(unsigned long long start, unsigned long long end, string outputFileName, GroupMap*& groupMap) {
try {
ofstream out;
m->openOutputFile(outputFileName, out);
ifstream in;
m->openInputFile(namefile, in);
in.seekg(start);
bool done = false;
int total = 0;
while (!done) {
if (m->control_pressed) { break; }
string firstCol, secondCol;
in >> firstCol; m->gobble(in); in >> secondCol; m->gobble(in);
//cout << firstCol << '\t' << secondCol << endl;
m->checkName(firstCol);
m->checkName(secondCol);
//cout << firstCol << '\t' << secondCol << endl;
vector<string> names;
m->splitAtChar(secondCol, names, ',');
if (groupfile != "") {
//set to 0
map<string, int> groupCounts;
int total = 0;
for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
//get counts for each of the users groups
for (int i = 0; i < names.size(); i++) {
string group = groupMap->getGroup(names[i]);
if (group == "not found") { m->mothurOut("[ERROR]: " + names[i] + " is not in your groupfile, please correct."); m->mothurOutEndLine(); }
else {
map<string, int>::iterator it = groupCounts.find(group);
//if not found, then this sequence is not from a group we care about
if (it != groupCounts.end()) {
it->second++;
total++;
}
}
}
if (total != 0) {
out << firstCol << '\t' << total << '\t';
for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
out << it->second << '\t';
}
out << endl;
}
}else {
out << firstCol << '\t' << names.size() << endl;
}
total += names.size();
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
unsigned long long pos = in.tellg();
if ((pos == -1) || (pos >= end)) { break; }
#else
if (in.eof()) { break; }
#endif
}
in.close();
out.close();
return total;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "driver");
exit(1);
}
}
//**********************************************************************************************************************
int CountSeqsCommand::processLarge(string outputFileName){
try {
set<string> namesOfGroups;
map<string, int> initial;
for (set<string>::iterator it = namesOfGroups.begin(); it != namesOfGroups.end(); it++) { initial[(*it)] = 0; }
ofstream out;
m->openOutputFile(outputFileName, out);
outputNames.push_back(outputFileName); outputTypes["count"].push_back(outputFileName);
out << "Representative_Sequence\ttotal\t";
if (groupfile == "") { out << endl; }
map<string, unsigned long long> namesToIndex;
string outfile = m->getRootName(groupfile) + "sorted.groups.temp";
string outName = m->getRootName(namefile) + "sorted.name.temp";
map<int, string> indexToName;
map<int, string> indexToGroup;
if (groupfile != "") {
time_t estart = time(NULL);
//convert name file to redundant -> unique. set unique name equal to index so we can use vectors, save name for later.
string newNameFile = m->getRootName(namefile) + ".name.temp";
string newGroupFile = m->getRootName(groupfile) + ".group.temp";
indexToName = processNameFile(newNameFile);
indexToGroup = getGroupNames(newGroupFile, namesOfGroups);
//sort file by first column so the names of sequences will be easier to find
//use the unix sort
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
string command = "sort -n " + newGroupFile + " -o " + outfile;
system(command.c_str());
command = "sort -n " + newNameFile + " -o " + outName;
system(command.c_str());
#else //sort using windows sort
string command = "sort " + newGroupFile + " /O " + outfile;
system(command.c_str());
command = "sort " + newNameFile + " /O " + outName;
system(command.c_str());
#endif
m->mothurRemove(newNameFile);
m->mothurRemove(newGroupFile);
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort and index the group and name files. "); m->mothurOutEndLine();
}else { outName = namefile; }
time_t estart = time(NULL);
//open input file
ifstream in;
m->openInputFile(outName, in);
//open input file
ifstream in2;
int total = 0;
vector< vector<int> > nameMapCount;
if (groupfile != "") {
m->openInputFile(outfile, in2);
nameMapCount.resize(indexToName.size());
for (int i = 0; i < nameMapCount.size(); i++) {
nameMapCount[i].resize(indexToGroup.size(), 0);
}
}
while (!in.eof()) {
if (m->control_pressed) { break; }
string firstCol;
in >> firstCol; m->gobble(in);
if (groupfile != "") {
int uniqueIndex;
in >> uniqueIndex; m->gobble(in);
string name; int groupIndex;
in2 >> name >> groupIndex; m->gobble(in2);
if (name != firstCol) { m->mothurOut("[ERROR]: found " + name + " in your groupfile, but " + firstCol + " was in your namefile, please correct.\n"); m->control_pressed = true; }
nameMapCount[uniqueIndex][groupIndex]++;
total++;
}else {
string secondCol;
in >> secondCol; m->gobble(in);
int num = m->getNumNames(secondCol);
out << firstCol << '\t' << num << endl;
total += num;
}
}
in.close();
if (groupfile != "") {
m->mothurRemove(outfile);
m->mothurRemove(outName);
in2.close();
for (map<int, string>::iterator it = indexToGroup.begin(); it != indexToGroup.end(); it++) { out << it->second << '\t'; }
out << endl;
for (int i = 0; i < nameMapCount.size(); i++) {
string totalsLine = "";
int seqTotal = 0;
for (int j = 0; j < nameMapCount[i].size(); j++) {
seqTotal += nameMapCount[i][j];
totalsLine += toString(nameMapCount[i][j]) + '\t';
}
out << indexToName[i] << '\t' << seqTotal << '\t' << totalsLine << endl;
}
}
out.close();
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to create the count table file. "); m->mothurOutEndLine();
return total;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "processLarge");
exit(1);
}
}
/**************************************************************************************************/
map<int, string> CountSeqsCommand::processNameFile(string name) {
try {
map<int, string> indexToNames;
ofstream out;
m->openOutputFile(name, out);
//open input file
ifstream in;
m->openInputFile(namefile, in);
string rest = "";
char buffer[4096];
bool pairDone = false;
bool columnOne = true;
string firstCol, secondCol;
int count = 0;
while (!in.eof()) {
if (m->control_pressed) { break; }
in.read(buffer, 4096);
vector<string> pieces = m->splitWhiteSpace(rest, buffer, in.gcount());
for (int i = 0; i < pieces.size(); i++) {
if (columnOne) { firstCol = pieces[i]; columnOne=false; }
else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
if (pairDone) {
m->checkName(firstCol);
m->checkName(secondCol);
//parse names into vector
vector<string> theseNames;
m->splitAtComma(secondCol, theseNames);
for (int i = 0; i < theseNames.size(); i++) { out << theseNames[i] << '\t' << count << endl; }
indexToNames[count] = firstCol;
pairDone = false;
count++;
}
}
}
in.close();
if (rest != "") {
vector<string> pieces = m->splitWhiteSpace(rest);
for (int i = 0; i < pieces.size(); i++) {
if (columnOne) { firstCol = pieces[i]; columnOne=false; }
else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
if (pairDone) {
m->checkName(firstCol);
m->checkName(secondCol);
//parse names into vector
vector<string> theseNames;
m->splitAtComma(secondCol, theseNames);
for (int i = 0; i < theseNames.size(); i++) { out << theseNames[i] << '\t' << count << endl; }
indexToNames[count] = firstCol;
pairDone = false;
count++;
}
}
}
out.close();
return indexToNames;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "processNameFile");
exit(1);
}
}
/**************************************************************************************************/
map<int, string> CountSeqsCommand::getGroupNames(string filename, set<string>& namesOfGroups) {
try {
map<int, string> indexToGroups;
map<string, int> groupIndex;
map<string, int>::iterator it;
ofstream out;
m->openOutputFile(filename, out);
//open input file
ifstream in;
m->openInputFile(groupfile, in);
string rest = "";
char buffer[4096];
bool pairDone = false;
bool columnOne = true;
string firstCol, secondCol;
int count = 0;
while (!in.eof()) {
if (m->control_pressed) { break; }
in.read(buffer, 4096);
vector<string> pieces = m->splitWhiteSpace(rest, buffer, in.gcount());
for (int i = 0; i < pieces.size(); i++) {
if (columnOne) { firstCol = pieces[i]; columnOne=false; }
else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
if (pairDone) {
m->checkName(firstCol);
it = groupIndex.find(secondCol);
if (it == groupIndex.end()) { //add group, assigning the group and number so we can use vectors above
groupIndex[secondCol] = count;
count++;
}
out << firstCol << '\t' << groupIndex[secondCol] << endl;
namesOfGroups.insert(secondCol);
pairDone = false;
}
}
}
in.close();
if (rest != "") {
vector<string> pieces = m->splitWhiteSpace(rest);
for (int i = 0; i < pieces.size(); i++) {
if (columnOne) { firstCol = pieces[i]; columnOne=false; }
else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
if (pairDone) {
m->checkName(firstCol);
it = groupIndex.find(secondCol);
if (it == groupIndex.end()) { //add group, assigning the group and number so we can use vectors above
groupIndex[secondCol] = count;
count++;
}
out << firstCol << '\t' << groupIndex[secondCol] << endl;
namesOfGroups.insert(secondCol);
pairDone = false;
}
}
}
out.close();
for (it = groupIndex.begin(); it != groupIndex.end(); it++) { indexToGroups[it->second] = it->first; }
return indexToGroups;
}
catch(exception& e) {
m->errorOut(e, "CountSeqsCommand", "getGroupNames");
exit(1);
}
}
//**********************************************************************************************************************
|