-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKmerMap.c
252 lines (200 loc) · 6.01 KB
/
KmerMap.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
/********************************************************************************************
*
* Print a BED-formatted file of the locations of FastK table k-mers in a specified FASTA file
*
* Author : Nancy Hansen
* Date : January 2025
* Modified: Gene Myers, February 2025
*
*********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <math.h>
#define DEBUG
#include "gene_core.h"
#include "libfastk.h"
// Usage
static char *Usage =
"[-vm] [-T<int(4)>] [-P<dir(/tmp)> <kmers>[.ktab] <target>[.\"dna\"] <out:bed>";
static int VERBOSE;
static int MERGE;
static int KMER;
static void write_bed(char *aroot, char *proot, char *pname, FILE *fmap)
{ Profile_Index *RP;
uint16 *prof;
int64 pmax, plen;
int nscf;
int p, x;
if (VERBOSE)
fprintf(stderr,"\n About to open relative kmer profile\n");
RP = Open_Profiles(pname);
if (RP == NULL)
{ fprintf(stderr,"\n%s: Cannot open/find FastK profile of %s relative to %s\n",
Prog_Name,aroot,proot);
exit (1);
}
pmax = 20000;
prof = Malloc(pmax*sizeof(uint16),"Profile array");
if (prof == NULL)
exit (1);
nscf = RP->nreads;
for (p = 0; p < nscf; p++)
{ int beg, end;
plen = Fetch_Profile(RP,p,pmax,prof);
if (plen > pmax)
{ pmax = 1.2*plen + 1000;
prof = Realloc(prof,pmax*sizeof(uint16),"Profile array");
if (prof == NULL)
exit (1);
Fetch_Profile(RP,p,pmax,prof);
}
if (MERGE)
{ beg = -1;
end = -1;
for (x = 0; x < plen; x++)
if (prof[x] > 0)
{ if (x > end)
{ if (beg >= 0)
fprintf(fmap,"%d\t%d\t%d\t%s\n",p,beg,end,proot);
beg = x;
}
end = x+KMER;
}
if (beg >= 0)
fprintf(fmap,"%d\t%d\t%d\t%s\n",p,beg,end,proot);
}
else
for (x = 0; x <= plen; x++)
if (prof[x] > 0)
fprintf(fmap,"%d\t%d\t%d\t%s\n",p,x,x+KMER,proot);
}
Free_Profiles(RP);
}
/****************************************************************************************
*
* Main Routine
*
*****************************************************************************************/
static int check_table(char *name, int lmer)
{ int kmer;
FILE *f;
f = fopen(name,"r");
if (f == NULL)
{ fprintf(stderr,"\n%s: Cannot find FastK table %s\n",Prog_Name,name);
exit (1);
}
else
{ fread(&kmer,sizeof(int),1,f);
if (lmer != 0 && kmer != lmer)
{ fprintf(stderr,"\n%s: Kmer (%d) of table %s != %d\n",Prog_Name,kmer,name,lmer);
exit (1);
}
fclose(f);
return (kmer);
}
}
int main(int argc, char *argv[])
{ char *KTAB, *ASM, *OUT;
char *AROOT, *KROOT;
char *KPROF;
int NTHREADS;
FILE *fmap;
char *SORT_PATH;
char command[5000];
// Command line processing
{ int i, j, k;
int flags[128];
char *eptr;
(void) eptr;
ARG_INIT("KmerMap");
NTHREADS = 4;
SORT_PATH = "/tmp";
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("vm")
break;
case 'P':
SORT_PATH = argv[i]+2;
break;
case 'T':
ARG_POSITIVE(NTHREADS,"Number of threads")
break;
}
else
argv[j++] = argv[i];
argc = j;
VERBOSE = flags['v'];
MERGE = flags['m'];
if (argc != 4)
{ fprintf(stderr,"\nUsage: %s %s\n",Prog_Name,Usage);
fprintf(stderr,"\n");
fprintf(stderr," -v: verbose output to stderr\n");
fprintf(stderr," -m: merge overlapping k-mer hits\n");
fprintf(stderr,"\n");
fprintf(stderr," -T: number of threads to use\n");
fprintf(stderr," -P: Place all temporary files in directory -P.\n");
exit (1);
}
KTAB = argv[1];
ASM = argv[2];
OUT = argv[3];
// Remove any suffixes from argument names
{ char *suffix[] = { ".cram", ".bam", ".sam", ".db", ".dam",
".fastq", ".fasta", ".fq", ".fa",
".fastq.gz", ".fasta.gz", ".fq.gz", ".fa.gz" };
char *KPATH;
int len;
for (j = 0; j < 13; j++)
{ len = strlen(ASM) - strlen(suffix[j]);
if (len > 0 && strcmp(ASM+len,suffix[j]) == 0)
break;
}
if (j >= 13)
AROOT = strdup(ASM);
else
AROOT = Root(ASM,suffix[j]);
KROOT = Root(KTAB,".ktab");
KPATH = PathTo(KTAB);
KMER = check_table(Catenate(KPATH,"/",KROOT,".ktab"),0);
free(KPATH);
if (VERBOSE)
fprintf(stderr,"\n Kmer size is %d\n",KMER);
}
}
{ char templateKP[20] = "._MQY_RP.XXXXXX";
// Create relative profile of k-mers against assembly
if (VERBOSE)
fprintf(stderr,"\n Creating a profile of %s against the assembly %s\n",KROOT,AROOT);
KPROF = mktemp(templateKP);
if (VERBOSE)
{ sprintf(command,"FastK -v -T%d -P%s -k%d -p:%s %s -N%s",
NTHREADS,SORT_PATH,KMER,KTAB,ASM,KPROF);
fprintf(stderr,"\n Running command %s\n",command);
}
else
sprintf(command,"FastK -T%d -P%s -k%d -p:%s %s -N%s",NTHREADS,SORT_PATH,KMER,KTAB,ASM,KPROF);
SystemX(command);
// Call write_bed to convert the kmer profile to a bed file
if (MERGE)
fmap = fopen(Catenate(OUT,".",AROOT,".kmers.merge.bed"),"w");
else
fmap = fopen(Catenate(OUT,".",AROOT,".kmers.bed"),"w");
write_bed(AROOT, KROOT, KPROF, fmap);
fclose(fmap);
sprintf(command,"Fastrm %s",KPROF);
SystemX(command);
if (VERBOSE)
printf("\n Done\n\n");
}
free(KROOT);
free(AROOT);
Catenate(NULL,NULL,NULL,NULL);
free(Prog_Name);
exit (0);
}