int readMatrix(Matrix matrix, int row, int col, float * value) { if ( row < 0 || row >= readRows(matrix) || col < 0 || col >= readColumns(matrix) ) return 6; *value = matrix.values[col*readRows(matrix)+ row]; return 0; }
// Write and read values MATRICE int writeMatrix(Matrix *matrix, int row, int col, float value) { if ( row < 0 || row >= readRows(*matrix) || col < 0 || col >= readColumns(*matrix) ) return 7; matrix->values[col*readRows(*matrix)+ row]=value; return 0; }
// IO functions int readFromBinFile(Matrix * matrix, int matrixToBeRead, char * file) { FILE * fp; Matrix temp; int i, errore_fine_file = 0; if ((fp = fopen(file,"rb"))!=NULL) { /* Per raggiungere la matrix da leggere bisogna passare in rassegna di tutte le precedenti, in quanto non si è a conoscenza della loro grandezza */ for(i=0; i<matrixToBeRead; i++) { fread(&temp,1,sizeof(matrix),fp); /* Una volta letta una matrix, si può determinare quante celle di memoria saltare in base alla sua dimensione */ fseek(fp, readColumns(temp)*readRows(temp)*sizeof(float),SEEK_CUR); } //Se si è alla fine del file, la matrix non esiste if (feof(fp)) { fclose(fp); return 3; } else { fread(&temp, 1, sizeof(matrix), fp); //Reinizializzo la matrix per evitare di sovrascrivere dati if (!initMatrix(matrix,readRows(temp),readColumns(temp))) //Si verifica che la matrix sia inizializzabile { //Adesso si leggono i values for(i=0; i<readRows(*matrix) && !errore_fine_file;i++) { if (fread(*(matrix->values + i),sizeof(float), readColumns(*matrix),fp)!=readColumns(*matrix)) errore_fine_file = 1; //Si controlla che vengano letti il giusto numero di dati. } if (errore_fine_file) return 4; return 0; } else return 5; } } else return 1; }
int read(Matrix *matrix) { int i,j,rows,cols; float value; do { printf("Inserire numero rows: "); scanf("%d",&rows); } while(rows<=0); do { printf("Inserire numero cols: "); scanf("%d",&cols); } while(cols<=0); initMatrix(matrix, rows, cols); for(i=0; i<readRows(*matrix); i++) for(j=0; j<readColumns(*matrix); j++) { printf("Inserisci value(%d,%d): ", i, j); scanf("%f",&value); writeMatrix(matrix, i, j, value); } return 0; }
int readMatrix(Matrix matrix, int row, int col, float * value) { if (row < 0 || row >= readRows(matrix) || col < 0 || col >= readColumns(matrix) ) return 6; *value = *(*(matrix.values + row)+col); return 0; }
// Write and read values MATRICE int writeMatrix(Matrix *matrix, int row, int col, float value) { if (row < 0 || row >= readRows(*matrix) || col < 0 || col >= readColumns(*matrix) ) return 7; *(*(matrix->values + row)+col)=value; return 0; }
void TableEditor::rowUpClicked() { if ( listRows->currentItem() <= 0 || listRows->count() < 2 ) return; int index = listRows->currentItem() - 1; QListBoxItem *i = listRows->item( listRows->currentItem() ); listRows->takeItem( i ); listRows->insertItem( i, index ); listRows->setCurrentItem( i ); readRows(); }
int writeToTxtFile(Matrix * matrix, char * file) { FILE * fp; int i,j; float matrixValue; if ((fp = fopen(file,"a"))!=NULL) { fprintf(fp,"\n%d %d ", readRows(*matrix), readColumns(*matrix)); for(i=0; i<readRows(*matrix);i++) for(j=0; j<readColumns(*matrix);j++) { readMatrix(*matrix, i, j, &matrixValue); fprintf(fp,"%f ",matrixValue); } fprintf(fp,"\n"); fclose(fp); return 0; } else return 1; }
usint readRows(const std::string& filename, std::vector<std::string>& rows, bool skip_empty_rows) { std::ifstream input(filename.c_str(), std::ios_base::binary); if(!input) { std::cerr << "readRows(): Cannot open input file " << filename << std::endl; return 0; } usint chars = readRows(input, rows, skip_empty_rows); input.close(); return chars; }
void TableEditor::deleteRowClicked() { if ( listRows->currentItem() == -1 ) return; #ifndef QT_NO_TABLE table->setNumRows( table->numRows() - 1 ); delete listRows->item( listRows->currentItem() ); readRows(); if ( listRows->firstItem() ) { listRows->setCurrentItem( listRows->firstItem() ); listRows->setSelected( listRows->firstItem(), TRUE ); } #endif }
// GUI int print(Matrix matrix) { int i,j; float matrixValue; puts("--------"); for(i=0; i<readRows(matrix); i++) { for(j=0; j<readColumns(matrix); j++) { readMatrix(matrix, i, j, &matrixValue); printf("%f ", matrixValue); } printf("\n"); } puts("--------"); return 0; }
int writeToBinFile(Matrix * matrix, char * file) { FILE * fp; int i; if ((fp = fopen(file,"ab"))!=NULL) { //Si scrive innanzitutto la struttura della matrix fwrite(matrix,sizeof(matrix),1,fp); for(i=0; i<readRows(*matrix);i++) { //Ora si scrivono i values fwrite(*(matrix->values + i),sizeof(float),readColumns(*matrix),fp); } fclose(fp); return 0; } else return 1; }
//********************************************************************************************************************** int SharedCommand::createSharedFromBiom() { try { //getting output filename string filename = biomfile; if (outputDir == "") { outputDir += util.hasPath(filename); } map<string, string> variables; variables["[filename]"] = outputDir + util.getRootName(util.getSimpleName(filename)); filename = getOutputFileName("shared",variables); outputNames.push_back(filename); outputTypes["shared"].push_back(filename); ofstream out; util.openOutputFile(filename, out); /*{ "id":"/Users/SarahsWork/Desktop/release/temp.job2.shared-unique", "format": "Biological Observation Matrix 0.9.1", "format_url": "http://biom-format.org", "type": "OTU table", "generated_by": "mothur1.24.0", "date": "Tue Apr 17 13:12:07 2012", */ ifstream in; util.openInputFile(biomfile, in); string matrixFormat = ""; int numRows = 0; int numCols = 0; int shapeNumRows = 0; int shapeNumCols = 0; vector<string> otuNames; vector<string> groupNames; map<string, string> fileLines; vector<string> names; int countOpenBrace = 0; int countClosedBrace = 0; int openParen = -1; //account for opening brace int closeParen = 0; bool ignoreCommas = false; bool atComma = false; string line = ""; string matrixElementType = ""; while (!in.eof()) { //split file by tags, so each "line" will have something like "id":"/Users/SarahsWork/Desktop/release/final.tx.1.subsample.1.pick.shared-1" if (m->getControl_pressed()) { break; } char c = in.get(); util.gobble(in); if (c == '[') { countOpenBrace++; } else if (c == ']') { countClosedBrace++; } else if (c == '{') { openParen++; } else if (c == '}') { closeParen++; } else if ((!ignoreCommas) && (c == ',')) { atComma = true; } if ((countOpenBrace != countClosedBrace) && (countOpenBrace != countClosedBrace)) { ignoreCommas = true; } else if ((countOpenBrace == countClosedBrace) && (countOpenBrace == countClosedBrace)) { ignoreCommas = false; } if (atComma && !ignoreCommas) { if (fileLines.size() == 0) { //clip first { line = line.substr(1); } string tag = getTag(line); fileLines[tag] = line; line = ""; atComma = false; ignoreCommas = false; }else { line += c; } } if (line != "") { line = line.substr(0, line.length()-1); string tag = getTag(line); fileLines[tag] = line; } in.close(); string biomType; map<string, string>::iterator it; it = fileLines.find("type"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a type provided.\n"); } else { string thisLine = it->second; biomType = getTag(thisLine); // if ((biomType != "OTU table") && (biomType != "OTUtable") && (biomType != "Taxon table") && (biomType != "Taxontable")) { m->mothurOut("[ERROR]: " + biomType + " is not a valid biom type for mothur. Only types allowed are OTU table and Taxon table.\n"); m->setControl_pressed(true); } } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } it = fileLines.find("matrix_type"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a matrix_type provided.\n"); } else { string thisLine = it->second; matrixFormat = getTag(thisLine); if ((matrixFormat != "sparse") && (matrixFormat != "dense")) { m->mothurOut("[ERROR]: " + matrixFormat + " is not a valid biom matrix_type for mothur. Types allowed are sparse and dense.\n"); m->setControl_pressed(true); } } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } it = fileLines.find("matrix_element_type"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a matrix_element_type provided.\n"); } else { string thisLine = it->second; matrixElementType = getTag(thisLine); if ((matrixElementType != "int") && (matrixElementType != "float")) { m->mothurOut("[ERROR]: " + matrixElementType + " is not a valid biom matrix_element_type for mothur. Types allowed are int and float.\n"); m->setControl_pressed(true); } if (matrixElementType == "float") { m->mothurOut("[WARNING]: the shared file only uses integers, any float values will be rounded down to the nearest integer.\n"); } } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } it = fileLines.find("rows"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a rows provided.\n"); } else { string thisLine = it->second; if ((biomType == "Taxon table") || (biomType == "Taxontable")) { string mapFilename = getOutputFileName("map",variables); outputNames.push_back(mapFilename); outputTypes["map"].push_back(mapFilename); ofstream outMap; util.openOutputFile(mapFilename, outMap); vector<string> taxonomies = readRows(thisLine, numRows); string snumBins = toString(numRows); for (int i = 0; i < numRows; i++) { //if there is a bin label use it otherwise make one string binLabel = "OTU"; string sbinNumber = toString(i+1); if (sbinNumber.length() < snumBins.length()) { int diff = snumBins.length() - sbinNumber.length(); for (int h = 0; h < diff; h++) { binLabel += "0"; } } binLabel += sbinNumber; otuNames.push_back(binLabel); outMap << otuNames[i] << '\t' << taxonomies[i] << endl; } outMap.close(); }else{ otuNames = readRows(thisLine, numRows); } } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } it = fileLines.find("columns"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a columns provided.\n"); } else { string thisLine = it->second; //read sample names groupNames = readRows(thisLine, numCols); //if users selected groups, then remove the groups not wanted. if (Groups.size() == 0) { Groups = groupNames; } else { groupNames = Groups; } //set fileroot fileroot = outputDir + util.getRootName(util.getSimpleName(biomfile)); } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } it = fileLines.find("shape"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a shape provided.\n"); } else { string thisLine = it->second; getDims(thisLine, shapeNumRows, shapeNumCols); //check shape if (shapeNumCols != numCols) { m->mothurOut("[ERROR]: shape indicates " + toString(shapeNumCols) + " columns, but I only read " + toString(numCols) + " columns.\n"); m->setControl_pressed(true); } if (shapeNumRows != numRows) { m->mothurOut("[ERROR]: shape indicates " + toString(shapeNumRows) + " rows, but I only read " + toString(numRows) + " rows.\n"); m->setControl_pressed(true); } } if (m->getControl_pressed()) { out.close(); util.mothurRemove(filename); return 0; } bool printHeaders = true; it = fileLines.find("data"); if (it == fileLines.end()) { m->mothurOut("[ERROR]: you file does not have a data provided.\n"); } else { string thisLine = it->second; //read data SharedRAbundVectors* lookup = readData(matrixFormat, thisLine, matrixElementType, groupNames, otuNames.size()); lookup->setOTUNames(otuNames); lookup->eliminateZeroOTUS(); m->mothurOutEndLine(); m->mothurOut(lookup->getLabel()+"\n"); printSharedData(lookup, out, printHeaders); } if (m->getControl_pressed()) { util.mothurRemove(filename); return 0; } return 0; } catch(exception& e) { m->errorOut(e, "SharedCommand", "createSharedFromBiom"); exit(1); } }