modified: SpatialOmicsCoordOld.py
[GalaxyCodeBases.git] / c_cpp / salusFstReCoord / src1 / worker.c
blob456dc11ff69d5f88af7b66afe8216a6f4a08305c
1 #include <math.h> // floor
3 #include "common.h"
5 FORCE_INLINE void transCorrd(double *ChipXY, const double *FovXY, const int_least16_t *FovRowCol) {
6 double new_x = (FovRowCol[1] - (CenterFOV_COL - 1)) * FOV_USED_WIDTH - FovXY[1] - 1;
7 double new_y = FovXY[0] + (FovRowCol[0] - (CenterFOV_ROW - 1) - 1) * FOV_USED_HEIGHT;
8 ChipXY[0] = floor(new_x * 100.0) / 100.0;
9 ChipXY[1] = floor(new_y * 100.0) / 100.0;
12 FORCE_INLINE char *strncpy_no_colon(char *restrict dest, const char *restrict src, size_t n) {
13 size_t j = 0; // destination index
14 for (size_t i = 0; i < n && src[i] != '\0'; i++) {
15 if (src[i] != ':') dest[j++] = src[i];
17 // for ( ; j < n; j++) dest[j] = '\0';
18 if (j < n) dest[j] = '\0';
19 return dest;
22 void worker(int_least16_t worker_id) {
23 workerArray_t *worker = &Parameters.worksQuene[worker_id];
24 regmatch_t matches[2];
25 char **splitSets = worker->tokens;
26 // char* readName = malloc(91); // for testing CHARsCPYSTR
27 char readName[MAXFQIDLEN + 1] = {0}; // 81 => [0,80]
28 char fovRC[9] = {0}; // R123C567
29 double unZoomRatio = (double)Parameters.unZoomRatio;
30 for (uint64_t index = 0; index < JOBITEMSIZE; index++) {
31 fstBCdata_t *fstBCdata_p = &worker->jobDatArray[index];
32 if (fstBCdata_p->name[0] == 0) {
33 break;
35 // fstBCoutput_t *fstBCoutput_p = &worker->output_array[index];
36 ARRAYcpySTR(readName, fstBCdata_p->name);
37 assert(readName[sizeof(fstBCdata_p->name)] == '\0');
38 #ifndef RELEASE
39 fprintf(stderr, "###### %llu\t[%s] %d [%s]<--\n", index, readName, readName[sizeof(fstBCdata_p->name)], fstBCdata_p->comment);
40 #else
41 fprintf(stderr, "###### %llu\t[%s] %d <--\n", index, readName, readName[sizeof(fstBCdata_p->name)]);
42 #endif
43 int_least16_t RowCol[2] = {0};
44 double oldXY[2] = {0.0};
45 double newXY[2] = {0.0};
46 char *delim = ":";
47 if (regexec(&Parameters.regex, readName, 2, matches, 0) != 0) {
48 fstBCdata_p->name[0] = 0;
49 continue;
51 //__builtin_dump_struct(&matches[0], &printf);
52 //__builtin_dump_struct(&matches[1], &printf);
53 size_t relen = matches[0].rm_eo - matches[0].rm_so;
54 strncpy_no_colon(fovRC, readName + matches[0].rm_so, relen);
55 // fovRC[relen-1] = '\0';
56 assert(fovRC[sizeof(fovRC) - 1] == '\0');
57 // memcpy(fstBCdata_p->RowCol, fovRC, relen-1);
58 fstBCdata_p->fov_row = (uint8_t)(RowCol[0] = atoi(fovRC + 1));
59 fstBCdata_p->fov_column = (uint8_t)(RowCol[1] = atoi(fovRC + 5));
60 if (unlikely(matches[1].rm_so == -1)) {
61 delim = "_";
62 } else {
63 delim = ":";
65 assert(relen == (delim[0] == ':' ? sizeof(fovRC) : sizeof(fovRC) - 1));
66 // printf("%llu\t[%s], delim:[%s], fov[%s]\n", index, readName, delim, fovRC);
67 const char *theDelim = delim;
68 char *saveptr = NULL;
69 char *token = strtok_r(readName, theDelim, &saveptr);
70 // printf("-f- [%zu] [%zu] [%s]\n", readName, token, token);
71 if (unlikely(token == NULL)) {
72 // printf("-b->\t[%s], delim:[%s]\n", readName, theDelim);
73 fstBCdata_p->name[0] = 0;
74 break;
75 } else {
76 int_least16_t idx = 0;
77 for (idx = 0; likely(token != NULL); token = strtok_r(NULL, theDelim, &saveptr), idx++) {
78 splitSets[idx] = token;
79 // printf("-s- %d:[%zu] [%s]\n", idx, splitSets[idx], token);
81 oldXY[1] = atof(splitSets[idx - 1]);
82 oldXY[0] = atof(splitSets[idx - 2]);
83 // printf("oldXY: [%s],[%s]\n", splitSets[idx - 2], splitSets[idx - 1]);
84 // printf("[%s]->[%s]=RC(%d,%d), X:%f Y:%f\n", readName, fovRC, RowCol[0], RowCol[1], oldXY[0], oldXY[1]);
85 /* for (idx = 0; idx < MAXDELIMITEMS; idx++) {
86 printf("-t- %d:[%zu] [%s]\n", idx, (void *)splitSets[idx], splitSets[idx]);
87 } */
89 if ((FOV_X_MIN < oldXY[0] && oldXY[0] <= FOV_X_MAX) && (FOV_Y_MIN < oldXY[1] && oldXY[1] <= FOV_Y_MAX)) {
90 if (unlikely(unZoomRatio != 1.0f)) {
91 oldXY[0] /= unZoomRatio;
92 oldXY[1] /= unZoomRatio;
94 oldXY[0] -= FOV_X_MIN;
95 oldXY[1] -= FOV_Y_MIN;
96 transCorrd(newXY, oldXY, RowCol);
97 // printf("-->gX:%.2f gY:%.2f\n", newXY[0], newXY[1]);
98 memcpy(fstBCdata_p->newXY, newXY, sizeof(newXY));
99 #ifdef DEBUG
100 char readSeq[BARCODELEN + 1];
101 ARRAYcpySTR(readSeq, fstBCdata_p->seq);
102 fprintf(stderr, "->SpatiaStr:[%s %.2f %.2f]\n", readSeq, fstBCdata_p->newXY[0], fstBCdata_p->newXY[1]);
103 #endif
104 } else {
105 fstBCdata_p->name[0] = 0;