File: fastamap.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (197 lines) | stat: -rw-r--r-- 5,780 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 *  fastamap.cpp
 *  mothur
 *
 *  Created by Sarah Westcott on 1/16/09.
 *  Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
 *
 */

#include "fastamap.h"
#include "sequence.hpp"

/*******************************************************************************/

void FastaMap::readFastaFile(string inFileName) {
	try {
		ifstream in;
		m->openInputFile(inFileName, in);
		string name, sequence, line;
		sequence = "";
		string temp;
		map<string, string>::iterator itName;
		
		
		while(!in.eof()){
			if (m->control_pressed) { break; }
			
			Sequence currSeq(in);
			name = currSeq.getName();
			
			if (name != "") {
				if(currSeq.getIsAligned())	{	sequence = currSeq.getAligned();	}
				else						{	sequence = currSeq.getUnaligned();	}
				
				itName = seqmap.find(name);
				if (itName == seqmap.end()) { seqmap[name] = sequence;  }
				else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); }
				
				map<string,group>::iterator it = data.find(sequence);
				if (it == data.end()) { 	//it's unique.
					data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
					//				data[sequence].groupnumber = 1;
					data[sequence].names = name;
				}else { // its a duplicate.
					data[sequence].names += "," + name;
					//				data[sequence].groupnumber++;
				}	
			}
			m->gobble(in);
		}
		in.close();		
	}
	catch(exception& e) {
		m->errorOut(e, "FastaMap", "readFastaFile");
		exit(1);
	}
}

/*******************************************************************************/

void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
	
	ifstream oldNameFile;
	m->openInputFile(oldNameFileName, oldNameFile);
	
	map<string,string> oldNameMap;
	map<string, string>::iterator itName;
	string name, list;
	while(!oldNameFile.eof()){
		if (m->control_pressed) { break; }
		
		oldNameFile >> name; m->gobble(oldNameFile);
		oldNameFile >> list;
		oldNameMap[name] = list;
		m->gobble(oldNameFile);
	}
	oldNameFile.close();
	
	ifstream inFASTA;
	m->openInputFile(inFastaFile, inFASTA);
	string sequence;
	while(!inFASTA.eof()){
		if (m->control_pressed) { break; }
		
		Sequence currSeq(inFASTA);
		name = currSeq.getName();
		
		if (name != "") {
			if(currSeq.getIsAligned())	{	sequence = currSeq.getAligned();	}
			else						{	sequence = currSeq.getUnaligned();	}
			
			itName = seqmap.find(name);
			if (itName == seqmap.end()) { seqmap[name] = sequence;  }
			else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); }
			
			seqmap[name] = sequence;  
			map<string,group>::iterator it = data.find(sequence);
			if (it == data.end()) { 	//it's unique.
				data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
				//			data[sequence].groupnumber = 1;
				data[sequence].names = oldNameMap[name];
			}else { // its a duplicate.
				data[sequence].names += "," + oldNameMap[name];
				//			data[sequence].groupnumber++;
			}	
		}
		m->gobble(inFASTA);
	}
	
	
	inFASTA.close();
}

/*******************************************************************************/

string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
	return data[seq].groupname;
}

/*******************************************************************************/

string FastaMap::getNames(string seq) {	//pass a sequence get the string of names in the group separated by ','s.
	return data[seq].names;
}

/*******************************************************************************/

string FastaMap::getSequence(string name) {
	
	map<string,string>::iterator it = seqmap.find(name);
	if (it == seqmap.end()) { 	return "not found";		}
	else					{	return it->second;		}
	
}	

/*******************************************************************************/

void FastaMap::push_back(string name, string seq) {
	
	map<string,group>::iterator it = data.find(seq);
	if (it == data.end()) { 	//it's unique.
		data[seq].groupname = name;  //group name will be the name of the first duplicate sequence found.
		data[seq].names = name;
	}else { // its a duplicate.
		data[seq].names += "," + name;
	}
	seqmap[name] = seq;
}

/*******************************************************************************/

int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
	return data.size();
}

/*******************************************************************************/

void FastaMap::printNamesFile(string outFileName){ //prints data
	try {
		ofstream outFile;
		m->openOutputFile(outFileName, outFile);
		
		// two column file created with groupname and them list of identical sequence names
		for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
			if (m->control_pressed) { break; }
			outFile << it->second.groupname << '\t' << it->second.names << endl;
		}
		outFile.close();
	}
	catch(exception& e) {
		m->errorOut(e, "FastaMap", "printNamesFile");
		exit(1);
	}
}

/*******************************************************************************/

void FastaMap::printCondensedFasta(string outFileName){ //prints data
	try {
		ofstream out;
		m->openOutputFile(outFileName, out);
		//creates a fasta file
		for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
			if (m->control_pressed) { break; }
			out << ">" << it->second.groupname << endl;
			out << it->first << endl;
		}
		out.close();
	}
	catch(exception& e) {
		m->errorOut(e, "FastaMap", "printCondensedFasta");
		exit(1);
	}
}

/*******************************************************************************/