]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
moved utilities out of mothur.h and into mothurOut class.
[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::getNumGroups() { return namesOfGroups.size();     }
51 /************************************************************/
52
53 string GroupMap::getGroup(string sequenceName) {
54                         
55         it = groupmap.find(sequenceName);
56         if (it != groupmap.end()) { //sequence name was in group file
57                 return it->second;      
58         }else {
59                 return "not found";
60         }
61 }
62
63 /************************************************************/
64 void GroupMap::setGroup(string sequenceName, string groupN) {
65         groupmap[sequenceName] = groupN;
66 }
67 /************************************************************/
68 void GroupMap::setNamesOfGroups(string seqGroup) {
69                         int i, count;
70                         count = 0;
71                         for (i=0; i<namesOfGroups.size(); i++) {
72                                 if (namesOfGroups[i] != seqGroup) {
73                                         count++; //you have not found this group
74                                 }else {
75                                         break; //you already have it
76                                 }
77                         }
78                         if (count == namesOfGroups.size()) {
79                                 namesOfGroups.push_back(seqGroup); //new group
80                                 seqsPerGroup[seqGroup] = 0;
81                                 groupIndex[seqGroup] = index;
82                                 index++;
83                         }
84 }
85 /************************************************************/
86 bool GroupMap::isValidGroup(string groupname) {
87         try {
88                 for (int i = 0; i < namesOfGroups.size(); i++) {
89                         if (groupname == namesOfGroups[i]) { return true; }
90                 }
91                 
92                 return false;
93         }
94         catch(exception& e) {
95                 m->errorOut(e, "GroupMap", "isValidGroup");
96                 exit(1);
97         }
98 }
99 /************************************************************/
100 int GroupMap::getNumSeqs(string group) {
101         try {
102                 
103                 map<string, int>::iterator itNum;
104                 
105                 itNum = seqsPerGroup.find(group);
106                 
107                 if (itNum == seqsPerGroup.end()) { return 0; }
108                 
109                 return seqsPerGroup[group];
110                 
111         }
112         catch(exception& e) {
113                 m->errorOut(e, "GroupMap", "getNumSeqs");
114                 exit(1);
115         }
116 }
117
118 /************************************************************/
119 vector<string> GroupMap::getNamesSeqs(){
120         try {
121         
122                 vector<string> names;
123                 
124                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
125                         names.push_back(it->first);
126                 }
127                 
128                 return names;
129         }
130         catch(exception& e) {
131                 m->errorOut(e, "GroupMap", "getNamesSeqs");
132                 exit(1);
133         }
134 }
135 /************************************************************/
136