kssdutils 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kssdutils-1.0.0/MANIFEST.in +3 -0
- kssdutils-1.0.0/PKG-INFO +10 -0
- kssdutils-1.0.0/README.md +8 -0
- kssdutils-1.0.0/co2mco.c +320 -0
- kssdutils-1.0.0/command_composite.c +168 -0
- kssdutils-1.0.0/command_dist.c +1625 -0
- kssdutils-1.0.0/command_dist_wrapper.c +47 -0
- kssdutils-1.0.0/command_set.c +671 -0
- kssdutils-1.0.0/command_shuffle.c +147 -0
- kssdutils-1.0.0/global_basic.c +449 -0
- kssdutils-1.0.0/iseq2comem.c +614 -0
- kssdutils-1.0.0/kssdheaders/co2mco.h +51 -0
- kssdutils-1.0.0/kssdheaders/command_composite.h +28 -0
- kssdutils-1.0.0/kssdheaders/command_dist.h +130 -0
- kssdutils-1.0.0/kssdheaders/command_dist_wrapper.h +56 -0
- kssdutils-1.0.0/kssdheaders/command_set.h +38 -0
- kssdutils-1.0.0/kssdheaders/command_shuffle.h +48 -0
- kssdutils-1.0.0/kssdheaders/global_basic.h +270 -0
- kssdutils-1.0.0/kssdheaders/iseq2comem.h +47 -0
- kssdutils-1.0.0/kssdheaders/mman.h +75 -0
- kssdutils-1.0.0/kssdheaders/mytime.h +23 -0
- kssdutils-1.0.0/kssdutils.egg-info/PKG-INFO +10 -0
- kssdutils-1.0.0/kssdutils.egg-info/SOURCES.txt +30 -0
- kssdutils-1.0.0/kssdutils.egg-info/dependency_links.txt +1 -0
- kssdutils-1.0.0/kssdutils.egg-info/not-zip-safe +1 -0
- kssdutils-1.0.0/kssdutils.egg-info/top_level.txt +2 -0
- kssdutils-1.0.0/kssdutils.py +162 -0
- kssdutils-1.0.0/mman.c +157 -0
- kssdutils-1.0.0/mytime.c +39 -0
- kssdutils-1.0.0/pykssd.c +661 -0
- kssdutils-1.0.0/setup.cfg +4 -0
- kssdutils-1.0.0/setup.py +71 -0
kssdutils-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: kssdutils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: kssdutils is a Python package for genome analysis, such as sketch, dist.
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: Hang Yang
|
|
7
|
+
Author-email: yhlink1207@gmail.com
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Kssdtree is a versatile Python package for phylogenetic analysis, offering three distinct pipelines: the Routine Pipeline, the Reference Subtraction Pipeline, and the GTDB-based Phylogenetic Placement Pipeline.
|
|
2
|
+
|
|
3
|
+
Routine Pipeline: A general-purpose tool for phylogenetic analysis of user genomic data.
|
|
4
|
+
Reference Subtraction Pipeline: Designed for intra-species phylogenomic analysis.
|
|
5
|
+
GTDB-based Phylogenetic Placement Pipeline: Facilitates the search for similar genomes in the Genome Taxonomy Database (GTDB), conducting phylogenetic analysis alongside these genomes and positioning the input genomes within the entire prokaryotic tree of life.
|
|
6
|
+
Kssdtree also provides one-stop tree construction and visualization. It can handle DNA sequences in both fasta and fastq formats, whether gzipped or not. Additionally, Kssdtree is compatible with multiple platforms (Linux, MacOS, and Windows) and can be run using Jupyter notebooks.
|
|
7
|
+
|
|
8
|
+
More usages about Kssdtree, please see Kssdtree documentation (https://kssdtree.readthedocs.io/en/latest).
|
kssdutils-1.0.0/co2mco.c
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Copyright 2019 Huiguang Yi. All Rights Reservered.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
#include "kssdheaders/co2mco.h"
|
|
15
|
+
#include "kssdheaders/command_dist.h"
|
|
16
|
+
#include "kssdheaders/global_basic.h"
|
|
17
|
+
|
|
18
|
+
#include <errno.h>
|
|
19
|
+
#include <math.h>
|
|
20
|
+
|
|
21
|
+
#ifdef _WIN32
|
|
22
|
+
#include "kssdheaders/mman.h"
|
|
23
|
+
#else
|
|
24
|
+
#include <sys/mman.h>
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
#ifdef _OPENMP
|
|
28
|
+
#include <omp.h>
|
|
29
|
+
#endif
|
|
30
|
+
const char mco_gids_prefix[] = "mco";
|
|
31
|
+
const char mco_idx_prefix[] = "mco.index";
|
|
32
|
+
|
|
33
|
+
void combco2mco(const char *mcodirname, const char *codirname, int cofnum, int comp_num, int p_fit_mem) {
|
|
34
|
+
char cbdcofname[PATHLEN];
|
|
35
|
+
char cbdcoindexf[PATHLEN];
|
|
36
|
+
char mcofname[PATHLEN];
|
|
37
|
+
char mcoindexf[PATHLEN];
|
|
38
|
+
size_t comp_sz = (1LLU << 4 * COMPONENT_SZ);
|
|
39
|
+
size_t *cbdcoindex = malloc(sizeof(size_t) * (cofnum + 1));
|
|
40
|
+
gidobj_t **mco = malloc(comp_sz * sizeof(gidobj_t *));
|
|
41
|
+
size_t *row_offset = malloc(comp_sz * sizeof(size_t));
|
|
42
|
+
for (int i = 0; i < comp_num; i++) {
|
|
43
|
+
memset(row_offset, 0, comp_sz * sizeof(size_t));
|
|
44
|
+
sprintf(cbdcoindexf, "%s/%s.%d", codirname, idx_prefix, i);
|
|
45
|
+
FILE *cbdindexfp = fopen(cbdcoindexf, "rb");
|
|
46
|
+
if (cbdindexfp == NULL) fprintf(stderr, "%s", cbdcoindexf);
|
|
47
|
+
fread(cbdcoindex, sizeof(size_t), cofnum + 1, cbdindexfp);
|
|
48
|
+
fclose(cbdindexfp);
|
|
49
|
+
sprintf(cbdcofname, "%s/%s.%d", codirname, skch_prefix, i);
|
|
50
|
+
mmp_uint_t mmpcbd_cofile = mmp_uint_arr(cbdcofname);
|
|
51
|
+
for (int j = 0; j < cofnum; j++) {
|
|
52
|
+
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
|
|
53
|
+
for (size_t k = cbdcoindex[j]; k < cbdcoindex[j + 1]; k++) {
|
|
54
|
+
unsigned int ind = mmpcbd_cofile.mmpco[k];
|
|
55
|
+
if (row_offset[ind] == 0) mco[ind] = malloc(GID_ARR_SZ * sizeof(gidobj_t));
|
|
56
|
+
else if ((row_offset[ind] * 2 >= GID_ARR_SZ)) {
|
|
57
|
+
#ifdef _WIN32
|
|
58
|
+
if (((unsigned long) row_offset[ind] & ((unsigned long) row_offset[ind] - 1)) == 0) {
|
|
59
|
+
mco[ind] = realloc(mco[ind], 2 * row_offset[ind] * sizeof(gidobj_t));
|
|
60
|
+
}
|
|
61
|
+
#else
|
|
62
|
+
if (((unsigned long) row_offset[ind] & ((unsigned long) row_offset[ind] - 1)) == 0) {
|
|
63
|
+
mco[ind] = realloc(mco[ind], 2 * row_offset[ind] * sizeof(gidobj_t));
|
|
64
|
+
}
|
|
65
|
+
#endif
|
|
66
|
+
}
|
|
67
|
+
mco[ind][row_offset[ind]] = j;
|
|
68
|
+
row_offset[ind]++;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
munmap(mmpcbd_cofile.mmpco, mmpcbd_cofile.fsize);
|
|
72
|
+
for (size_t n = 1; n < comp_sz; n++) row_offset[n] += row_offset[n - 1];
|
|
73
|
+
sprintf(mcoindexf, "%s/%s.%d", mcodirname, mco_idx_prefix, i);
|
|
74
|
+
FILE *arrmco_index_fp = fopen(mcoindexf, "wb");
|
|
75
|
+
if (arrmco_index_fp == NULL) fprintf(stderr, "%s", mcoindexf);
|
|
76
|
+
fwrite(row_offset, sizeof(size_t), comp_sz, arrmco_index_fp);
|
|
77
|
+
fclose(arrmco_index_fp);
|
|
78
|
+
sprintf(mcofname, "%s/%s.%d", mcodirname, mco_gids_prefix, i);
|
|
79
|
+
FILE *arrmco_fp = fopen(mcofname, "wb");
|
|
80
|
+
if (arrmco_fp == NULL) fprintf(stderr, "combco2mco()::%s", mcofname);
|
|
81
|
+
for (size_t s = 0; s < comp_sz; s++) {
|
|
82
|
+
int row_gnum = s > 0 ? row_offset[s] - row_offset[s - 1] : row_offset[0];
|
|
83
|
+
if (row_gnum > 0)
|
|
84
|
+
fwrite(mco[s], sizeof(gidobj_t), row_gnum, arrmco_fp);
|
|
85
|
+
free(mco[s]);
|
|
86
|
+
}
|
|
87
|
+
fclose(arrmco_fp);
|
|
88
|
+
}
|
|
89
|
+
free(mco);
|
|
90
|
+
free(cbdcoindex);
|
|
91
|
+
free(row_offset);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
void cdb_kmerf2kmerdb(const char *mcodirname, const char *codirname, int cofnum, int comp_num, int p_fit_mem) {
|
|
95
|
+
kmerdb_index_t mco_map;
|
|
96
|
+
size_t comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
97
|
+
int binnum = ceil((double) cofnum / BIN_SZ);
|
|
98
|
+
mco_map.row_bin_gnum = malloc((size_t) comp_sz * binnum * sizeof(unsigned int));
|
|
99
|
+
mco_map.row_gnum = malloc(comp_sz * sizeof(unsigned int));
|
|
100
|
+
mco_map.row_offset = malloc(comp_sz * sizeof(size_t));
|
|
101
|
+
mco_map.row_offset[0] = 0;
|
|
102
|
+
size_t *cbdcoindex = malloc(sizeof(size_t) * (cofnum + 1));
|
|
103
|
+
char cbdcofname[PATHLEN];
|
|
104
|
+
char cbdcoindexf[PATHLEN];
|
|
105
|
+
char mcofname[PATHLEN];
|
|
106
|
+
char mcoindexf[PATHLEN];
|
|
107
|
+
mmp_uint_t mmpcbd_cofile;
|
|
108
|
+
gid_arr_llist_t **mco = malloc(comp_sz * sizeof(gid_arr_llist_t *));
|
|
109
|
+
for (unsigned int i = 0; i < comp_num; i++) {
|
|
110
|
+
memset(mco_map.row_bin_gnum, 0, (size_t) comp_sz * binnum * sizeof(unsigned int));
|
|
111
|
+
memset(mco_map.row_gnum, 0, comp_sz * sizeof(unsigned int));
|
|
112
|
+
FILE *cbdfp, *cbdindexfp;
|
|
113
|
+
sprintf(cbdcoindexf, "%s/combco.index.%d", codirname, i);
|
|
114
|
+
sprintf(cbdcofname, "%s/combco.%d", codirname, i);
|
|
115
|
+
if ((cbdfp = fopen(cbdcofname, "rb")) == NULL) fprintf(stderr, "%s", cbdcofname);
|
|
116
|
+
if ((cbdindexfp = fopen(cbdcoindexf, "rb")) == NULL) fprintf(stderr, "%s", cbdcoindexf);
|
|
117
|
+
fread(cbdcoindex, sizeof(size_t), cofnum + 1, cbdindexfp);
|
|
118
|
+
mmpcbd_cofile = mmp_uint_arr(cbdcofname);
|
|
119
|
+
for (int j = 0; j < cofnum; j++) {
|
|
120
|
+
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
|
|
121
|
+
for (size_t k = cbdcoindex[j]; k < cbdcoindex[j + 1]; k++) {
|
|
122
|
+
unsigned int ind = mmpcbd_cofile.mmpco[k];
|
|
123
|
+
unsigned int mod = mco_map.row_gnum[ind] % GID_ARR_SZ;
|
|
124
|
+
gid_arr_llist_t *tmp;
|
|
125
|
+
if (mod == 0) {
|
|
126
|
+
tmp = mco[ind];
|
|
127
|
+
mco[ind] = malloc(sizeof(gid_arr_llist_t));
|
|
128
|
+
if (mco[ind] == NULL) fprintf(stderr, "cdb_kmerf2kmerdb()::mco[ind]");
|
|
129
|
+
mco[ind]->next = tmp;
|
|
130
|
+
}
|
|
131
|
+
mco[ind]->gidobj[mod] = j % BIN_SZ;
|
|
132
|
+
mco_map.row_gnum[ind]++;
|
|
133
|
+
mco_map.row_bin_gnum[(size_t) ind * binnum + j / BIN_SZ]++;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
munmap(mmpcbd_cofile.mmpco, mmpcbd_cofile.fsize);
|
|
137
|
+
for (size_t n = 1; n < comp_sz; n++)
|
|
138
|
+
mco_map.row_offset[n] = mco_map.row_offset[n - 1] + mco_map.row_gnum[n - 1];
|
|
139
|
+
sprintf(mcoindexf, "%s/mco.index.%d", mcodirname, i);
|
|
140
|
+
FILE *arrmco_index_fp = fopen(mcoindexf, "wb");
|
|
141
|
+
if (arrmco_index_fp == NULL) fprintf(stderr, "%s", mcoindexf);
|
|
142
|
+
fwrite(mco_map.row_offset, sizeof(size_t), comp_sz, arrmco_index_fp);
|
|
143
|
+
fwrite(mco_map.row_bin_gnum, sizeof(unsigned int), (size_t) comp_sz * binnum, arrmco_index_fp);
|
|
144
|
+
fclose(arrmco_index_fp);
|
|
145
|
+
sprintf(mcofname, "%s/mco.%d", mcodirname, i);
|
|
146
|
+
int arrmco_fp = open(mcofname, O_RDWR | O_CREAT, 0600);
|
|
147
|
+
if (arrmco_fp == -1) fprintf(stderr, "cdb_kmerf2kmerdb()::%s", mcofname);
|
|
148
|
+
size_t mco_comp_fsize = sizeof(gidobj_t) * (mco_map.row_offset[comp_sz - 1] + mco_map.row_gnum[comp_sz - 1]);
|
|
149
|
+
if (ftruncate(arrmco_fp, mco_comp_fsize) == -1) fprintf(stderr, "cdb_kmerf2kmerdb()::ftruncate");
|
|
150
|
+
gidobj_t *mco_mmpf = mmap(NULL, mco_comp_fsize, PROT_WRITE, MAP_SHARED, arrmco_fp, 0);
|
|
151
|
+
close(arrmco_fp);
|
|
152
|
+
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
|
|
153
|
+
for (size_t s = 0; s < comp_sz; s++) {
|
|
154
|
+
if (mco_map.row_gnum[s] == 0) continue;
|
|
155
|
+
gid_arr_llist_t *tmpblk;
|
|
156
|
+
gidobj_t *current_blkpos_mapin_arrmco = mco_mmpf + mco_map.row_offset[s] + mco_map.row_gnum[s];
|
|
157
|
+
int blk_num = mco_map.row_gnum[s] / GID_ARR_SZ;
|
|
158
|
+
int remainder = mco_map.row_gnum[s] % GID_ARR_SZ;
|
|
159
|
+
if (remainder > 0) blk_num += 1;
|
|
160
|
+
int blk_len;
|
|
161
|
+
for (int blk = 0; blk < blk_num; blk++) {
|
|
162
|
+
if ((blk == 0) && (remainder > 0)) blk_len = remainder;
|
|
163
|
+
else blk_len = GID_ARR_SZ;
|
|
164
|
+
current_blkpos_mapin_arrmco -= blk_len;
|
|
165
|
+
memcpy(current_blkpos_mapin_arrmco, mco[s]->gidobj, blk_len * sizeof(gidobj_t));
|
|
166
|
+
tmpblk = mco[s];
|
|
167
|
+
mco[s] = mco[s]->next;
|
|
168
|
+
free(tmpblk);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (msync(mco_mmpf, mco_comp_fsize, MS_ASYNC) < 0)
|
|
172
|
+
fprintf(stderr, "cdb_kmerf2kmerdb()::msync failed");
|
|
173
|
+
munmap(mco_mmpf, mco_comp_fsize);
|
|
174
|
+
fclose(cbdfp);
|
|
175
|
+
fclose(cbdindexfp);
|
|
176
|
+
}
|
|
177
|
+
free(mco_map.row_bin_gnum);
|
|
178
|
+
free(mco_map.row_gnum);
|
|
179
|
+
free(mco_map.row_offset);
|
|
180
|
+
free(cbdcoindex);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
mco_entry_stat_t **co2unitllmco(const char *codirname, int bin_sz, int bin_id, int component_id) {
|
|
184
|
+
unsigned int comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
185
|
+
mco_entry_stat_t **mco = calloc(comp_sz, sizeof(mco_entry_stat_t *));
|
|
186
|
+
gid_arr_llist_t *tmp;
|
|
187
|
+
char cofname[PATHLEN];
|
|
188
|
+
mmp_uint_t mmpcofile;
|
|
189
|
+
unsigned int ind;
|
|
190
|
+
int mod;
|
|
191
|
+
for (unsigned int i = 0; i < bin_sz; i++) {
|
|
192
|
+
sprintf(cofname, "%s/%d.%d.co.%d", codirname, bin_id, i, component_id);
|
|
193
|
+
mmpcofile = mmp_uint_arr(cofname);
|
|
194
|
+
int ctx_num = mmpcofile.fsize / sizeof(unsigned int);
|
|
195
|
+
for (int j = 0; j < ctx_num; j++) {
|
|
196
|
+
ind = mmpcofile.mmpco[j];
|
|
197
|
+
if (mco[ind] == NULL) mco[ind] = calloc(1, sizeof(mco_entry_stat_t));
|
|
198
|
+
mod = mco[ind]->g_num % GID_ARR_SZ;
|
|
199
|
+
if (mod == 0) {
|
|
200
|
+
if ((tmp = malloc(sizeof(gid_arr_llist_t))) == NULL) fprintf(stderr, "co2unitllmco()");
|
|
201
|
+
tmp->next = mco[ind]->next;
|
|
202
|
+
mco[ind]->next = tmp;
|
|
203
|
+
}
|
|
204
|
+
mco[ind]->next->gidobj[mod] = i;
|
|
205
|
+
mco[ind]->g_num++;
|
|
206
|
+
};
|
|
207
|
+
munmap(mmpcofile.mmpco, mmpcofile.fsize);
|
|
208
|
+
}
|
|
209
|
+
return mco;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
gidobj_t **llmco2arrmco(mco_entry_stat_t **llmco) {
|
|
213
|
+
unsigned int comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
214
|
+
gidobj_t **arrmco = calloc(comp_sz, sizeof(gidobj_t *));
|
|
215
|
+
gidobj_t *current_blkpos_mapin_arrmco;
|
|
216
|
+
gid_arr_llist_t *tmpblk, *tmpptr;
|
|
217
|
+
for (unsigned int i = 0; i < comp_sz; i++) {
|
|
218
|
+
if (llmco[i] == NULL) continue;
|
|
219
|
+
int arr_len = (int) llmco[i]->g_num + 1;
|
|
220
|
+
arrmco[i] = malloc(arr_len * sizeof(gidobj_t));
|
|
221
|
+
current_blkpos_mapin_arrmco = arrmco[i] + arr_len;
|
|
222
|
+
arrmco[i][0] = llmco[i]->g_num;
|
|
223
|
+
int blk_num = arrmco[i][0] % GID_ARR_SZ == 0 ? arrmco[i][0] / GID_ARR_SZ : arrmco[i][0] / GID_ARR_SZ + 1;
|
|
224
|
+
for (int blk = 0; blk < blk_num; blk++) {
|
|
225
|
+
if (blk == 0) {
|
|
226
|
+
tmpblk = llmco[i]->next;
|
|
227
|
+
if (arrmco[i][0] % GID_ARR_SZ == 0) {
|
|
228
|
+
current_blkpos_mapin_arrmco -= GID_ARR_SZ;
|
|
229
|
+
memcpy(current_blkpos_mapin_arrmco, tmpblk->gidobj, GID_ARR_SZ * sizeof(gidobj_t));
|
|
230
|
+
} else {
|
|
231
|
+
current_blkpos_mapin_arrmco -= arrmco[i][0] % GID_ARR_SZ;
|
|
232
|
+
memcpy(current_blkpos_mapin_arrmco, tmpblk->gidobj, (arrmco[i][0] % GID_ARR_SZ) * sizeof(gidobj_t));
|
|
233
|
+
}
|
|
234
|
+
free(llmco[i]);
|
|
235
|
+
} else {
|
|
236
|
+
tmpptr = tmpblk;
|
|
237
|
+
tmpblk = tmpblk->next;
|
|
238
|
+
current_blkpos_mapin_arrmco -= GID_ARR_SZ;
|
|
239
|
+
memcpy(current_blkpos_mapin_arrmco, tmpblk->gidobj, GID_ARR_SZ * sizeof(gidobj_t));
|
|
240
|
+
free(tmpptr);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
free(llmco);
|
|
245
|
+
return arrmco;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
unsigned int write_unit_arrmco_file(const char *unitmcofname, gidobj_t **arrmco) {
|
|
249
|
+
FILE *outf;
|
|
250
|
+
if ((outf = fopen(unitmcofname, "wb")) == NULL) fprintf(stderr, "write_unit_arrmco_file()");
|
|
251
|
+
unsigned int comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
252
|
+
unsigned int validrow = 0;
|
|
253
|
+
for (unsigned int i = 0; i < comp_sz; i++) {
|
|
254
|
+
if (arrmco[i] != NULL) {
|
|
255
|
+
validrow++;
|
|
256
|
+
fwrite(&i, sizeof(i), 1, outf);
|
|
257
|
+
fwrite(arrmco[i], sizeof(gidobj_t), (unsigned int) arrmco[i][0] + 1, outf);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
fclose(outf);
|
|
261
|
+
return validrow;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
gidobj_t **read_unit_arrmco_file(const char *mco_fncode) {
|
|
265
|
+
FILE *inf;
|
|
266
|
+
if ((inf = fopen(mco_fncode, "rb")) == NULL) fprintf(stderr, "read_unit_arrmco_file()");
|
|
267
|
+
unsigned int comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
268
|
+
unsigned int ind;
|
|
269
|
+
gidobj_t **arrmco = calloc(comp_sz, sizeof(gidobj_t *));
|
|
270
|
+
gidobj_t gid_arr_len;
|
|
271
|
+
while (fread(&ind, sizeof(ind), 1, inf) == 1) {
|
|
272
|
+
fread(&gid_arr_len, sizeof(gidobj_t), 1, inf);
|
|
273
|
+
arrmco[ind] = malloc(sizeof(gidobj_t) * ((unsigned int) gid_arr_len + 1));
|
|
274
|
+
arrmco[ind][0] = gid_arr_len;
|
|
275
|
+
fread(arrmco[ind] + 1, sizeof(gidobj_t), (unsigned int) gid_arr_len, inf);
|
|
276
|
+
}
|
|
277
|
+
fclose(inf);
|
|
278
|
+
return arrmco;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
void free_unit_arrmco(gidobj_t **unit_arrmco) {
|
|
282
|
+
unsigned int comp_sz = (1 << 4 * COMPONENT_SZ);
|
|
283
|
+
for (unsigned int i = 0; i < comp_sz; i++) {
|
|
284
|
+
if (unit_arrmco[i] != NULL)
|
|
285
|
+
free(unit_arrmco[i]);
|
|
286
|
+
}
|
|
287
|
+
free(unit_arrmco);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
size_t est_unitllmco_mem(void) {
|
|
291
|
+
size_t mem_sz = 0;
|
|
292
|
+
unsigned int comp_sz = (1U << 4 * COMPONENT_SZ);
|
|
293
|
+
mem_sz = comp_sz
|
|
294
|
+
* (sizeof(mco_entry_stat_t *)
|
|
295
|
+
+ sizeof(mco_entry_stat_t)
|
|
296
|
+
+ ((unsigned int) (((double) BIN_SZ / (1U << CTX_SPC_USE_L)) / GID_ARR_SZ) + 1)
|
|
297
|
+
* (sizeof(gidobj_t) * GID_ARR_SZ + sizeof(gid_arr_llist_t *))
|
|
298
|
+
);
|
|
299
|
+
return mem_sz;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
size_t precise_est_unitllmco_mem(const char *co_dstat_fpath) {
|
|
303
|
+
FILE *co_stat_fp;
|
|
304
|
+
if ((co_stat_fp = fopen(co_dstat_fpath, "rb")) == NULL)
|
|
305
|
+
fprintf(stderr, "precise_est_unitllmco_mem():%s", co_dstat_fpath);
|
|
306
|
+
co_dstat_t co_dstat_readin;
|
|
307
|
+
fread(&co_dstat_readin, sizeof(co_dstat_t), 1, co_stat_fp);
|
|
308
|
+
unsigned int comp_sz = (1U << 4 * COMPONENT_SZ);
|
|
309
|
+
double ctx_spc_use_rate = (double) co_dstat_readin.all_ctx_ct
|
|
310
|
+
/ co_dstat_readin.infile_num / co_dstat_readin.comp_num / comp_sz;
|
|
311
|
+
printf("ctx_spc_use_rate=%lf\n", ctx_spc_use_rate);
|
|
312
|
+
size_t mem_sz = comp_sz
|
|
313
|
+
* (sizeof(mco_entry_stat_t *)
|
|
314
|
+
+ sizeof(mco_entry_stat_t)
|
|
315
|
+
+ ((unsigned int) (((double) BIN_SZ * ctx_spc_use_rate) / GID_ARR_SZ) + 1)
|
|
316
|
+
* (sizeof(gidobj_t) * GID_ARR_SZ + sizeof(gid_arr_llist_t *))
|
|
317
|
+
);
|
|
318
|
+
fclose(co_stat_fp);
|
|
319
|
+
return mem_sz;
|
|
320
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Copyright 2019 Huiguang Yi. All Rights Reservered.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
#include "kssdheaders/command_composite.h"
|
|
15
|
+
#include "kssdheaders/global_basic.h"
|
|
16
|
+
#include <assert.h>
|
|
17
|
+
#include <stdarg.h>
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <stdlib.h>
|
|
20
|
+
#include <string.h>
|
|
21
|
+
#include <time.h>
|
|
22
|
+
#include <errno.h>
|
|
23
|
+
#include <math.h>
|
|
24
|
+
|
|
25
|
+
int **ref_abund;
|
|
26
|
+
|
|
27
|
+
int get_species_abundance(composite_opt_t *composite_opt) {
|
|
28
|
+
if (composite_opt->refdir[0] == '\0' || composite_opt->qrydir[0] == '\0' ||
|
|
29
|
+
strcmp(composite_opt->refdir, composite_opt->qrydir) == 0)
|
|
30
|
+
fprintf(stderr, "get_species_abundance(): refdir or qrydir is not initialized\n");
|
|
31
|
+
const char *ref_dstat_fpath = test_get_fullpath(composite_opt->refdir, co_dstat);
|
|
32
|
+
if (ref_dstat_fpath == NULL) fprintf(stderr, "cannot find %s under %s ", co_dstat, ref_dstat_fpath);
|
|
33
|
+
const char *qry_dstat_fpath = test_get_fullpath(composite_opt->qrydir, co_dstat);
|
|
34
|
+
if (qry_dstat_fpath == NULL) fprintf(stderr, "cannot find %s under %s ", co_dstat, qry_dstat_fpath);
|
|
35
|
+
FILE *ref_dstat_fp, *qry_dstat_fp;
|
|
36
|
+
if ((ref_dstat_fp = fopen(ref_dstat_fpath, "rb")) == NULL)
|
|
37
|
+
fprintf(stderr, "get_species_abundance():%s", ref_dstat_fpath);
|
|
38
|
+
if ((qry_dstat_fp = fopen(qry_dstat_fpath, "rb")) == NULL)
|
|
39
|
+
fprintf(stderr, "get_species_abundance():%s", qry_dstat_fpath);
|
|
40
|
+
co_dstat_t ref_dstat, qry_dstat;
|
|
41
|
+
fread(&ref_dstat, sizeof(co_dstat_t), 1, ref_dstat_fp);
|
|
42
|
+
fread(&qry_dstat, sizeof(co_dstat_t), 1, qry_dstat_fp);
|
|
43
|
+
if (!qry_dstat.koc) fprintf(stderr, "get_species_abundance(): query has not abundance");
|
|
44
|
+
if (qry_dstat.shuf_id != ref_dstat.shuf_id)
|
|
45
|
+
printf("get_species_abundance(): qry shuf_id %u not match ref shuf_id: %u\n", qry_dstat.shuf_id,
|
|
46
|
+
ref_dstat.shuf_id);
|
|
47
|
+
ctx_obj_ct_t *tmp_ct_list = malloc(sizeof(ctx_obj_ct_t) * ref_dstat.infile_num);
|
|
48
|
+
fread(tmp_ct_list, sizeof(ctx_obj_ct_t), ref_dstat.infile_num, ref_dstat_fp);
|
|
49
|
+
char (*refname)[PATHLEN] = malloc(PATHLEN * ref_dstat.infile_num);
|
|
50
|
+
fread(refname, PATHLEN, ref_dstat.infile_num, ref_dstat_fp);
|
|
51
|
+
fread(tmp_ct_list, sizeof(ctx_obj_ct_t), qry_dstat.infile_num, qry_dstat_fp);
|
|
52
|
+
free(tmp_ct_list);
|
|
53
|
+
char (*qryname)[PATHLEN] = malloc(PATHLEN * qry_dstat.infile_num);
|
|
54
|
+
fread(qryname, PATHLEN, qry_dstat.infile_num, qry_dstat_fp);
|
|
55
|
+
char tmpfname[PATHLEN];
|
|
56
|
+
struct stat tmpstat;
|
|
57
|
+
FILE *tmpfp;
|
|
58
|
+
#define REALLOC_US 8
|
|
59
|
+
ref_abund = (int **) malloc(ref_dstat.infile_num * sizeof(int *));
|
|
60
|
+
for (int i = 0; i < ref_dstat.infile_num; i++) ref_abund[i] = (int *) malloc(REALLOC_US * sizeof(int));
|
|
61
|
+
for (int qn = 0; qn < qry_dstat.infile_num; qn++) {
|
|
62
|
+
for (int i = 0; i < ref_dstat.infile_num; i++) ref_abund[i][0] = 0;
|
|
63
|
+
for (int c = 0; c < ref_dstat.comp_num; c++) {
|
|
64
|
+
sprintf(tmpfname, "%s/%s.%d", composite_opt->refdir, skch_prefix, c);
|
|
65
|
+
if ((tmpfp = fopen(tmpfname, "rb")) == NULL) fprintf(stderr, "get_species_abundance():%s", tmpfname);
|
|
66
|
+
stat(tmpfname, &tmpstat);
|
|
67
|
+
unsigned int *ref_combco = malloc(tmpstat.st_size);
|
|
68
|
+
fread(ref_combco, tmpstat.st_size, 1, tmpfp);
|
|
69
|
+
fclose(tmpfp);
|
|
70
|
+
sprintf(tmpfname, "%s/%s.%d", composite_opt->refdir, idx_prefix, c);
|
|
71
|
+
if ((tmpfp = fopen(tmpfname, "rb")) == NULL) fprintf(stderr, "get_species_abundance():%s", tmpfname);
|
|
72
|
+
stat(tmpfname, &tmpstat);
|
|
73
|
+
size_t *ref_index_combco = malloc(tmpstat.st_size);
|
|
74
|
+
fread(ref_index_combco, tmpstat.st_size, 1, tmpfp);
|
|
75
|
+
fclose(tmpfp);
|
|
76
|
+
sprintf(tmpfname, "%s/%s.%d", composite_opt->qrydir, skch_prefix, c);
|
|
77
|
+
if ((tmpfp = fopen(tmpfname, "rb")) == NULL) fprintf(stderr, "get_species_abundance():%s", tmpfname);
|
|
78
|
+
stat(tmpfname, &tmpstat);
|
|
79
|
+
unsigned int *qry_combco = malloc(tmpstat.st_size);
|
|
80
|
+
fread(qry_combco, tmpstat.st_size, 1, tmpfp);
|
|
81
|
+
fclose(tmpfp);
|
|
82
|
+
sprintf(tmpfname, "%s/%s.%d", composite_opt->qrydir, idx_prefix, c);
|
|
83
|
+
if ((tmpfp = fopen(tmpfname, "rb")) == NULL) fprintf(stderr, "get_species_abundance():%s", tmpfname);
|
|
84
|
+
stat(tmpfname, &tmpstat);
|
|
85
|
+
size_t *qry_index_combco = malloc(tmpstat.st_size);
|
|
86
|
+
fread(qry_index_combco, tmpstat.st_size, 1, tmpfp);
|
|
87
|
+
fclose(tmpfp);
|
|
88
|
+
sprintf(tmpfname, "%s/%s.%d.a", composite_opt->qrydir, skch_prefix, c);
|
|
89
|
+
if ((tmpfp = fopen(tmpfname, "rb")) == NULL) fprintf(stderr, "get_species_abundance():%s", tmpfname);
|
|
90
|
+
stat(tmpfname, &tmpstat);
|
|
91
|
+
unsigned short *qry_abund = malloc(tmpstat.st_size);
|
|
92
|
+
fread(qry_abund, tmpstat.st_size, 1, tmpfp);
|
|
93
|
+
fclose(tmpfp);
|
|
94
|
+
int hash_sz = nextPrime((int) ((double) (qry_index_combco[qn + 1] - qry_index_combco[qn]) / LD_FCTR));
|
|
95
|
+
size_t *km2abund_idx = calloc(hash_sz, sizeof(size_t));
|
|
96
|
+
for (size_t idx = qry_index_combco[qn]; idx < qry_index_combco[qn + 1]; idx++) {
|
|
97
|
+
for (int i = 0; i < hash_sz; i++) {
|
|
98
|
+
unsigned int tmphv = HASH(qry_combco[idx], i, hash_sz);
|
|
99
|
+
if (km2abund_idx[tmphv] == 0) {
|
|
100
|
+
km2abund_idx[tmphv] = idx + 1;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
#pragma omp parallel for num_threads(composite_opt->p) schedule(guided)
|
|
106
|
+
for (int rn = 0; rn < ref_dstat.infile_num; rn++) {
|
|
107
|
+
for (size_t ri = ref_index_combco[rn]; ri < ref_index_combco[rn + 1]; ri++) {
|
|
108
|
+
for (int i = 0; i < hash_sz; i++) {
|
|
109
|
+
unsigned int hv = HASH(ref_combco[ri], i, hash_sz);
|
|
110
|
+
if (km2abund_idx[hv] == 0) break;
|
|
111
|
+
else if (qry_combco[km2abund_idx[hv] - 1] == ref_combco[ri]) {
|
|
112
|
+
ref_abund[rn][0]++;
|
|
113
|
+
ref_abund[rn][ref_abund[rn][0]] = qry_abund[km2abund_idx[hv] - 1];
|
|
114
|
+
if (ref_abund[rn][0] % REALLOC_US == REALLOC_US - 1) {
|
|
115
|
+
int newsize = ((ref_abund[rn][0] / REALLOC_US) + 2) * REALLOC_US;
|
|
116
|
+
ref_abund[rn] = (int *) realloc(ref_abund[rn], newsize * sizeof(int));
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
free(km2abund_idx);
|
|
124
|
+
free(ref_combco);
|
|
125
|
+
free(ref_index_combco);
|
|
126
|
+
free(qry_combco);
|
|
127
|
+
free(qry_index_combco);
|
|
128
|
+
free(qry_abund);
|
|
129
|
+
}
|
|
130
|
+
#define MIN_KM_S 6
|
|
131
|
+
#define ST_PCTL (0.98)
|
|
132
|
+
#define ED_PCTL (0.99)
|
|
133
|
+
int *sort_ref = malloc(ref_dstat.infile_num * sizeof(int));
|
|
134
|
+
for (int i = 0; i < ref_dstat.infile_num; i++) sort_ref[i] = i;
|
|
135
|
+
qsort(sort_ref, ref_dstat.infile_num, sizeof(sort_ref[0]), comparator_idx);
|
|
136
|
+
for (int i = 0; i < ref_dstat.infile_num; i++) {
|
|
137
|
+
int kmer_num = ref_abund[sort_ref[i]][0];
|
|
138
|
+
if (kmer_num < MIN_KM_S) break;
|
|
139
|
+
qsort(ref_abund[sort_ref[i]] + 1, kmer_num, sizeof(int), comparator);
|
|
140
|
+
int sum = 0;
|
|
141
|
+
for (int n = 1; n <= kmer_num; n++) sum += ref_abund[sort_ref[i]][n];
|
|
142
|
+
int median_idx = kmer_num / 2;
|
|
143
|
+
int pct09_idx = kmer_num * ST_PCTL;
|
|
144
|
+
int lastsum = 0;
|
|
145
|
+
int lastn = 0;
|
|
146
|
+
for (int n = pct09_idx; n <= kmer_num * ED_PCTL; n++) {
|
|
147
|
+
lastsum += ref_abund[sort_ref[i]][n];
|
|
148
|
+
lastn++;
|
|
149
|
+
}
|
|
150
|
+
printf("%s\t%s\t%d\t%f\t%f\t%d\t%d\n", qryname[qn], refname[sort_ref[i]], kmer_num, (float) sum / kmer_num,
|
|
151
|
+
(float) lastsum / lastn, ref_abund[sort_ref[i]][median_idx], ref_abund[sort_ref[i]][pct09_idx]);
|
|
152
|
+
}
|
|
153
|
+
free(sort_ref);
|
|
154
|
+
}
|
|
155
|
+
for (int i = 0; i < ref_dstat.infile_num; i++) free(ref_abund[i]);
|
|
156
|
+
free(ref_abund);
|
|
157
|
+
free(refname);
|
|
158
|
+
free(qryname);
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
int comparator_idx(const void *a, const void *b) {
|
|
163
|
+
return (ref_abund[*(int *) b][0] - ref_abund[*(int *) a][0]);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
int comparator(const void *a, const void *b) {
|
|
167
|
+
return (*(int *) a - *(int *) b);
|
|
168
|
+
}
|