12 #include <gsl/gsl_statistics.h>
18 void split (const string& text, const string& separators, vector<string>& words);
19 char *strrevcomp(const string& input);
27 Read(string chr, unsigned int pos, string seq) { this->chr = chr; this->pos = pos; this->seq = seq; }
28 Read(const Read& r) { this->chr = r.chr; this->pos = r.pos; this->seq = r.seq; }
29 Read& operator=(const Read& r) {this->chr = r.chr; this->pos = r.pos; this->seq = r.seq; return *this;}
46 SNP(string name, string chr, unsigned int pos, char reference_base) {
50 this->A = 0; this->C = 0; this->G = 0; this->T = 0; this->total = 0;
51 this->reference_base = reference_base;
58 this->A = h.A; this->C = h.C; this->G = h.G; this->T = h.T; this->total = h.total;
59 this->reference_base = h.reference_base;
62 SNP& operator=(const SNP& h) {
66 this->A = h.A; this->C = h.C; this->G = h.G; this->T = h.T; this->total = h.total;
67 this->reference_base = h.reference_base;
71 void add_read(char nuc) {
92 bool SNP_lt_Read(const SNP& feat, const Read& read) {
93 //First compare based on chromosomes
94 if(read.chr == feat.chr) { return feat.pos < read.pos; }
95 else { return feat.chr < read.chr; }
98 ostream &operator<<( ostream &out, const SNP &h ) {
99 out << h.name.c_str() << "\t" << h.chr.c_str() << "\t" << h.pos << "\t" << h.reference_base << "\t" << h.A << "\t" << h.C << "\t" << h.G << "\t" << h.T << "\t" << h.total;
103 typedef vector<Read> Reads;
104 typedef vector<SNP> SNPs;
106 bool compare_snps(const SNP &a, const SNP &b) { if(a.chr == b.chr) { return a.pos < b.pos; } else { return a.pos < b.pos; } }
107 bool compare_reads(const Read &a, const Read &b) { if(a.chr == b.chr) { return a.pos < b.pos; } else { return a.chr < b.chr; } }
109 void read_snps(const char* filename, SNPs& snps) {
112 ifstream feat(filename);
114 while(feat.peek() != EOF) {
116 feat.getline(line,1024,'\n');
118 string line_str(line);
119 vector<string> fields;
120 split(line_str, delim, fields);
121 if(fields.size() != 4) { cerr << "Error (" << filename << "): wrong number of fields in feature list (line " << N << " has " << fields.size() << " fields)\n"; }
123 string name = fields[0];
124 string chr = fields[1];
125 unsigned int pos = atoi(fields[2].c_str());
126 char base = (fields[3])[0];
128 SNP snp(name,chr,pos,base);
132 //sort the features so we can run through it once
133 std::stable_sort(snps.begin(),snps.end(),compare_snps);
136 cerr << "Found and sorted " << snps.size() << " snps." << endl;
139 void read_align_file(char* filename, Reads& features) {
141 string location_delim(":");
142 char strand_str[2]; strand_str[1] = '\0';
143 ifstream seqs(filename);
145 while(seqs.peek() != EOF) {
147 seqs.getline(line,2048,'\n');
149 string line_str(line);
150 vector<string> fields;
151 split(line_str, delim, fields);
152 if(fields.size() != 7) { continue; }
155 vector<string> location; split(fields[3], location_delim, location);
156 string chr = location[0];
157 if(chr == "NA") { continue; }
158 int pos = atoi(location[1].c_str());
159 bool strand = ((fields[4].c_str())[0] == 'F')?0:1;
165 seq = strrevcomp(fields[0]);
167 Read read(chr,pos,seq);
168 features.push_back(read);
172 //sort the data so we can run through it once
173 std::sort(features.begin(),features.end(),compare_reads);
174 cerr << "Found and sorted " << features.size() << " reads." << endl;
177 void count_read_at_snps(SNPs& snps, Reads& data) {
178 SNPs::iterator snp_it = snps.begin();
180 //assume, for now, that all reads have the same length
181 unsigned int read_len = data[0].seq.length();
183 for(Reads::iterator i = data.begin(); i != data.end(); ++i) {
184 //skip to first feature after read
185 string start_chr = snp_it->chr;
186 while(snp_it != snps.end() && SNP_lt_Read(*snp_it, *i)) {
190 //stop if we have run out of features.
191 if(snp_it == snps.end()) { break; }
193 if(i->pos + read_len > snp_it->pos && i->pos <= snp_it->pos) {
194 cout << i->chr.c_str() << "\t" << i->pos << "\t" << i->seq.c_str() << endl;
195 snp_it->add_read(i->seq[snp_it->pos - i->pos]);
200 int main(int argc, char** argv) {
201 if(argc != 3) { cerr << "Usage: " << argv[0] << " snp_file read_file\n"; exit(1); }
203 char snp_filename[1024]; strcpy(snp_filename,argv[1]);
204 char read_filename[1024]; strcpy(read_filename,argv[2]);
206 SNPs snps; read_snps(snp_filename, snps);
207 Reads reads; read_align_file(read_filename, reads);
209 count_read_at_snps(snps, reads);
211 for(SNPs::iterator i = snps.begin(); i != snps.end(); ++i) {
212 cout << (*i) << endl;
216 void split (const string& text, const string& separators, vector<string>& words) {
218 size_t n = text.length ();
219 size_t start = text.find_first_not_of (separators);
222 size_t stop = text.find_first_of (separators, start);
223 if (stop > n) stop = n;
224 words.push_back (text.substr (start, stop-start));
225 start = text.find_first_not_of (separators, stop+1);
229 char *strrevcomp(const string& input)
231 char* str = new char[input.length()];
232 strcpy(str,input.c_str());
239 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
245 for (p1 = str; p1 < str + strlen(str); ++p1) {
246 if(*p1 == 'a' || *p1 == 'A') { *p1 = 'T'; }
247 else if(*p1 == 'c' || *p1 == 'C') { *p1 = 'G'; }
248 else if(*p1 == 'g' || *p1 == 'G') { *p1 = 'C'; }
249 else if(*p1 == 't' || *p1 == 'T') { *p1 = 'A'; }