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