//$MENU window=entry;popup=Sequence;insertpopupafter=Edit;name=Set frames // $button window=entry;hint=Set sequence frames string key,seqnames[],framechoices,strframes[],transtablelist,transtable,st,st2,st3,expername,kodon; string transcode; integer seqcount,i,j,k,frames[],yp,x,finished,vl,tl; DIALOG dlg,dlg2; FILE fp; INDEX kodonidx; SEQUENCE sq,sq2; string nuclseq,amino,aminoseq; integer baseidx,nuclseqlen,frame,minlen,stopkodoncount; key=DbCurrentKey; if key="" then { message("No entry selected"); stop; } //create translation frame field if necessary if DbGetFieldNr("TranslationFrames")=0 then DbAddField("TranslationFrames"); if not(FileOpenRead(fp,DbGetStartupDir+"\aminotrans.txt")) then { message("Could not find the file 'aminotrans.txt'"); stop; } while not(fileIsEnd(fp)) do { st=FileRead(fp,999); if length(st)>5 then transtablelist=transtablelist+splitstring(st," ")+" "; } FileClose(fp); transtable="Standard"; for i=1 to DbGetExperCount do if DbGetExperClass(i)="SEQ" then if find(DbGetExperName(i),"_TRANSL",1)=0 then if DbGetExpernr(DbGetExperName(i)+"_TRANSL")>0 then { seqcount=seqcount+1; seqnames[seqcount]=DbGetExperName(i); frames[seqcount]=1; } if seqcount=0 then { message("There are no sequences in the database that have an associated translation"); stop; } //read existing frames st=DbGetField(key,"TranslationFrames"); while st<>"" do { st2=splitstring(st,":="); st3=splitstring(st,"|"); for i=1 to seqcount do if st2=seqnames[i] then frames[i]=val(st3); } DlgAddList(dlg,transtablelist,transtable,15,15,250,300,"DROP"); framechoices="Frame 1 Frame 2 Frame 3"; yp=60; for i=1 to seqcount do { DlgAddText(dlg,seqnames[i],15,yp,100,15); strframes[i]="Frame "+str(frames[i],0,0); DlgAddList(dlg,framechoices,strframes[i],115,yp-2,70,80,"DROP"); DlgAddButton(dlg,"Translate...",100+i,200,yp-2,65,22); yp=yp+20; } yp=yp+40; DlgAddButton(dlg,"Cancel",99,80,yp,80,25); DlgAddButton(dlg,"Ok",11,185,yp,80,25); finished=0; while not(finished) do { x=DlgShow(dlg,"Sequence frames",290,200); for i=1 to seqcount do frames[i]=val(substring(strframes[i],7,999)); if (x=99) or (x=0) then stop; if x=11 then finished=1; //load translation info FileOpenRead(fp,DbGetStartupDir+"\aminotrans.txt"); transcode=""; while not(fileIsEnd(fp)) do { st=FileRead(fp,999); if splitstring(st," ")=transtable then transcode=st; } FileClose(fp); if transcode="" then { message("Unable to load the translation info"); stop; } //build kodon index; for i=0 to 3 do { for j=0 to 3 do { for k=0 to 3 do { kodon=""; if i=0 then kodon=kodon+"T"; if i=1 then kodon=kodon+"C"; if i=2 then kodon=kodon+"A"; if i=3 then kodon=kodon+"G"; if j=0 then kodon=kodon+"T"; if j=1 then kodon=kodon+"C"; if j=2 then kodon=kodon+"A"; if j=3 then kodon=kodon+"G"; if k=0 then kodon=kodon+"T"; if k=1 then kodon=kodon+"C"; if k=2 then kodon=kodon+"A"; if k=3 then kodon=kodon+"G"; vl=(i*4+j)*4+k+1; IdxSet(kodonidx,kodon,vl); } } } if (x>100) and (x<=100+seqcount) then { expername=seqnames[x-100]; frame=frames[x-100]; nuclseq="";minlen=-1;stopkodoncount=0; if SeqLoad(sq,key,expername) then SeqGet(sq,nuclseq); nuclseqlen=length(nuclseq);aminoseq="";i=frame; while i+3<=nuclseqlen do { baseidx=0; kodon=substring(nuclseq,i,i+2); baseidx=IdxGet(kodonidx,kodon); if baseidx>0 then amino=substring(transcode,baseidx,baseidx); else amino="X"; if amino="*" then { stopkodoncount=stopkodoncount+1; if minlen<0 then minlen=length(aminoseq); } aminoseq=aminoseq+amino; i=i+3; if (i-frame)/90=floor((i-frame)/90) then aminoseq=aminoseq+"~n"; } DlgReset(dlg2); DlgAddEdit(dlg2,aminoseq,15,15,265,150); DlgAddButton(dlg2,"Ok",11,110,210,80,25); st=str(stopkodoncount,0,0)+" stop kodons"; if minlen>=0 then st=st+" (translated length: "+str(minlen,0,0)+")"; DlgAddText(dlg2,st,15,180,265,15); DlgShow(dlg2,"Translation",300,280); } } st=""; for i=1 to seqcount do { if st<>"" then st=st+"|"; st=st+seqnames[i]+":="+str(frames[i],0,0); } DbSetField(key,"TranslationFrames",st); DbSaveFields; for tl=1 to seqcount do { expername=seqnames[tl]; if DbGetExperNr(expername+"_TRANSL")>0 then { frame=frames[tl]; nuclseq="";minlen=-1;stopkodoncount=0; if SeqLoad(sq,key,expername) then { SeqGet(sq,nuclseq); nuclseqlen=length(nuclseq);aminoseq="";i=frame; while i+3<=nuclseqlen do { baseidx=0; kodon=substring(nuclseq,i,i+2); baseidx=IdxGet(kodonidx,kodon); if baseidx>0 then amino=substring(transcode,baseidx,baseidx); else amino="X"; if amino="*" then amino="X"; aminoseq=aminoseq+amino; i=i+3; } if not(SeqLoad(sq2,key,expername+"_TRANSL")) then SeqCreate(sq2,expername+"_TRANSL","",key); SeqSet(sq2,aminoseq);SeqSave(sq2); } } }