string filename,st,st2,error,expername,key,curexpername,ID,delim; FILE fp; integer i,j,k,col_rn,col_dye,col_frag,col_h,col_std,x1,x2,tl1,tl2,tl3,lanenr,curresol,ispresent; integer totbandcount,finished,finished2,fnd,resol,assignrefbands,selectnew; string totband_rn[],totband_dye[],totband_key[],totband_pool[]; integer totband_std[]; float totband_frag[],totband_h[],h,ps; DIALOG dlg; XMLNODE xmldoc,xmlroot,node1,node2; integer dyecount,useddyecount; string dyenames[],useddyenames[],dyelist,seldyelist,pooltaglist,selpooltag; INDEX dyestatus,idx,idxfiles; integer poolcount; string pool_tag[]; integer keycount; string keys[]; CRFPRINT fpr[]; string curgelname[]; integer fprcount,fprlanecount; resol=2000; assignrefbands=1;selectnew=1; useddyecount=3;useddyenames[1]="D2";useddyenames[2]="D3";useddyenames[3]="D4"; delim=" "; //delim=","; //build an index of all experiment files for i=1 to DbGetFileCount do IdxSet(idxfiles,DbGetFileName(i),1); //read settings poolcount=0; xmldoc=XMLNodeReadFile(DbGetPath+"\VNTR_ImpBeck.xml",error); if (error="") and XMLNodeIsValid(xmldoc) then { xmlroot=XMLNodeGetChild(xmldoc,"VNTRImportBeckSett"); if XMLNodeIsValid(xmlroot) then { assignrefbands=0;selectnew=0; node1=XMLNodeGetChild(xmlroot,"Pools"); if XMLNodeIsValid(node1) then { for i=1 to XMLNodeGetChildCount(node1) do { node2=XMLNodeGetChildByNr(node1,i); poolcount=poolcount+1; pool_tag[poolcount]=XMLNodeGetText(XMLNodeGetChild(node2,"Tag")); } } node1=XMLNodeGetChild(xmlroot,"UsedDyes"); if XMLNodeIsValid(node1) then { useddyecount=0; for i=1 to XMLNodeGetChildCount(node1) do { node2=XMLNodeGetChildByNr(node1,i); useddyecount=useddyecount+1; useddyenames[useddyecount]=XMLNodeGetText(node2); } } node1=XMLNodeGetChild(xmlroot,"AssignRefBands"); if XMLNodeIsValid(node1) then assignrefbands=1; node1=XMLNodeGetChild(xmlroot,"SelectNew"); if XMLNodeIsValid(node1) then selectnew=1; } } filename="*.txt"; if delim="," then filename="*.csv"; if not(FilePromptName("Select Beckmann bands file to import",filename,0)) then stop; if not(FileOpenRead(fp,filename)) then { message("Unable to read file"); stop; } setbusy("Reading file..."); st=FileRead(fp,9999);i=0; col_rn=0;col_dye=0;col_frag=0;col_h=0;col_std=0; while st<>"" do { i=i+1; st2=splitstring(st,delim); if st2="RN" then col_rn=i; if st2="dye" then col_dye=i; if substring(st2,1,13)="est frag size" then col_frag=i; if substring(st2,1,9)="pk height" then col_h=i; if substring(st2,1,9)="size std" then col_std=i; } if col_rn=0 then { Message("ERROR: invalid file format~nColumn 'RN' is missing"); stop; } if col_dye=0 then { Message("ERROR: invalid file format~nColumn 'dye' is missing"); stop; } if col_frag=0 then { Message("ERROR: invalid file format~nColumn 'est frag size' is missing"); stop; } if col_h=0 then { Message("ERROR: invalid file format~nColumn 'pk height' is missing"); stop; } if col_std=0 then { Message("ERROR: invalid file format~nColumn 'size std' is missing"); stop; } totbandcount=0; while not(fileisend(fp)) do { st=FileRead(fp,9999); if length(st)>3 then { totbandcount=totbandcount+1; i=0; while st<>"" do { i=i+1;st2=splitstring(st,delim); if i=col_rn then totband_rn[totbandcount]=st2; if i=col_dye then totband_dye[totbandcount]=st2; if i=col_frag then totband_frag[totbandcount]=val(st2); if i=col_h then totband_h[totbandcount]=val(st2); if i=col_std then { if st2="Yes" then totband_std[totbandcount]=1; else totband_std[totbandcount]=0; } } } } FileClose(fp); dyecount=0; for i=1 to totbandcount do { j=IdxGet(dyestatus,totband_dye[i]); if j=0 then { IdxSet(dyestatus,totband_dye[i],1); dyecount=dyecount+1; dyenames[dyecount]=totband_dye[i]; } } for i=1 to dyecount do dyelist=dyelist+dyenames[i]+" "; seldyelist="";for i=1 to useddyecount do seldyelist=seldyelist+useddyenames[i]+" "; while find(filename,"\",1)>0 do splitstring(filename,"\"); filename=splitstring(filename,".txt"); filename=splitstring(filename,".csv"); setbusy(""); finished=0; while not(finished) do { pooltaglist="";for i=1 to poolcount do pooltaglist=pooltaglist+pool_tag[i]+" "; DlgReset(dlg); DlgAddText(dlg,"Fingerprint file name:",15,15,120,15); DlgAddEdit(dlg,filename,15,40,120,20); DlgAddText(dlg,"Dyes to import:",15,80,120,15); DlgAddList(dlg,dyelist,seldyelist,15,105,120,80,"MULTI"); DlgAddText(dlg,"Pool tags:",180,15,120,15); DlgAddList(dlg,pooltaglist,selpooltag,180,40,120,80,"LIST"); DlgAddButton(dlg,"Add...",91,185,120,50,25); DlgAddButton(dlg,"Delete...",92,240,120,50,25); DlgAddCheck(dlg,"Assign reference positions",assignrefbands,15,190,150,15); DlgAddCheck(dlg,"Select imported isolates",selectnew,15,215,150,15); DlgAddButton(dlg,"Ok",11,220,170,80,25); DlgAddButton(dlg,"Cancel",12,220,200,80,25); x1=DlgShow(dlg,"VNTR / Import Beckman data",330,280); if (x1=0) or (x1=12) then stop; //add new pool tag if x1=91 then { finished2=0;st=""; while not(finished2) do { DlgReset(dlg); DlgAddEdit(dlg,st,15,40,120,20); DlgAddText(dlg,"Enter the pool tag",15,15,150,15); x2=DlgShow(dlg,"Add pool tag",300,200); if x2=0 then finished2=1; if x2=1 then { finished2=1; for i=1 to poolcount do if st=pool_tag[i] then finished2=0; if not(finished2) then message("This pool tag exists already"); // if (finished2) and (st="") then { finished2=0; message("You should provide a non-empty pool tag"); } if finished2 then { poolcount=poolcount+1; pool_tag[poolcount]=st; selpooltag=st; } } } } //delete pool tag if x1=92 then { if selpooltag="" then message("No pool tag selected"); else { DlgReset(dlg); DlgAddText(dlg,"Are you sure you want to remove the pool tag '"+selpooltag+"'?",15,15,200,30); x2=DlgShow(dlg,"Confirmation",350,150); if x2=1 then { j=0;for i=1 to poolcount do if pool_tag[i]=selpooltag then j=i; if j>0 then { poolcount=poolcount-1; for i=j to poolcount do pool_tag[i]=pool_tag[i+1]; } } } } if x1=11 then { finished=1; st=seldyelist; while st<>"" do { for i=1 to 20 do { st2=filename+"_"+splitstring(st," ")+"_"+str(i,0,0); if IdxGet(idxfiles,st2)=1 then { if finished then { message("This file '"+st2+"' is already in use. Please enter a different name"); finished=0; } } } } } } useddyecount=0; while seldyelist<>"" do { useddyecount=useddyecount+1; useddyenames[useddyecount]=splitstring(seldyelist," "); } //save settings XMLNodeCreateDoc("VNTRImportBeckSett",xmldoc,xmlroot); node1=XMLNodeAddChild(xmlroot,"Pools"); for i=1 to poolcount do { node2=XMLNodeAddChild(node1,"Pool"); XMLNodeAddText(XMLNodeAddChild(node2,"Tag"),pool_tag[i]); } node1=XMLNodeAddChild(xmlroot,"UsedDyes"); for i=1 to useddyecount do { XMLNodeAddText(XMLNodeAddChild(node1,"Dye"),useddyenames[i]); } if assignrefbands then XMLNodeAddChild(xmlroot,"AssignRefBands"); if selectnew then XMLNodeAddChild(xmlroot,"SelectNew"); XMLNodeToFile(xmldoc,dbgetpath+"\VNTR_ImpBeck.xml"); XMLNodeCloseDocument(xmldoc); if poolcount=0 then { poolcount=1; pool_tag[1]=""; } //create new fingerprint types if necessary st="020"; st=st+""; st=st+""; st=st+""; st=st+"R012500"; st=st+"5R01"; expername="VNTRFpr"; if DbGetExperNr(expername)>0 then { if DbGetExperClass(DbGetExperNr(expername))<>"FPR" then { message("The experiment '"+expername+"' should be a fingerprint type"); stop; } } else { if DbGetExperCount>=99 then { message("Too many experiments present in the database"); stop; } DbCreateExperType(expername,"FPR"); node1=XMLNodeReadString(st,error); DbSetExperSettings(DbGetExperNr(expername),XMLNodeGetChild(node1,"st")); XMLNodeCloseDocument(node1); } for j=1 to poolcount do { for i=1 to useddyecount do { expername="VNTRFpr"+pool_tag[j]+"_"+useddyenames[i]; if DbGetExperNr(expername)>0 then { if DbGetExperClass(DbGetExperNr(expername))<>"FPR" then { message("The experiment '"+expername+"' should be a fingerprint type"); stop; } } else { if DbGetExperCount>=99 then { message("Too many experiments present in the database"); stop; } DbCreateExperType(expername,"FPR"); node1=XMLNodeReadString(st,error); DbSetExperSettings(DbGetExperNr(expername),XMLNodeGetChild(node1,"st")); XMLNodeCloseDocument(node1); } } } //clear selection if necessary if selectnew then { for i=1 to DbGetEntryCount do if DbGetSel(DbGetEntryKey(i)) then DbSetSel(DbGetEntryKey(i),0); } //determine different isolates in data set IdxReset(idx);keycount=0; for i=1 to totbandcount do if totband_std[i]=0 then { key=totband_rn[i];key=splitstring(key,"."); if (poolcount>0) or (pool_tag[1]<>"") then { fnd=0;totband_pool[i]=""; for j=1 to poolcount do if substring(key,length(key)-length(pool_tag[j])+1,999)=pool_tag[j] then { totband_key[i]=substring(key,1,length(key)-length(pool_tag[j])); totband_pool[i]=pool_tag[j]; fnd=1; } // if not(fnd) then { // message("ERROR: The run ID '"+key+"' does not contain any known pool tag"); // stop; // } } else { totband_key[i]=key; totband_pool[i]=""; } if IdxGet(idx,totband_key[i])=0 then { IdxSet(idx,totband_key[i],1); keycount=keycount+1; keys[keycount]=totband_key[i]; if not(DbIsKeyPresent(keys[keycount])) then DbAddEntry(keys[keycount]); DbSetSel(keys[keycount],1); } } setbusy("Importing data..."); for tl2=1 to useddyecount do { fprcount=1;fprlanecount=0; curgelname[fprcount]=filename+"_"+useddyenames[tl2]+"_"+str(fprcount,0,0); CrFprCreate(fpr[fprcount],curgelname[fprcount],"VNTRfpr"); for tl1=1 to poolcount do { curexpername="VNTRFpr"+pool_tag[tl1]+"_"+useddyenames[tl2]; for tl3=1 to keycount do { ispresent=0; for i=1 to totbandcount do if (totband_dye[i]=useddyenames[tl2]) and (totband_pool[i]=pool_tag[tl1]) and (totband_key[i]=keys[tl3]) and (totband_std[i]=0) and (totband_frag[i]>0) then ispresent=1; if ispresent then { ID=DbGetEntryLink(keys[tl3],curexpername); if ID<>"" then DbSetExperLink(ID,""); fprlanecount=fprlanecount+1; if fprlanecount>200 then { fprcount=fprcount+1; curgelname[fprcount]=filename+"_"+useddyenames[tl2]+"_"+str(fprcount,0,0); CrFprCreate(fpr[fprcount],curgelname[fprcount],"VNTRfpr"); fprlanecount=0; } lanenr=CrFprAddLane(fpr[fprcount],curexpername,keys[tl3]); for i=1 to totbandcount do if (totband_dye[i]=useddyenames[tl2]) and (totband_pool[i]=pool_tag[tl1]) and (totband_key[i]=keys[tl3]) and (totband_std[i]=0) and (totband_frag[i]>0) then { h=totband_h[i]/20.0; if h>60000 then h=60000; CrFprAddBand(fpr[fprcount],lanenr,totband_frag[i],h,2.0); } } } } for i=1 to fprcount do { CrFprBandsToCurve(fpr[i],resol); CrFprSave(fpr[i]); if assignrefbands then { AnFprOpen(curgelname[i]);AnFprSetStep("Normalisation"); for i=1 to AnFprGetLaneCount do { st=AnFprGetRefSystem;j=0; while st<>"" do { splitstring(st,":"); ps=val(splitstring(st," "));j=j+1; curresol=AnFprGetCurveResol(i); k=(curresol-1)*ps/100.0; AnFprSetRefLink(i,j,k); } } AnFprSave;AnFprClose; } } } if (poolcount=1) and pool_tag[1]="" then poolcount=0; setbusy("");