preserving newlines in input parameters
I would like to preserve the newlines in the input parameters of my tool. I already found out that I can preserve the other characters such as ^,@ and more using the following xml code. <inputs> <param name="input" type="text" area="true" size="40x80" label="some input"> <sanitizer> <valid initial="default"> <add preset="string.printable"/> <remove value="""/> <remove value="\"/> </valid> <mapping initial="none"> <add source="\" target="\\"/> <add source=""" target="\""/> </mapping> </sanitizer> </param> </inputs> But this makes it not possible to preserve newline characters, they are all still replaced by spaces.
I found a solution, it seemed that I made a stupid mistake somewhere. But the following solution will work to preserve the original text in the edit field. Add the following to your parameter <param name="myparameter" type="text" area="true" size="40x80" label="sample parameter"> <sanitizer> <valid initial="default"> <add preset="string.printable"/> <remove value="""/> <remove value="\"/> <remove value=" "/> <remove value=" "/> <remove value="&"/> </valid> <mapping initial="none"> <add source="\" target="\\"/> <add source=""" target="\""/> <add source="&" target="&&"/> <add source=" " target="&n"/> <add source=" " target="&r"/> </mapping> </sanitizer> </param> I make use of to escape characters I use \ for \ and " and use & for the newline and carage return, because otherwise \n will reformatted to \\n once i get it inside ruby. Then I use the following code in ruby to decode it again def unescape(val) res = "" escape = false val.each_char do |char| if escape escape = false case char when '&' res += '&' when '\\' res += '\\' when 'n' res += "\n" when '\"' res += '\"' end else if char == '\\' || char == '&' escape = true else res += char end end end return res end If you want to preserve tabs you will have to add them, but example should be clear on how to do that.
Hello Jesse, Dam, Jesse van wrote, On 06/19/2012 01:27 PM:
I found a solution, it seemed that I made a stupid mistake somewhere. But the following solution will work to preserve the original text in the edit field.
Add the following to your parameter
[...]
Then I use the following code in ruby to decode it again def unescape(val) res = "" escape = false [ ... ]
If you want to preserve tabs you will have to add them, but example should be clear on how to do that.
Alternatively, You can use the undocumented "url_paste", "file_data" variables which are never sanitized (but like all undocumented things - use at your own risk) [1]. Also, It seems you want to give your use a big text area in which to write complicated text, a quicker solution might be to use a config file, and in your ruby script simply read the file - this preserves everything the user entered, with no extra work required (besides reading the file). This is a snippet from an AWK tool [2], that allows users to enter long AWK programs: === <command interpreter="sh">awk_wrapper.sh '$input1' '$output' '$awk_script'</command> <inputs> <param format="txt" name="input1" type="data" label="File to process"/> <param name="url_paste" type="text" area="true" size="5x35" label="AWK Program" help="" /> </inputs> <outputs> <data format="input" name="output" metadata_source="input1"/> </outputs> <configfiles> <configfile name="awk_script">$url_paste</configfile> </configfiles> === Galaxy will create a temporary file (<configfile>) and put whatever the user entered there - newlines and everything. -gordon [1]: undocumented non-sanitized variables: $ grep NEVER lib/galaxy/util/__init__.py # is NEVER_SANITIZE required now that sanitizing for tool parameters can be controlled on a per parameter basis and occurs via InputValueWrappers? NEVER_SANITIZE = ['file_data', 'url_paste', 'URL', 'filesystem_paths'] if key not in self.NEVER_SANITIZE and True not in [ key.endswith( "|%s" % nonsanitize_parameter ) for nonsanitize_parameter in self.NEVER_SANITIZE ]: #sanitize check both ungrouped and grouped parameters by name. Anything relying on NEVER_SANITIZE should be changed to not require this and NEVER_SANITIZE should be removed [2] AWK tool: http://hannonlab.cshl.edu/galaxy_unix_tools/download.html
participants (3)
-
Assaf Gordon
-
Dam, Jesse van
-
Peter Cock