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
|
/*
* seqcoordcommand.cpp
* Mothur
*
* Created by Pat Schloss on 5/30/09.
* Copyright 2009 Patrick D. Schloss. All rights reserved.
*
*/
#include "seqsummarycommand.h"
#include "counttable.h"
//**********************************************************************************************************************
vector<string> SeqSummaryCommand::setParameters(){
try {
CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","summary",false,true,true); parameters.push_back(pfasta);
CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","",false,false,true); parameters.push_back(pname);
CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","",false,false,true); parameters.push_back(pcount);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
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, "SeqSummaryCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string SeqSummaryCommand::getHelpString(){
try {
string helpString = "";
helpString += "The summary.seqs command reads a fastafile and summarizes the sequences.\n";
helpString += "The summary.seqs command parameters are fasta, name, count and processors, fasta is required, unless you have a valid current fasta file.\n";
helpString += "The name parameter allows you to enter a name file associated with your fasta file. \n";
helpString += "The count parameter allows you to enter a count file associated with your fasta file. \n";
helpString += "The summary.seqs command should be in the following format: \n";
helpString += "summary.seqs(fasta=yourFastaFile, processors=2) \n";
helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string SeqSummaryCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "summary") { pattern = "[filename],summary"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
SeqSummaryCommand::SeqSummaryCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
exit(1);
}
}
//***************************************************************************************************************
SeqSummaryCommand::SeqSummaryCommand(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("summary.seqs");
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//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("fasta");
//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["fasta"] = inputDir + it->second; }
}
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("count");
//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["count"] = inputDir + it->second; }
}
}
//initialize outputTypes
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
//check for required parameters
fastafile = validParameter.validFile(parameters, "fasta", true);
if (fastafile == "not open") { abort = true; }
else if (fastafile == "not found") {
fastafile = m->getFastaFile();
if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
}else { m->setFastaFile(fastafile); }
namefile = validParameter.validFile(parameters, "name", true);
if (namefile == "not open") { namefile = ""; abort = true; }
else if (namefile == "not found") { namefile = ""; }
else { m->setNameFile(namefile); }
countfile = validParameter.validFile(parameters, "count", true);
if (countfile == "not open") { abort = true; countfile = ""; }
else if (countfile == "not found") { countfile = ""; }
else { m->setCountTableFile(countfile); }
if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
//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 = "";
outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
}
string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
m->setProcessors(temp);
m->mothurConvert(temp, processors);
if (countfile == "") {
if (namefile == "") {
vector<string> files; files.push_back(fastafile);
parser.getNameFile(files);
}
}
}
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
exit(1);
}
}
//***************************************************************************************************************
int SeqSummaryCommand::execute(){
try{
if (abort == true) { if (calledHelp) { return 0; } return 2; }
//set current fasta to fastafile
m->setFastaFile(fastafile);
map<string, string> variables;
variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
string summaryFile = getOutputFileName("summary",variables);
int numSeqs = 0;
vector<int> startPosition;
vector<int> endPosition;
vector<int> seqLength;
vector<int> ambigBases;
vector<int> longHomoPolymer;
if (namefile != "") { nameMap = m->readNames(namefile); }
else if (countfile != "") {
CountTable ct;
ct.readTable(countfile, false, false);
nameMap = ct.getNameMap();
}
if (m->control_pressed) { return 0; }
#ifdef USE_MPI
int pid, numSeqsPerProcessor;
int tag = 2001;
int startTag = 1; int endTag = 2; int lengthTag = 3; int baseTag = 4; int lhomoTag = 5;
int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
vector<unsigned long long> MPIPos;
MPI_Status status;
MPI_Status statusOut;
MPI_File inMPI;
MPI_File outMPI;
MPI_Comm_size(MPI_COMM_WORLD, &processors);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
char tempFileName[1024];
strcpy(tempFileName, fastafile.c_str());
char sumFileName[1024];
strcpy(sumFileName, summaryFile.c_str());
MPI_File_open(MPI_COMM_WORLD, tempFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
MPI_File_open(MPI_COMM_WORLD, sumFileName, outMode, MPI_INFO_NULL, &outMPI);
if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); return 0; }
if (pid == 0) { //you are the root process
//print header
string outputString = "seqname\tstart\tend\tnbases\tambigs\tpolymer\tnumSeqs\n";
int length = outputString.length();
char* buf2 = new char[length];
memcpy(buf2, outputString.c_str(), length);
MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &statusOut);
delete buf2;
MPIPos = m->setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs
for(int i = 1; i < processors; i++) {
MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
}
//figure out how many sequences you have to do
numSeqsPerProcessor = numSeqs / processors;
int startIndex = pid * numSeqsPerProcessor;
if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
//do your part
MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
}else { //i am the child process
MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
MPIPos.resize(numSeqs+1);
MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
//figure out how many sequences you have to align
numSeqsPerProcessor = numSeqs / processors;
int startIndex = pid * numSeqsPerProcessor;
if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
//do your part
MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
}
MPI_File_close(&inMPI);
MPI_File_close(&outMPI);
MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
if (pid == 0) {
//get the info from the child processes
for(int i = 1; i < processors; i++) {
int size;
MPI_Recv(&size, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
vector<int> temp; temp.resize(size+1);
for(int j = 0; j < 5; j++) {
MPI_Recv(&temp[0], (size+1), MPI_INT, i, 2001, MPI_COMM_WORLD, &status);
int receiveTag = temp[temp.size()-1]; //child process added a int to the end to indicate what count this is for
if (receiveTag == startTag) {
for (int k = 0; k < size; k++) { startPosition.push_back(temp[k]); }
}else if (receiveTag == endTag) {
for (int k = 0; k < size; k++) { endPosition.push_back(temp[k]); }
}else if (receiveTag == lengthTag) {
for (int k = 0; k < size; k++) { seqLength.push_back(temp[k]); }
}else if (receiveTag == baseTag) {
for (int k = 0; k < size; k++) { ambigBases.push_back(temp[k]); }
}else if (receiveTag == lhomoTag) {
for (int k = 0; k < size; k++) { longHomoPolymer.push_back(temp[k]); }
}
}
}
}else{
//send my counts
int size = startPosition.size();
MPI_Send(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
startPosition.push_back(startTag);
int ierr = MPI_Send(&(startPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
endPosition.push_back(endTag);
ierr = MPI_Send (&(endPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
seqLength.push_back(lengthTag);
ierr = MPI_Send(&(seqLength[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
ambigBases.push_back(baseTag);
ierr = MPI_Send(&(ambigBases[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
longHomoPolymer.push_back(lhomoTag);
ierr = MPI_Send(&(longHomoPolymer[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
#else
vector<unsigned long long> positions;
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
positions = m->divideFile(fastafile, processors);
for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(new linePair(positions[i], positions[(i+1)])); }
#else
positions = m->setFilePosFasta(fastafile, 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(new linePair(positions[startIndex], numSeqsPerProcessor));
}
#endif
if(processors == 1){
numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]);
}else{
numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile);
}
if (m->control_pressed) { return 0; }
#endif
#ifdef USE_MPI
if (pid == 0) {
#endif
sort(startPosition.begin(), startPosition.end());
sort(endPosition.begin(), endPosition.end());
sort(seqLength.begin(), seqLength.end());
sort(ambigBases.begin(), ambigBases.end());
sort(longHomoPolymer.begin(), longHomoPolymer.end());
int size = startPosition.size();
//find means
unsigned long long meanStartPosition, meanEndPosition, meanSeqLength, meanAmbigBases, meanLongHomoPolymer;
meanStartPosition = 0; meanEndPosition = 0; meanSeqLength = 0; meanAmbigBases = 0; meanLongHomoPolymer = 0;
for (int i = 0; i < size; i++) {
meanStartPosition += startPosition[i];
meanEndPosition += endPosition[i];
meanSeqLength += seqLength[i];
meanAmbigBases += ambigBases[i];
meanLongHomoPolymer += longHomoPolymer[i];
}
double meanstartPosition, meanendPosition, meanseqLength, meanambigBases, meanlongHomoPolymer;
meanstartPosition = meanStartPosition / (double) size; meanendPosition = meanEndPosition /(double) size; meanlongHomoPolymer = meanLongHomoPolymer / (double) size; meanseqLength = meanSeqLength / (double) size; meanambigBases = meanAmbigBases /(double) size;
int ptile0_25 = int(size * 0.025);
int ptile25 = int(size * 0.250);
int ptile50 = int(size * 0.500);
int ptile75 = int(size * 0.750);
int ptile97_5 = int(size * 0.975);
int ptile100 = size - 1;
//to compensate for blank sequences that would result in startPosition and endPostion equalling -1
if (startPosition[0] == -1) { startPosition[0] = 0; }
if (endPosition[0] == -1) { endPosition[0] = 0; }
if (m->control_pressed) { m->mothurRemove(summaryFile); return 0; }
m->mothurOutEndLine();
m->mothurOut("\t\tStart\tEnd\tNBases\tAmbigs\tPolymer\tNumSeqs"); m->mothurOutEndLine();
m->mothurOut("Minimum:\t" + toString(startPosition[0]) + "\t" + toString(endPosition[0]) + "\t" + toString(seqLength[0]) + "\t" + toString(ambigBases[0]) + "\t" + toString(longHomoPolymer[0]) + "\t" + toString(1)); m->mothurOutEndLine();
m->mothurOut("2.5%-tile:\t" + toString(startPosition[ptile0_25]) + "\t" + toString(endPosition[ptile0_25]) + "\t" + toString(seqLength[ptile0_25]) + "\t" + toString(ambigBases[ptile0_25]) + "\t"+ toString(longHomoPolymer[ptile0_25]) + "\t" + toString(ptile0_25+1)); m->mothurOutEndLine();
m->mothurOut("25%-tile:\t" + toString(startPosition[ptile25]) + "\t" + toString(endPosition[ptile25]) + "\t" + toString(seqLength[ptile25]) + "\t" + toString(ambigBases[ptile25]) + "\t" + toString(longHomoPolymer[ptile25]) + "\t" + toString(ptile25+1)); m->mothurOutEndLine();
m->mothurOut("Median: \t" + toString(startPosition[ptile50]) + "\t" + toString(endPosition[ptile50]) + "\t" + toString(seqLength[ptile50]) + "\t" + toString(ambigBases[ptile50]) + "\t" + toString(longHomoPolymer[ptile50]) + "\t" + toString(ptile50+1)); m->mothurOutEndLine();
m->mothurOut("75%-tile:\t" + toString(startPosition[ptile75]) + "\t" + toString(endPosition[ptile75]) + "\t" + toString(seqLength[ptile75]) + "\t" + toString(ambigBases[ptile75]) + "\t" + toString(longHomoPolymer[ptile75]) + "\t" + toString(ptile75+1)); m->mothurOutEndLine();
m->mothurOut("97.5%-tile:\t" + toString(startPosition[ptile97_5]) + "\t" + toString(endPosition[ptile97_5]) + "\t" + toString(seqLength[ptile97_5]) + "\t" + toString(ambigBases[ptile97_5]) + "\t" + toString(longHomoPolymer[ptile97_5]) + "\t" + toString(ptile97_5+1)); m->mothurOutEndLine();
m->mothurOut("Maximum:\t" + toString(startPosition[ptile100]) + "\t" + toString(endPosition[ptile100]) + "\t" + toString(seqLength[ptile100]) + "\t" + toString(ambigBases[ptile100]) + "\t" + toString(longHomoPolymer[ptile100]) + "\t" + toString(ptile100+1)); m->mothurOutEndLine();
m->mothurOut("Mean:\t" + toString(meanstartPosition) + "\t" + toString(meanendPosition) + "\t" + toString(meanseqLength) + "\t" + toString(meanambigBases) + "\t" + toString(meanlongHomoPolymer)); m->mothurOutEndLine();
if ((namefile == "") && (countfile == "")) { m->mothurOut("# of Seqs:\t" + toString(numSeqs)); m->mothurOutEndLine(); }
else { m->mothurOut("# of unique seqs:\t" + toString(numSeqs)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(startPosition.size())); m->mothurOutEndLine(); }
if (m->control_pressed) { m->mothurRemove(summaryFile); return 0; }
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
m->mothurOutEndLine();
#ifdef USE_MPI
}
#endif
//set fasta file as new current fastafile
string current = "";
itTypes = outputTypes.find("summary");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSummaryFile(current); }
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "execute");
exit(1);
}
}
/**************************************************************************************/
int SeqSummaryCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile, linePair* filePos) {
try {
ofstream outSummary;
m->openOutputFile(sumFile, outSummary);
//print header if you are process 0
if (filePos->start == 0) {
outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer\tnumSeqs" << endl;
}
ifstream in;
m->openInputFile(filename, in);
in.seekg(filePos->start);
bool done = false;
int count = 0;
while (!done) {
if (m->control_pressed) { in.close(); outSummary.close(); return 1; }
if (m->debug) { m->mothurOut("[DEBUG]: count = " + toString(count) + "\n"); }
Sequence current(in); m->gobble(in);
if (current.getName() != "") {
if (m->debug) { m->mothurOut("[DEBUG]: " + current.getName() + '\t' + toString(current.getNumBases()) + "\n"); }
int num = 1;
if ((namefile != "") || (countfile != "")) {
//make sure this sequence is in the namefile, else error
map<string, int>::iterator it = nameMap.find(current.getName());
if (it == nameMap.end()) { m->mothurOut("[ERROR]: '" + current.getName() + "' is not in your name or count file, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
else { num = it->second; }
}
//for each sequence this sequence represents
for (int i = 0; i < num; i++) {
startPosition.push_back(current.getStartPos());
endPosition.push_back(current.getEndPos());
seqLength.push_back(current.getNumBases());
ambigBases.push_back(current.getAmbigBases());
longHomoPolymer.push_back(current.getLongHomoPolymer());
}
count++;
outSummary << current.getName() << '\t';
outSummary << current.getStartPos() << '\t' << current.getEndPos() << '\t';
outSummary << current.getNumBases() << '\t' << current.getAmbigBases() << '\t';
outSummary << current.getLongHomoPolymer() << '\t' << num << endl;
if (m->debug) { m->mothurOut("[DEBUG]: " + current.getName() + '\t' + toString(current.getNumBases()) + "\n"); }
}
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
unsigned long long pos = in.tellg();
if ((pos == -1) || (pos >= filePos->end)) { break; }
#else
if (in.eof()) { break; }
#endif
}
in.close();
return count;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "driverCreateSummary");
exit(1);
}
}
#ifdef USE_MPI
/**************************************************************************************/
int SeqSummaryCommand::MPICreateSummary(int start, int num, vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long long>& MPIPos) {
try {
int pid;
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
for(int i=0;i<num;i++){
if (m->control_pressed) { return 0; }
//read next sequence
int length = MPIPos[start+i+1] - MPIPos[start+i];
char* buf4 = new char[length];
MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
string tempBuf = buf4;
if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
istringstream iss (tempBuf,istringstream::in);
delete buf4;
Sequence current(iss);
if (current.getName() != "") {
int num = 1;
if ((namefile != "") || (countfile != "")) {
//make sure this sequence is in the namefile, else error
map<string, int>::iterator it = nameMap.find(current.getName());
if (it == nameMap.end()) { cout << "[ERROR]: " << current.getName() << " is not in your name or count file, please correct." << endl; m->control_pressed = true; }
else { num = it->second; }
}
//for each sequence this sequence represents
for (int j = 0; j < num; j++) {
startPosition.push_back(current.getStartPos());
endPosition.push_back(current.getEndPos());
seqLength.push_back(current.getNumBases());
ambigBases.push_back(current.getAmbigBases());
longHomoPolymer.push_back(current.getLongHomoPolymer());
}
string outputString = current.getName() + "\t" + toString(current.getStartPos()) + "\t" + toString(current.getEndPos()) + "\t";
outputString += toString(current.getNumBases()) + "\t" + toString(current.getAmbigBases()) + "\t" + toString(current.getLongHomoPolymer()) + "\t" + toString(num) + "\n";
//output to file
length = outputString.length();
char* buf3 = new char[length];
memcpy(buf3, outputString.c_str(), length);
MPI_File_write_shared(outMPI, buf3, length, MPI_CHAR, &status);
delete buf3;
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "MPICreateSummary");
exit(1);
}
}
#endif
/**************************************************************************************************/
int SeqSummaryCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile) {
try {
int process = 1;
int num = 0;
processIDS.clear();
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
//loop through and create all the processes you want
while (process != processors) {
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){
num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile + toString(getpid()) + ".temp", lines[process]);
//pass numSeqs to parent
ofstream out;
string tempFile = fastafile + toString(getpid()) + ".num.temp";
m->openOutputFile(tempFile, out);
out << num << endl;
out << startPosition.size() << endl;
for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
out.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);
}
}
//do your part
num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile, lines[0]);
//force parent to wait until all the processes are done
for (int i=0;i<processIDS.size();i++) {
int temp = processIDS[i];
wait(&temp);
}
//parent reads in and combine Filter info
for (int i = 0; i < processIDS.size(); i++) {
string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
ifstream in;
m->openInputFile(tempFilename, in);
int temp, tempNum;
in >> tempNum; m->gobble(in); num += tempNum;
in >> tempNum; m->gobble(in);
for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
in.close();
m->mothurRemove(tempFilename);
m->appendFiles((sumFile + toString(processIDS[i]) + ".temp"), sumFile);
m->mothurRemove((sumFile + toString(processIDS[i]) + ".temp"));
}
#else
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Windows version shared memory, so be careful when passing variables through the seqSumData struct.
//Above fork() will clone, so memory is separate, but that's not the case with windows,
//Taking advantage of shared memory to allow both threads to add info to vectors.
//////////////////////////////////////////////////////////////////////////////////////////////////////
vector<seqSumData*> pDataArray;
DWORD dwThreadIdArray[processors-1];
HANDLE hThreadArray[processors-1];
bool hasNameMap = false;
if ((namefile !="") || (countfile != "")) { hasNameMap = true; }
//Create processor worker threads.
for( int i=0; i<processors-1; i++ ){
string extension = "";
if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
// Allocate memory for thread data.
seqSumData* tempSum = new seqSumData(filename, (sumFile+extension), m, lines[i]->start, lines[i]->end, hasNameMap, nameMap);
pDataArray.push_back(tempSum);
//MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
//default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
hThreadArray[i] = CreateThread(NULL, 0, MySeqSumThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
}
//do your part
num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, (sumFile+toString(processors-1)+".temp"), lines[processors-1]);
processIDS.push_back(processors-1);
//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++){
num += pDataArray[i]->count;
if (pDataArray[i]->count != pDataArray[i]->end) {
m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of " + toString(pDataArray[i]->end) + " sequences assigned to it, quitting. \n"); m->control_pressed = true;
}
for (int k = 0; k < pDataArray[i]->startPosition.size(); k++) { startPosition.push_back(pDataArray[i]->startPosition[k]); }
for (int k = 0; k < pDataArray[i]->endPosition.size(); k++) { endPosition.push_back(pDataArray[i]->endPosition[k]); }
for (int k = 0; k < pDataArray[i]->seqLength.size(); k++) { seqLength.push_back(pDataArray[i]->seqLength[k]); }
for (int k = 0; k < pDataArray[i]->ambigBases.size(); k++) { ambigBases.push_back(pDataArray[i]->ambigBases[k]); }
for (int k = 0; k < pDataArray[i]->longHomoPolymer.size(); k++) { longHomoPolymer.push_back(pDataArray[i]->longHomoPolymer[k]); }
CloseHandle(hThreadArray[i]);
delete pDataArray[i];
}
//append files
for(int i=0;i<processIDS.size();i++){
m->appendFiles((sumFile + toString(processIDS[i]) + ".temp"), sumFile);
m->mothurRemove((sumFile + toString(processIDS[i]) + ".temp"));
}
#endif
return num;
}
catch(exception& e) {
m->errorOut(e, "SeqSummaryCommand", "createProcessesCreateSummary");
exit(1);
}
}
/**********************************************************************************************************************/
|