]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
b627a6bda6e3fa63de073a4b3f6c20bee7a2a680
[mothur.git] / groupmap.cpp
1 /*
2  *  groupmap.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/1/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "groupmap.h"
11
12 /************************************************************/
13
14  GroupMap::GroupMap(string filename) {
15         groupFileName = filename;
16         openInputFile(filename, fileHandle);
17         index = 0;
18 }
19
20 /************************************************************/
21  GroupMap::~GroupMap(){}
22
23 /************************************************************/
24 void GroupMap::readMap() {
25                 string seqName, seqGroup;
26         
27                 while(fileHandle){
28                         fileHandle >> seqName;                  //read from first column
29                         fileHandle >> seqGroup;                 //read from second column
30                         
31                         setNamesOfGroups(seqGroup);
32                         
33                         groupmap[seqName] = seqGroup;   //store data in map
34                         seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
35                 
36                         gobble(fileHandle);
37                 }
38                 fileHandle.close();
39 }
40 /************************************************************/
41 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
42 /************************************************************/
43
44 string GroupMap::getGroup(string sequenceName) {
45                         
46         it = groupmap.find(sequenceName);
47         if (it != groupmap.end()) { //sequence name was in group file
48                 return it->second;      
49         }else {
50                 return "not found";
51         }
52 }
53
54 /************************************************************/
55 void GroupMap::setGroup(string sequenceName, string groupN) {
56         groupmap[sequenceName] = groupN;
57 }
58 /************************************************************/
59 void GroupMap::setNamesOfGroups(string seqGroup) {
60                         int i, count;
61                         count = 0;
62                         for (i=0; i<namesOfGroups.size(); i++) {
63                                 if (namesOfGroups[i] != seqGroup) {
64                                         count++; //you have not found this group
65                                 }else {
66                                         break; //you already have it
67                                 }
68                         }
69                         if (count == namesOfGroups.size()) {
70                                 namesOfGroups.push_back(seqGroup); //new group
71                                 seqsPerGroup[seqGroup] = 0;
72                                 groupIndex[seqGroup] = index;
73                                 index++;
74                         }
75 }
76 /************************************************************/
77 bool GroupMap::isValidGroup(string groupname) {
78         try {
79                 for (int i = 0; i < namesOfGroups.size(); i++) {
80                         if (groupname == namesOfGroups[i]) { return true; }
81                 }
82                 
83                 return false;
84         }
85         catch(exception& e) {
86                 errorOut(e, "GroupMap", "isValidGroup");
87                 exit(1);
88         }
89 }
90 /************************************************************/
91 int GroupMap::getNumSeqs(string group) {
92         try {
93                 
94                 map<string, int>::iterator itNum;
95                 
96                 itNum = seqsPerGroup.find(group);
97                 
98                 if (itNum == seqsPerGroup.end()) { return 0; }
99                 
100                 return seqsPerGroup[group];
101                 
102         }
103         catch(exception& e) {
104                 errorOut(e, "GroupMap", "getNumSeqs");
105                 exit(1);
106         }
107 }
108
109 /************************************************************/
110 vector<string> GroupMap::getNamesSeqs(){
111         try {
112         
113                 vector<string> names;
114                 
115                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
116                         names.push_back(it->first);
117                 }
118                 
119                 return names;
120         }
121         catch(exception& e) {
122                 errorOut(e, "GroupMap", "getNamesSeqs");
123                 exit(1);
124         }
125 }
126 /************************************************************/
127