8
std::string* getGenotype(std::string coding,std::string sep)
10
std::string* Genotype = new (std::nothrow) std::string [4];
11
std::string Letter0 = coding.substr(0,1);
12
std::string Letter1 = coding.substr(1,1);
13
Genotype[0] = "0"+sep+"0";
14
Genotype[1] = Letter0+sep+Letter0;
15
Genotype[2] = Letter0+sep+Letter1;
16
Genotype[3] = Letter1+sep+Letter1;
20
SEXP export_plink(SEXP Ids, SEXP Snpdata, SEXP Nsnps, SEXP NidsTotal, SEXP Coding, SEXP From, SEXP To,
21
SEXP Male, SEXP Traits, SEXP Pedfilename, SEXP Plink, SEXP Append)
24
int from = INTEGER(From)[0];
25
int to = INTEGER(To)[0];
26
std::vector<unsigned short int> sex;
28
unsigned short int sx;
29
for(unsigned int i=(from - 1);i<to;i++) {
30
sx = INTEGER(Male)[i];
32
//Rprintf("%d %d\n",i,sx);
35
std::vector<std::string> ids;
36
for(unsigned int i=0;i<((unsigned int) length(Ids));i++)
37
ids.push_back(CHAR(STRING_ELT(Ids,i)));
39
std::vector<std::string> coding;
40
for(unsigned int i=0;i<((unsigned int) length(Coding));i++)
41
coding.push_back(CHAR(STRING_ELT(Coding,i)));
44
unsigned int nsnps = INTEGER(Nsnps)[0];
45
int nids = to - from + 1;
46
int nidsTotal = INTEGER(NidsTotal)[0];
47
int ntraits = INTEGER(Traits)[0];
48
bool append = LOGICAL(Append)[0];
49
bool plink = LOGICAL(Plink)[0];
50
std::string filename = CHAR(STRING_ELT(Pedfilename,0));
51
std::ofstream fileWoA;
53
char * snpdata = (char *) RAW(Snpdata);
55
// int gtint[nidsTotal];
56
int *gtint = new (std::nothrow) int[nidsTotal];
58
//Rprintf("nsnps=%d\n",nsnps);
59
//Rprintf("nids=%d\n",nids);
60
//Rprintf("to=%d\n", to);
61
//Rprintf("from=%d\n", from);
63
//char gtMatrix[nids][nsnps];
64
char **gtMatrix = new (std::nothrow) char*[nids];
65
for (unsigned int i=0;i<nids;i++) gtMatrix[i] = new (std::nothrow) char[nsnps];
68
std::string* Genotype;
72
//Rprintf("nsnps=%d\n",nsnps);
73
//Rprintf("nids=%d\n",nids);
75
if ((nids % 4) == 0) nbytes = nidsTotal/4; else nbytes = ceil(1.*nidsTotal/4.);
80
fileWoA.open(filename.c_str(),std::fstream::app);
82
fileWoA.open(filename.c_str(),std::fstream::trunc);
85
for (unsigned int csnp=0;csnp<nsnps;csnp++) {
87
get_snps_many(snpdata+nbytes*csnp, &nidsTotal, &ieq1, gtint);
88
for (int iii=from-1;iii<to;iii++) {
89
//Rprintf(" %d",gtint[iii]);
90
gtMatrix[iii-from+1][csnp]=gtint[iii];
95
for (int i=0;i<nids;i++) {
96
fileWoA << i+from << " " << ids[i] << " 0 0 " << sex[i];
98
for (int j=0;j<ntraits;j++) fileWoA << " " << 0;
100
for (unsigned int csnp=0;csnp<nsnps;csnp++) {
101
Genotype = getGenotype(coding[csnp],sep);
102
// figure out the coding
103
fileWoA << " " << Genotype[gtMatrix[i][csnp]];
104
//fileWoA << " x" << Letter0 << Letter1 << Genotype[0] << Genotype[1] << Genotype[2] << Genotype[3];
113
//for (int i=0;i<10;i++) Rprintf("%d ",sex[i]);
114
//Rprintf("oooo!\n" );
8
std::string* getGenotype(std::string coding, std::string sep)
10
std::string* Genotype = new (std::nothrow) std::string [4];
11
std::string Letter0 = coding.substr(0,1);
12
std::string Letter1 = coding.substr(1,1);
13
Genotype[0] = "0"+sep+"0";
14
Genotype[1] = Letter0+sep+Letter0;
15
Genotype[2] = Letter0+sep+Letter1;
16
Genotype[3] = Letter1+sep+Letter1;
20
SEXP export_plink(SEXP Ids, SEXP Snpdata, SEXP Nsnps, SEXP NidsTotal,
21
SEXP Coding, SEXP From, SEXP To, SEXP Male, SEXP Traits,
22
SEXP Pedfilename, SEXP Plink, SEXP Append)
24
int from = INTEGER(From)[0];
25
int to = INTEGER(To)[0];
28
if(from <1 || from > to) {error("The function SEXP export_plink(SEXP Ids, SEXP Snpdata, SEXP Nsnps, SEXP NidsTotal,... reports: the variable FROM should be >=1 and less then the variable TO.");} //Maksim
31
std::vector<unsigned short int> sex;
33
unsigned short int sx;
34
for(int i=(from - 1); i<to; i++) {
35
sx = INTEGER(Male)[i];
37
//Rprintf("%d %d\n",i,sx);
40
std::vector<std::string> ids;
41
for(unsigned int i=0; i<((unsigned int) length(Ids)); i++)
42
ids.push_back(CHAR(STRING_ELT(Ids,i)));
44
std::vector<std::string> coding;
45
for(unsigned int i=0; i<((unsigned int) length(Coding)); i++)
46
coding.push_back(CHAR(STRING_ELT(Coding,i)));
49
unsigned int nsnps = INTEGER(Nsnps)[0];
50
int nids = to - from + 1;
51
int nidsTotal = INTEGER(NidsTotal)[0];
52
int ntraits = INTEGER(Traits)[0];
53
bool append = LOGICAL(Append)[0];
54
bool plink = LOGICAL(Plink)[0];
55
std::string filename = CHAR(STRING_ELT(Pedfilename,0));
56
std::ofstream fileWoA;
58
char * snpdata = (char *) RAW(Snpdata);
60
// int gtint[nidsTotal];
61
int *gtint = new (std::nothrow) int[nidsTotal];
63
//Rprintf("nsnps=%d\n",nsnps);
64
//Rprintf("nids=%d\n",nids);
65
//Rprintf("to=%d\n", to);
66
//Rprintf("from=%d\n", from);
68
//char gtMatrix[nids][nsnps];
69
char **gtMatrix = new (std::nothrow) char*[nids];
70
for (int i=0; i<nids; i++) {
71
gtMatrix[i] = new (std::nothrow) char[nsnps];
75
std::string* Genotype;
79
//Rprintf("nsnps=%d\n",nsnps);
80
//Rprintf("nids=%d\n",nids);
82
if ((nids % 4) == 0) {
86
nbytes = ceil(1.*nidsTotal/4.);
92
fileWoA.open(filename.c_str(),std::fstream::app);
94
fileWoA.open(filename.c_str(),std::fstream::trunc);
97
for (unsigned int csnp=0; csnp<nsnps; csnp++) {
99
get_snps_many(snpdata+nbytes*csnp, &nidsTotal, &ieq1, gtint);
100
for (int iii=from-1; iii<to; iii++) {
101
//Rprintf(" %d",gtint[iii]);
102
gtMatrix[iii-from+1][csnp] = gtint[iii];
108
for (int i=0; i<nids; i++) {
109
fileWoA << i+from << " " << ids[i] << " 0 0 " << sex[i];
111
for (int j=0; j<ntraits; j++) fileWoA << " " << 0;
113
for (unsigned int csnp=0; csnp<nsnps; csnp++) {
114
Genotype = getGenotype(coding[csnp], sep);
115
// figure out the coding
116
fileWoA << " " << Genotype[gtMatrix[i][csnp]];
126
//for (int i=0; i<10; i++) Rprintf("%d ",sex[i]);
127
//Rprintf("oooo!\n" );
131
for(int i=0; i<nids; i++) {
132
delete [] gtMatrix[i];
124
142
SEXP export_plink_tped(SEXP Snpnames, SEXP Chromosomes, SEXP Map,
125
SEXP Snpdata, SEXP Nsnps, SEXP Nids, SEXP Coding, SEXP Pedfilename,
143
SEXP Snpdata, SEXP Nsnps, SEXP Nids, SEXP Coding,
144
SEXP Pedfilename, SEXP ExportNumeric)
128
std::vector<std::string> snpName;
129
for(unsigned int i=0;i<((unsigned int) length(Snpnames));i++)
130
snpName.push_back(CHAR(STRING_ELT(Snpnames,i)));
132
std::vector<std::string> coding;
133
for(unsigned int i=0;i<((unsigned int) length(Coding));i++)
134
coding.push_back(CHAR(STRING_ELT(Coding,i)));
136
std::vector<std::string> chromosome;
137
for(unsigned int i=0;i<((unsigned int) length(Chromosomes));i++)
138
chromosome.push_back(CHAR(STRING_ELT(Chromosomes,i)));
140
std::vector<double> position;
141
for(unsigned int i=0;i<((unsigned int) length(Map));i++)
142
position.push_back(REAL(Map)[i]);
145
unsigned int nsnps = INTEGER(Nsnps)[0];
146
int nids = INTEGER(Nids)[0];
147
bool exportNumeric = LOGICAL(ExportNumeric)[0];
148
std::string filename = CHAR(STRING_ELT(Pedfilename,0));
149
std::ofstream fileWoA;
151
char * snpdata = (char *) RAW(Snpdata);
154
int *gtint = new (std::nothrow) int[nids];
156
//Rprintf("nsnps=%d\n",nsnps);
157
//Rprintf("nids=%d\n",nids);
160
std::string* Genotype;
164
//Rprintf("nsnps=%d\n",nsnps);
165
//Rprintf("nids=%d\n",nids);
167
if ((nids % 4) == 0) nbytes = nids/4; else nbytes = ceil(1.*nids/4.);
169
fileWoA.open(filename.c_str(),std::fstream::trunc);
172
for (unsigned int csnp=0;csnp<nsnps;csnp++) {
174
get_snps_many(snpdata+nbytes*csnp, &nids, &ieq1, gtint);
175
Genotype = getGenotype(coding[csnp],sep);
176
fileWoA << chromosome[csnp] << " " << snpName[csnp] << " 0 " << (unsigned long int) position[csnp];
177
if (!exportNumeric) {
178
for (int i=0;i<nids;i++) {
179
fileWoA << " " << Genotype[gtint[i]];
182
for (int i=0;i<nids;i++) {
186
fileWoA << " " << (gtint[i]-1);
194
for (int i=0;i<nids;i++) {
195
fileWoA << i+from << " " << ids[i] << " 0 0 " << sex[i];
196
for (int j=0;j<ntraits;j++) fileWoA << " " << 0;
198
for (unsigned int csnp=0;csnp<nsnps;csnp++) {
199
Genotype = getGenotype(coding[csnp],sep);
200
// figure out the coding
201
fileWoA << " " << Genotype[gtMatrix[i][csnp]];
202
//fileWoA << " x" << Letter0 << Letter1 << Genotype[0] << Genotype[1] << Genotype[2] << Genotype[3];
212
//for (int i=0;i<10;i++) Rprintf("%d ",sex[i]);
213
//Rprintf("oooo!\n" );
146
std::vector<std::string> snpName;
147
for(unsigned int i=0; i<((unsigned int) length(Snpnames)); i++)
148
snpName.push_back(CHAR(STRING_ELT(Snpnames,i)));
150
std::vector<std::string> coding;
151
for(unsigned int i=0; i<((unsigned int) length(Coding)); i++)
152
coding.push_back(CHAR(STRING_ELT(Coding,i)));
154
std::vector<std::string> chromosome;
155
for(unsigned int i=0; i<((unsigned int) length(Chromosomes)); i++)
156
chromosome.push_back(CHAR(STRING_ELT(Chromosomes,i)));
158
std::vector<double> position;
159
for(unsigned int i=0; i<((unsigned int) length(Map)); i++)
160
position.push_back(REAL(Map)[i]);
163
unsigned int nsnps = INTEGER(Nsnps)[0];
164
int nids = INTEGER(Nids)[0];
165
bool exportNumeric = LOGICAL(ExportNumeric)[0];
166
std::string filename = CHAR(STRING_ELT(Pedfilename,0));
167
std::ofstream fileWoA;
169
char * snpdata = (char *) RAW(Snpdata);
172
int *gtint = new (std::nothrow) int[nids];
174
//Rprintf("nsnps=%d\n",nsnps);
175
//Rprintf("nids=%d\n",nids);
178
std::string* Genotype;
182
//Rprintf("nsnps=%d\n",nsnps);
183
//Rprintf("nids=%d\n",nids);
185
if ((nids % 4) == 0) {
189
nbytes = ceil(1.*nids/4.);
192
fileWoA.open(filename.c_str(), std::fstream::trunc);
195
for (unsigned int csnp=0; csnp<nsnps; csnp++) {
197
get_snps_many(snpdata+nbytes*csnp, &nids, &ieq1, gtint);
198
Genotype = getGenotype(coding[csnp], sep);
199
fileWoA << chromosome[csnp] << " " << snpName[csnp]
200
<< " 0 " << (unsigned long int) position[csnp];
202
if (!exportNumeric) {
203
for (int i=0; i<nids; i++) {
204
fileWoA << " " << Genotype[gtint[i]];
207
for (int i=0; i<nids; i++) {
211
fileWoA << " " << (gtint[i]-1);
220
for (int i=0; i<nids; i++) {
221
fileWoA << i+from << " " << ids[i] << " 0 0 " << sex[i];
222
for (int j=0; j<ntraits; j++) fileWoA << " " << 0;
224
for (unsigned int csnp=0; csnp<nsnps; csnp++) {
225
Genotype = getGenotype(coding[csnp],sep);
226
// figure out the coding
227
fileWoA << " " << Genotype[gtMatrix[i][csnp]];
228
//fileWoA << " x" << Letter0 << Letter1 << Genotype[0] << Genotype[1] << Genotype[2] << Genotype[3];
238
//for (int i=0; i<10; i++) Rprintf("%d ",sex[i]);
239
//Rprintf("oooo!\n" );
221
246
#ifdef __cplusplus