forked from ANGSD/angsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcTemplate2.cpp
181 lines (130 loc) · 4.86 KB
/
abcTemplate2.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
174
175
176
177
178
179
/*
generally angsd will send the data for a genomic region through all analysis classes (abc*).
the data is encapsulated in a 'funkyPars' struct (found in shared.h)
For each abcClass it will do
1) ::run
2) ::print
3) ::clean
below are some toy example that will show
a) how the class system works ()
b) how to access the internal datastructure
-doTemplate2 1:
Will simply count the number of A, C, G, T, N's for both strand
We will put all analysis in the ::print function. therefore it will NOT be threaded
the raw analysis should be put into ::run (this is threaded in angsd)
but we wont do this in case of simplicity
Run command examples:
---------------------
./angsd -i YanaRef.bam -doTemplate 2 -r 3:100000-50000000 -ref ../ProbabilisticAncientDNA/HumanGenomeReference/hs.build37.1.fa
*/
#include <ctype.h> //<-used for isupper/islower
#include <cmath> //in order to construct large array/objects
#include <stdlib.h>
#include "shared.h" //<-contains the struct defintino for funkypars
#include "analysisFunction.h" //<-contains some utility functions, usefull for parsing args
#include "abcTemplate2.h"//contains the analysis class defition associated with this file
#include "aio.h"
//this msg is shown on screen if you type
// ./angsd -doTemplate
//otherwise this is printet to the file .arg
void abcTemplate2::printArg(FILE *argFile){
fprintf(argFile,"------------------------\n%s:\n",__FILE__);
fprintf(argFile,"-doTemplate2\t%d (Which analysis should we perform?)\n",doTemplate2);
fprintf(argFile,"\t\t1: Count and print basetypes in combination with strand\n");
if (doTemplate2 == 2) fprintf(argFile,"-ref\t%s\n",refName);
}
//this is the function that parses the parameters used for this analysis class
void abcTemplate2::getOptions(argStruct *arguments){
//from command line
doTemplate2=angsd::getArg("-doTemplate2",doTemplate2,arguments);
if (doTemplate2 == 2) refName = angsd::getArg("-ref", refName, arguments);
if ((doTemplate2 == 2) && (refName==NULL)) {
fprintf(stderr, "\t-> Must supply -ref \n");
printArg(stderr);
exit(0);
}
if(doTemplate2==0){
/*
if this class shouldnt do any analysis,
then setting this to zero will make sure nothing is run (apart from destructor)
this could also have been accomplished by
if(doTemplate==0) return
in ::run ::clean ::print
*/
shouldRun[index]=0;
return;
}
}
//constructor
abcTemplate2::abcTemplate2(const char *outfiles,argStruct *arguments,int inputtype){
doTemplate2 = 0; //defaults= dont do analysis
refName = NULL;
outfile = NULL;
char *empty;
//like_calc = new phys_genolike_calc( empty );
//first a hook for the interactive help:
// ./angsd -doTemplate
if (arguments->argc==2){
if(!strcasecmp(arguments->argv[1],"-doTemplate2")){
printArg(stdout);
exit(0);
} else
return;
}
//now parse the arguments
getOptions(arguments);
//now print the arguments
if(doTemplate2==0)
return ;
printArg(arguments->argumentFile);
//initalize outputfile
outfile = aio::openFile(outfiles,".results");
// fprintf(outfile,"Chromo\tPosition\t+A\t+C\t+G\t+T\t-A\t-C\t-G\t-T\n");
}
//destructor
abcTemplate2::~abcTemplate2(){
if(doTemplate2==0)
return;
if(outfile!=NULL)
fclose(outfile);
}
//this function is run, after ::run and ::print
void abcTemplate2::clean(funkyPars *pars){
//we havent done any allocation so we dont need to cleanup
}
void abcTemplate2::print(funkyPars *pars){
if(doTemplate2==1){
//count bases by strand
//rawseqdata is in chunkyT struct (bambi_interface.h)
chunkyT *chk = pars->chk;
//loop over sites;
for(int s=0;s<pars->numSites;s++){
int bases[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
//loop over samples
for(int i=0;i<pars->nInd;i++){
//all seqdata associated with single bamfile is in a tNode
tNode *nd = chk->nd[s][i];
//loop over the individual bases
for(int l=0;l<nd->l;l++){
char c = nd->seq[l]; //this is the base
char q = nd->qs[l]; //this is the associated qscore, fancy shit
int strand = isupper(nd->seq[l])==0; //strand is defined as either small/big letters
//there is a lookuptable called refToInt which maps
//a->0,A->0,c->1,C->1,g->2,G->2,t->3,T=>3,n->4,N->5
bases[strand][refToInt[c]]++;
}
//print chr and position
fprintf(outfile,"%s\t%d",header->target_name[pars->refId],pars->posi[s]+1);//position is zero index internally
//print the basecount
for(int i=0;i<2;i++)
for(int j=0;j<5;j++)
fprintf(outfile,"\t%d",bases[i][j]);
fprintf(outfile,"\n");
}
}
}
}
// -------------------------------------------------------------------------------------------------- //
void abcTemplate2::run(funkyPars *pars){
// -------------------------------------------------------------------------------------------------- //
}