-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputfile.c
117 lines (103 loc) · 4.56 KB
/
outputfile.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
/* Copyright(c) Ryuichiro Nakato <[email protected]>
* This file is a part of DROMPA sources.
*/
#include <stdlib.h>
#include <string.h>
#include "outputfile.h"
#include "filehandle.h"
#include "stringp.h"
#include "alloc.h"
#include "macro.h"
#include "compress.h"
void output_bindata(char *dir, char *prefix, RefGenome *g, TYPE_WIGARRAY *array, char *gtfile, int binsize, int binnum, int chr, PWfile_Type wtype, int showzero){
char *output_prefix = alloc_str_new(dir, strlen(prefix) + 1024);
if(wtype==TYPE_BEDGRAPH || wtype==TYPE_BIGWIG) sprintf(output_prefix, "%s/%s.%d", dir, prefix, binsize);
else sprintf(output_prefix, "%s/%s_%s.%d", dir, prefix, g->chr[chr].name, binsize);
char *outputfilename = alloc_str_new(output_prefix, 200);
if(wtype==TYPE_BINARY){
sprintf(outputfilename, "%s.bin", output_prefix);
make_binary(array, outputfilename, binnum);
}
else if(wtype==TYPE_BEDGRAPH || wtype==TYPE_BIGWIG){
sprintf(outputfilename, "%s.bedGraph", output_prefix);
make_bedGraph(g, array, outputfilename, output_prefix, binsize, binnum, chr, showzero);
if(chr == g->chrnum-1 && wtype==TYPE_BIGWIG) convert_bedGraph_to_bigWig(outputfilename, output_prefix, gtfile);
}
else if(wtype==TYPE_COMPRESSWIG || wtype==TYPE_UNCOMPRESSWIG){
sprintf(outputfilename, "%s.wig", output_prefix);
make_wig(g, array, outputfilename, prefix, binsize, binnum, chr, wtype, showzero);
}
MYFREE(output_prefix);
MYFREE(outputfilename);
return;
}
void make_binary(TYPE_WIGARRAY *array, char *outputfile, int binnum){
FILE *OUT = my_fopen(outputfile, FILE_MODE_WB);
if(fwrite(array, sizeof(TYPE_WIGARRAY) * binnum, 1, OUT) != 1){
fprintf(stderr,"[E] fwrite error:%s\n", outputfile);
exit(1);
}
fclose(OUT);
return;
}
void make_bedGraph(RefGenome *g, TYPE_WIGARRAY *array, char *outputfile, char *prefix, int binsize, int binnum, int chr, int showzero){
int i,e;
if(chr==1) remove_file(outputfile);
FILE *OUT = my_fopen(outputfile, FILE_MODE_A);
for(i=0; i<binnum; i++){
if(i==binnum -1) e = g->chr[chr].len-1; else e = (i+1)*binsize;
if(showzero || array[i]) fprintf(OUT, "%s %d %d %.3f\n", g->chr[chr].name, i*binsize, e, WIGARRAY2VALUE(array[i]));
}
fclose(OUT);
if(chr == g->chrnum-1){
char *command = alloc_str_new(outputfile, strlen(outputfile) + 1024);
sprintf(command, "sort -k1,1 -k2,2n %s > %s.sort", outputfile, outputfile);
LOG("%s\n", command);
my_system(command);
char *tempfile = alloc_str_new(outputfile, 1024);
sprintf(tempfile, "%s.temp", outputfile);
OUT = my_fopen(tempfile, FILE_MODE_A);
fprintf(OUT, "browser position %s:%ld-%ld\n", g->chr[1].name, g->chr[1].len/3, min(g->chr[1].len/3+1000000, g->chr[1].len-1));
fprintf(OUT, "browser hide all\n");
fprintf(OUT, "browser pack refGene encodeRegions\n");
fprintf(OUT, "browser full altGraph\n");
fprintf(OUT, "track type=bedGraph name=\"%s\" description=\"Merged tag counts for every %d bp\" visibility=full\n", prefix, binsize);
fclose(OUT);
sprintf(command, "cat %s %s.sort > %s", tempfile, outputfile, outputfile);
LOG("%s\n", command);
my_system(command);
sprintf(command, "rm %s.sort %s", outputfile, tempfile);
LOG("%s\n", command);
my_system(command);
MYFREE(command);
MYFREE(tempfile);
}
return;
}
void convert_bedGraph_to_bigWig(char *outputfile, char *output_prefix, char *gtfile){
printf("convert to bigWig format...\n");
char *command = alloc_str_new(outputfile, strlen(gtfile) + strlen(output_prefix) + 1024);
sprintf(command, "bedGraphToBigWig %s.bedGraph %s %s.bw", output_prefix, gtfile, output_prefix);
LOG("%s\n", command);
my_system(command);
remove(outputfile);
MYFREE(command);
return;
}
void make_wig(RefGenome *g, TYPE_WIGARRAY *array, char *outputfile, char *name, int binsize, int binnum, int chr, PWfile_Type wtype, int showzero){
int i;
FILE *OUT = my_fopen(outputfile, FILE_MODE_WRITE);
/* first line */
fprintf(OUT, "track type=wiggle_0\tname=\"%s_%s\"\tdescription=\"Merged tag counts for every %d bp\"\n", name, g->chr[chr].name, binsize);
/* second line */
if(!strcmp(g->chr[chr].name, "2micron")) fprintf(OUT, "variableStep\tchrom=%s\tspan=%d\n", g->chr[chr].name, binsize);
else fprintf(OUT, "variableStep\tchrom=%s\tspan=%d\n", g->chr[chr].name, binsize);
/* data line */
for(i=0; i<binnum; i++){
if(showzero || array[i]) fprintf(OUT, "%d\t%.3f\n", i*binsize +1, WIGARRAY2VALUE(array[i]));
}
fclose(OUT);
/* compression */
if(wtype==TYPE_COMPRESSWIG) compress2gz(outputfile);
return;
}