...
[iramuteq] / PrintRScript.py
index fd86a2c..c30276f 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 #Author: Pierre Ratinaud
 #Copyright (c) 2008-2011 Pierre Ratinaud
-#Lisense: GNU/GPL
+#License: GNU/GPL
 
 import tempfile
 from chemins import ffr
@@ -33,7 +33,7 @@ class PrintRScript :
 
     def sources(self, lsources) :
         for source in lsources :
-            self.add('source("%s")' % source)
+            self.add('source("%s", encoding = \'utf8\')' % source)
 
     def packages(self, lpks) :
         for pk in lpks :
@@ -293,7 +293,7 @@ def RchdQuest(DicoPath, RscriptPath, nbcl = 10, mincl = 10):
     chd.result<-Rchdquest("%s","%s","%s", nbt = nbt, mincl = mincl)
     n1 <- chd.result$n1
     classeuce1 <- chd.result$cuce1
-    """ % (DicoPath['mat01'], DicoPath['listeuce1'], DicoPath['uce'])
+    """ % (DicoPath['mat01.csv'], DicoPath['listeuce1'], DicoPath['uce'])
     
     txt += """
     tree_tot1 <- make_tree_tot(chd.result$chd)
@@ -390,15 +390,15 @@ write.csv2(gbcluster,file="%s")
     xyminmax <- PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=1, fin=(debsup-1), xlab = xlab, ylab = ylab)
     """ % (DictChdTxtOut['AFC2DL_OUT'])
         txt += """
-    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debsup, fin=(debet-1), xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2])
+    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debsup, fin=(debet-1), xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2], active=FALSE)
     """ % (DictChdTxtOut['AFC2DSL_OUT'])
         txt += """
         if ((fin - debet) > 2) {
-    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debet, fin=fin, xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2])
+    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debet, fin=fin, xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2], active = FALSE)
         }
     """ % (DictChdTxtOut['AFC2DEL_OUT'])
         txt += """
-    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", col=TRUE, what='coord', xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2])
+    PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", col=TRUE, what='coord', xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2], active=FALSE)
     """ % (DictChdTxtOut['AFC2DCL_OUT'])
 #        txt += """
  #   PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='crl', deb=1, fin=(debsup-1), xlab = xlab, ylab = ylab)
@@ -545,6 +545,7 @@ def barplot(table, rownames, colnames, rgraph, tmpgraph, intxt = False) :
     #    height = 400
     rownames = 'c("' + '","'.join(rownames) + '")'
     colnames = 'c("' + '","'.join(colnames) + '")'
+
     if not intxt :
         #FIXME
         txt = """
@@ -710,8 +711,12 @@ class PrintSimiScript(PrintRScript) :
             dm <-readMM(dm.path)
             cn <- read.table(cn.path, sep='\t', quote='"')
             colnames(dm) <- cn[,1]
-            sel.col <- read.csv2(selected.col, header = FALSE)
-            sel.col <- sel.col[,1] + 1
+            if (file.exists(selected.col)) {
+                sel.col <- read.csv2(selected.col, header = FALSE)
+                sel.col <- sel.col[,1] + 1
+            } else {
+                sel.col <- 1:ncol(dm)
+            }
             if (!word) {
                 dm <- dm[, sel.col]
             } else {
@@ -740,8 +745,12 @@ class PrintSimiScript(PrintRScript) :
             txt += """
             dm <-read.csv2(dm.path)
             dm <- as.matrix(dm)
-            sel.col <- read.csv2(selected.col, header = FALSE)
-            sel.col <- sel.col[,1] + 1
+            if (file.exists(selected.col)) {
+                sel.col <- read.csv2(selected.col, header = FALSE)
+                sel.col <- sel.col[,1] + 1
+            } else {
+                sel.col <- 1:ncol(dm)
+            }
             if (!word) {
                 dm <- dm[, sel.col]
             } else {
@@ -839,11 +848,28 @@ class PrintSimiScript(PrintRScript) :
         if self.parametres['type_graph'] == 1 : 
             graphnb = 1
             type = 'nplot'
-            dirout = os.path.dirname(self.pathout['mat01'])
+            dirout = os.path.dirname(self.pathout['mat01.csv'])
             while os.path.exists(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png')):
                 graphnb +=1
             self.filename = ffr(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png'))
         if self.parametres['type_graph'] == 2 : type = 'rgl'
+        if self.parametres['type_graph'] == 3 : 
+            graphnb = 1
+            type = 'web'
+            dirout = os.path.dirname(self.pathout['mat01.csv'])
+            while os.path.exists(os.path.join(dirout,'web_'+str(graphnb))):
+                graphnb +=1
+            self.filename = ffr(os.path.join(dirout,'web_'+str(graphnb)))
+            os.mkdir(self.filename)        
+            self.filename = os.path.join(self.filename, 'gexf.gexf')
+        if self.parametres['type_graph'] == 4 : 
+            graphnb = 1
+            type = 'rglweb'
+            dirout = os.path.dirname(self.pathout['mat01.csv'])
+            while os.path.exists(os.path.join(dirout,'webrgl_'+str(graphnb))):
+                graphnb +=1
+            self.filename = ffr(os.path.join(dirout,'webrgl_'+str(graphnb)))
+            os.mkdir(self.filename)
 
         if self.parametres['arbremax'] : 
             arbremax = 'TRUE'
@@ -1017,7 +1043,7 @@ class PrintSimiScript(PrintRScript) :
                     """
             else :
                 txt+="""
-                label.cex <- NULL
+                label.cex <- cex
                 """
             if self.parametres.get('sfromchi', False) :
                 txt += """
@@ -1028,7 +1054,8 @@ class PrintSimiScript(PrintRScript) :
                 vertex.size <- NULL
                 """
         else :
-            if self.parametres['type'] == 'clustersimitxt' : 
+            #print self.parametres
+            if (self.parametres['type'] == 'clustersimitxt' and self.parametres.get('tmpchi', False)) or (self.parametres['type'] == 'simimatrix' and 'tmpchi' in self.parametres): 
                 txt += """
                 lchi <- read.table("%s")
                 lchi <- lchi[,1]
@@ -1043,12 +1070,12 @@ class PrintSimiScript(PrintRScript) :
             else :
                 txt += """
             if (is.null(vcexminmax[1])) {
-                label.cex <- NULL
+                label.cex <- cex
             } else {
                 label.cex <- graph.simi$label.cex
             }
             """
-            if self.parametres['type'] == 'clustersimitxt' and self.parametres.get('sfromchi', False) :
+            if (self.parametres['type'] == 'clustersimitxt' or self.parametres['type'] == 'simimatrix') and self.parametres.get('sfromchi', False):
                 txt += """ 
                 vertex.size <- norm.vec(lchi, minmaxeff[1], minmaxeff[2])
                 if (!length(vertex.size)) vertex.size <- 0
@@ -1116,3 +1143,52 @@ class WordCloudRScript(PrintRScript) :
         """ % (ffr(self.analyse.pathout['actives_eff.csv']), ffr(self.analyse.pathout['selected.csv']), self.parametres['maxword'], ffr(self.parametres['graphout']), self.parametres['width'], self.parametres['height'], bg_col, self.parametres['maxcex'], self.parametres['mincex'], txt_col)
         self.add(txt)
         self.write()
+
+class ProtoScript(PrintRScript) :
+    def make_script(self) :
+        self.sources([self.analyse.parent.RscriptsPath['Rgraph'], self.analyse.parent.RscriptsPath['prototypical.R']])
+        self.packages(['wordcloud'])
+        txt = """
+        errorn <- function(x) {
+            qnorm(0.975)*sd(x)/sqrt(lenght(n))
+        }
+        errort <- function(x) {
+            qt(0.975,df=lenght(x)-1)*sd(x)/sqrt(lenght(x))
+        }
+        mat <- read.csv2("%s", header = FALSE, row.names=1, sep='\t', quote='"', dec='.')
+        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))
+        dev.off()
+        """ % (self.analyse.pathout['table.csv'], self.analyse.pathout['proto.png'], self.parametres['limfreq'], self.parametres['limrang'])
+        self.add(txt)
+        self.write()
+
+
+class ExportAfc(PrintRScript) :
+    def make_script(self) :
+        self.source([self.analyse.parent.RscriptsPath['Rgraph']])
+        self.packages(['rgexf'])
+        txt = """
+        """
+
+class TgenSpecScript(PrintRScript):
+    def make_script(self):
+        self.packages(['textometry'])
+        txt = """
+        tgen <- read.csv2("%s", row.names = 1, sep = '\\t')
+        """ % self.parametres['tgeneff']
+        txt += """
+        tot <- tgen[nrow(tgen), ]
+        result <- NULL
+        tgen <- tgen[-nrow(tgen),]
+        for (i in 1:nrow(tgen)) {
+            mat <- rbind(tgen[i,], tot - tgen[i,])
+            specmat <- specificities(mat)
+            result <- rbind(result, specmat[1,])
+        }
+        colnames(result) <- colnames(tgen)
+        row.names(result) <- rownames(tgen)
+        write.table(result, file = "%s", sep='\\t', col.names = NA)
+        """ % self.pathout['tgenspec.csv']
+        self.add(txt)
+