-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsameer_new.c
373 lines (289 loc) · 11.5 KB
/
sameer_new.c
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Author: Sreenu Vattipally
University of Glasgow, Glasgow
23/May/2013
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include "sameer.h"
#include "sammes.h"
// Global declaration of the structure
struct entries{
int Start, End, **CodonQual;
char Strand, GeneName[50];
int **ResVar;
};
// This is to store annotation entries
struct entries Pos[1000];
#define RECORDS 1000
#include "VarCount.h"
int main(int argc, char **argv){
FILE *input, *out;
char buffer[MAX_STR_LEN];
int i=0,j=0,k=0,l=0,entry=0;
char QNAME[TEMP_SIZE],RNAME[TEMP_SIZE],CIGAR[TEMP_SIZE],SEQ[TEMP_SIZE],QUAL[TEMP_SIZE],NewSEQ[TEMP_SIZE], conNucl, aa1, aa2, Codon[4], outFileName[20];
unsigned int FLAG,POS,PNEXT,TLEN, RefGenomeCount=0, mappedReadQual[50], unmappedReadQual[50], loanPairs=0,posRead1=0,posRead2=0,lenRead1=0, lenRead2=0,insertLen=0, insertLenArray[9000],minInsertLen=10000, maxInsertLen=0, aaIndex=1, aveReadLen=0;
// Output variables
char ProgramName[TEMP_SIZE], Version[TEMP_SIZE],*conSeq, *refGenome, *tempGene, base;
int RefLength, UnMapped=0, aveQual=0, Mapped=0,InDel=0, *coveragePos, *coverageNeg, GenomeCovered=0,*A,*T,*G,*C,*N, TotalEntries=0, GeneLen=0, flag=1, pnFlag=0;
long Depth=0, aveIns=0, totalIns=0;
float entropy=0, *entropyArray;
// First, get annotation records
if((input = fopen(argv[2], "r"))!=NULL){
while (!feof(input)) {
fgets(buffer,MAX_STR_LEN,input); if(feof(input)) break;
Pos[i].Start=GetInt(buffer,1);
Pos[i].End=GetInt(buffer,2);
Pos[i].Strand=GetChar(buffer,3);
GetStr(Pos[i].GeneName,buffer,4);
if(((Pos[i].End-Pos[i].Start)+1)%3!=0) { PrintError(4); exit(0);}
GeneLen=((Pos[i].End-Pos[i].Start)+1)/3;
Pos[i].ResVar = calloc(GeneLen, sizeof(int*));
for(j = 0; j < GeneLen; j++) {
Pos[i].ResVar[j] = calloc(22, sizeof(int));
}
Pos[i].CodonQual = calloc(GeneLen, sizeof(int*));
for(j = 0; j < GeneLen; j++) {
Pos[i].CodonQual[j] = calloc(4, sizeof(int));
}
i++;
// BUFFER OVERFLOW CHECK.
if(i == RECORDS){
printf("Sorry.... Number of mutation records are more than allowed limit (%d)\n\n\
Increase the memory size and re-compile the program\n\n",RECORDS);
fclose(input);
exit(0);
}
}
fclose(input);
TotalEntries=i;
}
// End of annotation file reading
if((input=fopen(argv[1],"r"))!=NULL){
while (!feof(input)) {
fgets(buffer,MAX_STR_LEN,input); if(feof(input)) break;
if(strlen(buffer)>=MAX_STR_LEN-1){ PrintError(1); exit(0); }
// If it is a header line, get Program name, version, reference name and length
if(buffer[0]=='@'){
// Get Program Name and Version
if(strstr(buffer,"PN:") && pnFlag==0){
GetProgram(ProgramName, Version, buffer);
pnFlag=1;
}
// Get Ref Name and Length
if(strstr(buffer,"LN:")){
RefLength=GetRefNameLen(RNAME, buffer);
coveragePos=(int*) calloc(RefLength+10,sizeof(int));
coverageNeg=(int*) calloc(RefLength+10,sizeof(int));
if(RefGenomeCount==0){ // if there is only one reference
// Nucleotide Array
A=(int*) calloc(RefLength,sizeof(int));
T=(int*) calloc(RefLength,sizeof(int));
G=(int*) calloc(RefLength,sizeof(int));
C=(int*) calloc(RefLength,sizeof(int));
N=(int*) calloc(RefLength,sizeof(int));
conSeq=(char*) calloc(RefLength,sizeof(char));
refGenome=(char*) calloc(RefLength,sizeof(char));
tempGene=(char*) calloc(RefLength,sizeof(char));
entropyArray=(float*) calloc(RefLength,sizeof(float));
}
RefGenomeCount++;
}
// If number of reference genomes are more than one
if(RefGenomeCount > 1){ PrintError(2); exit(0);}
}
else{
// If there is no reference genome
if(RefGenomeCount==0){ PrintError(3); exit(0);}
entry++;
QNAME[0]='\0'; FLAG=0, POS=0,CIGAR[0]='\0';PNEXT=0,TLEN=0,SEQ[0]='\0',QUAL[0]='\0', aveQual=0;
SplitSamStr(buffer, QNAME, &FLAG, &POS, CIGAR, &PNEXT, &TLEN, SEQ, QUAL);
aveQual=getAveQual(QUAL);
// If mapped
if((FLAG & (1 << 2))==0){
Mapped++;
mappedReadQual[aveQual]++;
if(strchr(CIGAR,'I')||strchr(CIGAR,'D')) InDel++;
CIGARReWrite(CIGAR,SEQ,NewSEQ);
// Get consensus sequences
for(i=0; i<strlen(NewSEQ); i++){
if(NewSEQ[i]=='A') A[POS+i-1]++;
else if(NewSEQ[i]=='T') T[POS+i-1]++;
else if(NewSEQ[i]=='G') G[POS+i-1]++;
else if(NewSEQ[i]=='C') C[POS+i-1]++;
else N[POS+i-1]++;
}
// Get Coverage
i=j=0;
for(i=POS-1; i<(POS+strlen(NewSEQ))-1;i++){
//if (NewSEQ[j]!='-')
if((FLAG & (1 << 4))==0)
coveragePos[i]++;
else
coverageNeg[i]++;
j++;
}
aveReadLen+=strlen(SEQ);
// Calculate insert length distribution
if(entry==1){posRead1=POS; lenRead1=strlen(SEQ);}
if(entry==2){
posRead2=POS; lenRead2=strlen(SEQ);
if(posRead1==0||posRead2==0) loanPairs++;
else{
if(posRead1>posRead2) insertLen=(posRead1+lenRead1)-posRead2;
else insertLen=(posRead2+lenRead2)-posRead1;
if(insertLen < 1000){
if (insertLen < minInsertLen) minInsertLen=insertLen;
if (insertLen > maxInsertLen) maxInsertLen=insertLen;
insertLenArray[insertLen]++;
}
// if insert length is more than 1000
else
insertLenArray[1000]++;
}
}
// Check whether the Tag has the requireds regions
for(j=0;j<TotalEntries;j++){
// Read falls withing a coding region - type1
if(POS >= Pos[j].Start && POS+strlen(NewSEQ) <= Pos[j].End)
CodonCount(NewSEQ,POS,Pos[j].Start,Pos[j].End,j,1);
// Beginning of the read is not part of a coding region - type2
if(POS < Pos[j].Start && POS+strlen(NewSEQ) > Pos[j].Start && POS+strlen(NewSEQ) <= Pos[j].End)
CodonCount(NewSEQ,POS,Pos[j].Start,Pos[j].End,j,2);
// Ending of the read is not part of a coding region - type3
if(POS >= Pos[j].Start && POS <= Pos[j].End && POS+strlen(NewSEQ) > Pos[j].End){
CodonCount(NewSEQ,POS,Pos[j].Start,Pos[j].End,j,3);
//printf("NewSeq:%s\tPOS:%d\tStart:%d\tEnd:%d\tj:%d\n",NewSEQ,POS,Pos[j].Start,Pos[j].End,j);
}
// Starting and ending of the read is not part of a coding region - type4
if(POS < Pos[j].Start && POS+strlen(NewSEQ) > Pos[j].End)
CodonCount(NewSEQ,POS,Pos[j].Start,Pos[j].End,j,4);
}
} // End of Mapped
// If unmapped
else{
UnMapped++;
unmappedReadQual[aveQual]++;
aveReadLen+=strlen(SEQ);
}
if(entry==2) {entry=posRead1=posRead2=lenRead1=lenRead2=0;}
}
}
// End of reading SAM file
fclose(input);
}
// Start reading reference file
if((input=fopen(argv[3],"r"))!=NULL) {
while ((base=fgetc(input))) {
if(feof(input)) {refGenome[i]='\0'; break; }
if(base == '>') flag=0;
if(flag==0 && base == '\n' ) {flag=1; i=0;}
if(flag==1 && base!='\n') refGenome[i++]=toupper(base);
}
fclose(input);
} // End of getting reference sequence
// Printing the report
strcpy(outFileName,"genStats"); out=fopen(outFileName,"w");
fprintf(out,"File name & {\\bf %s} \\\\ \n",argv[1]);
fprintf(out,"Ref name & {\\bf %s} \\\\ \n",RNAME);
fprintf(out,"Ref len & {\\bf %d} \\\\ \n",RefLength);
fprintf(out,"Program used & {\\bf %s %s} \\\\ \n\\midrule\n",ProgramName,Version);
fprintf(out,"Total reads & {\\bf %d} \\\\ \n",Mapped+UnMapped);
fprintf(out,"Mapped reads & {\\bf %d (%.2f\\%%)} \\\\ \n",Mapped,((float)Mapped/(float)(Mapped+UnMapped))*100);
fprintf(out,"Unmapped reads & {\\bf %d} \\\\ \n",UnMapped);
fprintf(out,"Mapped loan pairs & {\\bf %d} \\\\ \n",loanPairs);
fprintf(out,"Average read length & {\\bf %dnt} \\\\ \n\\midrule\n",(int)(aveReadLen/(Mapped+UnMapped)));
if(Mapped!=0){
// Print Coverage and Depth
for(i=1; i<=RefLength; i++){
if(coveragePos[i-1]!=0 || coverageNeg[i-1]!=0) {
GenomeCovered++;
Depth+=coveragePos[i-1];
Depth+=coverageNeg[i-1];
}
}
fprintf(out,"Coverage & {\\bf %dnt (%.2f\\%%)} \\\\ \n",GenomeCovered,((double)GenomeCovered*100)/(double)RefLength);
fprintf(out,"Average depth & {\\bf %lu reads/site} \\\\ \n",Depth/RefLength);
// Print average insert length
for(i=minInsertLen;i<=1000;i++) {aveIns+=i*insertLenArray[i];totalIns+=i;}
fprintf(out,"Average insert length & {\\bf %lunt} \\\\ \n",(long)(aveIns/totalIns));
}
fclose(out);
strcpy(outFileName,"mapPerc"); out=fopen(outFileName,"w");
fprintf(out,"Mapped\t%d\n",Mapped);
fprintf(out,"Unmapped\t%d\n",UnMapped);
fclose(out);
// Print read quality information if available
strcpy(outFileName,"quality"); out=fopen(outFileName,"w");
for(i=10;i<=40;i++) fprintf(out,"%d %d %d\n",i, mappedReadQual[i], unmappedReadQual[i]);
fclose(out);
//Insert distribution;
strcpy(outFileName,"insLen"); out=fopen(outFileName,"w");
for(i=minInsertLen;i<=1000;i++) fprintf(out,"%d %d\n",i,insertLenArray[i]);
fclose(out);
//Print each position coverage
strcpy(outFileName,"coverage"); out=fopen(outFileName,"w");
for(i=1; i<=RefLength; i++) fprintf(out,"%d\t%d\t-%d\n",i, coveragePos[i-1],coverageNeg[i-1]);
fclose(out);
// Print consensus seq
strcpy(outFileName,"consensus"); out=fopen(outFileName,"w");
i=1;
fprintf(out,"%8d \t",i);
for(i=0; i<RefLength; i++){
conNucl=GetConsensusEntropy(A[i],T[i],G[i],C[i],N[i],&entropy);
conSeq[i]=conNucl;
entropyArray[i]=entropy;
fprintf(out,"%c",conNucl);
if(i%70==69) fprintf(out,"\n%8d\t",i+1);
if(i%10==9) fprintf(out," ");
}
conSeq[i]='\0';
fclose(out);
// Print genome statistics
getGenomeStats(conSeq);
// Print Gaps in the assembly
//getGaps(coveragePos,coverageNeg,RefLength);
//printf("\n====\n");
// Print entropy
strcpy(outFileName,"entropy"); out=fopen(outFileName,"w");
for(i=0; i<RefLength; i++) fprintf(out,"%d\t%.2f\n",i+1, entropyArray[i]);
fclose(out);
//printf("Residue Variations\n");
for(i = 0; i < TotalEntries; i++){
//Translate Reference genome
k=0;
printf("%s\t%d-%d\t%d\n",Pos[i].GeneName,Pos[i].Start,Pos[i].End, ((Pos[i].End-Pos[i].Start)+1)/3);
for(j=Pos[i].Start-1;j<Pos[i].End-3;j+=3)
{
Codon[0]=refGenome[j];
Codon[1]=refGenome[j+1];
Codon[2]=refGenome[j+2];
Codon[3]='\0';
aa1=Translate(Codon);
Codon[0]=conSeq[j];
Codon[1]=conSeq[j+1];
Codon[2]=conSeq[j+2];
Codon[3]='\0';
aa2=Translate(Codon);
printf("%d %c %c\t",aaIndex, aa1,aa2);
if(aa2!='-') PrintVariation(i, (j+1)-Pos[i].Start);
printf("\n");
aaIndex++;
}
aaIndex=1;
}
// Free memory
free(coveragePos); free(coverageNeg); free(A); free(T); free(G); free(C); free(N), free(conSeq), free(entropyArray), free(refGenome);
for(i = 0; i < TotalEntries; i++){
GeneLen=((Pos[i].End-Pos[i].Start)+1)/3;
for(j = 0; j < GeneLen; j++){
free(Pos[i].ResVar[j]);
free(Pos[i].CodonQual[j]);
}
free(Pos[i].ResVar);
free(Pos[i].CodonQual);
}
}