pro g_blotn_rot_double
; can be compiled by any version of IDL. Code is best displayed in >120 character lines.
; adapted to quantify Western blots. "blot2" version is for two images, including a reference (e.g. GAPDH)
; many indications for interactive use are printed in the "output log" window, which must be visible to the user
; it uses as input any tiff files located in "storage" (see below).
; output is to storage_out folder. Change by editing code
; "blotn" versions are for n proteins using the same reference
; "rot" versions allow rotation to make lanes vertical. from 4 to -4 degrees allowed. 5 signals to keep last.
; this version allows full setting of regions from the beginning. By clicking low left corner.
; for preset sizes, enable lines x1moot and x1rmoot, and do a few more things
; note variety of source folders. Must edit by hand.
; "double" is for closely placed double bands, like calpain 76 and 80 kDa
; for "double", user clicks two diagonal corners (lower left, upper right) in lower band.  
; program automatically generates a symmetrical ROI for upper band
; upper right corner clicked must be at position separating both bands
; a version that finds the mid-position automatically is in "beta" stage

; DEFINITIONS
name=''
inname=strarr(10) ;& inname(*)='pGPnew'
rname=''
jump01:
jumpchgfiles:
cprot=0
For cprot=0,9 do begin ;(inname(cprot) ne '') do begin
Print, 'enter protein filename (pGPnew; '' to end set): '
read,name
if name eq '' then goto,jumpname
inname(cprot)=name
endfor
jumpname:
nthick=0    ;thickness of lines in display
inname=inname(0:cprot-1)
print,'working with ',cprot, ' proteins'
Print, 'enter reference name (GPnew): '     ;typically total GP or GAPDH
read,rname & rrname=rname
freqx=0.2 & freqt=freqx         ;
 nwig = 50 & nter=8  ;Gibbs wiggles and number of terms in filter
coefft = digital_filter(0,freqt,nwig,nter) & coeffx = digital_filter(0,freqx,nwig,nter)
coeff=transpose(coefft)##coeffx   ;for heavy filtering
;storage_out='D:\Dropbox\Triadin\WB_RyR1_062620\'                  ;output folder for home computer
storage_out='D:\Dropbox\JPh\'                  ;output folder for home computer
;storage_out='C:\Users\Eduardo\Dropbox\JPh\'       ;output folder for laptop
storage='D:\Dropbox\JPh\trimmed gels\'            ;input folder for home computer
;storage='D:\Dropbox\Triadin\WB_RyR1_062620\'       ;folder for RyR1 in triadin experiments
;storage='C:\Users\Eduardo\Dropbox\JPh\trimmed gels\'   ;input folder for laptop
basename= inname    ; a string array
for i=0,cprot-1 do basename(i)= inname(i) + '.tif'
basername= rname + '.tif'
rname = storage + basername
fname = storage + basename  ; a string array
    imr=read_tiff(rname)
    if (size(imr))(0) eq 3 then imr=reform(imr(1,*,*))
       simr=size(imr) & sx=simr(1) & sy=simr(2)
       imr= - float(imr - 255) > 0
       ;xlenr=700 & ylenr= sy*(float(700)/sx)
        xlenr=1000 & ylenr= sy*(float(1000)/sx);for office
       ;xlenr=1200 & ylenr= sy*(float(1200)/sx);for office special
       imr= congrid(imr,xlenr,ylenr,cubic=-0.5)
nxr=xlenr     ;file dimensions, change if necessary.
nyr=ylenr
im=intarr(cprot,nxr+300,800);/2)       ;to contain  images of various proteins
nx=intarr(cprot) & ny=intarr(cprot)
sibx=intarr(cprot) +17 & siby=Intarr(cprot)+32 & six= intarr(cprot)+ 41 & siy=six + 2
sc=1.5 & scr=2.1  & co=0 & cor=90.
goto,jumpset2
jumpsetscale:
print,'type scale: ',sc,'; scale reference: ',scr,'; contrast: ',co,'; contrast reference: ',cor
read,sc,scr,co,cor, prompt= '(type scale, scale reference, contrast, contr ref (1.,2.,0, 50): '
jumpset2:
for j=0,cprot-1 do begin
;if the reference image is produced by a phone camera change to imr(1,*,*)
       ima=read_tiff(fname(j)) & if (size(ima))(0) eq 3 then ima=reform(ima(1,*,*))
       sima=size(ima) & sx=sima(1) & sy=sima(2)
       ima= - float(ima - 255) > 0
        ;xlen=1200 & ylen= sy*1200./sx ;for office version
        xlen=1000 & ylen= 3*sy*1000./sx ;for office version
       ;xlen=700 & ylen= sy*700./sx ;for laptop
       ima= congrid(ima,xlen,ylen,cubic=-0.5)
       im(j,0:xlen-1,0:ylen-1) = ima
    xfst=0 & nx(j)=xlen       ;file dimensions, change if necessary.
    ny(j)=ylen
    ;window,j, xsize = xlen,ysize=ylen,xpos= 30, ypos=0,retain=2,title=inname(j) ;for laptop
     window,j, xsize = xlen,ysize=ylen, xpos= 30, ypos=0+j*(ylen+35),retain=2,title=inname(j)
    ;window,j, xsize = xlen,ysize=ylen, xpos= 30, ypos=0+j*(55),retain=2,title=inname(j) ;when cprot is large
    tv,0>(ima-co)*sc <255
    ;tv,0>(ima)*sc <255
endfor
;window,cprot, xsize = xlenr,ysize=ylenr,xpos=30, ypos= 40+ylen, retain=2,title=rrname      ;displays reference image
 window,cprot, xsize = xlenr,ysize=ylenr,xpos=30+xlen, ypos= 30, retain=2,title=rrname      ;laptop
;window,cprot, xsize = xlenr,ysize=ylenr,xpos=30 + , ypos= 50+ylen, retain=2,title=rrname      ;special
tv,0 >(imr-cor)*scr < 255     ;scale must be the same for all images, contrast used for reference
dummy=''
read, dummy, prompt='(OK?: to go on ENTER; any to change scale, f change file:)'
if dummy eq 'f' then goto, jumpchgfiles
if dummy ne '' then goto, jumpsetscale
stop          ;temporary, comment-out in general use
nbands=65
mx=max(nx)& my=max(ny)
im=im(*,0:mx-1,0:my-1)
    bandr=fltarr(nbands)  ;will contain up to 65 collected signal masses of reference bands
    bkgr=bandr & arer=intarr(nbands)
    bandm=fltarr(cprot,nbands)  ;will contain up to nbands collected signal masses, lower band in duo (i.e. higher MW)
    bandn=fltarr(cprot,nbands)  ;will contain up to nbands collected signal masses, upper band in duo
    bkg=bandm & aream=intarr(cprot,nbands) & arean=aream
    ratiom=bandm         ;will contain ratios of test/reference signal masses
    ration=bandn
    ratio = bandm			;will contain ratio of two bands
sirbx=5 & sirby=33  ;fixed size of reference background
sirx=22          ;fixed x size of reference
for i=0,nbands-1 do begin
    jumpredoband:
    wset,cprot          ;now work on reference blot
    ;if !mouse.button eq 4 then goto, jumpout
    print,'option to rotate reference, in degrees, clockwise, always from original'
    an=0 & nxc=100 & anu=0            ;starting angle
    jumprotate1:
    read, an, prompt='rotation angle, deg, 5 to continue:'
    ;anu=an
    if an eq 5 then goto,jumprotate2
    anu=an
    tv,0 >rot((imr-cor)*scr,anu,cubic=-0.5) < 255
    goto,jumprotate1
jumprotate2:
     Print, 'on reference window ', cprot, ' select  bckg region (one click at low left corner) ; '
    if i eq 0 then print, 'for first band do two corners; will keep size for subsequent bands' ;
    print, 'to signal last band, press right mouse button'
    print, 'click near low left corner to signal change size'
    cursor, xrb0, yrb0, /device, /down & print, 'xrb0 = ',xrb0
    ;reference background has fixed size. To restore choice must restore previous if up to endelse
    if !mouse.button eq 4 then goto, jumpout
    if xrb0 lt 15 and yrb0 lt 15 then begin
       Print, 'on reference window ', cprot, ' select bckg region (2 corners) ; '
       cursor, xrb0, yrb0, /device, /down & print, 'xrb0 = ',xrb0
       cursor, xrb1, yrb1, /device, /down & print, 'xrb1 = ',xrb1
       sirbx=xrb1-xrb0+1 & sirby=yrb1-yrb0+1 ;new size established
    endif else begin
       if i eq 0 then begin
       cursor, xrb1, yrb1, /device, /down & print, 'xrb1 = ',xrb1
       sirbx=xrb1-xrb0+1 & sirby=yrb1-yrb0+1
      endif
       xrb1=xrb0+sirbx-1 & yrb1=yrb0+sirby-1 & print, 'xrb1 = ',xrb1
    endelse
    plots,[xrb0,xrb1,xrb1,xrb0,xrb0],[yrb0,yrb0,yrb1,yrb1,yrb0],thick=nthick, color=255, /device
    refr0=(rot(imr,anu,cubic=-0.5))(xrb0:xrb1,yrb0:yrb1)
    bkgr(i)= total(refr0) /n_elements(refr0) ;bkgr is average fluorescence in ref of reference
    Print, 'on ref window ', cprot, ' select  active region (one click at low left corner) ; '
    if i eq 0 then print, 'for first band do two corners; will select size for subsequent bands' ;
    print, 'click near low left corner to signal change size'
    cursor, xr0, yr0, /device, /down
    if xr0 lt 15 and yr0 lt 15 then begin
       Print, 'on reference window ', cprot, ' select active region (2 corners) ; '
       cursor, xr0, yr0, /device, /down
       cursor, xr1, yr1, /device, /down
       sirx=xr1-xr0+1 & siry=yr1-yr0+1 ;new size established
    endif else begin
       if i eq 0 then begin
;      cursor, xr1moot, yr1, /device, /down
         cursor, xr1, yr1, /device, /down     ;enable previous for fixed size
         siry=yr1-yr0+1 ;& xr1=xr0+sirx-1
         & sirx=xr1-xr0+1  ; for sirx fixed to 22, comment this line and enable previous
       endif
       xr1=xr0+sirx-1 & yr1=yr0+siry-1
    endelse
    plots,[xr0,xr1,xr1,xr0,xr0],[yr0,yr0,yr1,yr1,yr0],thick=nthick, color=255, /device
    refr=(rot(imr,anu,cubic=-0.5))(xr0:xr1,yr0:yr1<(nyr-1)) & aref=N_elements(refr)
    ;(rot(imr,anu,1.0,nxc,nyr/2,cubic=-0.5))
    acr=((rot(imr,anu,cubic=-0.5))(xr0:xr1,yr0:yr1<(nyr-1))-bkgr(i)) > 0
;   acr=(imr(xr0:xr1,yr0:yr1<(nyr-1))-bkgr(i)) > 0
    bandr(i)=total(acr)/aref & arer(i)=aref
    print, 'for i = ',i, ';  backg ref = ', bkgr(i),';  reference = ', bandr(i)
    ;i=0
    jumpband:
    for j=0,cprot-1 do begin          ; work on test blots
        ima=reform(im(j,*,*))
      jumpredoprotein:
         wset,j
         Print, 'on protein window',j,' select  bckg region (one click at low left corner) ; '
        if i eq 0 then print, 'for first band do two corners; will keep size for subsequent bands' ;
        ;print, 'to signal last band, press right mouse button'
        ;print, 'click near low left corner to signal change size'
        Print, 'to redo  protein, press right mousebutton'
        print, 'if first protein, will redo band (i=i-1)'
        cursor, xb0, yb0, /device, /down
         if !mouse.button eq 4 then begin
            print,'right button pressed',!mouse.button
               if j eq 0 then begin
                 i=i-1
                 print,'Protein #0; will redo every protein in previous band #',i
                 goto, jumpredoband     ;will redo every protein in previous band
               endif else begin
                 print,'Protein #',j,'; will redo previous protein j=',j-1
                 j=j-1
                 goto,jumpredoprotein
               endelse
            endif
;       cursor, xb0, yb0, /device, /down
;       if !mouse.button eq 4 then goto, jumpout
        if xb0 lt 15 and yb0 lt 15 then begin
            Print, 'on protein window',j,' select  bckg region (2 corners) ; '
                    cursor, xb0, yb0, /device, /down
            cursor, xb1, yb1, /device, /down
            sibx(j)=xb1-xb0+1 & siby(j)=yb1-yb0+1 ;new size established
           endif else begin
            if i eq 0 then begin
             cursor, xb1, yb1, /device, /down
             sibx(j)=xb1-xb0+1 & siby(j)=yb1-yb0+1
         endif
            xb1=xb0+sibx(j)-1 & yb1=yb0+siby(j)-1
        endelse
        plots,[xb0,xb1,xb1,xb0,xb0],[yb0,yb0,yb1,yb1,yb0],color=255,thick=nthick, /device
         ref0= ima(xb0:xb1,yb0:yb1) ;& ref1= ima(xb2:xb3,yb2:yb3)
         bkg(j,i)= total(ref0) /n_elements(ref0) ;bkg is average fluorescence in reference
         jumpsamebkg:
            Print, 'on protein window',j,' select  active region (2 corners) ; '
            cursor, x0, y0, /device, /down
            cursor, x1, y1, /device, /down
            six(j)=x1-x0+1 & siy(j)=y1-y0+1 ;new size established
			y2=y1+siy(j)					;second window, same size
         plots,[x0,x1,x1,x0,x0],[y0,y0,y1,y1,y0],color=255,thick=nthick, /device
         plots,[x0,x1,x1,x0,x0],[y1+1,y1+1,y2,y2,y1+1],color=255,thick=nthick, /device
         acm=(ima(x0:x1,y0:y1<(ny(j)-1))-bkg(j,i)) > 0
         bandm(j,i)=total(acm) & aream(j,i)=n_elements(where(acm gt 0))
         acn=(ima(x0:x1,y1+1:y2<(ny(j)-1))-bkg(j,i)) > 0
         bandn(j,i)=total(acn) & arean(j,i)=n_elements(where(acn gt 0))
;____________
         ratiom(j,i)=bandm(j,i)/bandr(i)/1000.
         ration(j,i)=bandn(j,i)/bandr(i)/1000.
         ratio(j,i)=bandn(j,i)/bandm(j,i)
         print,'signal mass in heavy band',i,' = ',bandm(j,i), ' above ',bkg(j,i),' in area =',aream(j,i),' pixels'
         print,'signal mass in light band',i,' = ',bandn(j,i), ' above ',bkg(j,i),' in area =',arean(j,i),' pixels'
         print,'s mass/pix ref ',i,' = ',bandr(i), ' above ',bkgr(i),' in area =',arer(i),' pixels'
         print,'norm heavy band',i,' = ',ratiom(j,i), '; light band',i, ' = ',ration(j,i)
      endfor
endfor
stop
jumpout:
nbands=i
fout=fltarr(3,nbands)
fout(0,*)=ratiom(0,0:i-1)
fout(1,*)=ration(0,0:i-1)
fout(2,*)=ratio(0,0:i-1)

foutname=storage_out+inname(0)+'_multiband_ratiosb.txt'
;stop
openw,om,foutname,/get_lun
printf,om,strcompress(inname, /remove_all) ;& printf,om, ' ref.'
for j=0,i-1 do printf,om,fout(*,j);, format='(F7.0,f7.2,I7,F10.2,f8.2,I7,F6.2)'
close,om & free_lun,om
dummy=''
read,dummy,prompt='s to stop, f other file:'
if dummy eq 'f' then goto,jump01
stop
END

