|
| 1 | +<!DOCTYPE html> |
| 2 | + |
| 3 | +<html> |
| 4 | +<head> |
| 5 | + <title>bionode-sra</title> |
| 6 | + <meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 | + <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 | + <link rel="stylesheet" media="all" href="docco.css" /> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + <div id="container"> |
| 12 | + <div id="background"></div> |
| 13 | + |
| 14 | + <ul class="sections"> |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + <li id="section-1"> |
| 19 | + <div class="annotation"> |
| 20 | + |
| 21 | + <div class="pilwrap "> |
| 22 | + <a class="pilcrow" href="#section-1">¶</a> |
| 23 | + </div> |
| 24 | + <h1 id="bionode-sra">bionode-sra</h1> |
| 25 | +<blockquote> |
| 26 | +<p>A Node.js wrapper for SRA Toolkit.</p> |
| 27 | +<p>doi: <a href="?">?</a> |
| 28 | +author: <a href="http://bmpvieira.com">Bruno Vieira</a> |
| 29 | +email: <a href="mailto:mail@bmpvieira.com">mail@bmpvieira.com</a> |
| 30 | +license: <a href="https://raw.githubusercontent.com/bionode/bionode-sra/master/LICENSE">MIT</a></p> |
| 31 | +</blockquote> |
| 32 | +<hr> |
| 33 | + |
| 34 | + </div> |
| 35 | + |
| 36 | + </li> |
| 37 | + |
| 38 | + |
| 39 | + <li id="section-2"> |
| 40 | + <div class="annotation"> |
| 41 | + |
| 42 | + <div class="pilwrap "> |
| 43 | + <a class="pilcrow" href="#section-2">¶</a> |
| 44 | + </div> |
| 45 | + <h2 id="usage">Usage</h2> |
| 46 | +<p>This module can be used in Node.js as described further below, or as a command line tool. |
| 47 | +Examples:</p> |
| 48 | +<pre><code>$ npm install -g bionode-sra |
| 49 | + |
| 50 | +# bionode-sra [command] [<span class="hljs-built_in">arguments</span>...] |
| 51 | +$ bionode-sra fastq-dump SRR1509835.sra |
| 52 | +$ bionode-sra fastq-dump SRR1509835.sra fastqOutputDirectory |
| 53 | +</code></pre> |
| 54 | + </div> |
| 55 | + |
| 56 | + <div class="content"><div class='highlight'><pre> |
| 57 | + |
| 58 | +<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>) |
| 59 | +<span class="hljs-keyword">var</span> spawn = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>).spawn |
| 60 | +<span class="hljs-keyword">var</span> through = <span class="hljs-built_in">require</span>(<span class="hljs-string">'through2'</span>) |
| 61 | + |
| 62 | +<span class="hljs-built_in">module</span>.exports = exports = SRA</pre></div></div> |
| 63 | + |
| 64 | + </li> |
| 65 | + |
| 66 | + |
| 67 | + <li id="section-3"> |
| 68 | + <div class="annotation"> |
| 69 | + |
| 70 | + <div class="pilwrap "> |
| 71 | + <a class="pilcrow" href="#section-3">¶</a> |
| 72 | + </div> |
| 73 | + <h2 id="sra">SRA</h2> |
| 74 | +<p>Takes a SRA toolkit command and returns a Stream that accepts arguments for it. |
| 75 | +For example, for <code>fastq-dump</code>, arguments can be an array with <code>[srcSRA, destDir]</code>, |
| 76 | +a string like <code>'srcSRA destDir'</code>. If <code>destDir</code> is omitted, the current directory will be used.</p> |
| 77 | +<pre><code><span class="hljs-keyword">var</span> sra = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bionode-sra'</span>) |
| 78 | +<span class="hljs-keyword">var</span> fastqDump = sra(<span class="hljs-string">'fastq-dump'</span>)() |
| 79 | +fastqDump.on(<span class="hljs-string">'data'</span>, <span class="hljs-built_in">console</span>.log) |
| 80 | +fastqDump.write([<span class="hljs-string">'SRR1509835.sra'</span>, <span class="hljs-string">'fastqOutDir'</span>]) |
| 81 | +=> { srcFile: <span class="hljs-string">'SRR1509835.sra'</span>, |
| 82 | + command: <span class="hljs-string">'fastq-dump'</span>, |
| 83 | + destDir: <span class="hljs-string">'fastqOutDir'</span> } |
| 84 | +</code></pre><p>A callback style can also be used:</p> |
| 85 | +<pre><code><span class="hljs-keyword">var</span> fastqDump = sra(<span class="hljs-string">'fastq-dump'</span>) |
| 86 | +fastqDump(<span class="hljs-string">'SRR1509835.sra fastqOutDir'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err, data)</span> </span>{ |
| 87 | + <span class="hljs-built_in">console</span>.log(data) |
| 88 | +}) |
| 89 | +</code></pre><p>Or pipes, for example, from a file with just a list of SRA file names:</p> |
| 90 | +<pre><code><span class="hljs-keyword">var</span> split = <span class="hljs-built_in">require</span>(<span class="hljs-string">'split'</span>) |
| 91 | +<span class="hljs-keyword">var</span> fastqDump = sra(<span class="hljs-string">'fastq-dump'</span>)() |
| 92 | +fs.createReadStream(<span class="hljs-string">'sraFilesList.txt'</span>) |
| 93 | +.pipe(split()) |
| 94 | +.pipe(fastqDump) |
| 95 | +</code></pre> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div class="content"><div class='highlight'><pre> |
| 99 | +<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">SRA</span><span class="hljs-params">(command)</span> </span>{ |
| 100 | + <span class="hljs-keyword">var</span> command = command || <span class="hljs-string">'fastq-dump'</span> |
| 101 | + |
| 102 | + <span class="hljs-keyword">return</span> sraStream |
| 103 | + |
| 104 | + <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sraStream</span><span class="hljs-params">(srcFileDestDir, callback)</span> </span>{ |
| 105 | + <span class="hljs-keyword">var</span> stream = through.obj(transform) |
| 106 | + <span class="hljs-keyword">if</span> (srcFileDestDir) { stream.write(srcFileDestDir); stream.end() } |
| 107 | + <span class="hljs-keyword">if</span> (callback) { |
| 108 | + stream.on(<span class="hljs-string">'data'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(data)</span> </span>{ |
| 109 | + callback(<span class="hljs-literal">null</span>, data) |
| 110 | + }) |
| 111 | + stream.on(<span class="hljs-string">'error'</span>, callback) |
| 112 | + } |
| 113 | + <span class="hljs-keyword">return</span> stream |
| 114 | + |
| 115 | + <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transform</span><span class="hljs-params">(obj, enc, next)</span> </span>{ |
| 116 | + <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span> |
| 117 | + |
| 118 | + <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> obj === <span class="hljs-string">'string'</span>) { obj = obj.split(<span class="hljs-string">' '</span>) } |
| 119 | + |
| 120 | + <span class="hljs-keyword">if</span> (obj.length > <span class="hljs-number">2</span>) { self.emit(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Too many arguments for aspera'</span>)); next() } |
| 121 | + |
| 122 | + <span class="hljs-keyword">if</span> (obj.length === <span class="hljs-number">1</span>) { obj.push(<span class="hljs-string">'.'</span>) } |
| 123 | + |
| 124 | + <span class="hljs-keyword">var</span> sraPath = path.join(__dirname, <span class="hljs-string">'../sra/bin/'</span>+command) |
| 125 | + <span class="hljs-keyword">var</span> srcFile = obj[<span class="hljs-number">0</span>] |
| 126 | + <span class="hljs-keyword">var</span> destDir = obj[<span class="hljs-number">1</span>] |
| 127 | + <span class="hljs-keyword">var</span> options |
| 128 | + <span class="hljs-keyword">if</span> (destDir === <span class="hljs-string">'.'</span>) { |
| 129 | + options = [srcFile] |
| 130 | + } |
| 131 | + <span class="hljs-keyword">else</span> { |
| 132 | + <span class="hljs-keyword">var</span> options = [srcFile, <span class="hljs-string">'-O'</span>, destDir] |
| 133 | + } |
| 134 | + <span class="hljs-keyword">var</span> sra = spawn(sraPath, options) |
| 135 | + |
| 136 | + sra.stderr.on(<span class="hljs-string">'data'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(data)</span> </span>{ |
| 137 | + self.emit(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(data.toString())) |
| 138 | + next() |
| 139 | + }) |
| 140 | + |
| 141 | + sra.on(<span class="hljs-string">'close'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(code)</span> </span>{ |
| 142 | + <span class="hljs-keyword">if</span> (code) { |
| 143 | + self.emit(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Unknown error, check that "'</span>+obj[<span class="hljs-number">0</span>]+<span class="hljs-string">'" exists'</span>)) |
| 144 | + } |
| 145 | + <span class="hljs-keyword">else</span> { |
| 146 | + self.push({srcFile: obj[<span class="hljs-number">0</span>], command: command, destDir: obj[<span class="hljs-number">1</span>]}) |
| 147 | + next() |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | + } |
| 152 | +}</pre></div></div> |
| 153 | + |
| 154 | + </li> |
| 155 | + |
| 156 | + </ul> |
| 157 | + </div> |
| 158 | +</body> |
| 159 | +</html> |
0 commit comments