windows
[iramuteq] / PrintRScript.py
index 7f7e4c2..16cf7de 100644 (file)
@@ -4,7 +4,7 @@
 #License: GNU/GPL
 
 import tempfile
-from chemins import ffr
+from chemins import ffr, PathOut
 import os
 import locale
 from datetime import datetime
@@ -18,12 +18,13 @@ class PrintRScript :
         self.pathout = analyse.pathout
         self.analyse = analyse
         self.parametres = analyse.parametres
+        #self.scriptout = ffr(self.pathout['lastRscript.R'])
         self.scriptout = self.pathout['temp']
         self.script =  u"#Script genere par IRaMuTeQ - %s\n" % datetime.now().ctime()
-    
+
     def add(self, txt) :
         self.script = '\n'.join([self.script, txt])
-    
+
     def defvar(self, name, value) :
         self.add(' <- '.join([name, value]))
 
@@ -154,7 +155,7 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
     data1 <- as(data1, "dgCMatrix")
     row.names(data1) <- 1:nrow(data1)
     """ % ffr(DicoPath['TableUc1'])
-    
+
     if classif_mode == 0:
         txt += """
         data2 <- readMM("%s")
@@ -162,28 +163,32 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
         row.names(data2) <- 1:nrow(data2)
         """ % ffr(DicoPath['TableUc2'])
     txt += """
-    chd1<-CHD(data1, x = nbt, mode.patate = mode.patate, svd.method = svd.method, libsvdc.path = libsvdc.path)
-    """
-    
+    log1 <- "%s"
+    chd1<-CHD(data1, x = nbt, mode.patate = mode.patate, svd.method =
+    svd.method, libsvdc.path = libsvdc.path)#, log.file = log1)
+    """ % ffr(DicoPath['log-chd1.txt'])
+
     if classif_mode == 0:
         txt += """
-    chd2<-CHD(data2, x = nbt, mode.patate = mode.patate, svd.method = svd.method, libsvdc.path = libsvdc.path)
-    """
-    
+    log2 <- "%s"
+    chd2<-CHD(data2, x = nbt, mode.patate = mode.patate, svd.method =
+    svd.method, libsvdc.path = libsvdc.path)#, log.file = log2)
+    """ % ffr(DicoPath['log-chd2.txt'])
+
     txt += """
     #lecture des uce
     listuce1<-read.csv2("%s")
     """ % ffr(DicoPath['listeuce1'])
-    
+
     if classif_mode == 0:
         txt += """
         listuce2<-read.csv2("%s")
         """ % ffr(DicoPath['listeuce2'])
-        
+
     txt += """
     rm(data1)
     """
-    
+
     if classif_mode == 0:
         txt += """
         rm(data2)
@@ -203,14 +208,14 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
     write.csv2(n1, file="%s")
     rm(n1)
     """ % (classif_mode, mincl, ffr(DicoPath['uce']), ffr(DicoPath['n1.csv']))
-    
+
     txt += """
     tree.tot1 <- make_tree_tot(chd1)
 #    open_file_graph("%s", widt = 600, height=400)
 #    plot(tree.tot1$tree.cl)
 #    dev.off()
     """ % ffr(DicoPath['arbre1'])
-    
+
     if classif_mode == 0:
         txt += """
         classeuce2 <- chd.result$cuce2
@@ -218,19 +223,19 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
 #        open_file_graph("%s", width = 600, height=400)
 #        plot(tree.tot2$tree.cl)
 #        dev.off()
-        """ % ffr(DicoPath['arbre2'] ) 
-              
+        """ % ffr(DicoPath['arbre2'] )
+
     txt += """
     tree.cut1 <- make_dendro_cut_tuple(tree.tot1$dendro_tuple, chd.result$coord_ok, classeuce1, 1, nbt)
     save(tree.cut1, file="%s")
-    
+
     open_file_graph("%s", width = 600, height=400)
     plot.dendropr(tree.cut1$tree.cl,classes, histo=TRUE)
     open_file_graph("%s", width = 600, height=400)
     plot(tree.cut1$dendro_tot_cl)
     dev.off()
     """ % (ffr(DicoPath['Rdendro']), ffr(DicoPath['dendro1']), ffr(DicoPath['arbre1']))
-    
+
     if classif_mode == 0:
         txt += """
         tree.cut2 <- make_dendro_cut_tuple(tree.tot2$dendro_tuple, chd.result$coord_ok, classeuce2, 2, nbt)
@@ -238,15 +243,15 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
         plot(tree.cut2$tree.cl)
         dev.off()
         open_file_graph("%s", width = 600, height=400)
-        plot(tree.cut1$dendro_tot_cl)
+        plot(tree.cut2$dendro_tot_cl)
         dev.off()
         """ % (ffr(DicoPath['dendro2']), ffr(DicoPath['arbre2']))
-        
+
     txt += """
-    
+
     #save.image(file="%s")
     """ % (ffr(DicoPath['RData']))
-    
+
     fileout = open(DicoPath['Rchdtxt'], 'w')
     fileout.write(txt)
     fileout.close()
@@ -319,7 +324,7 @@ def RchdQuest(DicoPath, RscriptPath, nbcl = 10, mincl = 10):
     fileout.write(txt)
     fileout.close()
     
-def AlcesteTxtProf(DictChdTxtOut, RscriptsPath, clnb, taillecar):
+def ReinertTxtProf(DictChdTxtOut, RscriptsPath, clnb, taillecar):
     txt = "clnb<-%i\n" % clnb
     txt += """
 source("%s")
@@ -704,7 +709,7 @@ class PrintSimiScript(PrintRScript) :
         self.packages(['igraph', 'proxy', 'Matrix'])
         self.sources([self.analyse.parent.RscriptsPath['simi'], self.analyse.parent.RscriptsPath['Rgraph']])
         txt = ''
-        if not self.parametres['keep_coord'] and not self.parametres['type'] == 'simimatrix':
+        if not self.parametres['keep_coord'] and not (self.parametres['type'] == 'simimatrix' or self.parametres['type'] == 'simiclustermatrix') :
             txt += """
             dm.path <- "%s"
             cn.path <- "%s"
@@ -740,7 +745,7 @@ class PrintSimiScript(PrintRScript) :
                 index <- which(colnames(dm) == forme)
             }
             """
-        elif not self.parametres['keep_coord'] and self.parametres['type'] == 'simimatrix' :
+        elif not self.parametres['keep_coord'] and (self.parametres['type'] == 'simimatrix' or self.parametres['type'] == 'simiclustermatrix'):
             txt += """
             dm.path <- "%s"
             selected.col <- "%s"
@@ -909,7 +914,16 @@ class PrintSimiScript(PrintRScript) :
         
         if self.parametres['seuil_ok'] : seuil = str(self.parametres['seuil'])
         else : seuil = 'NULL'
-            
+        
+        if not self.parametres.get('edgecurved', False) :
+            ec = 'FALSE'
+        else :
+            ec = 'TRUE'
+        
+        txt += """
+        edge.curved <- %s
+        """ % ec
+        
         cols = str(self.parametres['cols']).replace(')',', max=255)')
         cola = str(self.parametres['cola']).replace(')',',max=255)')
 
@@ -926,7 +940,7 @@ class PrintSimiScript(PrintRScript) :
         if self.parametres['film'] : 
             txt += """
             film <- "%s"
-            """ % self.pathout['film']
+            """ % ffr(self.pathout['film'])
         else : 
             txt += """
             film <- NULL
@@ -935,7 +949,7 @@ class PrintSimiScript(PrintRScript) :
         seuil <- %s
         if (!is.null(seuil)) {
             if (method!='cooc') {
-                seuil <- seuil/100
+                seuil <- seuil/1000
             } 
         }
         """ % seuil
@@ -1072,7 +1086,7 @@ class PrintSimiScript(PrintRScript) :
                 """
         else :
             #print self.parametres
-            if (self.parametres['type'] == 'clustersimitxt' and self.parametres.get('tmpchi', False)) or (self.parametres['type'] == 'simimatrix' and 'tmpchi' in self.parametres): 
+            if (self.parametres['type'] == 'clustersimitxt' and self.parametres.get('tmpchi', False)) or (self.parametres['type'] in ['simimatrix','simiclustermatrix'] and 'tmpchi' in self.parametres): 
                 txt += """
                 lchi <- read.table("%s")
                 lchi <- lchi[,1]
@@ -1080,7 +1094,7 @@ class PrintSimiScript(PrintRScript) :
                 txt += """
                     lchi <- lchi[sel.col]
                     """
-            if self.parametres['type'] == 'clustersimitxt' and self.parametres.get('cexfromchi', False) :
+            if self.parametres['type'] in ['clustersimitxt', 'simimatrix', 'simiclustermatrix'] and self.parametres.get('cexfromchi', False) :
                 txt += """ 
                 label.cex <- norm.vec(lchi, vcexminmax[1], vcexminmax[2])
                 """
@@ -1092,7 +1106,7 @@ class PrintSimiScript(PrintRScript) :
                 label.cex <- graph.simi$label.cex
             }
             """
-            if (self.parametres['type'] == 'clustersimitxt' or self.parametres['type'] == 'simimatrix') and self.parametres.get('sfromchi', False):
+            if (self.parametres['type'] in ['clustersimitxt', 'simimatrix', 'simiclustermatrix']) and self.parametres.get('sfromchi', False):
                 txt += """ 
                 vertex.size <- norm.vec(lchi, minmaxeff[1], minmaxeff[2])
                 if (!length(vertex.size)) vertex.size <- 0
@@ -1123,7 +1137,7 @@ class PrintSimiScript(PrintRScript) :
                 vertex.label.color <- colm[membership(com)]
             }
         }
-        coords <- plot.simi(graph.simi, p.type='%s',filename="%s", vertex.label = label.v, edge.label = label.e, vertex.col = vertex.col, vertex.label.color = vertex.label.color, vertex.label.cex=label.cex, vertex.size = vertex.size, edge.col = cola, leg=leg, width = width, height = height, alpha = alpha, movie = film, svg = svg)
+        coords <- plot.simi(graph.simi, p.type='%s',filename="%s", vertex.label = label.v, edge.label = label.e, vertex.col = vertex.col, vertex.label.color = vertex.label.color, vertex.label.cex=label.cex, vertex.size = vertex.size, edge.col = cola, leg=leg, width = width, height = height, alpha = alpha, movie = film, edge.curved = edge.curved, svg = svg)
         save.image(file="%s")
         """ % (type, self.filename, ffr(self.pathout['RData']))
         
@@ -1180,7 +1194,7 @@ class ProtoScript(PrintRScript) :
         open_file_graph("%s",height=800, width=1000)
         prototypical(mat, mfreq = %s, mrank = %s, cloud = FALSE, cexrange=c(1,2.4), cexalpha= c(0.4, 1), type = '%s')
         dev.off()
-        """ % (self.analyse.pathout['table.csv'], self.analyse.pathout['proto.png'], self.parametres['limfreq'], self.parametres['limrang'], self.parametres['typegraph'])
+        """ % (ffr(self.analyse.pathout['table.csv']), ffr(self.analyse.pathout['proto.png']), self.parametres['limfreq'], self.parametres['limrang'], self.parametres['typegraph'])
         self.add(txt)
         self.write()
 
@@ -1192,6 +1206,42 @@ class ExportAfc(PrintRScript) :
         txt = """
         """
 
+class MergeGraphes(PrintRScript) :
+    def __init__(self, analyse):
+        self.script = u"#Script genere par IRaMuTeQ - %s\n" % datetime.now().ctime()
+        self.pathout = PathOut()
+        self.parametres = analyse.parametres
+        self.scriptout = self.pathout['temp']
+        self.analyse = analyse 
+
+    def make_script(self) :
+        #FIXME
+
+        txt = """
+        library(igraph)
+        library(Matrix)
+        graphs <- list()
+        """
+        load = """
+        load("%s")
+        g <- graph.simi$graph
+        V(g)$weight <- (graph.simi$mat.eff/nrow(dm))*100
+        graphs[['%s']] <- g
+        """
+        for i, graph in enumerate(self.parametres['graphs']) :
+            path = os.path.dirname(graph)
+            gname = ''.join(['g', `i`])
+            RData = os.path.join(path,'RData.RData')
+            txt += load % (ffr(RData), gname)
+        self.add(txt)
+        self.sources([self.analyse.parent.RscriptsPath['simi']])
+        txt = """
+        ng <- merge.graph(graphs)
+        ngraph <- list(graph=ng, layout=layout.fruchterman.reingold(ng, dim=3), labex.cex=V(ng)$weight)
+        write.graph(ng, "%s", format = 'graphml')
+        """ % ffr(self.parametres['grapheout'])
+        self.add(txt)
+    
 class TgenSpecScript(PrintRScript):
     def make_script(self):
         self.packages(['textometry'])
@@ -1210,6 +1260,235 @@ class TgenSpecScript(PrintRScript):
         colnames(result) <- colnames(tgen)
         row.names(result) <- rownames(tgen)
         write.table(result, file = "%s", sep='\\t', col.names = NA)
-        """ % self.pathout['tgenspec.csv']
+        """ % ffr(self.pathout['tgenspec.csv'])
+        self.add(txt)
+        
+class TgenProfScript(PrintRScript):
+    def make_script(self):
+        self.sources([self.analyse.ira.RscriptsPath['chdfunct']])
+        txt = """
+        tgen <- read.csv2("%s", row.names = 1, sep = '\\t')
+        """ % ffr(self.parametres['tgeneff'])
+        txt += """
+        tgenlem <- read.csv2("%s", row.names = 1, sep = '\\t')
+        """ % ffr(self.parametres['tgenlemeff'])
+        txt += """
+        res <- build.prof.tgen(tgen)
+        write.table(res$chi2, file = "%s", sep='\\t', col.names = NA)
+        write.table(res$pchi2, file = "%s", sep='\\t', col.names = NA)
+        """ % (ffr(self.pathout['tgenchi2.csv']), ffr(self.pathout['tgenpchi2.csv']))
+        txt += """
+        reslem <- build.prof.tgen(tgenlem)
+        write.table(reslem$chi2, file = "%s", sep='\\t', col.names = NA)
+        write.table(reslem$pchi2, file = "%s", sep='\\t', col.names = NA)
+        """ % (ffr(self.pathout['tgenlemchi2.csv']), ffr(self.pathout['tgenlempchi2.csv']))        
         self.add(txt)
         
+class FreqMultiScript(PrintRScript):
+    def make_script(self):
+        self.sources([self.analyse.parent.RscriptsPath['Rgraph']])
+        txt = """
+        freq <- read.csv2("%s", row.names=1, sep='\\t', dec='.')
+        """ % ffr(self.pathout['frequences.csv'])
+        txt += """
+        toplot <- freq[order(freq[,2]) ,2]
+        toplot.names = rownames(freq)[order(freq[,2])]
+        h <- 80 + (20 * nrow(freq))
+        open_file_graph("%s",height=h, width=500)
+        par(mar=c(3,20,3,3))
+        barplot(toplot, names = toplot.names, horiz=TRUE, las =1, col = rainbow(nrow(freq)))
+        dev.off()
+        """ % ffr(self.pathout['barplotfreq.png'])
+        txt += """
+        toplot <- freq[order(freq[,4]) ,4]
+        toplot.names = rownames(freq)[order(freq[,4])]
+        open_file_graph("%s",height=h, width=500)
+        par(mar=c(3,20,3,3))
+        barplot(toplot, names = toplot.names, horiz=TRUE, las =1, col = rainbow(nrow(freq)))
+        dev.off()
+        """ % ffr(self.pathout['barplotrow.png'])
+        self.add(txt)
+        self.write()
+
+class LabbeScript(PrintRScript) :
+    def make_script(self) :
+        self.sources([self.analyse.parent.RscriptsPath['distance-labbe.R'],
+                      self.analyse.parent.RscriptsPath['Rgraph']])
+        txt = """
+        tab <- read.csv2("%s", header=TRUE, sep=';', row.names=1)
+        """ % (ffr(self.pathout['tableafcm.csv']))
+        txt += """
+        dist.mat <- dist.labbe(tab)
+        dist.mat <- as.dist(dist.mat, upper=F, diag=F)
+        write.table(as.matrix(dist.mat), "%s", sep='\t')
+        library(cluster)
+        library(ape)
+        chd <- hclust(dist.mat, method="ward.D2")
+        open_file_graph("%s", width=1000, height=1000, svg=F)
+        par(cex=1.2)
+        plot.phylo(as.phylo(chd), type='unrooted', lab4ut="axial")
+        dev.off()
+        """ % (ffr(self.pathout['distmat.csv']), ffr(self.pathout['labbe-tree.png']))
+        txt +="""
+        open_file_graph("%s", width=1000, height=1000, svg=F)
+        par(mar=c(10,1,1,10))
+        heatmap(as.matrix(dist.mat), symm = T, distfun=function(x) as.dist(x))
+        dev.off()
+        """ % ffr(self.pathout['labbe-heatmap.png'])
+        txt += """
+        #http://stackoverflow.com/questions/3081066/what-techniques-exists-in-r-to-visualize-a-distance-matrix
+        dst <- data.matrix(dist.mat)
+        dim <- ncol(dst)
+        rn <- row.names(as.matrix(dist.mat))
+        open_file_graph("%s", width=1500, height=1000, svg=F)
+        par(mar=c(10,10,3,3))
+        image(1:dim, 1:dim, dst, axes = FALSE, xlab="", ylab="")
+        axis(1, 1:dim, rn, cex.axis = 0.9, las=3)
+        axis(2, 1:dim, rn, cex.axis = 0.9, las=1)
+        text(expand.grid(1:dim, 1:dim), sprintf("%%0.2f", dst), cex=0.6)
+        dev.off()
+        """  % ffr(self.pathout['labbe-matrix.png'])
+        self.add(txt)
+        self.write()
+
+class ChronoChi2Script(PrintRScript) :
+    def make_script(self) :
+        self.sources([self.analyse.parent.RscriptsPath['Rgraph']])
+        print self.parametres
+        txt = """
+        inRData <- "%s"
+        dendrof <- "%s"
+        load(inRData)
+        load(dendrof)
+        """ % (ffr(self.pathout['RData.RData']), ffr(self.pathout['dendrogramme.RData']))
+        txt += """
+        svg <- %s
+        """ % self.parametres['svg']
+        txt += """
+        tc <- which(grepl("%s",rownames(chistabletot)))
+        rn <- rownames(chistabletot)[tc]
+        tc <- tc[order(rn)]
+        dpt <- chistabletot[tc,]
+        tot <- afctable[tc,]
+        tcp <- rowSums(tot)
+        ptc <- tcp/sum(tcp)
+        dpt <- t(dpt)
+        dd <- dpt
+        """ % self.parametres['var'].replace(u'*', u"\\\\*")
+        txt += """
+        classes <- n1[,ncol(n1)]
+        tcl <- table(classes)
+        if ('0' %in% names(tcl)) {
+            to.vire <- which(names(tcl) == '0')
+            tcl <- tcl[-to.vire]
+        }
+        tclp <- tcl/sum(tcl)
+
+        #chi2 colors
+        library(ape)
+        k <- 1e-02
+        lcol <- NULL
+        lk <- k
+        for (i in 1:5) {
+            lcol <- c(lcol, qchisq(1-k,1))
+            k <- k/10
+            lk <- c(lk,k)
+        }
+        lcol <- c(3.84, lcol)
+        lcol <- c(-Inf,lcol)
+        lcol <- c(lcol, Inf)
+        lk <- c(0.05,lk)
+        breaks <- lcol
+        alphas <- seq(0,1, length.out=length(breaks))
+        clod <- rev(as.numeric(tree.cut1$tree.cl$tip.label))
+        #end
+        """
+        txt += """
+        open_file_graph("%s", w=%i, h=%i, svg=svg)
+        """ % (ffr(self.parametres['tmpgraph']), self.parametres['width'], self.parametres['height'])
+        txt += """
+        par(mar=c(3,3,3,3))
+        mat.graphic <- matrix(c(rep(1,nrow(dd)),c(2:(nrow(dd)+1))), ncol=2)
+        mat.graphic <- rbind(mat.graphic, c(max(mat.graphic) + 1 , max(mat.graphic) + 2))
+        hauteur <- tclp[clod] * 0.9
+        heights.graphic <- append(hauteur, 0.1)
+        layout(mat.graphic, heights=heights.graphic, widths=c(0.15,0.85))
+        par(mar=c(0,0,0,0))
+        tree.toplot <- tree.cut1$tree.cl
+        num.label <- as.numeric(tree.cut1$tree.cl$tip.label)
+        col.tree <- rainbow(length(num.label))[num.label]
+        tree.toplot$tip.label <- paste('classe ', tree.toplot$tip.label)
+        plot.phylo(tree.toplot,label.offset=0.1, cex=1.1, no.margin=T, x.lim=20, tip.color = col.tree)
+        for (i in clod) {
+            print(i)
+            par(mar=c(0,0,0,0))
+            lcol <- cut(dd[i,], breaks, include.lowest=TRUE)
+            ulcol <- names(table(lcol))
+            lcol <- as.character(lcol)
+            for (j in 1:length(ulcol)) {
+                lcol[which(lcol==ulcol[j])] <- j
+            }
+            lcol <- as.numeric(lcol)
+            mcol <- rainbow(nrow(dd))[i]
+            last.col <- NULL
+            for (k in alphas) {
+                last.col <- c(last.col, rgb(r=col2rgb(mcol)[1]/255, g=col2rgb(mcol)[2]/255, b=col2rgb(mcol)[3]/255, a=k))
+            }
+            #print(last.col)
+
+            barplot(rep(1,ncol(dd)), width=ptc, names.arg=FALSE, axes=FALSE, col=last.col[lcol], border=rgb(r=0, g=0, b=0, a=0.3))
+        }
+        plot(0,type='n',axes=FALSE,ann=FALSE)
+        label.coords <- barplot(rep(1, ncol(dd)), width=ptc, names.arg = F, las=2, axes=F, ylim=c(0,1), plot=T, col='white')
+        text(x=label.coords, y=0.5, labels=rn[order(rn)], srt=90)
+        dev.off()
+        """
+        self.add(txt)
+        self.write()
+
+class ChronoPropScript(PrintRScript) :
+    def make_script(self) :
+        self.sources([self.analyse.parent.RscriptsPath['Rgraph']])
+        print self.parametres
+        txt = """
+        inRData <- "%s"
+        dendrof <- "%s"
+        load(inRData)
+        load(dendrof)
+        """ % (ffr(self.pathout['RData.RData']), ffr(self.pathout['dendrogramme.RData']))
+        txt += """
+        svg <- %s
+        """ % self.parametres['svg']
+        txt += """
+        tc <- which(grepl("%s",rownames(chistabletot)))
+        rn <- rownames(chistabletot)[tc]
+        tc <- tc[order(rn)]
+        dpt <- chistabletot[tc,]
+        tot <- afctable[tc,]
+        tcp <- rowSums(tot)
+        ptc <- tcp/sum(tcp)
+        dpt <- t(dpt)
+        dd <- dpt
+        """ % self.parametres['var'].replace(u'*', u"\\\\*")
+        txt += """
+        classes <- n1[,ncol(n1)]
+        tcl <- table(classes)
+        if ('0' %in% names(tcl)) {
+            to.vire <- which(names(tcl) == '0')
+            tcl <- tcl[-to.vire]
+        }
+        tclp <- tcl/sum(tcl)
+        """
+        txt += """
+        open_file_graph("%s", w=%i, h=%i, svg=svg)
+        """ % (ffr(self.parametres['tmpgraph']), self.parametres['width'], self.parametres['height'])
+        txt+= """
+        ptt <- prop.table(as.matrix(tot), 1)
+        par(mar=c(10,2,2,2))
+        barplot(t(ptt)[as.numeric(tree.cut1$tree.cl$tip.label),], col=rainbow(ncol(ptt))[as.numeric(tree.cut1$tree.cl$tip.label)], width=ptc, las=3, space=0.05, cex.axis=0.7, border=NA)
+        dev.off()
+        """
+        self.add(txt)
+        self.write()
+
+