9#include "tpcclibConfig.h"
26static char *info[] = {
27 "Non-linear fitting of dual input compartment model, with one tissue",
28 "compartment for each input (parent tracer and its labelled metabolite):",
30 " _____ K1p ______ k3p ______ ",
31 " | Cap | ----> | Ct1p | ----> | Ct2p | ",
32 " |_____| <---- |______| <---- |______| ",
36 " | Cam | ----> | Ctm | ",
37 " |_____| <---- |_____| ",
40 "Note that the model may be too complex to provide reliable parameter",
41 "estimates in most practical use cases.",
43 "Usage: @P [Options] ptacfile mtacfile btacfile ttacfile endtime resultfile",
47 " Specify the constraints for model parameters;",
48 " This file with default values can be created by giving this",
49 " option as the only command-line argument to this program.",
50 " Without filename the default values are printed on screen.",
52 " Standard deviations are calculated and saved in results (y),",
53 " or not calculated (N, default).",
54 " Program runs a lot faster if SD and CL are not calculated.",
56 " 95% Confidence limits are calculated and saved in results (y), or",
57 " not calculated (N, default).",
59 " Enter a fixed Vb; fitted by default.",
60 " If Vb (vascular blood volume) is pre-corrected or to be ignored, set",
61 " it to 0; btacfile can be set to 'none'.",
63 " Fit-based metabolite corrected regional TACs are written in the file.",
65 " Fitted regional TACs are written in the file.",
67 " Fitted and measured TACs are plotted in specified SVG file.",
70 "Sample times must be in minutes.",
72 "Example: fitting with default settings",
73 " @P ia919apc.kbq ia919apm.kbq ia919ab.kbq ia919.dft 60 a919k4di.res",
75 "See also: fitk2di, fitk2, fitk4, sim_3tcm, tacweigh, taccbv",
77 "Keywords: TAC, modelling, distribution volume, dual-input",
93const int parNr=7, parVb=6;
95double *petmeas, *petsim;
100static char *parName[] = {
"K1p",
"V1p",
"k3p",
"k4p",
"K1m",
"V1m",
"Vb", 0};
103double func2TCMdi(
int parNr,
double *p,
void*);
110int main(
int argc,
char **argv)
112 int ai, help=0, version=0, verbose=1;
113 int ri, fi, pi, n, ret;
114 char dfile[FILENAME_MAX], bfile[FILENAME_MAX],
115 pfile[FILENAME_MAX], mfile[FILENAME_MAX],
116 ffile[FILENAME_MAX], mcfile[FILENAME_MAX],
117 rfile[FILENAME_MAX], limfile[FILENAME_MAX], svgfile[FILENAME_MAX];
118 char *cptr, tmp[FILENAME_MAX];
120 double f, fitdur, wss;
122 int doBootstrap=0, doSD=0, doCL=0;
123 double *sd, *cl1, *cl2;
125 int tgoNr=0, neighNr=0, iterNr=0, fittedparNr=0;
130 def_pmin[0]=0.0; def_pmax[0]=10.0;
131 def_pmin[1]=0.0; def_pmax[1]=500.0;
132 def_pmin[2]=0.0; def_pmax[2]=1.0;
133 def_pmin[3]=0.0; def_pmax[3]=1.0;
134 def_pmin[4]=0.0; def_pmax[4]=10.0;
135 def_pmin[5]=0.0; def_pmax[5]=5.0;
136 def_pmin[6]=0.0; def_pmax[6]=0.10;
142 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
143 dfile[0]=pfile[0]=mfile[0]=bfile[0]=(char)0;
144 rfile[0]=ffile[0]=limfile[0]=svgfile[0]=mcfile[0]=(char)0;
148 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
149 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
151 if(strncasecmp(cptr,
"CL", 2)==0) {
152 if(strlen(cptr)==2) {doCL=1;
continue;}
153 cptr+=2;
if(*cptr==
'=') {
155 if(*cptr==
'Y' || *cptr==
'y') {doCL=1;
continue;}
156 if(*cptr==
'N' || *cptr==
'n') {doCL=0;
continue;}
158 }
else if(strncasecmp(cptr,
"SD", 2)==0) {
159 if(strlen(cptr)==2) {doSD=1;
continue;}
160 cptr+=2;
if(*cptr==
'=') {
162 if(*cptr==
'Y' || *cptr==
'y') {doSD=1;
continue;}
163 if(*cptr==
'N' || *cptr==
'n') {doSD=0;
continue;}
165 }
else if(strncasecmp(cptr,
"LIM=", 4)==0 && strlen(cptr)>4) {
166 strlcpy(limfile, cptr+4, FILENAME_MAX);
continue;
167 }
else if(strcasecmp(cptr,
"LIM")==0) {
168 strcpy(limfile,
"stdout");
continue;
169 }
else if(strncasecmp(cptr,
"Vb=", 3)==0 && strlen(cptr)>3) {
171 if(fVb>=0.0 && fVb<1.0) {
172 if(fVb<0.01) fprintf(stderr,
"Warning: Vb was set to %g%%\n", 100.*fVb);
173 def_pmin[parVb]=def_pmax[parVb]=fVb;
177 }
else if(strncasecmp(cptr,
"MC=", 3)==0) {
178 strlcpy(mcfile, cptr+3, FILENAME_MAX);
if(strlen(mcfile)>0)
continue;
179 }
else if(strncasecmp(cptr,
"FIT=", 4)==0) {
180 strlcpy(ffile, cptr+4, FILENAME_MAX);
if(strlen(ffile)>0)
continue;
181 }
else if(strncasecmp(cptr,
"SVG=", 4)==0) {
182 strlcpy(svgfile, cptr+4, FILENAME_MAX);
if(strlen(svgfile)>0)
continue;
184 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
190 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
195 for(ai=1; ai<argc; ai++)
if(*argv[ai]!=
'-') {
196 if(!pfile[0]) {
strlcpy(pfile, argv[ai], FILENAME_MAX);
continue;}
197 else if(!mfile[0]) {
strlcpy(mfile, argv[ai], FILENAME_MAX);
continue;}
198 else if(!bfile[0]) {
strlcpy(bfile, argv[ai], FILENAME_MAX);
continue;}
199 else if(!dfile[0]) {
strlcpy(dfile, argv[ai], FILENAME_MAX);
continue;}
200 else if(fitdur<0) {fitdur=
atof_dpi(argv[ai]);
if(fitdur>0.0)
continue;}
201 else if(!rfile[0]) {
strlcpy(rfile, argv[ai], FILENAME_MAX);
continue;}
203 fprintf(stderr,
"Error: too many arguments: '%s'.\n", argv[ai]);
206 if(doSD || doCL) doBootstrap=1;
else doBootstrap=0;
208 if(strcasecmp(bfile,
"NONE")==0) {strcpy(bfile,
""); fVb=0.0;}
212 if(limfile[0] && !pfile[0]) {
214 if(strcasecmp(limfile,
"stdout")!=0 && access(limfile, 0) != -1) {
215 fprintf(stderr,
"Error: parameter constraint file %s exists.\n", limfile);
218 if(verbose>1) printf(
"writing parameter constraints file\n");
221 for(pi=0; pi<parNr; pi++) {
222 if(pi==1)
iftPut(&ift, NULL,
"V1p=K1p/k2p",
"#", 0);
223 else if(pi==5)
iftPut(&ift, NULL,
"V1m=K1m/k2m",
"#", 0);
224 sprintf(tmp,
"%s_lower", parName[pi]);
226 sprintf(tmp,
"%s_upper", parName[pi]);
229 if((ret=
iftWrite(&ift, limfile, 0))!=0) {
230 fprintf(stderr,
"Error in writing '%s': %s\n", limfile, ift.
status);
233 if(strcasecmp(limfile,
"stdout")!=0) fprintf(stdout,
234 "Parameter file %s with initial values written.\n", limfile);
239 if(fitdur==0) fitdur=1.0E+100;
240 else if(fitdur<0) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
242 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
248 printf(
"pfile := %s\n", pfile);
249 printf(
"mfile := %s\n", mfile);
250 printf(
"dfile :=%s\n", dfile);
251 printf(
"rfile := %s\n", rfile);
252 printf(
"mcfile := %s\n", mcfile);
253 printf(
"ffile := %s\n", ffile);
254 printf(
"svgfile := %s\n", svgfile);
255 printf(
"limfile := %s\n", limfile);
256 printf(
"fitdur := %g\n", fitdur);
257 printf(
"doBootstrap := %d\n", doBootstrap);
258 printf(
"doSD := %d\n", doSD);
259 printf(
"doCL := %d\n", doCL);
269 if(verbose>1) printf(
"reading %s\n", limfile);
270 ret=
iftRead(&ift, limfile, 1, 0);
272 fprintf(stderr,
"Error in reading '%s': %s\n", limfile, ift.
status);
275 if(verbose>10)
iftWrite(&ift,
"stdout", 0);
278 for(pi=0; pi<parNr; pi++) {
279 sprintf(tmp,
"%s_lower", parName[pi]);
281 sprintf(tmp,
"%s_upper", parName[pi]);
285 if(n==0) {fprintf(stderr,
"Error: invalid parameter file.\n");
return(9);}
288 for(pi=n=0, ret=0; pi<parNr; pi++) {
289 if(def_pmin[pi]<0.0) ret++;
290 if(def_pmax[pi]<def_pmin[pi]) ret++;
291 if(def_pmax[pi]>def_pmin[pi]) n++;
294 fprintf(stderr,
"Error: invalid parameter constraints.\n");
298 fprintf(stderr,
"Error: no model parameters left free for fitting.\n");
303 if(fVb>=0.0) def_pmin[parVb]=def_pmax[parVb]=fVb;
304 if(def_pmin[parVb]==def_pmax[parVb]) fVb=def_pmin[parVb];
305 if(fVb==0.0) strcpy(bfile,
"");
307 printf(
"bfile := %s\n", bfile);
311 if(def_pmax[1]==0.0) {
312 def_pmin[2]=def_pmax[2]=def_pmin[3]=def_pmax[3]=0.0;
313 if(def_pmax[0]>0.0) irrev=1;
316 if(def_pmax[3]==0.0 && def_pmax[2]>0.0) irrev=1;
318 if(def_pmax[2]==0.0) def_pmin[3]=def_pmax[3]=0.0;
320 if(!(def_pmax[4]>0.0)) def_pmin[5]=def_pmax[5]=0.0;
326 if(verbose>1) printf(
"reading tissue and input data\n");
328 &fitframeNr, &dft, &input, stdout, verbose-2, tmp);
330 fprintf(stderr,
"Error: %s\n", tmp);
333 if(fitframeNr<parNr+1 || input.
frameNr<parNr+1) {
334 fprintf(stderr,
"Error: too few samples in specified fit duration.\n");
338 fprintf(stderr,
"Error: valid plasma TACs must be provided.\n");
343 if(verbose>2) printf(
"setting blood tac to zero\n");
346 fprintf(stderr,
"Error: cannot allocate more memory.\n");
350 for(fi=0; fi<input.
frameNr; fi++) input.
voi[2].
y[fi]=0.0;
353 def_pmin[parVb]=def_pmax[parVb]=fVb=0.0;
356 if(fVb>=0.0) printf(
"fVb := %g\n", fVb);
362 fprintf(stdout,
"common_data_weights := %g", dft.
w[0]);
363 for(fi=1; fi<dft.
frameNr; fi++) fprintf(stdout,
", %g", dft.
w[fi]);
364 fprintf(stdout,
"\n");
371 fprintf(stderr,
"Error: cannot allocate more memory.\n");
383 if(verbose>1) printf(
"initializing result data\n");
385 fprintf(stderr,
"Error: cannot setup memory for results.\n");
396 if(fVb>=0.0) res.
Vb=100.0*fVb;
else res.
Vb=-1.0;
406 pi=0; strcpy(res.
parname[pi],
"K1p"); strcpy(res.
parunit[pi],
"ml/(min*ml)");
407 pi++; strcpy(res.
parname[pi],
"K1p/k2p"); strcpy(res.
parunit[pi],
"ml/ml");
408 pi++; strcpy(res.
parname[pi],
"k3p"); strcpy(res.
parunit[pi],
"1/min");
409 pi++; strcpy(res.
parname[pi],
"k4p"); strcpy(res.
parunit[pi],
"1/min");
410 pi++; strcpy(res.
parname[pi],
"K1m"); strcpy(res.
parunit[pi],
"ml/(min*ml)");
411 pi++; strcpy(res.
parname[pi],
"K1m/k2m"); strcpy(res.
parunit[pi],
"ml/ml");
412 pi++; strcpy(res.
parname[pi],
"Vb"); strcpy(res.
parunit[pi],
"%");
414 if(irrev==0) {strcpy(res.
parname[pi],
"DV"); strcpy(res.
parunit[pi],
"ml/ml");}
415 else {strcpy(res.
parname[pi],
"Ki"); strcpy(res.
parunit[pi],
"ml/(min*ml)");}
416 pi++; strcpy(res.
parname[pi],
"WSS"); strcpy(res.
parunit[pi],
"");
423 fprintf(stderr,
"Error %d in memory allocation for fitted curves.\n", ret);
433 fprintf(stdout,
"fitting regional TACs: ");
434 if(verbose>1) fprintf(stdout,
"\n");
437 for(ri=0; ri<dft.
voiNr; ri++) {
438 if(verbose>2) printf(
"\n %d %s\n", ri, dft.
voi[ri].
name);
441 petmeas=dft.
voi[ri].
y; petsim=fit.
voi[ri].
y;
444 for(pi=0; pi<parNr; pi++) {pmin[pi]=def_pmin[pi]; pmax[pi]=def_pmax[pi];}
445 for(pi=fittedparNr=0; pi<parNr; pi++)
if(pmax[pi]>pmin[pi]) fittedparNr++;
446 if(ri==0 && verbose>1) {
447 printf(
" constraints :=");
448 for(pi=0; pi<parNr; pi++) printf(
" [%g,%g]", pmin[pi], pmax[pi]);
450 printf(
"fittedparNr := %d\n", fittedparNr);
456 tgoNr=60+50*fittedparNr;
457 neighNr=5*fittedparNr;
460 pmin, pmax, func2TCMdi, NULL, parNr, neighNr,
463 fprintf(stderr,
"\nError in optimization (%d).\n", ret);
474 if(verbose>2) printf(
"\n bootstrapping\n");
477 if(doSD) sd=res.
voi[ri].
sd;
else sd=NULL;
478 if(doCL) {cl1=res.
voi[ri].
cl1; cl2=res.
voi[ri].
cl2;}
else cl1=cl2=NULL;
488 parNr, dft.
w, func2TCMdi, tmp, verbose-4
491 fprintf(stderr,
"Error in bootstrap: %s\n", tmp);
492 for(pi=0; pi<parNr; pi++) {
493 if(doSD) sd[pi]=nan(
"");
494 if(doCL) cl1[pi]=cl2[pi]=nan(
"");
515 if(dft.
voiNr>2 && verbose==1) {
516 fprintf(stdout,
"."); fflush(stdout);}
519 if(verbose>0) {fprintf(stdout,
"\n"); fflush(stdout);}
522 for(ri=0; ri<res.
voiNr; ri++) {
524 if(!isnan(res.
voi[ri].
cl1[parVb])) res.
voi[ri].
cl1[parVb]*=100.;
525 if(!isnan(res.
voi[ri].
cl2[parVb])) res.
voi[ri].
cl2[parVb]*=100.;
526 if(!isnan(res.
voi[ri].
sd[parVb])) res.
voi[ri].
sd[parVb]*=100.;
530 for(ri=0; ri<res.
voiNr; ri++) {
535 macrop=k1k2;
if(k3k4>0.0) macrop*=(1.0+k3k4);
540 macrop=k1*k3/(k2+k3);
548 if(verbose>0) {
resPrint(&res); fprintf(stdout,
"\n");}
554 if(verbose>1) printf(
"saving results\n");
555 ret=
resWrite(&res, rfile, verbose-3);
557 fprintf(stderr,
"Error in writing '%s': %s\n", rfile,
reserrmsg);
561 if(verbose>0) fprintf(stdout,
"Model parameters written in %s\n", rfile);
568 if(verbose>1) printf(
"calculating mc curves\n");
574 fprintf(stderr,
"Error: cannot make mc curves.\n");
581 double K1p, k2p, k3p, k4p, Vb;
582 for(ri=0; ri<dft2.
voiNr; ri++) {
592 K1p, k2p, k3p, k4p, 0, 0, 0, 0, 0, 0,
593 0.0, Vb, 1.0, input.
voi[0].
y2, NULL, NULL, NULL, NULL, NULL, NULL, verbose-20);
595 if(verbose>1) printf(
"error %d in simulation\n", ret);
596 fprintf(stderr,
"Error: cannot calculate metabolite-free curve.\n");
610 if(verbose>1) printf(
"error %d in interpolation\n", ret);
611 fprintf(stderr,
"Error: cannot interpolate metabolite-free curve.\n");
618 if(verbose>1) printf(
"saving mc curves\n");
621 sprintf(dft2.
comments,
"# program := %s\n", tmp);
623 fprintf(stderr,
"Error in writing '%s': %s\n", mcfile,
dfterrmsg);
627 if(verbose>0) printf(
"MC TACs written in %s\n", mcfile);
636 if(svgfile[0] || ffile[0]) {
643 if(verbose>1) printf(
"saving SVG plot\n");
644 sprintf(tmp,
"2TCM fit with dual input: ");
647 0.0, nan(
""), svgfile, verbose-5);
649 fprintf(stderr,
"Error (%d) in writing '%s'.\n", ret, svgfile);
653 if(verbose>0) printf(
"Plots written in %s\n", svgfile);
658 if(verbose>1) printf(
"saving fitted curves\n");
660 sprintf(fit.
comments,
"# program := %s\n", tmp);
662 fprintf(stderr,
"Error in writing '%s': %s\n", ffile,
dfterrmsg);
666 if(verbose>0) printf(
"Fitted TACs written in %s\n", ffile);
682double func2TCMdi(
int parNr,
double *p,
void *fdata)
685 double K1p, k2p, k3p, k4p, K1m, k2m, Vb, d, wss=0.0;
694 K1p=pa[0];
if(pa[1]>0.0 && K1p>0.0) k2p=K1p/pa[1];
else k2p=0.0;
696 k3p=pa[2]; k4p=pa[3];
if(k3p==0.0) k4p=0;
700 K1m=pa[4];
if(pa[5]>0.0 && K1m>0.0) k2m=K1m/pa[5];
else k2m=0.0;
706 K1p, k2p, k3p, k4p, 0, 0, 0, 0, K1m, k2m,
707 0.0, Vb, 1.0, input.
voi[0].
y2, NULL, NULL, NULL, NULL, NULL, NULL, 0);
709 printf(
"error %d in simulation\n", ret);
717 dft.
x1, dft.
x2, petsim, NULL, NULL, fitframeNr);
721 dft.
x, petsim, NULL, NULL, fitframeNr);
723 printf(
"error %d in interpolation\n", ret);
728 for(fi=0; fi<fitframeNr; fi++)
if(dft.
w[fi]>0.0) {
729 d=petmeas[fi]-petsim[fi];
733 if(0) printf(
"%g %g %g %g %g => %g\n", K1p, k2p, K1m, k2m,Vb,wss);
int bootstrap(int iterNr, double *cLim1, double *cLim2, double *SD, double *parameter, double *lowlim, double *uplim, int frameNr, double *origTac, double *fitTac, double *bsTac, int parNr, double *weight, double(*objf)(int, double *, void *), char *status, int verbose)
int modelCheckParameters(int par_nr, double *lower_p, double *upper_p, double *test_p, double *accept_p, double *penalty)
double atof_dpi(char *str)
int dftdup(DFT *dft1, DFT *dft2)
int dftAddmem(DFT *dft, int voiNr)
int dftWrite(DFT *data, char *filename)
int res_allocate_with_dft(RES *res, DFT *dft)
int iftPutDouble(IFT *ift, char *key, double value, char *cmt_type, int verbose)
int iftPut(IFT *ift, char *key, char *value, char *cmt_type, int verbose)
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
int iftWrite(IFT *ift, char *filename, int verbose)
int iftGetDoubleValue(IFT *ift, int si, const char *key, double *value, int verbose)
int interpolate(double *x, double *y, int nr, double *newx, double *newy, double *newyi, double *newyii, int newnr)
Linear interpolation and integration.
int interpolate4pet(double *x, double *y, int nr, double *newx1, double *newx2, double *newy, double *newyi, double *newyii, int newnr)
Interpolate and integrate TAC to PET frames.
Header file for libtpccurveio.
int resWrite(RES *res, char *filename, int verbose)
#define DFT_TIME_STARTEND
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
void tpcProgramName(const char *program, int version, int copyright, char *prname, int n)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Header file for libtpcmodel.
int simC4DIvp(double *t, double *ca1, double *ca2, double *cb, int nr, double k1, double k2, double k3, double k4, double k5, double k6, double k7, double km, double k1b, double k2b, double f, double vb, double fa, double *scpet, double *sct1, double *sct2, double *sct3, double *sct1b, double *sctab, double *sctvb, int verbose)
int tgo(double *lowlim, double *uplim, double(*objf)(int, double *, void *), void *objfData, int dim, int neighNr, double *fmin, double *gmin, int samNr, int tgoNr, int verbose)
Header file for libtpcmodext.
int plot_fitrange_svg(DFT *dft1, DFT *dft2, char *main_title, double x1, double x2, double y1, double y2, char *fname, int verbose)
Header file for libtpcsvg.
char studynr[MAX_STUDYNR_LEN+1]
char comments[_DFT_COMMENT_LEN+1]
char plasmafile2[FILENAME_MAX]
char parname[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]
char plasmafile[FILENAME_MAX]
char datafile[FILENAME_MAX]
char parunit[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]
char bloodfile[FILENAME_MAX]
double parameter[MAX_RESPARAMS]
double cl2[MAX_RESPARAMS]
double cl1[MAX_RESPARAMS]
char voiname[MAX_REGIONSUBNAME_LEN+1]
char name[MAX_REGIONNAME_LEN+1]