Small edits to make code nicer.
authorTim Reddy Tim <treddy@hudsonalpha.org>
Fri, 12 Dec 2008 15:36:26 +0000 (15:36 +0000)
committerTim Reddy Tim <treddy@hudsonalpha.org>
Fri, 12 Dec 2008 15:36:26 +0000 (15:36 +0000)
htswanalysis/src/GetReadsInSnps/Makefile
htswanalysis/src/GetReadsInSnps/getReadsInSnps.cpp

index a5225be6ac04d660f8ebb57935c0c9c9b6de6c05..236a59dc46b26b668d5ab7c328a10c5f474ddcc5 100644 (file)
@@ -1,2 +1,4 @@
-CPPFLAGS=-g -Wall -O3 -I/opt/local/include
+CPPFLAGS=-g -Wall -O3  -I/opt/local/include
 LDFLAGS=-lgsl -lgslcblas -lm -L/opt/local/lib
+
+all: getReadsInSnps
index eb2b5dc87a40f763ebfac212e53aed9d2c04bbcf..34d7d83b4b15816931aaf3eaf44f5b7671940239 100755 (executable)
@@ -18,23 +18,32 @@ using namespace std;
 void split (const string& text, const string& separators, vector<string>& words);
 char *strrevcomp(const string& input);
 
-class Read {
+class Loci {
   public:
     string chr;
     unsigned int pos;
+
+    Loci(string chr, unsigned int pos) { this->chr = chr; this->pos = pos; }
+    Loci(const Loci& l) { this->chr = l.chr; this->pos = l.pos; }
+    Loci& operator=(const Loci& l) { this->chr = l.chr; this->pos = l.pos; return *this; }
+
+    bool operator<(const Loci& a) const { if(this->chr == a.chr) { return this->pos < a.pos; } else { return this->chr < a.chr; } }
+
+};
+
+class Read : public Loci {
+  public:
     string seq;
 
-    Read(string chr, unsigned int pos, string seq) { this->chr = chr; this->pos = pos; this->seq = seq; }
-    Read(const Read& r) { this->chr = r.chr; this->pos = r.pos; this->seq = r.seq; }
-    Read& operator=(const Read& r) {this->chr = r.chr; this->pos = r.pos; this->seq = r.seq; return *this;}
+    Read(string chr, unsigned int pos, string seq) : Loci(chr,pos) { this->seq = seq; }
+    Read(const Read& r) : Loci(r) { this->seq = r.seq; }
+    Read& operator=(const Read& r) { this->chr = r.chr; this->pos = r.pos; this->seq = r.seq; return *this;}
 };
 
-class SNP {
+class SNP : public Loci {
   public:
 
     string name;
-    string chr;
-    unsigned int pos;
     char reference_base;
     unsigned int A;
     unsigned int C;
@@ -43,18 +52,14 @@ class SNP {
     unsigned int N;
     unsigned int total;
 
-    SNP(string name, string chr, unsigned int pos, char reference_base) { 
+    SNP(string name, string chr, unsigned int pos, char reference_base) : Loci(chr,pos) 
       this->name = name; 
-      this->chr = chr; 
-      this->pos = pos; 
       this->A = 0; this->C = 0; this->G = 0; this->T = 0; this->total = 0;
       this->reference_base = reference_base; 
     }
 
-    SNP(const SNP& h) {
+    SNP(const SNP& h) : Loci(h) {
       this->name = h.name; 
-      this->chr = h.chr; 
-      this->pos = h.pos; 
       this->A = h.A; this->C = h.C; this->G = h.G; this->T = h.T; this->total = h.total;
       this->reference_base = h.reference_base; 
     }
@@ -89,12 +94,6 @@ class SNP {
     }
 };
 
-bool SNP_lt_Read(const SNP& feat, const Read& read) {
-  //First compare based on chromosomes
-  if(read.chr == feat.chr) { return feat.pos < read.pos; }
-  else { return feat.chr < read.chr; }
-}
-
 ostream &operator<<( ostream &out, const SNP &h ) {
   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;
   return out;
@@ -103,9 +102,6 @@ ostream &operator<<( ostream &out, const SNP &h ) {
 typedef vector<Read> Reads;
 typedef vector<SNP> SNPs;
 
-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; } }
-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; } }
-
 void read_snps(const char* filename, SNPs& snps) {
   string delim("\t");
 
@@ -130,7 +126,7 @@ void read_snps(const char* filename, SNPs& snps) {
   } 
 
   //sort the features so we can run through it once
-  std::stable_sort(snps.begin(),snps.end(),compare_snps);
+  std::stable_sort(snps.begin(),snps.end());
   feat.close();
 
   cerr << "Found and sorted " << snps.size() << " snps." << endl;
@@ -170,7 +166,7 @@ void read_align_file(char* filename, Reads& features) {
   seqs.close(); 
 
   //sort the data so we can run through it once
-  std::sort(features.begin(),features.end(),compare_reads);
+  std::sort(features.begin(),features.end());
   cerr << "Found and sorted " << features.size() << " reads." << endl;
 }
 
@@ -183,7 +179,7 @@ void count_read_at_snps(SNPs& snps, Reads& data) {
   for(Reads::iterator i = data.begin(); i != data.end(); ++i) {
     //skip to first feature after read
     string start_chr = snp_it->chr;
-    while(snp_it != snps.end() && SNP_lt_Read(*snp_it, *i)) {
+    while(snp_it != snps.end() && *snp_it < *i) {
       snp_it++;
     }
     
@@ -191,7 +187,6 @@ void count_read_at_snps(SNPs& snps, Reads& data) {
     if(snp_it == snps.end()) { break; }
 
     if(i->pos + read_len > snp_it->pos && i->pos <= snp_it->pos) {
-      cout << i->chr.c_str() << "\t" << i->pos << "\t" << i->seq.c_str() << endl;
       snp_it->add_read(i->seq[snp_it->pos - i->pos]);
     }
   }