TPCCLIB
Loading...
Searching...
No Matches
fit2auc.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpctac.h"
18#include "tpcpar.h"
19#include "tpctacmod.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Calculation of PTAC AUC from specified time to infinity, based on parameters",
25 "of decaying sum of exponentials.",
26 " ",
27 "Usage: @P [Options] parfile starttime [aucfile]",
28 " ",
29 "Options:",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "See also: fit_dexp, fit2dat, extrapol, paucinf, tacln, parcoll",
33 " ",
34 "Keywords: AUC, input, pharmacokinetics, clearance, elimination rate",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 char parfile[FILENAME_MAX], aucfile[FILENAME_MAX];
57 double starttime=nan("");
58
59
60 /*
61 * Get arguments
62 */
63 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
64 parfile[0]=aucfile[0]=(char)0;
65 /* Options */
66 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
67 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
68 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
69 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
70 return(1);
71 } else break;
72
73 TPCSTATUS status; statusInit(&status);
74 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
75 status.verbose=verbose-3;
76
77 /* Print help or version? */
78 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
79 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
80 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
81
82 /* Process other arguments, starting from the first non-option */
83 if(ai<argc) strlcpy(parfile, argv[ai++], FILENAME_MAX);
84 if(ai<argc) {
85 starttime=atofVerified(argv[ai]);
86 if(starttime>=0.0) {ai++;}
87 else {fprintf(stderr, "Error: invalid start time '%s'.\n", argv[ai]); return(1);}
88 }
89 if(ai<argc) strlcpy(aucfile, argv[ai++], FILENAME_MAX);
90 if(ai<argc) {
91 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
92 return(1);
93 }
94 /* Did we get all the information that we need? */
95 if(!(starttime>=0.0)) {
96 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
97 return(1);
98 }
99
100
101 /* In verbose mode print arguments and options */
102 if(verbose>1) {
103 printf("parfile := %s\n", parfile);
104 printf("starttime := %g\n", starttime);
105 if(aucfile[0]) printf("aucfile := %s\n", aucfile);
106 fflush(stdout);
107 }
108
109
110 /*
111 * Read the file
112 */
113 if(verbose>1) printf("reading %s\n", parfile);
114 PAR par; parInit(&par);
115 if(parRead(&par, parfile, &status)!=TPCERROR_OK) {
116 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
117 parFree(&par); return(2);
118 }
119 if(verbose>2) {
120 printf("fileformat := %s\n", parFormattxt(par.format));
121 printf("parNr := %d\n", par.parNr);
122 printf("tacNr := %d\n", par.tacNr);
123 }
124
125 /* Try to get data units */
126 int cunit=UNIT_UNKNOWN, tunit=UNIT_UNKNOWN;
127 {
128 int i;
129
130 i=iftFindKey(&par.h, "unit", 0);
131 if(i<0) i=iftFindKey(&par.h, "calibration_unit", 0);
132 if(i>=0) cunit=unitIdentify(par.h.item[i].value);
133
134 i=iftFindKey(&par.h, "timeunit", 0);
135 if(i<0) i=iftFindKey(&par.h, "time_unit", 0);
136 if(i>=0) tunit=unitIdentify(par.h.item[i].value);
137 //if(!unitIsTime(tunit)) tunit=UNIT_UNKNOWN;
138 }
139 if(verbose>2) {
140 printf("time_unit := %s\n", unitName(tunit));
141 printf("calibration_unit := %s\n", unitName(cunit));
142 }
143
144
145
146 /*
147 * Allocate place for AUCs
148 */
149 PAR auc; parInit(&auc);
150 if(parAllocate(&auc, 1, par.tacNr)!=TPCERROR_OK) {
151 fprintf(stderr, "Error: cannot allocate memory.\n");
152 parFree(&par); return(5);
153 }
154 auc.parNr=1; auc.tacNr=0;
155 /* Set the file format */
157 if(aucfile[0]) auc.format=parFormatFromExtension(aucfile);
159 /* Set header contents */
160 iftPut(&auc.h, "datafile", parfile, 0, NULL);
161 {
162 char buf[256];
163 time_t t=time(NULL);
164 iftPut(&auc.h, "analysis_time", ctime_r_int(&t, buf), 0, NULL);
165 tpcProgramName(argv[0], 1, 1, buf, 256);
166 iftPut(&auc.h, "program", buf, 0, NULL);
167 }
168 /* Set parameter name and unit */
169 strcpy(auc.n[0].name, "AUC");
170 if(unitIsTime(tunit)) {
171 int aucunit=unitMultiply(cunit, tunit);
172 if(verbose>2) printf("auc_unit := %s\n", unitName(aucunit));
173 auc.n[0].unit=aucunit;
174 }
175
176
177 /*
178 * Compute the AUCs (if possible)
179 */
180 for(int ti=0; ti<par.tacNr; ti++) {
181 if(verbose>2) printf("%s\n", par.r[ti].name);
182 double a;
183 if(mfEvalIntToInf(modelCode(par.r[ti].model), par.parNr, par.r[ti].p, starttime, &a, verbose-2)) {
184 if(verbose>0) fprintf(stderr, "Error: model %s not supported.\n", modelCode(par.r[ti].model));
185 continue;
186 }
187 if(verbose>1) printf("AUC for %s := %g\n", par.r[ti].name, a);
188 strcpy(auc.r[auc.tacNr].name, par.r[ti].name);
189 auc.r[auc.tacNr].p[0]=a;
190 auc.tacNr++;
191 } // next curve
192 if(auc.tacNr==0) {
193 fprintf(stderr, "Error: model not supported.\n");
194 parFree(&par); parFree(&auc);
195 return(3);
196 }
197
198
199 /*
200 * Print and save results
201 */
202 if(verbose>0 || !aucfile[0]) parWrite(&auc, stdout, PAR_FORMAT_TSV_UK, 0, NULL);
203 if(aucfile[0]) {
204 /* Save file */
205 if(verbose>1) printf(" saving %s\n", aucfile);
206 FILE *fp;
207 fp=fopen(aucfile, "w");
208 if(fp==NULL) {
209 fprintf(stderr, "Error: cannot open file for writing.\n");
210 parFree(&par); parFree(&auc); return(11);
211 }
212 int ret=parWrite(&auc, fp, PAR_FORMAT_UNKNOWN, 1, &status);
213 fclose(fp);
214 if(ret!=TPCERROR_OK) {
215 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
216 parFree(&par); parFree(&auc); return(12);
217 }
218 if(verbose>0) printf("AUC(s) saved in %s\n", aucfile);
219 }
220
221
222 /* Free memory */
223 parFree(&par); parFree(&auc);
224
225 return(0);
226}
227/*****************************************************************************/
228
229/*****************************************************************************/
char * ctime_r_int(const time_t *t, char *buf)
Convert calendar time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
Definition datetime.c:119
double atofVerified(const char *s)
Definition decpoint.c:75
int mfEvalIntToInf(const char *fid, const int parNr, const double *p, const double x, double *v, const int verbose)
Definition func.c:969
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
char * modelCode(const unsigned int i)
Definition modell.c:176
void parFree(PAR *par)
Definition par.c:75
int parAllocate(PAR *par, int parNr, int tacNr)
Definition par.c:108
void parInit(PAR *par)
Definition par.c:25
char * parFormattxt(parformat c)
Definition pario.c:59
int parWrite(PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
Definition pario.c:148
int parRead(PAR *par, const char *fname, TPCSTATUS *status)
Definition pario.c:232
int parFormatFromExtension(const char *s)
Definition pario.c:102
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
void tpcProgramName(const char *program, int version, int copyright, char *prname, int n)
Definition proginfo.c:406
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * value
Definition tpcift.h:37
IFT_ITEM * item
Definition tpcift.h:57
Definition tpcpar.h:100
int format
Definition tpcpar.h:102
IFT h
Optional (but often useful) header information.
Definition tpcpar.h:147
int parNr
Definition tpcpar.h:108
int tacNr
Definition tpcpar.h:104
PARR * r
Definition tpcpar.h:114
PARN * n
Definition tpcpar.h:112
int unit
Definition tpcpar.h:86
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:82
char name[MAX_TACNAME_LEN+1]
Definition tpcpar.h:50
unsigned int model
Definition tpcpar.h:48
double * p
Definition tpcpar.h:64
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpcextensions.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_OK
No error.
int unitIdentify(const char *s)
Definition units.c:162
char * unitName(int unit_code)
Definition units.c:143
int unitMultiply(int ua, int ub)
Definition units.c:809
int unitIsTime(int u)
Definition units.c:359
Header file for library libtpcift.
Header file for libtpcpar.
@ PAR_FORMAT_UNKNOWN
Unknown format.
Definition tpcpar.h:28
@ PAR_FORMAT_TSV_UK
UK TSV (point as decimal separator).
Definition tpcpar.h:35
Header file for library libtpctac.
Header file for libtpctacmod.