]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
added design parameter to the indicator command
[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         m = MothurOut::getInstance();
16         groupFileName = filename;
17         m->openInputFile(filename, fileHandle);
18         index = 0;
19 }
20
21 /************************************************************/
22  GroupMap::~GroupMap(){}
23
24 /************************************************************/
25 int GroupMap::readMap() {
26                 string seqName, seqGroup;
27                 int error = 0;
28
29                 while(fileHandle){
30                         fileHandle >> seqName;  m->gobble(fileHandle);          //read from first column
31                         fileHandle >> seqGroup;                 //read from second column
32                         
33                         if (m->control_pressed) {  fileHandle.close();  return 1; }
34         
35                         setNamesOfGroups(seqGroup);
36                         
37                         it = groupmap.find(seqName);
38                         
39                         if (it != groupmap.end()) { error = 1; m->mothurOut("Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
40                         else {
41                                 groupmap[seqName] = seqGroup;   //store data in map
42                                 seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
43                         }
44                         m->gobble(fileHandle);
45                 }
46                 fileHandle.close();
47                 return error;
48 }
49 /************************************************************/
50 int GroupMap::readDesignMap() {
51                 string seqName, seqGroup;
52                 int error = 0;
53
54                 while(fileHandle){
55                         fileHandle >> seqName;  m->gobble(fileHandle);          //read from first column
56                         fileHandle >> seqGroup;                 //read from second column
57                         
58                         if (m->control_pressed) {  fileHandle.close();  return 1; }
59         
60                         setNamesOfGroups(seqGroup);
61                         
62                         it = groupmap.find(seqName);
63                         
64                         if (it != groupmap.end()) { error = 1; m->mothurOut("Your designfile contains more than 1 group named " + seqName + ", group names must be unique. Please correct."); m->mothurOutEndLine();  }
65                         else {
66                                 groupmap[seqName] = seqGroup;   //store data in map
67                                 seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
68                         }
69                         m->gobble(fileHandle);
70                 }
71                 fileHandle.close();
72                 return error;
73 }
74
75 /************************************************************/
76 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
77 /************************************************************/
78
79 string GroupMap::getGroup(string sequenceName) {
80                         
81         it = groupmap.find(sequenceName);
82         if (it != groupmap.end()) { //sequence name was in group file
83                 return it->second;      
84         }else {
85                 return "not found";
86         }
87 }
88
89 /************************************************************/
90
91 void GroupMap::setGroup(string sequenceName, string groupN) {
92         groupmap[sequenceName] = groupN;
93 }
94
95 /************************************************************/
96 void GroupMap::setNamesOfGroups(string seqGroup) {
97         int i, count;
98         count = 0;
99         for (i=0; i<namesOfGroups.size(); i++) {
100                 if (namesOfGroups[i] != seqGroup) {
101                         count++; //you have not found this group
102                 }else {
103                         break; //you already have it
104                 }
105         }
106         if (count == namesOfGroups.size()) {
107                 namesOfGroups.push_back(seqGroup); //new group
108                 seqsPerGroup[seqGroup] = 0;
109                 groupIndex[seqGroup] = index;
110                 index++;
111         }
112 }
113 /************************************************************/
114 bool GroupMap::isValidGroup(string groupname) {
115         try {
116                 for (int i = 0; i < namesOfGroups.size(); i++) {
117                         if (groupname == namesOfGroups[i]) { return true; }
118                 }
119                 
120                 return false;
121         }
122         catch(exception& e) {
123                 m->errorOut(e, "GroupMap", "isValidGroup");
124                 exit(1);
125         }
126 }
127 /************************************************************/
128 int GroupMap::getNumSeqs(string group) {
129         try {
130                 
131                 map<string, int>::iterator itNum;
132                 
133                 itNum = seqsPerGroup.find(group);
134                 
135                 if (itNum == seqsPerGroup.end()) { return 0; }
136                 
137                 return seqsPerGroup[group];
138                 
139         }
140         catch(exception& e) {
141                 m->errorOut(e, "GroupMap", "getNumSeqs");
142                 exit(1);
143         }
144 }
145
146 /************************************************************/
147 vector<string> GroupMap::getNamesSeqs(){
148         try {
149         
150                 vector<string> names;
151                 
152                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
153                         names.push_back(it->first);
154                 }
155                 
156                 return names;
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "GroupMap", "getNamesSeqs");
160                 exit(1);
161         }
162 }
163 /************************************************************/
164 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
165         try {
166                 
167                 vector<string> names;
168                 
169                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
170                         //if you are belong to one the the groups in the picked vector add you
171                         if (m->inUsersGroups(it->second, picked)) {
172                                 names.push_back(it->first);
173                         }
174                 }
175                 
176                 return names;
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "GroupMap", "getNamesSeqs");
180                 exit(1);
181         }
182 }
183
184 /************************************************************/
185