Commit ad4abee7 authored by Matthias Schroeder's avatar Matthias Schroeder

reduced mem requirements, improved output readability, fixed several bugs ...

parent a33cf87e
......@@ -32,13 +32,20 @@ typedef struct {
int64_t last; // list length
Moli* a; // list of moli tuples
} Moli1;
Moli1* mols[MAX_MASS]; // array of lists tio store mass and formula tuples
// both just for reading textfiles
Moli* mols0;
int64_t mols0_last = 0;
typedef struct {
uint64_t mass;
int a[MAX_ELE];
} Masses;
Masses masses[2500000];
int last_masses = 0;
typedef struct {
char formel[10];
uint64_t mass;
......@@ -102,7 +109,6 @@ Count counts[] = {
int NBR_COUNTS= sizeof(counts)/sizeof(Count);
void dump_aF(char* buf, int* aF) {
buf[0]= '\0';
for (int i=0; i<MAX_ELE; i++) {
......@@ -167,8 +173,17 @@ void formel2aF(int* aF, char* f) {
}
char* aF2list(char* f, int* aF) {
f[0]= '\0';
for (int i=0; i<NBR_ELEMENTS; i++) {
sprintf(f+strlen(f), "%d\t", aF[i]);
}
f[strlen(f)-1]='\0';
return f;
}
char* aF2formel(char* f, int* aF) {
int i=0;
f[0]= '\0';
for (int i=0; i<NBR_ELEMENTS; i++) {
......@@ -176,7 +191,7 @@ char* aF2formel(char* f, int* aF) {
sprintf(f+strlen(f), "%s_%d ", elements[i].formel, aF[i]);
}
}
f[strlen(f)-1]='\0';
return f;
}
......@@ -243,6 +258,21 @@ uint64_t aF2int(int* aF) {
}
void mass2str(char* s, uint64_t m) {
char r[256];
sprintf(r, "%010ld", m);
char* nk= r+(strlen(r)-9);
char* p=r;
char* q=s;
while (*p) {
*q=*p;
p++;
q++;
if (p==nk) *q++= '.';
}
*q= '\0';
}
uint64_t mass_aF(int* aF) {
uint64_t m= 0;
for (int i = 0; i < NBR_ELEMENTS; ++i) {
......@@ -351,7 +381,7 @@ void read_file(char* infilename) {
mols0 = (Moli*)malloc(sizeof(Moli) * 100000000);
if( (fp=fopen(infilename,"r")) == NULL) {
fprintf(stderr, "Kann %s nicht oeffnen\n", infilename);
// fprintf(stderr, "Kann %s nicht oeffnen\n", infilename);
exit(9);
}
while(fgets(line, ZEILENLAENGE, fp)) {
......@@ -386,7 +416,7 @@ Moli1* read_binfile(char* infilename) {
m= (Moli1*)malloc(sizeof(Moli1));
if( (fp=fopen(infilename,"rb")) == NULL) {
fprintf(stderr, "Kann %s nicht oeffnen\n", infilename);
// fprintf(stderr, "Kann %s nicht oeffnen\n", infilename);
m->last= 0;
return m;
}
......@@ -410,6 +440,14 @@ void read_filesfor(int64_t w, int64_t tolerance) {
char fn[256];
int iweight;
iweight = weight2int(w-tolerance);
if (iweight>5 && iweight<2000) {
for (int i=0; i<iweight-5; i++) {
if (mols[i] && mols[i]->last) {
free(mols[i]->a);
mols[i]->last=0;
}
}
}
if (iweight>0 && iweight<2000) {
sprintf(fn, "weights/w%04d.bin", iweight);
if (!mols[iweight])
......@@ -492,15 +530,30 @@ void init_alternatives() {
}
}
}
for (int i = 0; i < NBR_COUNTS; ++i) {
printf("%8s Delta=%12ld (%12ld)\n", counts[i].formel, counts[i].delta, counts[i].e.mass);
// for (int i = 0; i < NBR_COUNTS; ++i) {
// printf("%8s Delta=%12ld (%12ld) R=%s %d\n", counts[i].formel, counts[i].delta, counts[i].e.mass, counts[i].ref, counts[i].iref);
// }
}
int check_mol(int* aF) {
int sum= 0;
for (int i=COUNTS_OFFSET; i<NBR_ELEMENTS; i++) {
sum+= aF[i];
}
if (sum>2) return 0;
for (int i=0; i<NBR_ELEMENTS; i++) {
if (aF[i]<0) return 0;
}
if (val_aF(aF) % 2 == 1) return 0;
return 1;
}
void search_weight_moli1(int iw, uint64_t searchweight, uint64_t tolerance) {
int aF[MAX_ELE];
char formel[256];
char formel[256], slist[256], str[128];
if (iw<0) return;
Moli1* m = mols[iw];
if (!m) return;
......@@ -508,14 +561,19 @@ void search_weight_moli1(int iw, uint64_t searchweight, uint64_t tolerance) {
for (int i=0; i< m->last; i++) {
if (labs(m->a[i].mass - searchweight) < tolerance) {
int2aF(aF, m->a[i].formel);
for (int j; j<NBR_COUNTS; j++) {
for (int j=0; j<NBR_COUNTS; j++) {
aF[j+COUNTS_OFFSET]= counts[j].cur;
if (counts[j].cur && counts[j].ref && aF[counts[j].iref] >= counts[j].cur) {
if (counts[j].cur && counts[j].ref) {
aF[counts[j].iref] -= counts[j].cur;
}
}
aF2formel(formel, aF);
printf ("%ld, %s (%ld <%ld>)\n", m->a[i].mass, formel, labs(m->a[i].mass - searchweight), tolerance);
if (check_mol(aF)) {
aF2formel(formel, aF);
aF2list(slist, aF);
mass2str(str, mass_aF(aF));
printf ("%s\t%s\t%s\n",
str, slist, formel);
}
}
}
}
......@@ -535,6 +593,32 @@ void search_weight(uint64_t searchweight, uint64_t tolerance) {
}
void push_mass(uint64_t mass) {
masses[last_masses].mass= mass;
for (int j=0; j<NBR_COUNTS; j++) {
masses[last_masses].a[j]= counts[j].cur;
}
last_masses++;
}
void pull_mass(int i) {
for (int j=0; j<NBR_COUNTS; j++) {
counts[j].cur= masses[i].a[j];
}
}
int compareMasses( const void* a, const void* b)
{
Masses ma = * ( (Masses*) a );
Masses mb = * ( (Masses*) b );
if ( ma.mass == mb.mass ) return 0;
else if ( ma.mass < mb.mass ) return -1;
else return 1;
}
void main(int argc, char* *argv) {
char* outfilename;
int doread= 0;
......@@ -578,19 +662,32 @@ void main(int argc, char* *argv) {
write_binfile(outfilename);
} else if (doweight) {
init_alternatives();
uint64_t dw;
read_filesfor(searchweight, tolerance);
search_weight(searchweight, tolerance);
uint64_t dw=0;
init_alternatives();
push_mass(searchweight);
while (next_mol()) {
dw= mass_mol();
if (dw < searchweight) {
read_filesfor(searchweight-mass_mol(), tolerance);
search_weight(searchweight-mass_mol(), tolerance);
push_mass(searchweight-dw);
}
}
//print table header
char molsnames[256];
molsnames[0]=0;
for (int i=0; i<NBR_ELEMENTS; i++)
sprintf(molsnames+strlen(molsnames),"%s\t", elements[i].formel);
*(molsnames+strlen(molsnames)-1)= '\0';
printf("mol_mass1\t%s\tformula\n",molsnames);
qsort(masses, last_masses, sizeof(Masses), compareMasses);
for (int i=0; i<last_masses; i++) {
pull_mass(i);
read_filesfor(masses[i].mass, tolerance);
search_weight(masses[i].mass, tolerance);
}
} else {
......
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