+ """ % (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'])
+ if self.parametres.get('cloud', False):
+ cloud = 'TRUE'
+ else:
+ cloud = 'FALSE'
+ 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), type = '%s', mat.col.path='/tmp/matcol.csv')
+ dev.off()
+ """ % (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()
+
+
+class ExportAfc(PrintRScript):
+
+ def make_script(self):
+ self.source([self.analyse.parent.RscriptsPath['Rgraph']])
+ self.packages(['rgexf'])
+ txt = """
+ """
+
+
+class MergeGraphes(PrintRScript):
+
+ def __init__(self, analyse):
+ self.script = "#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', repr(i)])
+ RData = os.path.join(path,'RData.RData')
+ txt += load % (ffr(RData), gname)
+ self.add(txt)
+ self.sources([self.analyse.parent.RscriptsPath['simi']])
+ txt = """
+ merge.type <- 'proto'
+ if (merge.type == 'normal') {
+ ng <- merge.graph(graphs)
+ } else {
+ ng <- merge.graph.proto(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'])
+ txt = """
+ tgen <- read.csv2("%s", row.names = 1, sep = '\\t')
+ """ % ffr(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)
+ """ % 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 += """
+ cs <- colSums(tab)
+ if (min(cs) == 0) {
+ print('empty columns !!')
+ vide <- which(cs==0)
+ print(vide)
+ tab <- tab[,-vide]
+ }
+ #print('#### RcppIramuteq for C++ Labbe ####')
+ #library(RcppIramuteq)
+ #dist.mat <- labbe(as.matrix(tab))
+ #rownames(dist.mat) <- colnames(tab)
+ 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), margins=c(10,10))
+ 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="", col=heat.colors(99), breaks=seq(0.01,1,0.01))
+ 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'])
+ txt += """
+ library(igraph)
+ g <- graph.adjacency(as.matrix(1-dist.mat), mode="lower", weighted=T)
+ write.graph(g, file="%s", format='graphml')
+ open_file_graph("%s", width=1000, height=1000, svg=F)
+ plot(g)
+ dev.off()
+ E(g)$weight <- 1 - E(g)$weight
+ g <- minimum.spanning.tree(g)
+ E(g)$weight <- 1 - E(g)$weight
+ write.graph(g, file="%s", format='graphml')
+ open_file_graph("%s", width=1000, height=1000, svg=F)
+ plot(g)
+ dev.off()
+ """ % (ffr(self.pathout['graph_tot.graphml']), ffr(self.pathout['graph_tot.png']), ffr(self.pathout['graph_min.graphml']), ffr(self.pathout['graph_min.png']))