-
Notifications
You must be signed in to change notification settings - Fork 51
/
abcScounts.cpp
148 lines (120 loc) · 3.38 KB
/
abcScounts.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
#include <ctype.h>
#include "shared.h"
#include "analysisFunction.h"
#include "abcScounts.h"
#include "aio.h"
typedef unsigned char uchar;
typedef struct{
unsigned int rel_pos;
uchar A:2;
uchar C:2;
uchar G:2;
uchar T:2;
}counts;
aMap readvcf(const char *fname){
gzFile gz = Z_NULL;
gz=gzopen(fname,"rb");
if(gz==Z_NULL){
fprintf(stderr,"\t-> Problem opening file: %s\n",fname);
exit(0);
}
int at=0;
char buf[1024];
aMap am;
while(gzgets(gz,buf,1024)){
char chr[1024];
int pos;
char al1,al2;
if(4!=sscanf(buf,"%s\t%d\t%c\t%c\n",chr,&pos,&al1,&al2)){
fprintf(stderr,"\t-> problem parsing line: %d which looks like: %s\n",at,buf);
}
char tmpnam[2048];
snprintf(tmpnam,2048,"%s %d",chr,pos);
aMap::iterator it=am.find(tmpnam);
if(it!=am.end()){
fprintf(stderr,"\t-> Problem with duplicate positions: %s \n",tmpnam);
// exit(0);
}
am[strdup(tmpnam)]= at++;
}
fprintf(stderr,"\t-> read nsites from vcf:%lu\n",am.size());
return am;
}
void abcScounts::printArg(FILE *argFile){
fprintf(argFile,"------------------------\n%s:\n",__FILE__);
fprintf(argFile,"-doScounts\t%d \n",doScounts);
fprintf(argFile,"-vcfname\t%s \n",vcfname);
}
void abcScounts::getOptions(argStruct *arguments){
doScounts=angsd::getArg("-doScounts",doScounts,arguments);
vcfname = angsd::getArg("-vcfname",vcfname,arguments);
if(doScounts==0){
shouldRun[index]=0;
return;
}
}
abcScounts::abcScounts(const char *outfiles,argStruct *arguments,int inputtype){
vcfname = NULL;
doScounts = 0;
outfile = NULL;
if(arguments->argc==2){
if(!strcasecmp(arguments->argv[1],"-doScounts")){
printArg(stdout);
exit(0);
}else
return;
}
getOptions(arguments);
if(doScounts==0)
return ;
printArg(arguments->argumentFile);
outfile = aio::openFileBG(outfiles,".scounts.gz");
if(!vcfname){
fprintf(stderr,"\t-> Must supply a file for screening\n");
exit(0);
}
int docounts =0;
docounts = angsd::getArg("-docounts",docounts,arguments);
if(docounts==0){
fprintf(stderr,"\t-> -doScounts requires -doCounts\n");
exit(0);
}
am = readvcf(vcfname);
}
abcScounts::~abcScounts(){
if(outfile!=NULL)
bgzf_close(outfile);
}
void abcScounts::clean(funkyPars *pars){
}
void abcScounts::print(funkyPars *pars){
if(doScounts==0)
return;
for(int s=0;s<pars->numSites;s++){
if(pars->keepSites[s]!=0){
char tmpname[1024];
sprintf(tmpname,"%s %d",header->target_name[pars->refId],pars->posi[s]+1);
aMap::iterator it = am.find(tmpname);
if(it==am.end()){
fprintf(stderr,"\t-> problem finding site: %s\n",tmpname);
continue;
}
counts cnts;
if(pars->counts[s][0]>3||pars->counts[s][1]>3||pars->counts[s][2]>3||pars->counts[s][3]>3){
fprintf(stderr,"\t-> skipping posi tmpname:%s du to depth>3\n",tmpname);
continue;
}
if(pars->counts[s][0]+pars->counts[s][1]+pars->counts[s][2]+pars->counts[3]==0)
continue;
cnts.rel_pos = it->second;
// fprintf(stderr,"realpos: %d rel_pos:%d\n",pars->posi[s]+1,it->second);
cnts.A = pars->counts[s][0];
cnts.C = pars->counts[s][1];
cnts.G = pars->counts[s][2];
cnts.T = pars->counts[s][3];
aio::doAssert(sizeof(counts)==bgzf_write(outfile,&cnts,sizeof(counts)*1),1,AT,"");
}
}
}
void abcScounts::run(funkyPars *pars){
}