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