Commits

Faheem Mitha committed 84936a4

Rename cm-datafiletable to cm-genetable.

  • Participants
  • Parent commits 14979d8

Comments (0)

Files changed (2)

File cm-datafiletable

-#!/usr/bin/Rscript
-
-# Copyright 2004-2013 Faheem Mitha <faheem@faheem.info>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-
-##http://dba.stackexchange.com/questions/30514/combining-two-similar-sql-queries
-
-#options(error=traceback)
-#options(error=recover)
-#options(error = quote({dump.frames(to.file=TRUE); q()}))
-
-get_gene <- function(schema, connection)
-{
-  Gene <- data.frame(dbGetQuery(connection,
-                               sprintf("SELECT accnum, array_to_string(array_agg(orientation), ', ')
-                                        FROM (SELECT * FROM %1$s.gene ORDER BY accnum, orientation) x
-                                        GROUP BY accnum;", schema)))
-
-  return(Gene)
-}
-
-## https://github.com/hadley/ggplot2/wiki/labeller
-label_wrap_gen <- function(width = 25)
-{
-  function(variable, value) {
-    lapply(strwrap(as.character(value), width=width, simplify=FALSE),
-          paste, collapse="\n")
-  }
-}
-
-get_bottomstuff <- function(schema, connection, bottomstuffstring)
-{
-  tot <- dbGetQuery(connection, sprintf("SELECT count(*) from %1$s.gene", schema))
-  if(!isTRUE(all.equal( max(tot), min(tot))))
-    {
-      stop("values of the tot vector in get_bottomstuff must all be the same")
-    }
-  bs = sprintf(bottomstuffstring, tot[1, 1])
-  return(bs)
-}
-
-extenddatatable <- function(dataframe, rownum)
-{
-  rownum1 = nrow(dataframe)
-  if(rownum1<rownum)
-    for(i in (rownum1+1):rownum)
-      {
-        dataframe[i,] = c("", "")
-      }
-  return(dataframe)
-}
-
-make_table <- function(schema, filename, label, caption, bottomstuffstring)
-  {
-    library(Hmisc)
-    library(RPostgreSQL)
-    library(yaml)
-    if(file.exists("../corrmodel/default_conf.yaml"))
-      conf = yaml.load_file("../corrmodel/default_conf.yaml")
-    else if(file.exists("/etc/corrmodel/default_conf.yaml"))
-      conf = yaml.load_file("/etc/corrmodel/default_conf.yaml")
-    else
-      stop("cannot find config file 'default_conf.yaml'")
-
-    tryCatch(expr=
-             {
-               drv <- dbDriver("PostgreSQL")
-               con <- dbConnect(drv, password=conf$password, dbname=conf$dbname)
-               Gene = get_gene(schema, con)
-               bottomstuff = get_bottomstuff(schema, con, bottomstuffstring)
-             },
-             finally =
-             {
-               dbDisconnect(con)
-               dbUnloadDriver(drv)}
-             )
-    rownum = nrow(Gene)
-    tablerows = ceil(rownum/3)
-    rownum1 = tablerows
-    if(rownum <= tablerows) ## case where table has one column. This is a degenerate case
-      {
-        Gene1 = Gene[1:rownum1, ]
-        Genesplit = Gene1
-        colnames(Genesplit) = c("File", "Orientation")
-      }
-    else if(rownum <= 2*tablerows) ## case where table has two columns. This is a degenerate case
-      {
-        Gene1 = Gene[1:tablerows, ]
-        Gene2 = Gene[(tablerows+1):rownum, ]
-        Gene2 = extenddatatable(Gene2, tablerows)
-        Genesplit = cbind(Gene1, Gene2)
-        colnames(Genesplit) = c("File", "Orientation", "File", "Orientation")
-      }
-    else     ## case where table has three columns.
-      {
-        Gene1 = Gene[1:tablerows, ]
-        Gene2 = Gene[(tablerows+1):(2*tablerows), ]
-        Gene3 = Gene[(2*tablerows+1):rownum, ]
-        Gene3 = extenddatatable(Gene3, tablerows)
-        Genesplit = cbind(Gene1, Gene2, Gene3)
-        colnames(Genesplit) = c("File", "Orientation", "File", "Orientation", "File", "Orientation")
-      }
-
-    ##w <- latex(Genesplit, file=filename, rowname=NULL, col.just=c(">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}"), style=c("array"), where='!ht', multicol=FALSE,
-               ## center="centering",
-               ## label=label,
-               ## caption=caption,
-               ## insert.bottom=bottomstuff)
-
-    w <- latex(Genesplit, file=filename, rowname=NULL, style=c("array"), where='!ht', multicol=FALSE,
-               center="centering",
-               label=label,
-               caption=caption,
-               insert.bottom=bottomstuff)
-    return(Genesplit)
-  }
-
-Genehuman = make_table("human12rss", "GENETABLEHUMAN.tex", "genehumantab", "\\bf{List of Human gene segments containing RSS}",
-    "\\begin{flushleft}\\end{flushleft}")
-
-Genemouse = make_table("mouse12rss", "GENETABLEMOUSE.tex", "genemousetab", "\\bf{List of Mouse gene segments containing RSS}",
-    "\\begin{flushleft}\\end{flushleft}")

File cm-genetable

+#!/usr/bin/Rscript
+
+# Copyright 2004-2013 Faheem Mitha <faheem@faheem.info>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+##http://dba.stackexchange.com/questions/30514/combining-two-similar-sql-queries
+
+#options(error=traceback)
+#options(error=recover)
+#options(error = quote({dump.frames(to.file=TRUE); q()}))
+
+get_gene <- function(schema, connection)
+{
+  Gene <- data.frame(dbGetQuery(connection,
+                               sprintf("SELECT accnum, array_to_string(array_agg(orientation), ', ')
+                                        FROM (SELECT * FROM %1$s.gene ORDER BY accnum, orientation) x
+                                        GROUP BY accnum;", schema)))
+
+  return(Gene)
+}
+
+## https://github.com/hadley/ggplot2/wiki/labeller
+label_wrap_gen <- function(width = 25)
+{
+  function(variable, value) {
+    lapply(strwrap(as.character(value), width=width, simplify=FALSE),
+          paste, collapse="\n")
+  }
+}
+
+get_bottomstuff <- function(schema, connection, bottomstuffstring)
+{
+  tot <- dbGetQuery(connection, sprintf("SELECT count(*) from %1$s.gene", schema))
+  if(!isTRUE(all.equal( max(tot), min(tot))))
+    {
+      stop("values of the tot vector in get_bottomstuff must all be the same")
+    }
+  bs = sprintf(bottomstuffstring, tot[1, 1])
+  return(bs)
+}
+
+extenddatatable <- function(dataframe, rownum)
+{
+  rownum1 = nrow(dataframe)
+  if(rownum1<rownum)
+    for(i in (rownum1+1):rownum)
+      {
+        dataframe[i,] = c("", "")
+      }
+  return(dataframe)
+}
+
+make_table <- function(schema, filename, label, caption, bottomstuffstring)
+  {
+    library(Hmisc)
+    library(RPostgreSQL)
+    library(yaml)
+    if(file.exists("../corrmodel/default_conf.yaml"))
+      conf = yaml.load_file("../corrmodel/default_conf.yaml")
+    else if(file.exists("/etc/corrmodel/default_conf.yaml"))
+      conf = yaml.load_file("/etc/corrmodel/default_conf.yaml")
+    else
+      stop("cannot find config file 'default_conf.yaml'")
+
+    tryCatch(expr=
+             {
+               drv <- dbDriver("PostgreSQL")
+               con <- dbConnect(drv, password=conf$password, dbname=conf$dbname)
+               Gene = get_gene(schema, con)
+               bottomstuff = get_bottomstuff(schema, con, bottomstuffstring)
+             },
+             finally =
+             {
+               dbDisconnect(con)
+               dbUnloadDriver(drv)}
+             )
+    rownum = nrow(Gene)
+    tablerows = ceil(rownum/3)
+    rownum1 = tablerows
+    if(rownum <= tablerows) ## case where table has one column. This is a degenerate case
+      {
+        Gene1 = Gene[1:rownum1, ]
+        Genesplit = Gene1
+        colnames(Genesplit) = c("File", "Orientation")
+      }
+    else if(rownum <= 2*tablerows) ## case where table has two columns. This is a degenerate case
+      {
+        Gene1 = Gene[1:tablerows, ]
+        Gene2 = Gene[(tablerows+1):rownum, ]
+        Gene2 = extenddatatable(Gene2, tablerows)
+        Genesplit = cbind(Gene1, Gene2)
+        colnames(Genesplit) = c("File", "Orientation", "File", "Orientation")
+      }
+    else     ## case where table has three columns.
+      {
+        Gene1 = Gene[1:tablerows, ]
+        Gene2 = Gene[(tablerows+1):(2*tablerows), ]
+        Gene3 = Gene[(2*tablerows+1):rownum, ]
+        Gene3 = extenddatatable(Gene3, tablerows)
+        Genesplit = cbind(Gene1, Gene2, Gene3)
+        colnames(Genesplit) = c("File", "Orientation", "File", "Orientation", "File", "Orientation")
+      }
+
+    ##w <- latex(Genesplit, file=filename, rowname=NULL, col.just=c(">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}", ">{\\centering\\arraybackslash}p{2.5cm}"), style=c("array"), where='!ht', multicol=FALSE,
+               ## center="centering",
+               ## label=label,
+               ## caption=caption,
+               ## insert.bottom=bottomstuff)
+
+    w <- latex(Genesplit, file=filename, rowname=NULL, style=c("array"), where='!ht', multicol=FALSE,
+               center="centering",
+               label=label,
+               caption=caption,
+               insert.bottom=bottomstuff)
+    return(Genesplit)
+  }
+
+Genehuman = make_table("human12rss", "GENETABLEHUMAN.tex", "genehumantab", "\\bf{List of Human gene segments containing RSS}",
+    "\\begin{flushleft}\\end{flushleft}")
+
+Genemouse = make_table("mouse12rss", "GENETABLEMOUSE.tex", "genemousetab", "\\bf{List of Mouse gene segments containing RSS}",
+    "\\begin{flushleft}\\end{flushleft}")