17 Read(string chr, int pos, bool strand) { this->chr = chr; this->pos = pos; this->strand = strand; }
18 Read(const Read& r) { this->chr = r.chr; this->pos = r.pos; this->strand = r.strand; }
19 Read& operator=(const Read& r) {this->chr = r.chr; this->pos = r.pos; this->strand = r.strand; return *this;}
22 typedef vector<Read> Reads;
24 void print_wig_header(char* name, char* desc) {
25 //cout << "browser position chr1" << endl;
26 cout << "browser hide all" << endl;
27 cout << "browser full altGraph" << endl;
28 cout << "track type=wig name=" << name << " description=" << desc << " visibility=full autoScale=on color=0,0,0 priority=10" << endl;
31 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; } }
33 void split (const string& text, const string& separators, vector<string>& words);
34 void read_align_file(char* filename, Reads& features);
36 int main(int argc, char** argv) {
37 if(argc != 4) { cerr << "Usage: " << argv[0] << " Aligned_reads Wiggle_Track_Name Wiggle_Track_Description" << endl; return(0); }
38 char filename[1024]; strcpy(filename,argv[1]);
41 char track_name[1024]; strcpy(track_name, argv[2]);
42 char track_desc[1024]; strcpy(track_desc, argv[3]);
44 print_wig_header(track_name, track_desc);
45 read_align_file(filename, data);
47 unsigned int n_aligned_reads = data.size();
48 cerr << n_aligned_reads/1000000.00 << "M reads\n";
51 for(Reads::iterator i = data.begin(); i != data.end();) {
52 if(Q.empty() || Q.front().chr != (*i).chr) {
53 while(!Q.empty()) { Q.pop(); }
54 cout << "variableStep chrom=" << i->chr.c_str() << endl;
57 string chr_string = i->chr;
59 while(i != data.end() && i->chr == chr_string && i->pos == posnum) {
63 while(!Q.empty() && Q.front().pos + WINDOW < Q.back().pos) {
68 cout << Q.back().pos << " " << Q.size() / (double)(n_aligned_reads/1000000.0) << endl;
73 void read_align_file(char* filename, Reads& features) {
75 string location_delim(":");
76 char strand_str[2]; strand_str[1] = '\0';
77 ifstream seqs(filename);
79 while(seqs.peek() != EOF) {
81 seqs.getline(line,2048,'\n');
83 string line_str(line);
84 vector<string> fields;
85 split(line_str, delim, fields);
86 if(fields.size() != 7) { continue; }
88 vector<string> location; split(fields[3], location_delim, location);
89 string chr = location[0];
90 if(chr == "NA") { continue; }
91 int pos = atoi(location[1].c_str());
92 bool strand = ((fields[4].c_str())[0] == 'F')?0:1;
95 Read read(chr,pos,false);
96 features.push_back(read);
98 Read read(chr,pos+25,true);
99 features.push_back(read);
104 //sort the data so we can run through it once
105 std::sort(features.begin(),features.end(),compare_reads);
106 cerr << "Reads sorted\n";
109 void split (const string& text, const string& separators, vector<string>& words) {
111 size_t n = text.length ();
112 size_t start = text.find_first_not_of (separators);
115 size_t stop = text.find_first_of (separators, start);
116 if (stop > n) stop = n;
117 words.push_back (text.substr (start, stop-start));
118 start = text.find_first_not_of (separators, stop+1);