changeset 3:93c4d2fad9af

Fixing bugs and bad code
author Kyle Ellrott <kellrott@gmail.com>
date Wed, 24 Oct 2012 23:31:13 -0700
parents a104cc98359c
children 353be5809eeb
files synapse_create.xml synapse_download.xml synapse_query.xml
diffstat 3 files changed, 165 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/synapse_create.xml	Tue Jul 24 17:34:37 2012 -0400
+++ b/synapse_create.xml	Wed Oct 24 23:31:13 2012 -0700
@@ -1,4 +1,4 @@
-<tool id="synapse_create" name="Synapse Create" version="1.0">
+<tool id="synapse_create" name="Synapse Create" version="2.0.0">
 	<description>Create Synapse</description>
 	<command interpreter="python">$script_file</command>
 	<inputs>
@@ -13,6 +13,28 @@
 			<option value="Data">Data</option>			
 		</param>
 		<repeat name="properties" title="Properties">
+			<param name="name" type="select" size="90" label="Name">
+				<option value="name">Name</option>
+				<option value="parentId">ParentID</option>
+				<option value="description">Description</option>
+				<option value="species">Species</option>
+				<option value="numSamples">Number of Samples</option>
+				<option value="disease">Disease</option>
+				<option value="tissueType">Tissue Type</option>
+			</param>
+			<param name="value" type="text" size="90" label="Value">
+				<sanitizer>
+					<valid initial="string.printable">
+						<remove value="&quot;"/>
+					</valid>
+					<mapping initial="none">
+						<add source="&quot;" target="\&quot;"/>
+					</mapping>
+				</sanitizer>
+			</param>
+		</repeat>
+		
+		<repeat name="annotations" title="Annotations">
 			<param name="name" type="text" size="90" label="Name"/>
 			<param name="value" type="text" size="90" label="Value">
 				<sanitizer>
@@ -26,11 +48,6 @@
 			</param>		
 		</repeat>
 		
-		<repeat name="annotations" title="Annotations">
-			<param name="name" type="text" size="90" label="Name"/>
-			<param name="value" type="text" size="90" label="Value"/>		
-		</repeat>
-		
 	</inputs>
 	<outputs>
 		<data format="txt" name="outfile" />
@@ -40,33 +57,52 @@
 rcode="""
 require(synapseClient)
 
-if (nchar("$user") > 0 && nchar("$pass") > 0) {
-	synapseLogin(username="$user", password="$pass")
+galaxyClean <- function(x) { 
+y = gsub("__gt__", ">", x);
+y = gsub("__lt__", "<", y );
+y = gsub("__sq__", "'", y);
+y = gsub("__dq__", "\\"",y);
+y = gsub("__ob__", "[", y);
+y = gsub("__cb__", "]", y);
+y = gsub("__oc__", "{", y);
+y = gsub("__cc__", "}", y);
+y = gsub("__at__", "@", y);
+y = gsub("__cn__", "\\n", y);
+y = gsub("__cr__", "\\r", y);
+y = gsub("__tc__", "\\t", y);
+y = gsub("__pd__", "#", y);
+y = gsub("__at__", "@", y);
+return(y);
 }
 
-ent <- ${etype}( list(name="${name}") )
+if (nchar("$user") && nchar("$pass")) {
+	synapseLogin(username=galaxyClean("$user"), password=galaxyClean("$pass"))
+}
+
+
+ent <- ${etype}( list(name=galaxyClean("${name}")) )
 
 #for $a in $annotations:
-annotValue(ent, "${a.name}") <- "${a.value}"
+annotValue(ent, galaxyClean("${a.name}")) <- galaxyClean("${a.value}")
 #end for
 
 #for a in $properties:
-propertyValue(ent, "${a.name}") <- "${a.value}"
+propertyValue(ent, galaxyClean("${a.name}")) <- galaxyClean("${a.value}")
 #end for
 
 ent <- createEntity(ent)
 
 #if $attachment != 'None' 
 #if $attachment_name == '':
-ent <- addFile(ent, "${attachment}")
+ent <- addFile(ent, galaxyClean("${attachment}"))
 #else
-ent <- addFile(ent, "${attachment}", "${attachment_name}")
+ent <- addFile(ent, galaxyClean("${attachment}"), galaxyClean("${attachment_name}"))
 #end if
 #end if
 
 ent <- storeEntity(ent)
 
-write(properties(ent)\$id, file="${outfile}");
+write(properties(ent)\$id, file=galaxyClean("${outfile}"));
 
 """
 
@@ -89,4 +125,14 @@
 
 ]]></configfile>
 	</configfiles>
-</tool>
+		<help>
+
+Install R SynapseClient:
+    source('http://depot.sagebase.org/CRAN.R')
+    pkgInstall("synapseClient")
+
+`To setup auto login (so you don't have to type in your password) &lt;https://sagebionetworks.jira.com/wiki/display/SYNR/How+to+configure+automatic+login&gt;`_ 
+
+	</help>
+
+</tool>
\ No newline at end of file
--- a/synapse_download.xml	Tue Jul 24 17:34:37 2012 -0400
+++ b/synapse_download.xml	Wed Oct 24 23:31:13 2012 -0700
@@ -1,23 +1,13 @@
-<tool id="synapse_download" name="Synapse Download" version="1.0">
+<tool id="synapse_download" name="Synapse Download" version="2.0.0">
 	<description>Download Synapse Entity</description>
-	<command interpreter="python">$script_file
-#if str( $user ) != '':
-  -u "$user"
-#end if
-#if str( $pass ) != '':
-  -p "$pass"
-#end if    
--s $synid
-$data_type
--o $outfile
-	</command>
+	<command interpreter="python">$script_file</command>
 	<inputs>
 		<param name="user" type="text" size="90" label="Username"/>
 		<param name="pass" type="text" size="90" label="Password"/>
 		<param name="synid" type="text" label="Synapse ID"/>
 		<param name="data_type" type="select" label="Data Type">
-			<option value="-e">Affy Eset</option>
-			<option value="-a">Attachment</option>
+			<option value="e">Affy Eset</option>
+			<option value="a">Attachment</option>
 		</param> 
 	</inputs>
 	<outputs>
@@ -26,33 +16,44 @@
 	<configfiles>
         	<configfile name="script_file"><![CDATA[#!/usr/bin/env python
 rcode = """
-library('getopt');
 library(affy)
 require(synapseClient)
 
-opt = getopt(c(
-	'user', 'u', 2, "character",
-	'pass', 'p', 2, "character",
-	'outfile', 'o', 2, "character",
-	'synid', 's', 2, "character",
-	'eset', 'e', 0, "logical",
-	'attach', 'a', 0, "logical"	
-));
-
-if ( !is.null(opt\$user) && !is.null(opt\$pass)) {
-	synapseLogin(username=user, password=pass)
+galaxyClean <- function(x) { 
+y = gsub("__gt__", ">", x);
+y = gsub("__lt__", "<", y );
+y = gsub("__sq__", "'", y);
+y = gsub("__dq__", "\\"",y);
+y = gsub("__ob__", "[", y);
+y = gsub("__cb__", "]", y);
+y = gsub("__oc__", "{", y);
+y = gsub("__cc__", "}", y);
+y = gsub("__at__", "@", y);
+y = gsub("__cn__", "\\n", y);
+y = gsub("__cr__", "\\r", y);
+y = gsub("__tc__", "\\t", y);
+y = gsub("__pd__", "#", y);
+y = gsub("__at__", "@", y);
+return(y);
 }
 
-ent <- loadEntity(opt\$synid);
+if (nchar("$user") && nchar("$pass")) {
+	synapseLogin(username=galaxyClean("$user"), password=galaxyClean("$pass"))
+}
+
 
-if (!is.null(opt\$eset)) {
+cat("Loading...")
+ent <- loadEntity("$synid");
+cat("Done\n")
+
+if ("e" == "$data_type") {
 	eset <- exprs(ent\$objects\$eset);
-	write(paste(c("PROBE", colnames(eset)), collapse="\\t"), file=opt\$outfile)
-	write.table(eset, opt\$outfile, col.names=FALSE, sep="\\t", quote=FALSE, append=TRUE);
+	write(paste(c("PROBE", colnames(eset)), collapse="\\t"), file="$outfile")
+	write.table(eset, "$outfile", col.names=FALSE, sep="\\t", quote=FALSE, append=TRUE);
 }
-if (!is.null(opt\$attach)) {
+if ("a" == "$data_type") {
 	fpath <- file.path(ent\$cacheDir, ent\$files[[1]]);
-	file.copy(fpath, opt\$outfile, overwrite=T);
+	file.copy(fpath, "$outfile", overwrite=T);
 }
 
 """
@@ -66,9 +67,26 @@
 os.write(h,rcode)
 os.close(h)
 
-print subprocess.check_output( ["Rscript", path] + sys.argv[1:], stderr=subprocess.STDOUT )
+cmd_args = ["R", "CMD", "BATCH", path ] 
+proc = subprocess.Popen(cmd_args, stderr=subprocess.PIPE)
+stdout, stderr = proc.communicate()	
+if proc.poll() != 0:
+	sys.stderr.write(stderr)
 os.unlink(path)
-
 ]]></configfile>
 	</configfiles>
-</tool>
+	<help>
+
+This tool current supports two types of data from Synapse: Affy eset data and attachment files.
+For Affy eset data, it will attempt to turn the eset into a table and write a tab separated file. For attachment files, it will 
+save the first attached file as the output.
+
+Install R SynapseClient::
+
+    source('http://depot.sagebase.org/CRAN.R')
+    pkgInstall("synapseClient")
+
+`To setup auto login (so you don't have to type in your password) &lt;https://sagebionetworks.jira.com/wiki/display/SYNR/How+to+configure+automatic+login&gt;`_ 
+
+	</help>
+</tool>
\ No newline at end of file
--- a/synapse_query.xml	Tue Jul 24 17:34:37 2012 -0400
+++ b/synapse_query.xml	Wed Oct 24 23:31:13 2012 -0700
@@ -1,19 +1,12 @@
 <tool id="synapse_query" name="Synapse Query" version="1.0">
 	<description>Query Synapse</description>
-	<command interpreter="python">$script_file
-#if str( $user ) != '':
-  -u "$user"
-#end if    
-
-#if str( $pass ) != '':
-  -p "$pass"
-#end if    
--o $outfile
-	</command>
+	<command interpreter="python">$script_file</command>
 	<inputs>
 		<param name="user" type="text" size="90" label="Username"/>
 		<param name="pass" type="text" size="90" label="Password"/>
-		<param name="query" type="text" area="True" size="5x35" label="Query">
+		<param name="query" type="text" area="True" size="5x35" label="Query"/>
+		
+		<!--
 			<sanitizer>
 				<valid initial="string.printable">
 					<remove value="&quot;"/>
@@ -22,32 +15,47 @@
 					<add source="&quot;" target="&apos;"/>
 				</mapping>
 			</sanitizer>
-		</param>		
+		</param>
+		-->		
 	</inputs>
 	<outputs>
-		<data format="txt" name="outfile" />
+		<data format="tabular" name="outfile" />
 	</outputs>
 	<configfiles>
         	<configfile name="script_file"><![CDATA[#!/usr/bin/env python
 rcode="""
-library('getopt');
 library(affy)
 require(synapseClient)
 
-opt = getopt(c(
-	'user', 'u', 2, "character",
-	'pass', 'p', 2, "character",
-	'outfile', 'o', 2, "character"
-));
-
-query <- "${query}";
-cat(query)
-if ( !is.null(opt\$user) && !is.null(opt\$pass)) {
-	synapseLogin(username=user, password=pass)
+galaxyClean <- function(x) { 
+y = gsub("__gt__", ">", x);
+y = gsub("__lt__", "<", y );
+y = gsub("__sq__", "'", y);
+y = gsub("__dq__", "\\"",y);
+y = gsub("__ob__", "[", y);
+y = gsub("__cb__", "]", y);
+y = gsub("__oc__", "{", y);
+y = gsub("__cc__", "}", y);
+y = gsub("__at__", "@", y);
+y = gsub("__cn__", "\\n", y);
+y = gsub("__cr__", "\\r", y);
+y = gsub("__tc__", "\\t", y);
+y = gsub("__pd__", "#", y);
+y = gsub("__at__", "@", y);
+return(y);
 }
 
+if (nchar("$user") && nchar("$pass")) {
+	synapseLogin(username=galaxyClean("$user"), password=galaxyClean("$pass"))
+}
+
+
+
+query <- galaxyClean("${query}");
+cat(query)
+
 query.out <- synapseQuery(query);
-write.table(query.out, quote=F, file=opt\$outfile, row.name=F, sep="\\t")
+write.table(query.out, quote=F, file="$outfile", row.name=F, sep="\\t")
 
 """
 import tempfile
@@ -55,18 +63,32 @@
 import sys
 import subprocess
 
-h, path = tempfile.mkstemp()
+h, path = tempfile.mkstemp(dir="/tmp")
 os.write(h,rcode)
 os.close(h)
 
-proc = subprocess.Popen( ["Rscript", path] + sys.argv[1:], stderr=subprocess.PIPE, stdout=subprocess.PIPE )
+proc = subprocess.Popen( ["Rscript", path], stderr=subprocess.PIPE, stdout=subprocess.PIPE )
 (stdoutdata, stderrdata) = proc.communicate()
 if proc.returncode:
 	sys.stderr.write(stderrdata)
 sys.stdout.write(stdoutdata)
 os.unlink(path)
 
-
 ]]></configfile>
 	</configfiles>
-</tool>
+		<help>
+
+Install R SynapseClient::
+
+    source('http://depot.sagebase.org/CRAN.R')
+    pkgInstall("synapseClient")
+
+`To setup auto login (so you don't have to type in your password) &lt;https://sagebionetworks.jira.com/wiki/display/SYNR/How+to+configure+automatic+login&gt;`_ 
+
+Example Query:
+
+select * from entity where parentId=="syn300013"
+
+	</help>
+
+</tool>
\ No newline at end of file