//************************************************************* // Determines allelles for Multi-Locus Sequence Typing //************************************************************* // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // ! This script is provided "as is" by Applied Maths. ! // ! You are free to use and modify this script for your own needs. ! // ! Redistribution or reproduction of the script is prohibited. ! // ! DISCLAIMER: ! // ! Improper use of scripts may corrupt your database. ! // ! Running this script is entirely at your own responsibility. ! // ! Applied Maths accepts no lialibility for any consequences ! // ! resulting from its use. ! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! integer mlst_count,mlst_variantcount[],mlst_idx[],addallvariants,rs; string mlst_name[],mlst_variantseq[][]; string st1,st2,key,seqstring,list,sel,newlist,newsel; SEQUENCE sq; CHARSET cs; FILE fl; integer i,j,idx,tpe,found,ok,mismatchcount; DIALOG dlg0,dlg; //check if at least some entries are selected found=0; for i=1 to DbGetEntryCount do if DbGetSel(DbGetEntryKey(i)) then found=1; if not(found) then { message("You should select some entries to update"); stop; } //check if a character type with name 'MLST' exists ok=0; for j=1 to DbGetExperCount do if (DbGetExperClass(j)="CHR") and (DbGetExperName(j)="MLST") then ok=1; if not(ok) then { message("ERROR: you must first create a character type with name 'MLST' !"); stop; } //read existing MLST variant info from file mlst_count=0; if FileOpenRead(fl,DbGetPath+"\MLST.TXT") then { while not(FileIsEnd(fl)) do { st1=FileRead(fl,1000);st2=splitstring(st1," ");ok=0; if st2="[SEQTYPE]" then { ok=1; mlst_count=mlst_count+1; mlst_name[mlst_count]=st1; mlst_variantcount[mlst_count]=0; } if st2="[VARIANT]" then { ok=1; idx=mlst_variantcount[mlst_count]+1; mlst_variantcount[mlst_count]=idx; mlst_variantseq[mlst_count][idx]=st1; } if (not(ok)) and (length(st1)+length(st2)>0) then { message("Syntax error in file 'MLST.TXT'"); stop; } } FileClose(fl); } //check if all MLST sequence types are indeed present in the database for i=1 to mlst_count do { ok=0; for j=1 to DbGetExperCount do if (DbGetExperClass(j)="SEQ") and (DbGetExperName(j)=mlst_name[i]) then ok=1; if not(ok) then { message("ERROR: MLST sequence type '"+mlst_name[i]+"' is not present in the database!"); stop; } } //Shows dialog box that allows one to add new MLST sequence types; st1="MLST sequence types:~n"; for i=1 to mlst_count do st1=st1+" - "+mlst_name[i]+"~n"; DlgAddText(dlg,st1,15,15,150,200); DlgAddText(dlg,"Add new sequence type(s)",190,15,150,15); newlist=""; for i=1 to DbGetExperCount do if DbGetExperClass(i)="SEQ" then { found=0; for j=1 to mlst_count do if DbGetExperName(i)=mlst_name[j] then found=1; if not(found) then newlist=newlist+DbGetExperName(i)+" "; } DlgAddList(dlg,newlist,newsel,190,40,150,200,"MULTI"); if not(DlgShow(dlg,"Update MLST information",370,350)) then stop; while length(newsel)>0 do { mlst_count=mlst_count+1; mlst_name[mlst_count]=splitstring(newsel," "); mlst_variantcount[mlst_count]=0; } //Looks up the corresponding characters in 'MLST', and create new ones if necessary for i=1 to mlst_count do { mlst_idx[i]=ChrSetFindChar("MLST",mlst_name[i]); if mlst_idx[i]<=0 then mlst_idx[i]=ChrSetAddChar("MLST",mlst_name[i],200); } //Loops through all selected database entries and updates MLST info for idx=1 to DbGetEntryCount do if DbGetSel(DbGetEntryKey(idx)) then { setbusy("Updating entry "+str(idx,0,0)); key=DbGetEntryKey(idx); if not(ChrLoad(cs,key,"MLST")) then ChrCreate(cs,"MLST","",key); for tpe=1 to mlst_count do { found=0; SeqLoad(sq,key,mlst_name[tpe]);SeqGet(sq,seqstring); if length(seqstring)>0 then { for i=1 to mlst_variantcount[tpe] do if seqstring=mlst_variantseq[tpe][i] then found=i; //Check if variant is not found if found=0 then { DlgReset(dlg); DlgAddText(dlg,"Key: "+key,15,15,250,15); DlgAddText(dlg,"Sequence: "+mlst_name[tpe],15,35,250,15); DlgAddEdit(dlg,seqstring,15,60,400,18); list=""; for i=1 to mlst_variantcount[tpe] do { list=list+str(i,3,0)+" ) "; st1=mlst_variantseq[tpe][i];mismatchcount=0;st2=""; for j=1 to length(st1) do if substring(st1,j,j)<>substring(seqstring,j,j) then { mismatchcount=mismatchcount+1; st2=st2+"["+str(j,3,0)+" : "+substring(st1,j,j) +" > "+substring(seqstring,j,j)+"] "; } list=list+str(mismatchcount,3,0)+" "+st2+" "; } DlgAddText(dlg,"Best existing matches:",15,100,400,15); DlgAddList(dlg,list,sel,15,120,400,150,"LIST"); DlgAddButton(dlg,"Add this",11,20,280,80,25); DlgAddButton(dlg,"Add all",12,120,280,80,25); DlgAddButton(dlg,"Ignore",13,220,280,80,25); DlgAddButton(dlg,"Cancel",14,320,280,80,25); setbusy(""); if addallvariants then rs=11; if not(addallvariants) then rs=DlgShow(dlg,"New variant found",450,350); if rs=14 then stop; if rs=12 then { addallvariants=1; rs=11; } //add a new variant if required if rs=11 then { idx=mlst_variantcount[tpe]+1; mlst_variantcount[tpe]=idx; mlst_variantseq[tpe][idx]=seqstring; found=idx; } } } if found>0 then ChrSetVal(cs,mlst_idx[tpe],found); if found=0 then ChrSetVal(cs,mlst_idx[tpe],-1.0); } ChrSave(cs); } //Saves changed variant info to file if not(FileOpenWrite(fl,DbGetPath+"\MLST.TXT")) then { message("Error: unable to save the file 'MLST.TXT'"); stop; } for i=1 to mlst_count do { FileWrite(fl,"[SEQTYPE] "+mlst_name[i]+"~n"); for j=1 to mlst_variantcount[i] do FileWrite(fl,"[VARIANT] "+mlst_variantseq[i][j]+"~n"); FileWrite(fl,"~n"); } FileClose(fl); setbusy("");