-
Notifications
You must be signed in to change notification settings - Fork 2
/
ACF2FASTA.cpp
163 lines (113 loc) · 3.85 KB
/
ACF2FASTA.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
#include "ACF2FASTA.h"
using namespace std;
ACF2FASTA::ACF2FASTA(){
}
ACF2FASTA::~ACF2FASTA(){
}
string ACF2FASTA::usage() const{
string usage=string("glactools")+" acf2fasta [options] <ACF file>"+
"\nThis program takes an ACF file and prints a FASTA file using the allele information\nwith one record per population. Each site generates one base pair.\n\n"+
"\tOptions\n"+
"\t\t"+"--noanc"+"\t"+"Do not print the root/anc (Default: "+boolStringify(printRoot)+" )\n"
"\t\t"+"--het"+"\t"+"Produce two fasta files containing the alleles for het sites (Default: "+boolStringify(produceTwo)+" )\n\n\n";
return usage;
}
int ACF2FASTA::run(int argc, char *argv[]){
//int main (int argc, char *argv[]) {
//all but last arg
int lastOpt=1;
for(int i=1;i<(argc);i++){
if((string(argv[i]) == "-") ){
lastOpt=i;
break;
}
if(string(argv[i])[0] != '-' ){
lastOpt=i;
break;
}
if( string(argv[i]) == "--noanc"){
printRoot=false;
continue;
}
if( string(argv[i]) == "--het"){
produceTwo=true;
continue;
}
cerr<<"Error unknown option "<<argv[i]<<endl;
return 1;
}
if(argc == 1 ||
(argc == 2 && (string(argv[1]) == "-h" || string(argv[1]) == "--help") )
){
cerr << "Usage "<<usage()<<endl;
return 1;
}
string glacfile = string(argv[lastOpt]);
GlacParser gp (glacfile);
AlleleRecords * record;
unsigned int firstIndex=0;
if(!printRoot)
firstIndex=2;
vector<string> deflines;
vector<string> sequences;
if(produceTwo){
for(unsigned int i=firstIndex;i<gp.getPopulationsNames()->size();i++){
//indFileS<<gp.getPopulationsNames()->at(i)<<"\tU\t"<<gp.getPopulationsNames()->at(i)<<endl;
deflines.push_back(gp.getPopulationsNames()->at(i)+"-1");
sequences.push_back("");
deflines.push_back(gp.getPopulationsNames()->at(i)+"-2");
sequences.push_back("");
}
}else{
for(unsigned int i=firstIndex;i<gp.getPopulationsNames()->size();i++){
//indFileS<<gp.getPopulationsNames()->at(i)<<"\tU\t"<<gp.getPopulationsNames()->at(i)<<endl;
deflines.push_back(gp.getPopulationsNames()->at(i));
sequences.push_back("");
}
// indFileS.close();
}
//unsigned int counter=0;
while(gp.hasData()){
record = gp.getData();
// // cout<<"ok"<<endl;
// cout<<*record<<endl;
// // if(!isResolvedDNA(record->alt))
// // continue;
// snpFileS<<"snp#"<<(counter++)<<"\t"<<record->chr<<"\t"<<stringify(double(record->coordinate)/double(1000000))<<"\t"<<stringify(record->coordinate)<<"\t"<<record->ref<<"\t"<<record->alt<<endl;
unsigned int firstIndex=0;
if(!printRoot)
firstIndex=2;
unsigned int indexVec=0;
if(produceTwo){
for(unsigned int i=firstIndex;i<record->vectorAlleles->size();i++){
char c = record->vectorAlleles->at(i).generateRandomAllele(record->ref,record->alt);//otherwise, a file will have the major allele and the other one the minor.
char c2;
if(record->vectorAlleles->at(i).isHeterozygous()){
sequences[ int(indexVec*2.0) ] += c;
if(record->ref == c)
c2=record->alt;
else
c2=record->ref;
sequences[ int(indexVec*2.0)+1 ] += c2;
}else{
sequences[ int(indexVec*2.0) ] += c;
sequences[ int(indexVec*2.0)+1 ] += c;
}
indexVec++;
}
}else{
for(unsigned int i=firstIndex;i<record->vectorAlleles->size();i++){
sequences[ indexVec++ ] += record->vectorAlleles->at(i).generateRandomAlleleBias(record->ref,record->alt);
}
}
// genoFileS<<endl;
}
// genoFileS.close();
// snpFileS.close();
for(unsigned int i=0;i<sequences.size();i++){
cout<<">"<<deflines[i]<<endl;
cout<<sequences[i]<<endl;
}
cerr<<"Program "<<argv[0]<<" terminated gracefully"<<endl;
return 0;
}