-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetExonCds.c
More file actions
307 lines (262 loc) · 8.07 KB
/
GetExonCds.c
File metadata and controls
307 lines (262 loc) · 8.07 KB
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
/******************************************************************
> File Name: GetExonCds.c
> Author: yys
> mail: shayy0919@163.com
> Created Time: 2019年04月21日 星期日 14时00分52秒
******************************************************************/
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <stdbool.h>
#define PATH_MAX 256
#define GENE_MAX 128
enum SEQTYPE { CDS = 0, EXON = 1 };
typedef struct __exon_id exon_id;
struct __exon_id{
uint64_t start;
uint64_t end;
exon_id *link;
};
typedef struct __cds_id cds_id;
struct __cds_id{
uint64_t start;
uint64_t end;
cds_id *link;
};
typedef struct __gene_e_t {
char gene[0x80];
int exon_n;
exon_id *exon;
}gene_e_t;
typedef struct __gene_c_t {
char gene[0x80];
int cds_n;
cds_id *cds;
}gene_c_t;
typedef struct __chr_t {
char chr[0x80];
int gene_n;
gene_e_t *gene_e;
}chr_t;
typedef struct __gtf_t {
int chrnum;
chr_t *chr;
}gtf_t;
typedef struct __arg_t {
int help;
int region; // CDS(cds) or EXON(exon)
char gtfile[PATH_MAX];
char gene[PATH_MAX];
char outdir[PATH_MAX];
} arg_t;
#define err_open(_fp, _fn, _mode) do {\
_fp = fopen(_fn, _mode); \
if (!_fp) { \
fprintf(stderr, "\nerr: failed to open %s!\n", _fn); exit(-1); \
}\
}while(0)
#define err_realloc(_p, _n, _type) do { \
_type *tem = (_type *)realloc((_p), (_n)*sizeof(_type)); \
if (!tem) { \
fprintf(stderr, "\nerr: failed to realloced memory!\n"); exit(-1); \
} (_p) = tem; \
}while(0)
#define err_calloc(_p, _n, _type) do { \
_type *tem = (_type *)calloc(_n,sizeof(_type)); \
if (!tem) { \
fprintf(stderr, "\nerr: failed to calloc memory!\n"); exit(-1); \
} (_p) = tem; \
}while(0)
void Usage(void)
{
char *usage =
"\nUsage: GetExonCds [options]\n"
"Version: 1.0\n"
"\n"
"Options:\n"
" -h|--help print help infomation\n"
" -r|--region [required] the region type [exon|cds]\n"
" -f|--gtfile [required] input gtf file [.gtf]\n"
" -g|--gene [required] required gene name [\"chr7-EGFR, chr17-TP53\"]\n"
" -o|--outdir [required] output directory for trimed fastq file [dir]\n\n";
fprintf(stderr, "%s", usage);
exit(-1);
}
static const struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "region", required_argument, NULL, 'r' },
{ "gtfile", required_argument, NULL, 'f' },
{ "gene", required_argument, NULL, 'g' },
{ "outdir", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 }
};
static void ArgInit(arg_t *Arg)
{
Arg->region = -1;
}
arg_t *ParseOpt( int argc, char **argv )
{
int opt =0, opterr =0;
arg_t *Arg;
err_calloc(Arg, 1, arg_t);
ArgInit(Arg);
while ( (opt = getopt_long(argc, argv, "r:f:g:o:h", long_options, NULL)) != -1 )
{
switch (opt) {
case 'h': Arg->help = 1; break;
case 'r': if (!strcmp(optarg, "cds")) Arg->region = CDS;
else if (!strcmp(optarg, "exon")) Arg->region = EXON;
else Arg->region = -1; break;
case 'f': strcpy(Arg->gtfile, optarg); break;
case 'g': strcpy(Arg->gene, optarg); break;
case 'o': strcpy(Arg->outdir, optarg); break;
case '?': fprintf(stderr, \
"[Err::%s::%d] Option error occour!.\n", __func__, __LINE__);
Arg->help = 1;
}
}
if (!Arg->gtfile[0] || !Arg->gene[0] || (Arg->region == -1) || !Arg->outdir[0]) {
fprintf(stderr, \
"[Err::%s::%d] Please give the [requied] parmeters!\n", __func__, __LINE__);
Arg->help = 1;
}
return Arg;
}
void substr(char *des, char *src, uint64_t start, uint64_t end)
{
char *p = src+start+11;
int nlen = start;
}
int InsertNodeExon(exon_id **linkp, uint64_t start, uint64_t end)
{
exon_id *current;
exon_id *new;
while((current = *linkp) != NULL) {
//next node address
linkp = ¤t->link;
}
new = (exon_id *)calloc(1,sizeof(exon_id));
if(new == NULL) return false;
new->start = start;
new->end = end;
new->link = current;
*linkp = new;
return true;
}
void PrintList(exon_id *S, char *chr, char *gene, char *outdir)
{
exon_id *p;
p = S;
FILE *fp;
char outfile[PATH_MAX]={'\0'};
strcat(outfile, outdir);
strcat(outfile, gene);
strcat(outfile, ".bed");
fp = fopen(outfile, "w");
while(p != NULL) {
fprintf(fp,"%s\t%s\t%d\t%d\n", chr, gene, p->start, p->end);
p = p->link;
}
}
int CalCommaCount(char *genelist, char (*buf)[PATH_MAX])
{
int n = 0;
char *token;
//char buf[GENE_MAX][PATH_MAX] = {'\0'};
token = strtok(genelist, ",");
while(token != NULL) {
strcpy(buf[n], token);n++;
token = strtok(NULL, ",");
}
return n;
}
gtf_t *GetExonCds(char *filename)
{
FILE *gtfp;
int status = 0;
regex_t reg;
regmatch_t pmatch[1];
int flag = REG_EXTENDED;
const size_t nmatch = 1;
const char *pattern ="gene_name \"\\w*\\W?\\w*?\\W?\\w*?\\W?\\w?\";";
uint64_t start, end;
char buf[0x1000];
char chr[0x80], info[0x80], suffix[0x1000];
gtf_t *gtf;
chr_t *chromsome;
gene_e_t *gene_e;
char curgene[0x80], postgene[0x80]="\0";
char curchr[0x80], postchr[0x80]="\0";
err_open(gtfp, filename, "r");
gtf = (gtf_t *)calloc(1, sizeof(gtf_t));
while(fgets(buf, 0x1000, gtfp)) {
if(buf[0] == '#') continue;
sscanf(buf, "%s%*s%s%ld%ld", curchr, info, &start, &end);
regcomp(®, pattern, flag);
status = regexec(®, buf, nmatch, pmatch, 0);
if(status == REG_NOMATCH){
printf("no match\n");
}
else if(status == 0){
if (!strcmp(info, "exon")) {
strncpy(curgene,buf+pmatch[0].rm_so+11,pmatch[0].rm_eo-pmatch[0].rm_so-13);
curgene[pmatch[0].rm_eo-pmatch[0].rm_so-13]='\0';
if (strcmp(curchr, postchr)) {
if (gtf->chrnum % 0x20 == 0) {
err_realloc(gtf->chr, gtf->chrnum+0x20, chr_t);
}
chromsome = >f->chr[gtf->chrnum++];
memset(chromsome, 0, sizeof(chr_t));
strcpy(chromsome->chr, curchr);
strcpy(postchr, curchr);
}
if (strcmp(curgene, postgene)) {
if (chromsome->gene_n % 0x20 == 0) {
err_realloc(chromsome->gene_e, chromsome->gene_n+0x20, gene_e_t);
}
gene_e = &chromsome->gene_e[chromsome->gene_n++];
memset(gene_e, 0, sizeof(gene_e_t));
strcpy(gene_e->gene, curgene);
strcpy(postgene, curgene);
}
gene_e->exon_n++;
InsertNodeExon(&gene_e->exon, start, end);
}
regfree(®);
}
}
return gtf;
}
int main(int argc, char **argv)
{
arg_t *args = ParseOpt(argc, argv);
if (args->help) Usage();
int gene_count;
char ch[0x80],ge[0x80];
char buf[GENE_MAX][PATH_MAX] = {'\0'};
gtf_t *gtf;
chr_t *chrom;
gene_e_t *gene;
gtf = GetExonCds(args->gtfile);
gene_count = CalCommaCount(args->gene,buf);
for(int i=0; i<gene_count; i++) {
strcpy(ch,strtok(buf[i],"-"));
strcpy(ge,strtok(NULL,"-"));
for (int chr_n=0; chr_n<gtf->chrnum; chr_n++) {
chrom = >f->chr[chr_n];
if (!strcmp(chrom->chr, ch)) {
for (int gene_n=0; gene_n<chrom->gene_n; gene_n++) {
gene = &chrom->gene_e[gene_n];
if (!strcmp(gene->gene,ge)) {
if (args->region)
PrintList(gene->exon, chrom->chr, gene->gene, args->outdir);
}
}
}
}
}
}