-
Notifications
You must be signed in to change notification settings - Fork 2
/
aux.c
200 lines (182 loc) · 8.09 KB
/
aux.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
#include "bison.h"
KSTREAM_INIT(gzFile, gzread, 16384)
FILE *popen_fd;
/******************************************************************************
*
* Return the number of nodes what will actually be run (as opposed to the
* number allocated)
*
*******************************************************************************/
#ifdef DEBUG
int effective_nodes() {
return(8);
}
#else
int effective_nodes() {
int output, remainder;
MPI_Comm_size(MPI_COMM_WORLD, &output);
--output; //Ignore the master node
if(config.directional) {
remainder = output % 2;
} else {
remainder = output % 4;
}
output -= remainder;
return(output);
}
#endif
/******************************************************************************
*
* quit, while performing some cleanup
*
* int FLAG: What to free/close/etc.
* 0x1 things created by create_fastq_names()
* 0x2 things pthreads are closed and bam headers destroyed
* In addition, the master node will free chromosomes.genome, close
* the BAM file, and free everything in the chromosomes struct.
*
* int rv: return value
*
*******************************************************************************/
void quit(int FLAG, int rv) {
int taskid, i;
free(config.bowtie2_options);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
if(FLAG & 1) { //FASTQ filenames set
#ifndef DEBUG
if(taskid == MASTER) {
if(config.FASTQ1CT != NULL) remove(config.FASTQ1CT);
if(config.paired && (config.FASTQ2GA != NULL)) remove(config.FASTQ2GA);
if(!config.directional) {
if(config.FASTQ1GA != NULL) remove(config.FASTQ1GA);
if(config.paired && (config.FASTQ2CT != NULL)) remove(config.FASTQ2CT);
}
}
#endif
if(config.FASTQ1CT != NULL) free(config.FASTQ1CT);
if(config.FASTQ1GA != NULL) free(config.FASTQ1GA);
if(config.unmapped1 != NULL) free(config.unmapped1);
if(config.paired) {
if(config.FASTQ2CT != NULL) free(config.FASTQ2CT);
if(config.FASTQ2GA != NULL) free(config.FASTQ2GA);
if(config.unmapped2 != NULL) free(config.unmapped2);
}
free(config.basename);
free(config.outname);
if(config.fai) free(config.fai);
}
if(taskid == MASTER) {
free(chromosomes.genome);
for(i=0; i<chromosomes.nchromosomes; i++) {
free((chromosomes.chromosome[i])->chrom);
free(*(chromosomes.chromosome+i));
}
free(chromosomes.chromosome);
if(FLAG && OUTPUT_BAM) sam_close(OUTPUT_BAM);
}
MPI_Finalize();
if(taskid == MASTER && FLAG > 0) {
#ifdef DEBUG
if(fp1) sam_close(fp1);
if(fp2) sam_close(fp2);
if(!config.directional) {
if(fp3) sam_close(fp3);
if(fp4) sam_close(fp4);
}
#else
if(config.unmapped) {
pclose(unmapped1);
if(config.paired) pclose(unmapped2);
}
#endif
}
exit(rv);
}
void print_metrics() {
char *of = malloc(sizeof(char) * (strlen(config.odir)+5+strlen(config.basename)));
FILE *fp;
unsigned long long m_reads = m_reads_OT + m_reads_OB + m_reads_CTOT + m_reads_CTOB;
assert(of);
sprintf(of, "%s%s.txt", config.odir, config.basename);
fp = fopen(of, "w");
if(!config.quiet) printf("Alignment:\n");
fprintf(fp,"Alignment:\n");
if(config.paired) {
if(!config.quiet) {
printf("\t%llu total reads analysed\n", t_reads);
printf("\t%llu reads mapped (%6.2f%%).\n", m_reads, ((float) (100*m_reads))/((float) t_reads));
printf("\n");
printf("\t%llu concordant pairs\n", t_concordant);
printf("\t%llu discordant pairs\n", t_discordant);
printf("\t%llu reads aligned as singletons\n", t_singletons);
printf("\n");
}
fprintf(fp, "\t%llu total reads analysed\n", t_reads);
fprintf(fp, "\t%llu paired-end reads mapped (%6.2f%%).\n", m_reads, ((float) (100*m_reads))/((float) t_reads));
fprintf(fp, "\n");
fprintf(fp, "\t%llu concordant pairs\n", t_concordant);
fprintf(fp, "\t%llu discordant pairs\n", t_discordant);
fprintf(fp, "\t%llu reads aligned as singletons\n", t_singletons);
fprintf(fp, "\n");
} else {
if(!config.quiet) {
printf("\t%llu total reads analysed\n", t_reads);
printf("\t%llu reads mapped (%6.2f%%).\n", m_reads, ((float) (100*m_reads))/((float) t_reads));
printf("\n");
}
fprintf(fp,"\t%llu total reads analysed\n", t_reads);
fprintf(fp,"\t%llu reads mapped (%6.2f%%).\n", m_reads, ((float) (100*m_reads))/((float) t_reads));
fprintf(fp,"\n");
}
if(!config.quiet) {
printf("Number of hits aligning to each of the orientations:\n");
printf("\t%llu\t%6.2f%%\tOT (original top strand)\n", m_reads_OT, ((float) (100*m_reads_OT))/((float) t_reads));
printf("\t%llu\t%6.2f%%\tOB (original bottom strand)\n", m_reads_OB, ((float) (100*m_reads_OB))/((float) t_reads));
if(!config.directional) printf("\t%llu\t%6.2f%%\tCTOT (complementary to the original top strand)\n", m_reads_CTOT, ((float) (100*m_reads_CTOT))/((float) t_reads));
if(!config.directional) printf("\t%llu\t%6.2f%%\tCTOB (complementary to the original bottom strand)\n", m_reads_CTOB, ((float) (100*m_reads_CTOB))/((float) t_reads));
printf("\n");
printf("Cytosine Methylation (N.B., statistics from overlapping mates are added together!):\n");
printf("\tNumber of C's in a CpG context: %llu\n", t_CpG);
printf("\tPercentage of methylated C's in a CpG context: %6.2f%%\n", ((float) (100*m_CpG))/((float) t_CpG));
printf("\tNumber of C's in a CHG context: %llu\n", t_CHG);
printf("\tPercentage of methylated C's in a CHG context: %6.2f%%\n", ((float) (100*m_CHG))/((float) t_CHG));
printf("\tNumber of C's in a CHH context: %llu\n", t_CHH);
printf("\tPercentage of methylated C's in a CHH context: %6.2f%%\n", ((float) (100*m_CHH))/((float) t_CHH));
}
fprintf(fp,"Number of hits aligning to each of the orientations:\n");
fprintf(fp,"\t%llu\t%6.2f%%\tOT (original top strand)\n", m_reads_OT, ((float) (100*m_reads_OT))/((float) t_reads));
fprintf(fp,"\t%llu\t%6.2f%%\tOB (original bottom strand)\n", m_reads_OB, ((float) (100*m_reads_OB))/((float) t_reads));
if(!config.directional) fprintf(fp,"\t%llu\t%6.2f%%\tCTOT (complementary to the original top strand)\n", m_reads_CTOT, ((float) (100*m_reads_CTOT))/((float) t_reads));
if(!config.directional) fprintf(fp,"\t%llu\t%6.2f%%\tCTOB (complementary to the original bottom strand)\n", m_reads_CTOB, ((float) (100*m_reads_CTOB))/((float) t_reads));
fprintf(fp,"\n");
fprintf(fp,"Cytosine Methylation (N.B., statistics from overlapping mates are added together!):\n");
fprintf(fp,"\tNumber of C's in a CpG context: %llu\n", t_CpG);
fprintf(fp,"\tPercentage of methylated C's in a CpG context: %6.2f%%\n", ((float) (100*m_CpG))/((float) t_CpG));
fprintf(fp,"\tNumber of C's in a CHG context: %llu\n", t_CHG);
fprintf(fp,"\tPercentage of methylated C's in a CHG context: %6.2f%%\n", ((float) (100*m_CHG))/((float) t_CHG));
fprintf(fp,"\tNumber of C's in a CHH context: %llu\n", t_CHH);
fprintf(fp,"\tPercentage of methylated C's in a CHH context: %6.2f%%\n", ((float) (100*m_CHH))/((float) t_CHH));
fclose(fp);
free(of);
}
htsFile * sam_popen(char *cmd) {
htsFile *fp = (htsFile*)calloc(1, sizeof(htsFile));
int fid, fid2;
assert(fp);
popen_fd = popen(cmd, "r"); //Global
fid = fileno(popen_fd);
fid2 = dup(fid); //otherwise, the file descriptor is closed by zlib and pclose() won't work!!
if(popen_fd == NULL) return 0;
if(fp == NULL) return 0;
hFILE *hfile = hdopen(fid2, "r"); //does this exist?
if(hfile == NULL) return 0;
fp->is_be = ed_is_big();
BGZF *gzfp = bgzf_hopen(hfile, "r");
fp->fp.voidp = ks_init(gzfp);
fp->format.format = sam;
return(fp);
}
void sam_pclose(htsFile *fp) {
hts_close(fp);
pclose(popen_fd);
}