]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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->namesOfGroups = 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->namesOfGroups = 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         groupmap[sequenceName] = groupN;
95 }
96
97 /************************************************************/
98 void GroupMap::setNamesOfGroups(string seqGroup) {
99         int i, count;
100         count = 0;
101         for (i=0; i<namesOfGroups.size(); i++) {
102                 if (namesOfGroups[i] != seqGroup) {
103                         count++; //you have not found this group
104                 }else {
105                         break; //you already have it
106                 }
107         }
108         if (count == namesOfGroups.size()) {
109                 namesOfGroups.push_back(seqGroup); //new group
110                 seqsPerGroup[seqGroup] = 0;
111                 groupIndex[seqGroup] = index;
112                 index++;
113         }
114 }
115 /************************************************************/
116 bool GroupMap::isValidGroup(string groupname) {
117         try {
118                 for (int i = 0; i < namesOfGroups.size(); i++) {
119                         if (groupname == namesOfGroups[i]) { return true; }
120                 }
121                 
122                 return false;
123         }
124         catch(exception& e) {
125                 m->errorOut(e, "GroupMap", "isValidGroup");
126                 exit(1);
127         }
128 }
129 /************************************************************/
130 int GroupMap::getNumSeqs(string group) {
131         try {
132                 
133                 map<string, int>::iterator itNum;
134                 
135                 itNum = seqsPerGroup.find(group);
136                 
137                 if (itNum == seqsPerGroup.end()) { return 0; }
138                 
139                 return seqsPerGroup[group];
140                 
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "GroupMap", "getNumSeqs");
144                 exit(1);
145         }
146 }
147
148 /************************************************************/
149 vector<string> GroupMap::getNamesSeqs(){
150         try {
151         
152                 vector<string> names;
153                 
154                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
155                         names.push_back(it->first);
156                 }
157                 
158                 return names;
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "GroupMap", "getNamesSeqs");
162                 exit(1);
163         }
164 }
165 /************************************************************/
166 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
167         try {
168                 
169                 vector<string> names;
170                 
171                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
172                         //if you are belong to one the the groups in the picked vector add you
173                         if (m->inUsersGroups(it->second, picked)) {
174                                 names.push_back(it->first);
175                         }
176                 }
177                 
178                 return names;
179         }
180         catch(exception& e) {
181                 m->errorOut(e, "GroupMap", "getNamesSeqs");
182                 exit(1);
183         }
184 }
185
186 /************************************************************/
187