forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdamage2profile.cpp
174 lines (134 loc) · 4.06 KB
/
damage2profile.cpp
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
/*
* damage2profile
* Date: Jun-08-2014
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include <iostream>
#include <fstream>
#include <gzstream.h>
#include <algorithm>
#include "utils.h"
using namespace std;
int main (int argc, char *argv[]) {
bool singleS=false;
bool doubleS=false;
int length = 5;
const string usage=string("\t"+string(argv[0])+
" [options] [file produced by damage-patterns] "+"\n\n"+
"\t\t"+"-length [length]" +"\t" +"Do not consider bases beyond this length (default "+stringify(length)+") "+"\n"+
"\t\t"+"-single" +"\t\t\t"+"Use a single strand profile (default: "+booleanAsString(singleS)+")"+"\n"+
"\t\t"+"-double" +"\t\t\t"+"Use a double strand profile (default: "+booleanAsString(doubleS)+")"+"\n"+
"");
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ){
cout<<"Usage:"<<endl;
cout<<""<<endl;
cout<<usage<<endl;
return 1;
}
for(int i=1;i<(argc-1);i++){
if(string(argv[i]) == "-length" ){
length=destringify<int>(argv[i+1]);
i++;
continue;
}
if(string(argv[i]) == "-single" ){
singleS=true;
continue;
}
if(string(argv[i]) == "-double" ){
doubleS=true;
continue;
}
cerr<<"Wrong option "<<string(argv[i])<<endl;
return 1;
}
if(doubleS && singleS){
cerr<<"Cannot specify both single and double stranded protocols"<<endl;
return 1;
}
string line;
igzstream myFile;
string filename = string(argv[argc-1]);
myFile.open(filename.c_str(), ios::in);
bool firstLine=true;
bool is5p=true;
string dnaAlphabet="ACGT";
vector<string> header;
for(int nuc1=0;nuc1<4;nuc1++){
for(int nuc2=0;nuc2<4;nuc2++){
if(nuc1==nuc2){ // prob of error is 0 if both nucleotides are identical
}else{ // rely on the substitution frequency
//cout<<dnaAlphabet[nuc1]<<endl;
string s1= stringify(dnaAlphabet[nuc1]);
string s2= stringify(dnaAlphabet[nuc2]);
string toadd = s1+">"+s2;
header.push_back(toadd);
}
}
}
cout<<vectorToString(header,"\t")<<endl;
vector<string> stacksStrings;
if (myFile.good()){
getline (myFile,line);//header
while ( getline (myFile,line)){
//cout<<line<<endl;
vector<string> fields = allTokens(line,'\t');
if(firstLine){
is5p=(fields[0]=="0");
firstLine=false;
}
int pos = destringify<int>(fields[0]);
if(is5p){
if(pos>=length)
break;
vector<double> toprint;
for(unsigned int i=1;i<fields.size();i++){
if(i==6){ //C->T regardless of protocol
vector<string> fields2 = allTokens(fields[i],' ');
toprint.push_back(destringify<double>(fields2[0]));
}else{
toprint.push_back(0.0);
}
}
//cout<<vectorToString(toprint,"\t")<<endl;
stacksStrings.push_back( vectorToString(toprint,"\t") );
}else{
// cout<<(-1.0*pos)<<endl;
if( (-1.0*pos)>=length)
continue;
vector<double> toprint;
for(unsigned int i=1;i<fields.size();i++){
if(singleS){ //single strand
if(i==6){ //C->T
vector<string> fields2 = allTokens(fields[i],' ');
toprint.push_back(destringify<double>(fields2[0]));
}else{
toprint.push_back(0.0);
}
}else{ //double
if(i==7){ //G->A
vector<string> fields2 = allTokens(fields[i],' ');
toprint.push_back(destringify<double>(fields2[0]));
}else{
toprint.push_back(0.0);
}
}
}
//cout<<vectorToString(toprint,"\t")<<endl;
stacksStrings.push_back( vectorToString(toprint,"\t") );
}
}
myFile.close();
}else{
cerr << "Unable to open file "<<filename<<endl;
return 1;
}
if(!is5p)
reverse(stacksStrings.begin(),stacksStrings.end());
cout<<vectorToString(stacksStrings,"\n")<<endl;
return 0;
}