Commit b8d8021d authored by Matthias Schroeder's avatar Matthias Schroeder

first working version

parent f540d17d
......@@ -16,11 +16,33 @@ typedef struct {
int C, H, O;
char rest[32];
int group;
int rgroup;
} MOLS;
MOLS* mols;
int lastmols = 0;
int hom_ch2 = 1,
hom_o = 0,
hom_h2 = 0,
hom_co2 = 0,
hom_h2o = 0;
int cmpMOLS (const void * a, const void * b) {
MOLS *ma, *mb;
int r = 0;
ma = (MOLS *)a;
mb = (MOLS *)b;
r = strcmp(ma->rest, mb->rest);
if (r!=0) return r;
if (ma->C < mb->C) return -1;
if (ma->C > mb->C) return 1;
if (ma->H < mb->H) return -1;
if (ma->H > mb->H) return 1;
if (ma->O < mb->O) return -1;
if (ma->O > mb->O) return 1;
return 0;
}
void read_homsearch_csv(char* fname) {
char line[LINE_SIZE], buf[32];
int line_count, bufcnt, colidx = 0,
......@@ -58,7 +80,7 @@ void read_homsearch_csv(char* fname) {
}
}
colMass = colidx - 1;
printf("C:%d\nH:%d\nO:%d\nMASS:%d\n", colC, colH, colO, colMass);
fprintf(stderr, "C:%d\nH:%d\nO:%d\nMASS:%d\n", colC, colH, colO, colMass);
int i = 0;
char* end;
......@@ -87,19 +109,91 @@ void read_homsearch_csv(char* fname) {
buf[bufcnt++] = *c;
}
}
printf("id=%8d C:%3d H:%3d O:%3d rest:%s\n", mols[i].id, mols[i].C, mols[i].H, mols[i].O, mols[i].rest);
lastmols = i;
i+=1;
}
fclose(infh);
qsort(mols, lastmols, sizeof(MOLS), cmpMOLS);
int rgroup = 0;
for (int i=0; i<lastmols; i++) {
if (strcmp(line, mols[i].rest)) {
rgroup += 1;
strcpy(line, mols[i].rest);
}
mols[i].rgroup = rgroup;
mols[i].group = 0;
}
// for (int i=0; i<lastmols; i++) {
// printf("id=%8d C:%3d H:%3d O:%3d rest:%s (%d)\n", mols[i].id, mols[i].C, mols[i].H, mols[i].O, mols[i].rest, mols[i].rgroup);
// }
}
int homcheck(MOLS *ma, MOLS *mb) {
if (ma->O == mb->O && ma->C == mb->C && ma->H == mb->H) return 9;
// CH2
if (hom_ch2 && ma->O == mb->O && ma->C+1 == mb->C && ma->H+2 == mb->H) return 1;
// O
if (hom_o && ma->C == mb->C && ma->H == mb->H && ma->O+1 == mb->O) return 2;
// H2
if (hom_h2 && ma->C == mb->C && ma->O == mb->O && ma->H+2 == mb->H) return 3;
// CO2
if (hom_co2 && ma->H == mb->H && ma->C+1 == mb->C && ma->O+2 == mb->O) return 4;
// h2O
if (hom_h2o && ma->C == mb->C && ma->H+2 == mb->H && ma->O+1 == mb->O) return 5;
return 0;
}
void overwritegroup(int old, int new) {
if (old==new) return;
// printf("ov %d > %d\n", old, new);
for (int i=0; i<lastmols; i++) {
if (mols[i].group == old) {
mols[i].group = new;
}
}
}
void do_homsearch() {
idx = 0;
int agroup;
int newhomgroup = 0;
int homgroup;
for (int idx=0; idx<lastmols; idx++) {
agroup = mols[idx].rgroup;
if (mols[idx].group == 0) {
newhomgroup += 1;
mols[idx].group = newhomgroup;
}
homgroup = mols[idx].group;
for (int bidx=idx+1; bidx<lastmols && mols[bidx].rgroup == agroup; bidx++) {
// CH2
if (homcheck(&(mols[idx]), &(mols[bidx]))) {
if (mols[bidx].group == 0) {
mols[bidx].group = homgroup;
} else {
overwritegroup(mols[bidx].group, homgroup);
}
}
}
}
}
void main(int argc, char* *argv) {
read_homsearch_csv("homsearch.csv");
char homfilename[128];
if (argc>1) {
strcpy(homfilename, argv[1]);
} else {
strcpy(homfilename, "homsearch.csv");
}
read_homsearch_csv(homfilename);
do_homsearch();
for (int i=0; i<lastmols; i++) {
// fprintf(stderr, "id=%8d C:%3d H:%3d O:%3d rest:%s (%d) G%d\n", mols[i].id, mols[i].C, mols[i].H, mols[i].O, mols[i].rest, mols[i].rgroup, mols[i].group);
printf("%d,%d\n", mols[i].id, mols[i].group);
}
free(mols);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment