123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- //loadFile is in fileManager.js
- var crfReviewSection={};
- crfReviewSection.set=
- function(parentClass){
- if ("parent" in this)
- return;
- this.parent=parentClass;
- }
- crfReviewSection.generateErrorMessage=
- function (id,listName,msg){
- this.parent.print('generateErrorMessage:');
- let eid=listName+"_errorMsg";
- let el=config.document.getElementById(eid);
- if (el===null){
- el=config.document.createElement("p");
- config.document.getElementById(id).appendChild(el);
- }
- el.innerHTML=msg;
- }
- crfReviewSection.clearErrorMessage=
- function(listName){
- let eid=listName+"_errorMsg";
- let el=config.document.getElementById(eid);
- if (el===null) return;
- el.remove();
- }
- crfReviewSection.generateSection=
- function(listName,id,callback){
- let that=this;
- let action=function(){that.fcontinue(listName,id,callback);};
- LABKEY.requiresScript(["crfTecant/fileManager.js"],action);
- }
- crfReviewSection.fcontinue=
- function(listName,id,callback){
- //callback should be generateReviewSectionCB and it takes no arguments
- this.parent.print("generateReviewSection");
- let config=this.parent.config;
- //need base path
- config.loadFileConfig=new Object();
-
-
- config.loadFileConfig.cb=callback;
- config.loadFileConfig.id=id;
- config.loadFileConfig.url=fileManager.getBasePath()+'/@files/reportSetup/'+listName+'.json';
- fileManager.loadFile();
- //load file and continue in the next function
- }
- crfReviewSection.getParticipantCode=
- function(pid){
- let filters=[LABKEY.Filter.create("crfRef",this.parent.getCRFref())];
- let config=this.parent.config;
- let mfId=config.formConfig.form['masterQuery'];
- let queryName=config.formConfig.queryMap[mfId];
- let that=this;
- pid.afterId=function(id){that.setParticipantCode(id);};
- pid.participantField=config.formConfig.studyData["SubjectColumnName"];
- let cb=function(data){that.afterRegistration(pid,data);}
- //untested
- this.parent.selectRows('lists',queryName,filters,cb,this.parent.getContainer('data'));
- }
- crfReviewSection.visitCodeFromVisitId=
- function(visitId){
- if (visitId<0) return "NONE";
- let project=this.parent.getContainer('data');
- this.parent.print('visitCodeFromVisitId: '+project.search('retro'));
- if (project.search('retro')>-1)
- visitId-=1;
- return 'VISIT_'+visitId.toString();
- }
- crfReviewSection.replaceSlash=
- function(x){
- return x.replace(/\//,'_');
- }
- crfReviewSection.setParticipantCode=
- function(pid){
- let fName='[setParticipantCode]';
- let rows=pid.registration.rows;
- let config=this.parent.config;
- //pick from study
- let participantField=config.formConfig.studyData["SubjectColumnName"];
- if (rows.length==1){
- this.parent.print(fName+': '+rows[0][participantField]+'/'+rows[0].visitId);
- let visitCode=this.visitCodeFromVisitId(rows[0].visitId);
- this.parent.print('setParticipantCode: '+pid.participantId+'/'+visitCode);
- pid.participantCode=this.replaceSlash(pid.participantId);
- pid.visitCode=visitCode;
- }
- this.generateReviewSection2(pid);
- }
- crfReviewSection.CB=
- function(){
- let config=this.parent.config;
- let listName=config.loadFileConfig.listName;
- let id=config.loadFileConfig.id;
- this.parent.clearErrorMessage(listName);
- let pid=new Object();
- pid.participantCode="NONE";
- pid.visitCode="NONE";
- this.getParticipantCode(pid);
- this.parent.print('Get participant code sent');
- //involves database search, continue after callback
- }
- crfReviewSection.getValueFromElement=
- function(id,defaultValue){
- let config=this.parent.config;
- let e=config.document.getElementById(id);
- if (e!=null){
- defaultValue=e.innerHTML;
- }
- return defaultValue;
- }
- crfReviewSection.pickParticipantCodeFromPage=
- function(){
- let pid=new Object();
- pid.participantCode=this.getValueFromElement("participantCode","NIX-LJU-D2002-IRAE-A000");
- pid.visitCode=this.getValueFromElement("visitCode","VISIT_1");
- this.generateReviewSection2(pid);
- }
- crfReviewSection.patternReplace=
- function(src,replacements,values){
- for (rep in replacements){
- let txt1=src.replace(new RegExp(rep),values[replacements[rep]]);
- src=txt1;
- }
- return src;
- }
- crfReviewSection.plotImage=
- function(cell,k,row,rowVariable,obj,pid){
- let config=this.parent.config;
- let baseDir=this.patternReplace(obj.imageDir,obj.replacements,pid);
- this.parent.print('Base dir: '+pid.basePath);
- pid[obj.variable]=obj.values[k];
- cell.id=pid[obj.variable]+"_"+rowVariable+pid[rowVariable];
- let img=null;
- let imgId=cell.id+'_img_';
- img=config.document.getElementById(imgId);
- if (img===null){
- img=config.document.createElement('img');
- img.id=imgId;
- cell.appendChild(img);
- }
- let imgSrc=patternReplace(obj.file,obj.replacements,pid);
- this.parent.print('Image: '+imgSrc);
- let imagePath=pid.basePath+'/'+baseDir+'/'+imgSrc;
-
- img.src=imagePath;
- img.width="300";
-
- }
- crfReviewSection.showReport=
- function(cell,k,row,rowVariable,obj,pid){
- cell.width="300px";
- cell.id='report_'+obj.values[k]+"_"+rowVariable+pid[rowVariable];
- let reportConfig=new Object();
- reportConfig.partName="Report";
- reportConfig.renderTo=cell.id;
- //reportConfig.showFrame=false;
- //reportConfig.width="300";
- reportConfig.frame="none";
- reportConfig.partConfig=new Object();
- reportConfig.partConfig.width="300";
- reportConfig.partConfig.title="R Report";
- reportConfig.partConfig.reportName=obj.values[k];
- for (f in obj.parameters){
- reportConfig.partConfig[f]=pid[f];
- }
- reportConfig.partConfig.showSection="myscatterplot";
- let reportWebPartRenderer = new LABKEY.WebPart(reportConfig);
- this.parent.print('Render to: '+reportConfig.renderTo);
- reportWebPartRenderer.render();
- }
- crfReviewSection.showProbability=
- function(cell,k,row,rowSetup,j,obj,pid){
- this.parent.print('showProbability: '+rowSetup);
- let rowVariable=rowSetup.variable;
- cell.id='prob_'+obj.values[k]+"_"+rowVariable+pid[rowVariable];
- let probDensity=new Object();
- probDensity.mean=rowSetup.mean[j];
- probDensity.sigma=rowSetup.sigma[j];
- this.parent.print('showProbability: mean '+probDensity.mean+' sigma '+probDensity.sigma);
- probDensity.func=obj.values[k];
- probDensity.organCode=pid.organCode;
- pid[obj.variable]=rowSetup[obj.variable][j];
- probDensity.percentile=pid.percentile;
- let selectRows=new Object();
- selectRows.queryName=obj.queryName;
- selectRows.schemaName="study";
- selectRows.filterArray=[];
- selectRows.containerPath=getContainer('data');
- for (let f in obj.filters){
- selectRows.filterArray.push(
- LABKEY.Filter.create(f,pid[obj.filters[f]]));
- this.parent.print('Filter ['+f+']: '+pid[obj.filters[f]]);
- }
- selectRows.success=function(data){
- this.drawProbability(data,cell,obj,pid,probDensity);}
- LABKEY.Query.selectRows(selectRows);
- }
- crfReviewSection.erf=
- function(x){
- let fx=[0,0.02,0.04,0.06,0.08,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,
- 1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,
- 2.1,2.2,2.3,2.4,2.5,3,3.5];
- let fy=[0,0.222702589,0.328626759,0.428392355,0.520499878,
- 0.603856091,0.677801194,0.742100965,0.796908212,
- 0.842700793,0.880205070,0.910313978,0.934007945,
- 0.952285120,0.966105146,0.976348383,
- 0.983790459,0.989090502,0.992790429,0.995322265,
- 0.997020533,0.998137154,0.998856823,0.999311486,
- 0.999593048,0.999977910,0.999999257];
- let n=32;
- let i0=n-1;
- for (let i=1;i<n;i++){
- if (Math.abs(x)>fx[i]) continue;
- i0=i-1;
- break;
- }
- let fval=1;
- if (i0<n-1){
- //interpolate
- let y1=fy[i0+1];
- let y0=fy[i0];
- let x1=fx[i0+1];
- let x0=fx[i0];
- fval=y0+(y1-y0)/(x1-x0)*(Math.abs(x)-x0);
- }
- this.parent.print('Erf: '+fval);
- if (x<0) return -fval;
- return fval;
- }
- crfReviewSection.setLine=
- function(fbox,name,value,fontSize){
- let fpId=fbox.id+name;
- let fp=config.document.getElementById(fpId);
- if (fp===null){
- fp=config.document.createElement("p");
- fp.id=fpId;
- fbox.appendChild(fp);
- }
- fp.classList.add("center");
- fp.style.textAlign="center";
- fp.style.fontSize=fontSize;
- fp.innerText=value;
- }
- crfReviewSection.drawProbability=
- function(data,cell,obj,pid,probDensity){
- this.parent.print('drawProbability');
- if (data.rows.length!=1){
- this.parent.print("drawProbability row length mismatch: "+data.rows.length);
- return;
- }
- //possible mismatch; I assume the dataset will have a field called value
- let val=data.rows[0].value;
- let prob=0;
- let fz=-100;
- if (probDensity.func=="gaus"){
- fz=(val-probDensity.mean)/probDensity.sigma/Math.sqrt(2);
- prob=0.5+0.5*erf(fz);
- }
- let color="red";
- let fzx=fz*Math.sqrt(2);
- this.parent.print('drawProbability '+fzx);
- for (let i=1;i<obj.intervals.n;i++){
- if (fzx>obj.intervals.zlimits[i]) continue;
- color=obj.intervals.colors[i-1];
- break;
- }
-
- let fboxId=cell.id+'_fbox_';
- let fbox=config.document.getElementById(fboxId);
- if (fbox===null){
- fbox=config.document.createElement("div");
- fbox.id=fboxId;
- cell.appendChild(fbox);
- }
- fbox.style.backgroundColor=color;
- fbox.style.width="180px";
- fbox.style.height="180px";
-
- this.parent.print('organCode '+probDensity.organCode);
- let organName="Lung";
- if (probDensity.organCode==4){
- organName="Thyroid";
- }
- if (probDensity.organCode==5){
- organName="Bowel";
- }
- this.setLine(fbox,'_fp4_',organName,"16px");
- this.setLine(fbox,'_fp_',val.toPrecision(3),"25px");
- this.setLine(fbox,'_fp1_',"SUV("+probDensity.percentile+"%)","16px");
- this.setLine(fbox,'_fp2_',fzx.toPrecision(3),"25px");
- this.setLine(fbox,'_fp3_',"z-value","16px");
- }
- crfReviewSection.generateReviewSection2=
- function(pid){
- let config=this.parent.config;
- let listName=config.loadFileConfig.listName;
- let id=config.loadFileConfig.id;
-
- this.parent.print('generateReviewSection2: '+pid.participantCode+'/'+
- pid.visitCode);
- if (pid.participantCode=="NONE" || pid.visitCode=="NONE"){
- this.generateErrorMessage(id,listName,
- "ParticipantId/visitId not set");
- return;
- }
-
- this.parent.print('JSON: '+config.loadFileConfig.json);
- let json=config.loadFileConfig.json;
- let nrows=json.rows.values.length;
- let ncol=json.columns.length;
- pid.basePath=fileManager.getBasePath()+"/@files";
-
-
- let el=config.document.getElementById(id);
- let tableId=id+'_Table';
- let table=config.document.getElementById(tableId);
- if (table==null){
- table=config.document.createElement('table');
- table.id=tableId;
- el.appendChild(table);
- }
- table.style.tableLayout="fixed";
- table.style.columnWidth="300px";
-
- for (let i=0;i<nrows;i++){
- pid[json.rows.variable]=json.rows.values[i];
- //let organ=organs[i];
- let row=null;
- if (i<table.rows.length)
- row=table.rows[i];
- else
- row=table.insertRow();
- let ic=0;
- for (let j=0;j<ncol;j++){
- let obj=json.columns[j];
- let nv=obj.values.length;
- for (let k=0;k<nv;k++){
- let cell=null;
- if (ic<row.cells.length)
- cell=row.cells[ic];
- else
- cell=row.insertCell();
- if (obj.display=="image")
- this.plotImage(cell,k,row,json.rows.variable,obj,pid);
- if (obj.display=="report")
- this.showReport(cell,k,row,json.rows.variable,obj,pid);
- if (obj.display=="probability"){
- this.showProbability(cell,k,row,json.rows,i,obj,pid);
- }
- ic++;
- }
- }
-
- }
- }
- ///>>>>>>>>>>>>>>end of reviewSection(REPORT)
- crfReviewSection.afterRegistration=
- function(data,fc){
- let fName='[afterRegistration/'+data.queryName+']';
- this.parent.print(fName+": rows:"+data.rows.length);
- fc.registration=data;
- let registrationData=fc.registration;
- this.parent.clearErr();
- if (registrationData.rows.length!=1){
- let msg=fName+": ERROR: Found "+registrationData.rows.length;
- msg+=" registration entries for crfrefid "+this.parent.getCRFref();
- this.parent.print(msg);
- fc.afterId(fc);
- return;
- }
- this.parent.print(fName+'registration participant field: '+fc.participantField);
- fc.participantId=registrationData.rows[0][fc.participantField];
- //could be a lookup field (particularly for studies)
- this.parent.print('ID: '+fc.participantId);
- let fields=registrationData.metaData.fields;
- let field="NONE";
- for (f in fields){
- if (fields[f]["name"]==fc.participantField)
- field=fields[f];
- }
- if ("lookup" in field){
- let pid=fc.participantId;
- this.parent.print("Using lookup for participantId: "+pid);
- let lookup=field["lookup"];
- this.parent.print("Lookup: ["+lookup.schemaName+','+lookup.queryName+']');
- //load lookup
- let that=this;
- let cb=function(data){that.afterRegistrationLookup(data,lookup.displayColumn,fc)};
- let filters=[LABKEY.Filter.create(lookup.keyColumn,pid)];
- this.parent.selectRows(lookup.schemaName,lookup.queryName,filters,cb,lookup.containerPath);
- }
- else{
- //afterParticipantId(configUpload);
- fc.afterId(fc);
- }
- }
- crfReviewSection.afterRegistrationLookup=
- function(data,displayColumn,fc){
- this.parent.print("afterRegistrationLookup");
- let entry=data.rows[0];
- fc.participantId=entry[displayColumn];
- this.parent.print('Setting to '+fc.participantId);
- fc.afterId(fc);
- //afterParticipantId(configUpload);
- }
|