]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
598e301648978ab642274edb69ffa41bbea6f205
[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 int GroupMap::readMap() {
25                 string seqName, seqGroup;
26                 int error = 0;
27         
28                 while(fileHandle){
29                         fileHandle >> seqName;                  //read from first column
30                         fileHandle >> seqGroup;                 //read from second column
31                         
32                         setNamesOfGroups(seqGroup);
33                         
34                         it = groupmap.find(seqName);
35                         
36                         if (it != groupmap.end()) { error = 1; mothurOut("Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); mothurOutEndLine();  }
37                         else {
38                                 groupmap[seqName] = seqGroup;   //store data in map
39                                 seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
40                         }
41                         gobble(fileHandle);
42                 }
43                 fileHandle.close();
44                 return error;
45 }
46 /************************************************************/
47 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
48 /************************************************************/
49
50 string GroupMap::getGroup(string sequenceName) {
51                         
52         it = groupmap.find(sequenceName);
53         if (it != groupmap.end()) { //sequence name was in group file
54                 return it->second;      
55         }else {
56                 return "not found";
57         }
58 }
59
60 /************************************************************/
61 void GroupMap::setGroup(string sequenceName, string groupN) {
62         groupmap[sequenceName] = groupN;
63 }
64 /************************************************************/
65 void GroupMap::setNamesOfGroups(string seqGroup) {
66                         int i, count;
67                         count = 0;
68                         for (i=0; i<namesOfGroups.size(); i++) {
69                                 if (namesOfGroups[i] != seqGroup) {
70                                         count++; //you have not found this group
71                                 }else {
72                                         break; //you already have it
73                                 }
74                         }
75                         if (count == namesOfGroups.size()) {
76                                 namesOfGroups.push_back(seqGroup); //new group
77                                 seqsPerGroup[seqGroup] = 0;
78                                 groupIndex[seqGroup] = index;
79                                 index++;
80                         }
81 }
82 /************************************************************/
83 bool GroupMap::isValidGroup(string groupname) {
84         try {
85                 for (int i = 0; i < namesOfGroups.size(); i++) {
86                         if (groupname == namesOfGroups[i]) { return true; }
87                 }
88                 
89                 return false;
90         }
91         catch(exception& e) {
92                 errorOut(e, "GroupMap", "isValidGroup");
93                 exit(1);
94         }
95 }
96 /************************************************************/
97 int GroupMap::getNumSeqs(string group) {
98         try {
99                 
100                 map<string, int>::iterator itNum;
101                 
102                 itNum = seqsPerGroup.find(group);
103                 
104                 if (itNum == seqsPerGroup.end()) { return 0; }
105                 
106                 return seqsPerGroup[group];
107                 
108         }
109         catch(exception& e) {
110                 errorOut(e, "GroupMap", "getNumSeqs");
111                 exit(1);
112         }
113 }
114
115 /************************************************************/
116 vector<string> GroupMap::getNamesSeqs(){
117         try {
118         
119                 vector<string> names;
120                 
121                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
122                         names.push_back(it->first);
123                 }
124                 
125                 return names;
126         }
127         catch(exception& e) {
128                 errorOut(e, "GroupMap", "getNamesSeqs");
129                 exit(1);
130         }
131 }
132 /************************************************************/
133