Coupure prévue mercredi 4 Août pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

ordali_xml.tcl 28.3 KB
Newer Older
luc.moulinier's avatar
luc.moulinier committed
1
2
3
#
# ordali_xml.tcl
#
luc.moulinier's avatar
luc.moulinier committed
4
5


6
7
8
9
10
11
12
13
proc CreeLeMacsim {{out "dehors"} {LNoms ""} {LSeqs ""}} {
    global LNOrdali LSOrdali TabSF TpI TMolW
    global GroupeDeLaSequence
    global NomAlignement
    global AlnName AlnScore AlnNote
    global TColScore
    global {*}[info globals "TD*"]

luc.moulinier's avatar
luc.moulinier committed
14
15
16
17
18
19
20
    # First, check if file is writable
    if {0 && ! [file is writable $out]} {
	FaireLire "Warning !\nThe file named :\n>$out<\ncan not be written ! Check location or rights permission."
	
	return
    }

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    dom createDocument "macsim" doc

    # Cre les commandes de creation de noeuds
    dom createNodeCmd "elementNode" MS::alignment
    dom createNodeCmd "elementNode" MS::aln-name
    dom createNodeCmd "elementNode" MS::aln-score
    dom createNodeCmd "elementNode" MS::aln-note
    dom createNodeCmd "elementNode" MS::sequence
    dom createNodeCmd "elementNode" MS::seq-name
    dom createNodeCmd "elementNode" MS::seq-info
    dom createNodeCmd "elementNode" MS::accession
    dom createNodeCmd "elementNode" MS::nid
    dom createNodeCmd "elementNode" MS::definition
    dom createNodeCmd "elementNode" MS::organism
    dom createNodeCmd "elementNode" MS::taxid
    dom createNodeCmd "elementNode" MS::group
    dom createNodeCmd "elementNode" MS::lifedomain
    dom createNodeCmd "elementNode" MS::hydrophobicity
    dom createNodeCmd "elementNode" MS::ec
    dom createNodeCmd "elementNode" MS::length
    dom createNodeCmd "elementNode" MS::lineage
    dom createNodeCmd "elementNode" MS::taxon
    dom createNodeCmd "elementNode" MS::molecularweight
    dom createNodeCmd "elementNode" MS::isoelectricpoint
    dom createNodeCmd "elementNode" MS::seq-data
    dom createNodeCmd "elementNode" MS::ftable
    dom createNodeCmd "elementNode" MS::fitem
    dom createNodeCmd "elementNode" MS::ftype
    dom createNodeCmd "elementNode" MS::fstart
    dom createNodeCmd "elementNode" MS::fstop
    dom createNodeCmd "elementNode" MS::fcolor
    dom createNodeCmd "elementNode" MS::fscore
    dom createNodeCmd "elementNode" MS::fnote

    dom createNodeCmd "elementNode" MS::goxreflist
    dom createNodeCmd "elementNode" MS::goxref
    dom createNodeCmd "elementNode" MS::goid
    dom createNodeCmd "elementNode" MS::goclass
    dom createNodeCmd "elementNode" MS::godesc
    dom createNodeCmd "elementNode" MS::goevidence

    dom createNodeCmd "elementNode" MS::column-score
    dom createNodeCmd "elementNode" MS::colsco-name
    dom createNodeCmd "elementNode" MS::colsco-owner
    dom createNodeCmd "elementNode" MS::colsco-type
    dom createNodeCmd "elementNode" MS::colsco-group
    dom createNodeCmd "elementNode" MS::colsco-data
    dom createNodeCmd "textNode"    MS::text

    $doc documentElement root
    $root appendFromScript {
	MS::alignment
    }
    set aln [$root lastChild]

    append AlnNote " Created by Ordali"
    foreach n [list "aln-name" "aln-score" "aln-note"] k [list "$AlnName" "AlnScore" "$AlnNote"] {
	$aln appendFromScript {
	    MS::$n {MS::text $k}
	}
    }
    
    # Boucle sur les noms, remplit champ <sequence>
    if {$LNoms eq ""} {
	set LNoms $LNOrdali
	set LSeqs $LSOrdali
    }

    foreach n $LNoms s $LSeqs {
	if {[string trim $n] eq ""} {continue}

	set IsPDB [EstUnAccessPDB $n]
	if {$IsPDB} {
	    set mol [DonneIdDeAccessPDB $n]
	    set chn [DonneChainDeAccessPDB $n]
	    if {$chn eq ""} {set chn "a"}
	}
	set s [string map [list "." - "Z" -] $s]
	set s [string tolower $s]
	set lgs [string length [string map {- ""} $s]]

	$aln appendFromScript {
	    MS::sequence -seq-type Protein
	}
	set ns [$aln lastChild]
	$ns appendFromScript {
	    MS::seq-name {MS::text [set TDesSeqnames($n)]}
	}
	$ns appendFromScript {
	    MS::seq-info
	}
	# sequence infos
	set si [$ns lastChild]
	$si appendFromScript {
	    set ac [set TDesAccess($n)]
	    MS::accession {MS::text $ac}
	}
	if {[info exists TDesBId($n)]} {
	    $si appendFromScript {
		set bid [set TDesBId($n)]
		MS::nid {MS::text $bid}
	    }
	}
	$si appendFromScript {
	    set g [set GroupeDeLaSequence($n)]
	    set g [string range $g 5 end]
	    MS::group {MS::text $g}
	}
	if {[info exists TDesDescriptifs($n)]} {
	    $si appendFromScript {
		set des [set TDesDescriptifs($n)]
		MS::definition {MS::text $des}
	    }
	}
	if {[info exists TDesOrganismes($n)]} {
	    $si appendFromScript {
		set orga [set TDesOrganismes($n)]
		MS::organism {MS::text $orga}
	    }
	}
	if {[info exists TDesTaxId($n)]} {
	    $si appendFromScript {
		set tax [set TDesTaxId($n)]
		MS::taxid {MS::text $tax}
	    }
	}
	$si appendFromScript {
	    MS::length {MS::text $lgs}
	}
	if {[info exists TDesPhylum($n)]} {
	    $si appendFromScript {
		MS::lifedomain {MS::text [set TDesPhylum($n)]}
	    }
	}
	if {[info exists TDuLineage($n)]} {
	    $si appendFromScript {
		MS::lineage
		set tx [$si lastChild]
		foreach t [set TDuLineage($n)] {
		    $tx appendFromScript {
			MS::taxon {MS::text $t}
		    }
		}
	    }
	}
	if {[info exists TDesGO($n)]} {
	    $si appendFromScript {
		MS::goxreflist
		set ng [$si lastChild]
		foreach e [set TDesGO($n)] {
		    $ng appendFromScript {
			MS::goxref
			set nv [$ng lastChild]
			$nv appendFromScript {
			    foreach {k v} $e {
				MS::$k {MS::text $v}
			    }
			}
		    }
		}
	    }
	}

	if {[info exists TDesHydro($n)]} {
	    $si appendFromScript {
		MS::hydrophobicity {MS::text [set TDesHydro($n)]}
	    }
	}
	if {[info exists TDesEC($n)]} {
	    $si appendFromScript {
		MS::ec {MS::text [set TDesEC($n)]}
	    }
	}

	# Features
	if {[array names TabSF "$n,*"] != {} } {
	    $si appendFromScript {
		MS::ftable
	    }
	    set ft [$si lastChild]
	    foreach nf [lsort [array names TabSF "$n,*"]] {
		lassign [split $nf ,] tmp type
luc.moulinier's avatar
luc.moulinier committed
203
204
205
		if {[string first "Cons-" $type] > -1} {
		    continue
		}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
		foreach f [set TabSF($nf)] {
		    DecortiqueUneFeature $f iStart iStop color score note csys target

		    if {$csys eq "local" || $csys eq ""} {
			# DonnePosGS gives pos seq. 
			# starting at 1
			set start [DonnePosGS $n $iStart]
			set stop  [DonnePosGS $n $iStop]
			if {$IsPDB} {
			    # CoordsPDB2Seq gives 
			    # values starting at 0 !
			    set start [expr {[$mol CoordsPDB2Seq $chn $start] + 1}]
			    set stop  [expr {[$mol CoordsPDB2Seq $chn $stop] + 1}]
			}
		    } else {
			set start [expr {$iStart + 1}]
			set stop  [expr {$iStop  + 1}]
		    }
		    if {$target ne "" && $target ne "seq"} {
			$ft appendFromScript {
			    MS::fitem -coords $csys -target $target
			}
		    } else {
			$ft appendFromScript {
			    MS::fitem -coords $csys
			}
		    }

		    set fi [$ft lastChild]
		    $fi appendFromScript {
			MS::ftype {MS::text $type}
		    }
		    $fi appendFromScript {
			MS::fstart {MS::text $start}
		    }
		    $fi appendFromScript {
			MS::fstop {MS::text $stop}
		    }
		    $fi appendFromScript {
			MS::fcolor {MS::text $color}
		    }
		    $fi appendFromScript {
			MS::fscore {MS::text $score}
		    }
		    $fi appendFromScript {
			MS::fnote {MS::text $note}
		    }
		}
	    }
	}
	# data section -> the aa sequence
	$ns appendFromScript {
	    MS::seq-data {MS::text $s}
	}
	$si appendFromScript {
	    MS::molecularweight {MS::text [set TDesMolW($n)]}
	}
	$si appendFromScript {
	    MS::isoelectricpoint {MS::text [set TDespI($n)]}
	}
    }

    if {0} {                    ;#rRRR
    # Column Score
    foreach n $TColScore(Names) {
	#rRRR puts "$n"
	foreach d $TColScore($n,Data) {
	    #rRRR puts "  [llength $d]"
	}
    }
}

    if {[info exists TColScore]} {
	# insert each colmeasurement
	foreach name $TColScore(Names) {
	    set ixc 0
	    foreach owner [set TColScore($name,Owner)] {
		$aln appendFromScript {
		    MS::column-score
		}
		set cs [$aln lastChild]
		
		if {[regexp {Group[0-9]+$} $owner]} {
		    set io [string range $owner 5 end]
		} else {
		    set io [string range $owner [string last "_" $owner]+1 end]
		}

		$cs appendFromScript {
		    MS::colsco-name {MS::text "${name}_$io"}
		}

		foreach cle {colsco-owner colsco-type colsco-data colsco-group} k {Owner Type Data Group} {
		    $cs appendFromScript {
			MS::$cle {MS::text [lindex [set TColScore($name,$k)] $ixc]}
		    }
		}
		incr ixc
	    }
	}
    }
        
    set fout [open $out w]
    puts $fout "<?xml version=\"1.0\"?>"
    puts $fout "<!DOCTYPE macsim SYSTEM \"http://www.lbgi.fr/macsim.dtd\">"
    $doc asXML -channel $fout
    close $fout
    
    $doc delete

    return
}


proc DecortiqueUnXml {f anom aSeq} {

    #package require tdom

    upvar $aSeq Sequences
    upvar $anom LNOrdali

    FaireAttendre .pxml "Please wait ...\nParsing macsim file ..."

    if {$f eq ""} {return 0}
    set LXml {}
    if {[regexp "\n" $f]} {
	set LXml [join $f "\n"]
    } else {
	if {[file exists $f]} {
	    set LXml [ContenuDuFichier $f]
	}
    }
    if {$LXml eq {} } {return 0}

    set Id  [dom parse $LXml]
    set RId [$Id documentElement]
    set LNoeudsNoms [$RId selectNodes //seq-name]
    set Lseq [$RId selectNodes //seq-data]

    set LNOrdali {}
    foreach tn $LNoeudsNoms ts $Lseq {
	set n [$tn selectNodes string(.)]
	set n [string trim $n]

	set s [$ts selectNodes string(.)]
	set s [string map [list "-" "." "~" "." "\n" "" " " ""] $s]
	set s [string toupper $s]

	set Sequences($n) $s
	lappend LNOrdali $n
    }

    FaireLaSuite .pxml

    return $Id
}



proc ExtraitListeTypesFeatures {id aLFeat} {

    upvar $aLFeat ListeTypesFeatures

    set RId [$id documentElement]
    set LNoeudFeatures [$RId selectNodes //ftype]

    foreach n $LNoeudFeatures {
	set f [$n selectNodes string(.)]
	lappend ListeTypesFeatures $f
    }
    set ListeTypesFeatures [lsort -unique $ListeTypesFeatures]

    return $ListeTypesFeatures
}



proc ExtraitListeItemFeatures {id aLItFeat} {

    upvar $aLItFeat ListeItemFeatures

    set RId [$id documentElement]

    set LNoeudFeatures [$RId selectNodes //fitem/*]
    foreach n $LNoeudFeatures {
	set f [$n selectNodes name(.)]
	lappend ListeItemFeatures $f
    }
    set ListeItemFeatures [lsort -unique $ListeItemFeatures]

    return $ListeItemFeatures
}



proc ExtraitLesDescriptifs {id aLDes} {

    global LNOrdali
    global TDesDescriptifs

    set TDesDescriptifs {}
    set RId [$id documentElement]

    set TDesDescriptifs {}
    foreach n $LNOrdali {
	set qds "//*\[string(seq-name)='$n'\]/descendant::*\[name()='definition'\]"
	set nds [$RId selectNodes "$qds"]
	if {$nds != ""} {
	    set d [$nds selectNodes string(.)]
	    set TDesDescriptifs($n) $d
	}
    }

    return
}



proc ExtraitLesTaxId {id aLTax} {

    global LesSequencesAssocieesClassesCanon
    global LNOrdali
    upvar $aLTax LesTaxId

    set LesTaxId {}
    set RId [$id documentElement]

    set LesSequencesAssocieesClassesCanon(eukaryota) {}
    set LesSequencesAssocieesClassesCanon(bacteria) {}
    set LesSequencesAssocieesClassesCanon(archaea) {}
    set LesSequencesAssocieesClassesCanon(viruses) {}
    set LesSequencesAssocieesClassesCanon(undefined) {}
    set LClass [list eukaryota archaea bacteria viruses]
    foreach n $LNOrdali {
	if {[regexp {^[0-9]} $n]} {continue}
	set tid ""
	set qds "//*\[string(seq-name)='$n'\]/descendant::*\[name()='lifedomain'\]"
	set nds [$RId selectNodes "$qds"]
	if {$nds != ""} {
	    set tid [string tolower [$nds selectNodes string(.)]]
	    if {[lsearch $LClass $tid] == -1 || $tid == ""} {set tid "undefined"}
	    lappend LesTaxId ${tid}
	    lappend LesSequencesAssocieesClassesCanon($tid) $n
	} else {
	    lappend LesTaxId "undefined"
	    lappend LesSequencesAssocieesClassesCanon(undefined) $n
	}
    }

    return $LesTaxId
}



luc.moulinier's avatar
luc.moulinier committed
460
proc ExtraitLesPoidsDuXML {id aLWgt} {
461
462
463
464
465
466
467
    global LNOrdali
    upvar $aLWgt LnonesPoidsDesSeqs

    set RId [$id documentElement]

    set LesPoidsDesSeqs {}
    foreach n $LNOrdali {
luc.moulinier's avatar
luc.moulinier committed
468
469
470
	if {$n eq ""} {
	    continue
	}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
	set qds "//*\[string(seq-name)='$n'\]/descendant::*\[name()='weight'\]"
	set nds [$RId selectNodes "$qds"]
	if {$nds != ""} {
	    set d [$nds selectNodes string(.)]
	    lappend LesPoidsDesSeqs $d
	}
    }

    return $LesPoidsDesSeqs
}



proc ExtraitLeGroupeDesSeqs {id aLGS} {

    global LNOrdali
    upvar $aLGS LeGroupe

    set LeGroupe {}
    set RId [$id documentElement]

    foreach n $LNOrdali {
	set n [string trim $n]
	if {$n eq ""} {continue}
	if {[EstUnAccessPDB $n]} {set n [EnlevePrefixeBank $n]}
	set qds "//*\[string(seq-name)='$n'\]/descendant::*\[name()='group'\]"
	set nds [$RId selectNodes "$qds"]
	if {$nds ne ""} {
	    set d [[lindex $nds 0] selectNodes string(.)]
	    lappend LeGroupe $d
	}
    }

    return $LeGroupe
}



proc TabFeaturesDesSeqs {id aTab} {

    upvar $aTab tb
    global LNOrdali

    set RId [$id documentElement]
    ExtraitListeItemFeatures $id LItemFeat
    set tb(Items) $LItemFeat

    foreach n $LNOrdali {
	set query "//*\[string(seq-name)='$n'\]/descendant::*\[name()='fitem'\]"
	set lneitem [$RId selectNodes "$query"]
	set tb($n) {}
	foreach f $lneitem {
	    set ValI {}
	    foreach i $LItemFeat {
		set nfi [$f selectNodes "./$i"]
		set val [$nfi selectNodes string(.)]
		regsub -all " " $val {_} val
		lappend ValI $val
	    }

	    lappend tb($n) $ValI
	}
    }

    return
}



proc DonneTabFeatureDuRsf {aTab} {
    upvar $aTab tb
    global LNOrdali Couleurs Defauts

    FaireAttendre "Please wait ...\nRetrieving feature from RSF file ..."

    set LFS [array names tb "*,Type,*"]

    set csys "global" ; set score 0.0 ; set note ""
    foreach f $LFS {
	set ln [split $f ","]
	set ln [lreplace $ln 1 1]
	set nom [join $ln ","]
	foreach l [set tb($f)] {
	    scan $l "%d %d %d %d %s %s %s" dg fg ds fs col form style
	    set nl [list fstart $dg fstop $fg fcolor $col fscore $score fnote $note fcsys $csys]
	    lappend Ttmp($nom) $nl
	}
    }

    unset -nocomplain tb
    array set tb [array get Ttmp]

    FaireLaSuite .tabf

    return
}


proc DonneTabFeature {aTab} {
    global ListeTypesDeFeatures Defauts LNOrdali LSOrdali TDuGroupeDesSeqs LesSeqsDuGroupe
    upvar $aTab tbf

    #################
    # 
    # !! ATTENTION !!
    # 
    # On prend maintenant les positions 
    # fstart et fstop TELLES QUELLES (position dans
    # la sequence ou pas, et commencent a 1). On transforme 
    # en position absolues quend on cree une vue
    # 
    #################
        
    # record features to be propageted, according
    # to the target attribute
    set Lall {} ; set Lgrp {}
    set ListeTypesDeFeatures {}
    foreach nom $LNOrdali seq $LSOrdali {
	if {! [info exists tbf($nom)]} {continue}

	set lgt [string length [string map {"." ""} $seq]]
	foreach i [set tbf($nom)] {
	    foreach {f v} $i {
		switch $f {
		    "target" {set g $v}
		    "coords" {set y $v}
		    "ftype"  {set t $v}
		    "fstart" {set s $v}
		    "fstop"  {set e $v}
		    "fcolor" {
			if {[regexp {^[0-9]} $v]} {
			    set c [CouleurDuStock $v]
			}  else {
			    set c $v
			}
		    }
		    "fscore" {set r $v}
		    "fnote"  {set n $v}
		    default {lappend nl $f $v}
		}
	    }
	    if {$t eq "STRUCT"} {
		if {[regexp "HELIX" $n]} {
		    set c "red"
		} elseif {[regexp "STRAND" $n]} {
		    set c "green"
		}
	    }
	    # ERRORS in Macsim
	    # 1 - start/stop should not be empty !!
	    if {$e eq "" || $s eq ""} {
		#puts "start ou stop vide !"
		continue
	    }
	    # 2 - stop > start !
	    if {$e < $s} {
		#puts "$nom $t | start > stop : $s $e"
		continue
	    }
	    # 3 - start and stop < length
	    if {$y eq "local"} {
		if {$e > $lgt || $s > $lgt} {
		    #puts "$nom $t | start or stop > length : $s $e - $lgt"
		    continue
		}
	    }

	    set nl [list fstart $s fstop $e fcolor $c fscore $r fnote $n fcsys $y target $g]
	    switch $g {
		"all"   {lappend Lall $nom $t $nl}
		"group" {lappend Lgrp $nom $t $nl}
	    }

	    lappend tbf(${nom},$t) $nl
	    lappend ListeTypesDeFeatures $t
	}
    }
    
    set ListeTypesDeFeatures [lsort -unique $ListeTypesDeFeatures]

    # propagete some feature to all seqs
    if {$Lall != {}} {
	foreach {n type ft} $Lall {
	    foreach nseq $LNOrdali {
		if {$nseq eq ""} {continue}
		if {$n eq $nseq} {continue}
		
		lappend tbf($nseq,$type) $nl
	    }
	}
    }

    # progate some features to the group of seqs
    if {$Lgrp != {} } {
	foreach {n type ft} $Lgrp {
	    set grp [set TDuGroupeDesSeqs($n)]
	    foreach ns $LesSeqsDuGroupe($grp) {
		if {$n eq $ns} {continue}
		
		lappend tbf($ns,$type) $ft
	    }
	}
    }

    return
}


proc DecortiqueUnXml2 {FichierOuTexteXML} {
    # Repompe de rR pour parsing de xml
    # fait tout d'un coup, plus de routines separe pour chaque chose
    
    global LesSequencesAssocieesClassesCanon
    global LNDG SDG
    global LesSeqsDuGroupe
    global LNOrdali Sequences TDesSeqnames
    global TDuGroupeDesSeqs
    global TabSF
    global NoeudMacsim DocMacsim
    global AlnName AlnScore AlnNote TColScore

    if {[file exists $FichierOuTexteXML]} {
	set MacsimXml [ContenuDuFichier $FichierOuTexteXML]
	set FromOrdalie 1
    } else {
	set MacsimXml $FichierOuTexteXML
	set FromOrdalie 0
    }
    
    foreach e {eukaryota bacteria archaea viruses undefined} {
	set LesSequencesAssocieesClassesCanon($e) {}
    }
    
    # Caution !!
    # MacsimXml is not a list, but a text string !!
    set DocMacsim   [dom parse $MacsimXml]
    set NoeudMacsim [$DocMacsim documentElement]
    
    # Initialise arrays for features
    InitInfoArrays

    unset -nocomplain LesSeqsDuGroupe
    
    # alignment header
    set NoeudAli [$NoeudMacsim selectNodes "alignment"]
    set AlnName  [$NoeudAli selectNodes "string(aln-name)"]
    set AlnScore [$NoeudAli selectNodes "string(aln-score)"]
    set AlnNote  [$NoeudAli selectNodes "string(aln-note)"]
    set TColScore(Names) [list]
    
    foreach NoeudLigne [$NoeudAli childNodes] {
	set NodeName [$NoeudLigne nodeName]
	if {[regexp {aln\-} $NodeName]} {
	    continue
	}
	if {[regexp {column\-} $NodeName]} {
	    DecortiqueColumnScore $NoeudLigne
	} else {
	    DecortiqueNoeudXml $NoeudLigne TabSF
	}
    }

    # Verifie qu'on a pas sequences en double !
    set Ltmp $LNOrdali
    set LNOrdali [list]
    foreach n $Ltmp {
	if {[string trim $n] eq ""} {continue}
	lappend LNOrdali $n
    }
    if {[llength $LNOrdali] != [llength [lsort -unique $LNOrdali]]} {
	set ld [DonneDoublons $LNOrdali]
	LogWscope "Beware !\nSeveral sequences have the same name :[join $ld]"
	FaireLire "Beware !\nSeveral sequences have the same name :[join $ld]"
    }
    # premier grp = GroupeToutLeMonde
    InitPremierGroupe
    
    # Ajoute groupes du xml
    set Lg [ReordonneGroupes [array names LesSeqsDuGroupe]]
    if {[llength $Lg] > 1} {
	set LNDG [concat $LNDG $Lg]
	foreach g $Lg {
	    set LesSeqsDuGroupe($g) [lunique [set LesSeqsDuGroupe($g)]]
	    set SDG($g) [set LesSeqsDuGroupe($g)]
	}
    }

    $NoeudMacsim delete
    $DocMacsim delete
    unset -nocomplain DocMacsim NoeudMacsim

    return 1
}


proc DecortiqueNoeudXml {Noeud aTab} {
    upvar $aTab tb
    global {*}[info globals "TD*"] Defauts LesSeqsDuGroupe SDG ListeItemFeatures LesSequencesAssocieesClassesCanon Sequences LNOrdali 
    set ListLifeD $Defauts(LifeDomains)
    set LClass [list eukaryota archaea bacteria viruses]

    set NoeudSeqAli $Noeud

    set SeqNameOri [$NoeudSeqAli selectNodes "string(seq-name)"]
luc.moulinier's avatar
luc.moulinier committed
775
    set SeqName [BonAccess $SeqNameOri "AsIs"]
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
    set SeqData [$NoeudSeqAli selectNodes "string(seq-data)"]
    set SeqData [BonneSequencePourAlignement $SeqData]
    if {$SeqData eq ""} {
	puts "\n>$SeqName< is an empty sequence !"
	return
    }

    set TDesSeqnamesOri($SeqName) $SeqNameOri
    set TDesSeqnames($SeqName) $SeqName
    lappend LNOrdali $SeqName
    set Sequences($SeqName) $SeqData

    set NoeudInfos [$NoeudSeqAli selectNodes "seq-info"]
    set LifeD "undefined"
    set isACGI 0 ; set isBIdRefseq 0
    foreach NoeudInfo [$NoeudInfos childNodes] {
	set NodeName [$NoeudInfo nodeName]
	set v [$NoeudInfo selectNodes "string()"]
	switch $NodeName {
	    "accession" {
		# for compatibility;
		# NCBI GI numbers are IDs, not access
		set bonac [BonAccess $v]
		set isACGI [EstUnGI $bonac]
		set TDesAccess($SeqName) $bonac
	    }
	    "nid" {
		# for compatibility;
		# NCBI GI numbers are IDs, not access
		set bonid [BonAccess $v]
		set isBIdRefseq [EstUnRefseq $bonid]
		set TDesBId($SeqName) $bonid
	    }
	    "lifedomain" {
		set LifeD [string totitle $v]
		if {$LifeD ni $ListLifeD} {set LifeD "undefined"}
		set TDesPhylum($SeqName) $LifeD
	    }
	    "organism" {
		set GenreEspece [string map {"." " "} $v]
		set TDesOrganismes($SeqName) $GenreEspece
		regsub -all { +} $GenreEspece " " GenreEspece
		set Lgs [split $GenreEspece " "]
		set Genre [lindex $Lgs 0]
		set Espece [lindex $Lgs 1]
		set TDesGS($SeqName) "[string range $Genre 0 3].[string range $Espece 0 3]"
	    }
	    "taxid" {
		set TDesTaxId($SeqName) $v
	    }
	    "definition" {
		set TDesDescriptifs($SeqName) $v
	    }
	    "length" {
		set TDesLength($SeqName) $v
	    }
	    "group" {
		set g "Group$v"
		lappend LesSeqsDuGroupe($g) $SeqName
		set TDuGroupeDesSeqs($SeqName) $g
	    }
	    "keywordlist" {
		foreach NoeudKeywordList [$NoeudInfo selectNodes "string(keywordlist)"] {
		    foreach NoeudKeyword [$NoeudKeywordList childNodes] {
			set Keyword [$NoeudKeyword selectNodes "string()"]
		    }
		}
	    }
	    "lineage" {
		set NoeudLin $NoeudInfo
		set Lt {}
		foreach n [$NoeudLin selectNodes "taxon"] {
		    lappend Lt [$n selectNodes string()]
		}
		if {$Lt != {} } {set TDuLineage($SeqName) $Lt}
	    }
	    "goxreflist" {
		set NoeudGO $NoeudInfo
		set Lv {}
		foreach NG [$NoeudGO selectNodes "goxref"] {
		    set LNoeudG [$NG childNodes]
		    foreach n $LNoeudG {
			set f [$n selectNodes name(.)]
			set v [$n selectNodes string()]
			lappend Lv [list $f $v]
		    }
		}
		if {$Lv != {}} {set TDesGO($SeqName) $Lv}
	    }
	    "ftable" {
		set NoeudFtable $NoeudInfo
		set tb($SeqName) {}
		foreach NF [$NoeudFtable selectNodes "fitem"] {
		    set LNoeudFeatures [$NF childNodes]
		    set coords [$NF getAttribute coords local]
		    set target [$NF getAttribute target seq]
		    set Lv {}
		    lappend Lv "coords" $coords "target" $target
		    foreach n $LNoeudFeatures {
			set f [$n selectNodes name(.)]
			set v [$n selectNodes "string()"]
			set v [string map [list " " "_"] $v]
			lappend Lv $f $v
		    }
		    lappend tb($SeqName) $Lv
		}
	    }
	    "ec" {
		set TDesEC($SeqName) $v
	    }
	    "status" {
		set TDesStatus($SeqName) $v
	    }
	    "complex" {
		set TDesComplex($SeqName) $v
	    }
	    "weight" {
		set TDesWeight($SeqName) $v
	    }
	    "sense" {
		set TDesSense($SeqName) $v
	    }
	    "fragment" {
		set TDesFragment($SeqName) $v
	    }
	    "score" {
		set TDesScore($SeqName) $v
	    }
	    "hydrophobicity" {
		set TDesHydro($SeqName) $v
	    }
	    default {
		set Info $v
	    }
	}
    }
    
    if {$isACGI && $isBIdRefseq} {
	# invert the two for compatibility with UniProt
	set tmp $TDesAccess($SeqName)
	set TDesAccess($SeqName) $TDesBId($SeqName)
	set TDesBId($SeqName) $tmp
    }
    
    if {! [info exists TDuGroupeDesSeqs($SeqName)]} {
	set g "Group0"
	lappend LesSeqsDuGroupe($g) $SeqName
	set TDuGroupeDesSeqs($SeqName) $g
    }

    lappend LesSequencesAssocieesClassesCanon($LifeD) $SeqName

    return
}


proc DecortiqueColumnScore {NoeudCol} {
    global TColScore
    
    # Store column-score name in TColScore(Names)
    # For a given name (i.e. normd) create list of lists:
    # TColScore(normd,Group) list of groups
    # TColScore(normd,Owner) list of owners of the data
    # TColScore(normd,Data) List of data attached to each group
    # TColScore(normd,Type) list of data types
    # 
    # the group 'all' stands for all sequences
    #

    set name [$NoeudCol selectNodes "string(colsco-name)"]
    # special case :
    # macsim names should be trucated after '_'
    set suf [string range $name [string last "_" $name]+1 end]
    if {$suf eq "all" || [string is integer -strict $suf]} {
	set name [string range $name 0 [string last "_" $name]-1]
    }
    if {$name ni $TColScore(Names)} {
	lappend TColScore(Names) $name
    }

    lappend TColScore($name,Owner) [$NoeudCol selectNodes "string(colsco-owner)"]
    lappend TColScore($name,Type) [$NoeudCol selectNodes "string(colsco-type)"]
    set cgrp ""
    catch {set cgrp [$NoeudCol selectNodes "string(colsco-group)"]}
    lappend TColScore($name,Group) $cgrp
    lappend TColScore($name,Data)  [$NoeudCol selectNodes "string(colsco-data)"]

    return
}


proc IndexationParAccess {} {
    global {*}[info globals "TD*"] LesSeqsDuGroupe SDG ListeItemFeatures LesSequencesAssocieesClassesCanon Sequences LNOrdali TabSF

    # Verifie qu'on a un access/bid pour tout le monde
    foreach n [lunique $LNOrdali] {
	if {! [info exists TDesAccess($n)]} {
	    set TDesAccess($n) $n
	}
	if {! [info exists TDesBId($n)]} {
	    set TDesBId($n) $n
	}
    }

    # et que les access ne sont pas en double
    set Lacc [list]
    foreach n [lunique $LNOrdali] {
	lappend Lacc [set TDesAccess($n)]
    }
    set l1 [lsort $Lacc]
    set l2 [lsort -unique $Lacc]
    if {[llength $l1] != [llength $l2]} {
	FaireLire "Beware !\nThere are duplicated access, Ordalie may not work properly :\n[join [lor $l1 $l2] " "]"
    }

    # pour coloriage selon phylum dans tree, etc...
    foreach a [array names LesSequencesAssocieesClassesCanon] {
	set Ls [set LesSequencesAssocieesClassesCanon($a)]
	unset LesSequencesAssocieesClassesCanon($a)
	foreach s $Ls {
	    if {$s ne "" && ! [EstUnAccessPDB $s]} {
		lappend LesSequencesAssocieesClassesCanon($a) [set TDesAccess($s)]
	    } else {
		lappend LesSequencesAssocieesClassesCanon($a) $s
	    }
	}
    }

    # idem pour groupes <-> sequences
    foreach a [array names LesSeqsDuGroupe] {
	set Ls [set LesSeqsDuGroupe($a)]
	unset LesSeqsDuGroupe($a)
	foreach s $Ls {
	    if {$s eq "" || [EstUnAccessPDB $s]} {
		lappend LesSeqsDuGroupe($a) $s
	    } else {
		lappend LesSeqsDuGroupe($a) [set TDesAccess($s)]
	    }
	}
    }

    # Tableau des sequences aa
    foreach a [array names Sequences] {
	set s [set Sequences($a)]
	unset Sequences($a)
	set Sequences([set TDesAccess($a)]) $s
    }

    # et pour les pre-features
    foreach a [array names TabSF] {
	set v [set TabSF($a)]
	unset TabSF($a)
	set TabSF([set TDesAccess($a)]) $v
    }

    # Et pour tous les tableaux
    set Ltmp [lunique $LNOrdali]
    set LNOrdali [list]
    foreach sqn $Ltmp {
	set acc [set TDesAccess($sqn)]
	lappend LNOrdali $acc

	foreach t [info globals "TD*"] {
	    if {! [info exists ${t}($sqn)]} {continue}
	    set r [set ${t}($sqn)]
	    unset ${t}($sqn)
	    set ${t}($acc) $r
	}
    }

    return
}


proc CreeLeXml {{filout ""}} {
    global OrdEtcDir tcl_platform DocMacsim NoeudMacsim IdXml

    if {$filout eq ""} {
	set filout [DemandeEtSauveFichier xml 1]
    }
luc.moulinier's avatar
luc.moulinier committed
1056
1057
1058
    if {$filout eq ""} {
	return
    }
1059
1060

    lassign [SequencesASauver] LNoms LSeqs
luc.moulinier's avatar
luc.moulinier committed
1061
1062
1063
    if {$LNoms == {} || $LSeqs == {}} {
	return
    }
1064

luc.moulinier's avatar
luc.moulinier committed
1065
    Annotations2Features
1066
    CreeLeMacsim $filout $LNoms $LSeqs
luc.moulinier's avatar
luc.moulinier committed
1067
    RemoveAnnotationFeatures
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

    return
}



proc MetAJourArbreXml {} {
    global Sequences
    global LNOrdali
    global TabSF
    global NoeudMacsim
    global LNDG
    global SDG

    set RId $NoeudMacsim

    # Met a jour sequences

    set Lseq [$RId selectNodes //seq-name]
    set Ldat [$RId selectNodes //seq-data]
    foreach n $Lseq d $Ldat {
	set s [$n selectNodes string(.)]
	set ix [lsearch $LNOrdali $s]
	if {$ix == -1} {
	    set p [$n parentNode]
	    $p delete
	} else {
	    if {[regexp {^PDB_[0-9]} $s]} {
		set s [string range $s 4 7]
	    }
	    set seq [set Sequences($s)]
	    regsub -all {\.} [string tolower $seq] "-" seq
	    set nodes [$d selectNodes text()]
	    set d [lindex $nodes 0]
	    $d nodeValue $seq
	}
    }

    # Met a jour groupes

    foreach g $LNDG {
	if {$g eq "grise"} {continue}
	if {$g eq "GroupeToutLeMonde"} {continue}
	set i [string range $g 5 end]
	foreach s [set SDG($g)] {
	    set qds "//*\[string(seq-name)='$s'\]/descendant::*\[name()='group'\]"
	    set nds [$RId selectNodes "$qds"]
	    foreach n $nds {
		set nodes [$n selectNodes text()]
		set n [lindex $nodes 0]
		$n nodeValue $i
	    }
	}
    }

    return

    # Met a jour features
    array set TF [array get TabSF]

    foreach nm $LNOrdali {
	set qds "//*\[string(seq-name)='$nm'\]/descendant::*\[name()='fitem'\]"
	set Lnds [$RId selectNodes "$qds"]
	foreach n $Lnds {
	    set qri "./*\[name()='ftype'\]"
	    set nft [$n selectNodes $qri]
	    set ft [$nft selectNodes string(.)]
	    set Lfta [set TF($nm,$ft)]
	    set f [lindex $Lfta 0]
	    DecortiqueUneFeature $f dg fg col score note
	    set nx1 [$n selectNodes "./fstart/text()"]
	    $nx1 nodeValue $dg
	    set nx2 [$n selectNodes "./fstop/text()"]
	    $nx1 nodeValue $fg
	    set TF($nm,$ft) [lreplace [set TF($nm,$ft)] 0 0]
	}
    }

    return
}


proc XML4Macsim {f} {
    set Ll [LesLignesDuFichier $f]
    set o [open $f w]
    foreach l $Ll {
	if {! [regexp {goxref|goid|goclass|molecularweight|isoelectric} $l]} {
	    puts $o $l
	}
    }
    close $o

    return
}