]> git.donarmstrong.com Git - mothur.git/blobdiff - kmerdb.cpp
fixes while testing 1.33.0
[mothur.git] / kmerdb.cpp
index 108b207d87c469b9f95df83921332b1bf5a756ae..9a5d23500be955b459413beee989601f6c0ec78f 100644 (file)
@@ -57,9 +57,12 @@ KmerDB::~KmerDB(){}
 
 vector<int> KmerDB::findClosestSequences(Sequence* candidateSeq, int num){
        try {
+               if (num > numSeqs) { m->mothurOut("[WARNING]: you requested " + toString(num) + " closest sequences, but the template only contains " + toString(numSeqs) + ", adjusting."); m->mothurOutEndLine(); num = numSeqs; }
+               
                vector<int> topMatches;
                Kmer kmer(kmerSize);
                searchScore = 0;
+               Scores.clear();
                
                vector<int> matches(numSeqs, 0);                                                //      a record of the sequences with shared kmers
                vector<int> timesKmerFound(kmerLocations.size()+1, 0);  //      a record of the kmers that we have already found
@@ -75,24 +78,42 @@ vector<int> KmerDB::findClosestSequences(Sequence* candidateSeq, int num){
                        }
                        timesKmerFound[kmerNumber] = 1;                                         //      ok, we've seen the kmer now
                }
-       
-               vector<seqMatch> seqMatches;
-               for(int i=0;i<numSeqs;i++){             
-                       seqMatch temp(i, matches[i]);
-                       seqMatches.push_back(temp);
-               }
                
-               //sorts putting largest matches first
-               sort(seqMatches.begin(), seqMatches.end(), compareSeqMatches);
-               
-               searchScore = seqMatches[0].match;
-               searchScore = 100 * searchScore / (float) numKmers;             //      return the Sequence object corresponding to the db
-       
-               //save top matches
-               for (int i = 0; i < num; i++) {
-                       topMatches.push_back(seqMatches[i].seq);
+               if (num != 1) {
+                       vector<seqMatch> seqMatches; seqMatches.resize(numSeqs);
+                       for(int i=0;i<numSeqs;i++){             
+                               seqMatches[i].seq = i;
+                               seqMatches[i].match = matches[i];
+                       }
+                       
+                       //sorts putting largest matches first
+                       sort(seqMatches.begin(), seqMatches.end(), compareSeqMatches);
+                       
+                       searchScore = seqMatches[0].match;
+                       searchScore = 100 * searchScore / (float) numKmers;             //      return the Sequence object corresponding to the db
+               
+                       //save top matches
+                       for (int i = 0; i < num; i++) {
+                               topMatches.push_back(seqMatches[i].seq);
+                               float thisScore = 100 * seqMatches[i].match / (float) numKmers;
+                               Scores.push_back(thisScore);
+                       }
+               }else{
+                       int bestIndex = 0;
+                       int bestMatch = -1;
+                       for(int i=0;i<numSeqs;i++){     
+                               
+                               if (matches[i] > bestMatch) {
+                                       bestIndex = i;
+                                       bestMatch = matches[i];
+                               }
+                       }
+                       
+                       searchScore = bestMatch;
+                       searchScore = 100 * searchScore / (float) numKmers;             //      return the Sequence object corresponding to the db
+                       topMatches.push_back(bestIndex);
+                       Scores.push_back(searchScore);
                }
-               
                return topMatches;              
        }
        catch(exception& e) {
@@ -196,6 +217,20 @@ int KmerDB::getCount(int kmer) {
        }       
 }
 /**************************************************************************************************/
+int KmerDB::getReversed(int kmerNumber) {
+       try {
+               Kmer kmer(kmerSize);
+               
+               if (kmerNumber < 0) { return 0; }  //if user gives negative number
+               else if (kmerNumber > maxKmer) {        return 0;       }  //or a kmer that is bigger than maxkmer
+               else {  return kmer.getReverseKmerNumber(kmerNumber);   }  // kmer is in vector range
+       }
+       catch(exception& e) {
+               m->errorOut(e, "KmerDB", "getReversed");
+               exit(1);
+       }       
+}
+/**************************************************************************************************/
 vector<int> KmerDB::getSequencesWithKmer(int kmer) {
        try {