Tk Source Code

Artifact [afeb8584]
Login

Artifact afeb8584c9f85bac45fc3f40c799343a2ffe0e65:

Attachment "ttext3.tcl" to ticket [1755313f] added by luckyluke67 2007-07-17 14:17:12.
#!/usr/local/bin/wish
##!/home/moumou/TclTk-8.5/bin/wish8.5

package require Tk

set LignesCoul ">seqlab
I white magenta
L white magenta
M white magenta
V white magenta
ILMV white magenta
R white blue
K white blue
RK white blue
F white red
Y white red
W white red
FYW white red
D white forestgreen
E white forestgreen
DE white forestgreen
Q white green
P white black
G black orange
H black cyan
N black cyan
HN white cyan
S white darkviolet
T white darkviolet
A white darkviolet
C white darkviolet
STAC white darkviolet
SPC white darkslategrey
"

###################################
# Configure tags for 20 amino acids
# + physicochemical groups
# almost taken from ordali
proc ConfTag {} {
    global LignesCoul
    global wt 
    global Lgaa Lfreg

    foreach c [lrange [split $LignesCoul "\n"] 1 end] {
	foreach {n f b} $c {}
	$wt tag configure Tag$n -foreground $f -background $b
    }

    set Lgaa [list STAC DE ILMV FYW P RK Q G HN]
    foreach g $Lgaa {
	set freg [join [split $g ""] "|"]
	lappend Lfreg $freg
    }

    return
}

#################
# Data generation
# 
# Junk data or read a TFA file given as argument
proc JunkData {} {
    global data Long Haut    

    set l "A.C.P.Q.G.I.K.D.N.F."
    set rep [expr {int($Long/20)}]
    set dt [string repeat $l $rep]
    set data "$dt\n"
    for {set i 1} {$i < $Haut} {incr i} {
	set s [string range $dt $i end]
	append s "[string range $dt 0 [expr {$i-1}]]\n"
	append data $s
    }
#    set data "[string repeat $dt $Haut]"
    set Long [string length $dt]

    return $data
}

##############################
# Window and attached commands
#
# trash window test ... no grid , nothing ...
proc InitWindow {} {
    global data
    global wt wx wy
    global Long Haut

    set wt .f3.t
    set wx .f3.sx
    set wy .f3.sy

    frame .f3 
    text .f3.t -font "Courier 10 bold" -wrap none \
	    -yscrollcommand "$wy set" \
	    -xscrollcommand "$wx set" \
	    -height 50 -width 50
    scrollbar .f3.sy -command "$wt yview"
    scrollbar .f3.sx -command "$wt xview" -orient horizontal

    grid .f3.t .f3.sy -sticky ns
    grid .f3.sx -sticky we
    pack .f3 -side top
    update

    $wt configure -foreground white -background "#303030303030"
    $wt insert end $data
    $wt see "100.500"

    bind $wt <4> "$wt yview scroll -2 units"
    bind $wt <5> "$wt yview scroll  2 units"
    
    update idletasks

    return
}

proc TagAll {} {
    global wt Lgaa Lfreg data

    set i 0
    foreach s [split $data "\n"] {
	incr i
	foreach freg $Lfreg g $Lgaa {
	    set lv [regexp -all -inline -indices -- $freg $s]
	    set lt {}
	    foreach e $lv {
		foreach {d f} $e {}
		lappend lt $i.$d $i.[expr {$f+1}]
	    }
	    if {$lt != {}} {eval [list $wt tag add Tag$g] $lt}
	}
    }
    update idletasks	
    
    return
}

proc DerouleX {} {
    global wt
    set f 0.0
    while {$f <= 1.0} {
	$wt xview moveto $f
	update idletasks
	set f [expr {$f+0.1}]
    }
    
    return
}

proc DerouleY {} {
    global wt
    set f 0.0
    while {$f < 1.0} {
	$wt yview moveto $f
	update idletasks
	set f [expr {$f+0.1}]
    }

    return
}

proc InitXY {q} {
    global wt
    
    $wt xview moveto 0.0
    $wt yview moveto 0.0
    update idletasks

    foreach {ymin xmin} [split [$wt index @0,0] .] {}
    foreach {ymax xmax} [split [$wt index @[winfo width $wt],[winfo height $wt]] .] {}
    if {$q eq "x"} {$wt xview moveto 0.1} {$wt yview moveto 0.1}
    update idletasks
    foreach {yscr xscr} [split [$wt index @[winfo width $wt],[winfo height $wt]] .] {}

    if {$q eq "x"} {
	set ydep $ymin
	set yfin $ymax
	set xdep $xmax
	set xfin $xscr
    } else {
	set ydep $ymax
	set yfin $yscr
	set xdep $xmin
	set xfin [expr {$xmax+1}]
    }
    puts "xdep=$xdep xfin=$xfin ydep=$ydep yfin=$yfin"
    set i $ydep
    set nt 0
    while {$i <= $yfin} {
	set lt [$wt dump -tag -text $i.$xdep $i.$xfin]
	foreach {k v x} $lt {
	    switch $k {
		"text" {
		    set n [expr {[string length $v] - [regsub -all {\.} $v "" tmp]}]
		    incr nt $n
		}
		"tagon" {
		    if {! [info exists tag($v)]} {set tag($v) 0}
		    incr tag($v)
		}
		"tagoff" {}
	    }
	}
	incr i
    }

    puts "$nt caracteres"
    puts "[llength [array names tag]] tags differents"
    set ttot 0
    foreach g [array names tag] {
	puts "\t[set tag($g)] pour $g"
	incr ttot [set tag($g)]
    }

    return
}


#################################

set Long 500
set Haut 500
set data [JunkData]

InitWindow
puts "Length $Long Height $Haut"
ConfTag 

puts "\n Tagging"
puts "[time {TagAll} ]"

puts "\n Stats"
puts "10% scroll X means"
InitXY x
puts "10% scroll Y means"
InitXY y

puts "\n X scroll"
$wt xview moveto 0.0
$wt yview moveto 0.0
update idletasks
puts "[time {DerouleX} ]"

puts "\n Y scroll"
$wt xview moveto 0.0
$wt yview moveto 0.0
update idletasks
puts "[time {DerouleY} ]"

exit
########### la fin des haricots ##############