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
|
//
// optimatrix.cpp
// Mothur
//
// Created by Sarah Westcott on 4/20/16.
// Copyright (c) 2016 Schloss Lab. All rights reserved.
//
#include "optimatrix.h"
#include "counttable.h"
/***********************************************************************/
OptiMatrix::OptiMatrix(vector< set<long long> > close, vector<string> name, vector<string> single, double c) : OptiData(c) {
closeness = close;
nameMap = name;
singletons = single;
}
/***********************************************************************/
OptiMatrix::OptiMatrix(string d, string nc, string f, string df, double c, bool s) : distFile(d), distFormat(df), format(f), sim(s), OptiData(c) {
if (format == "name") { namefile = nc; countfile = ""; }
else if (format == "count") { countfile = nc; namefile = ""; }
else { countfile = ""; namefile = ""; }
if (distFormat == "phylip") { readPhylip(); }
else if (distFormat == "column") { readColumn(); }
}
/***********************************************************************/
int OptiMatrix::readPhylip(){
try {
nameMap.clear();
float distance;
long long nseqs;
bool square = false;
string name;
map<long long, long long> singletonIndexSwap;
ifstream fileHandle;
string numTest;
Utils util; util.openInputFile(distFile, fileHandle);
fileHandle >> numTest >> name;
nameMap.push_back(name);
singletonIndexSwap[0] = 0;
if (!util.isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting.\n"); exit(1); }
else { convert(numTest, nseqs); }
//square test
char d;
while((d=fileHandle.get()) != EOF){
if(isalnum(d)){ square = true; fileHandle.putback(d); for(int i=0;i<nseqs;i++){ fileHandle >> distance; } break; }
if(d == '\n'){ square = false; break; }
}
vector<bool> singleton; singleton.resize(nseqs, true);
///////////////////// Read to eliminate singletons ///////////////////////
if(square){
for(long long i=1;i<nseqs;i++){
if (m->getControl_pressed()) { fileHandle.close(); return 0; }
fileHandle >> name; nameMap.push_back(name); singletonIndexSwap[i] = i;
for(long long j=0;j<i;j++){
fileHandle >> distance;
if (util.isEqual(distance,-1)) { distance = 1000000; } else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff){
singleton[i] = false;
singleton[j] = false;
}
}
}
}else{
for(long long i=1;i<nseqs;i++){
if (m->getControl_pressed()) { fileHandle.close(); return 0; }
fileHandle >> name; nameMap.push_back(name); singletonIndexSwap[i] = i;
for(long long j=0;j<nseqs;j++){
fileHandle >> distance;
if (util.isEqual(distance,-1)) { distance = 1000000; } else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff && j < i){
singleton[i] = false;
singleton[j] = false;
}
}
}
}
fileHandle.close();
//////////////////////////////////////////////////////////////////////////
long long nonSingletonCount = 0;
for (long long i = 0; i < singleton.size(); i++) {
if (!singleton[i]) { //if you are not a singleton
singletonIndexSwap[i] = nonSingletonCount;
nonSingletonCount++;
}else { singletons.push_back(nameMap[i]); }
}
singleton.clear();
closeness.resize(nonSingletonCount);
map<string, string> names;
if (namefile != "") {
util.readNames(namefile, names);
for (long long i = 0; i < singletons.size(); i++) {
singletons[i] = names[singletons[i]];
}
}
ifstream in; util.openInputFile(distFile, in);
in >> nseqs >> name;
if (namefile != "") { name = names[name]; } //redundant names
nameMap[singletonIndexSwap[0]] = name;
string line = "";
if(square){
int index = 0;
for(long long i=1;i<nseqs;i++){
if (m->getControl_pressed()) { in.close(); return 0; }
in >> name; gobble(in);
if (namefile != "") { name = names[name]; } //redundant names
nameMap[singletonIndexSwap[i]] = name;
for(long long j=0;j<i;j++){
in >> distance; gobble(in);
if (util.isEqual(distance,-1)) { distance = 1000000; } else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff){
long long newB = singletonIndexSwap[j];
long long newA = singletonIndexSwap[i];
closeness[newA].insert(newB);
closeness[newB].insert(newA);
}
index++;
}
}
}else{
long long index = nseqs;
for(long long i=0;i<nseqs;i++){ in >> distance; } gobble(in);
for(long long i=1;i<nseqs;i++){
if (m->getControl_pressed()) { in.close(); return 0; }
in >> name; gobble(in);
if (namefile != "") { name = names[name]; } //redundant names
nameMap[singletonIndexSwap[i]] = name;
for(long long j=0;j<nseqs;j++){
in >> distance; gobble(in);
if (util.isEqual(distance,-1)) { distance = 1000000; } else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff && j < i){
long long newB = singletonIndexSwap[j];
long long newA = singletonIndexSwap[i];
closeness[newA].insert(newB);
closeness[newB].insert(newA);
}
index++;
}
}
}
in.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "OptiMatrix", "readPhylip");
exit(1);
}
}
/***********************************************************************/
int OptiMatrix::readColumn(){
try {
Utils util;
map<string, long long> nameAssignment;
if (namefile != "") { util.readNames(namefile, nameAssignment); }
else {
CountTable ct; ct.readTable(countfile, false, true);
map<string, int> temp = ct.getNameMap();
for (map<string, int>::iterator it = temp.begin(); it!= temp.end(); it++) { nameAssignment[it->first] = it->second; }
}
long long count = 0;
for (map<string, long long>::iterator it = nameAssignment.begin(); it!= nameAssignment.end(); it++) {
it->second = count; count++;
nameMap.push_back(it->first);
}
string firstName, secondName;
float distance;
///////////////////// Read to eliminate singletons ///////////////////////
ifstream fileHandle;
util.openInputFile(distFile, fileHandle);
vector<bool> singleton; singleton.resize(nameAssignment.size(), true);
map<long long, long long> singletonIndexSwap;
while(fileHandle){ //let's assume it's a triangular matrix...
fileHandle >> firstName; gobble(fileHandle);
fileHandle >> secondName; gobble(fileHandle);
fileHandle >> distance; gobble(fileHandle); // get the row and column names and distance
if (m->getDebug()) { cout << firstName << '\t' << secondName << '\t' << distance << endl; }
if (m->getControl_pressed()) { fileHandle.close(); return 0; }
if (util.isEqual(distance,-1)) { distance = 1000000; }
else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff){
map<string,long long>::iterator itA = nameAssignment.find(firstName);
map<string,long long>::iterator itB = nameAssignment.find(secondName);
if(itA == nameAssignment.end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the name or count file, please correct\n"); exit(1); }
if(itB == nameAssignment.end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the name or count file, please correct\n"); exit(1); }
long long indexA = (itA->second);
long long indexB = (itB->second);
singleton[indexA] = false;
singleton[indexB] = false;
singletonIndexSwap[indexA] = indexA;
singletonIndexSwap[indexB] = indexB;
}
}
fileHandle.close();
//////////////////////////////////////////////////////////////////////////
long long nonSingletonCount = 0;
for (long long i = 0; i < singleton.size(); i++) {
if (!singleton[i]) { //if you are a singleton
singletonIndexSwap[i] = nonSingletonCount;
nonSingletonCount++;
}else { singletons.push_back(nameMap[i]); }
}
singleton.clear();
closeness.resize(nonSingletonCount);
map<string, string> names;
if (namefile != "") {
util.readNames(namefile, names);
for (long long i = 0; i < singletons.size(); i++) {
singletons[i] = names[singletons[i]];
}
}
ifstream in; util.openInputFile(distFile, in);
while(in){ //let's assume it's a triangular matrix...
in >> firstName; gobble(in);
in >> secondName; gobble(in);
in >> distance; gobble(in); // get the row and column names and distance
if (m->getDebug()) { cout << firstName << '\t' << secondName << '\t' << distance << endl; }
if (m->getControl_pressed()) { in.close(); return 0; }
if (util.isEqual(distance,-1)) { distance = 1000000; }
else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
if(distance <= cutoff){
map<string,long long>::iterator itA = nameAssignment.find(firstName);
map<string,long long>::iterator itB = nameAssignment.find(secondName);
if(itA == nameAssignment.end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the name or count file, please correct\n"); exit(1); }
if(itB == nameAssignment.end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the name or count file, please correct\n"); exit(1); }
long long indexA = (itA->second);
long long indexB = (itB->second);
long long newB = singletonIndexSwap[indexB];
long long newA = singletonIndexSwap[indexA];
closeness[newA].insert(newB);
closeness[newB].insert(newA);
if (namefile != "") {
firstName = names[firstName]; //redundant names
secondName = names[secondName]; //redundant names
}
nameMap[newA] = firstName;
nameMap[newB] = secondName;
}
}
in.close();
nameAssignment.clear();
return 1;
}
catch(exception& e) {
m->errorOut(e, "OptiMatrix", "readColumn");
exit(1);
}
}
/***********************************************************************/
|