Answers.Body: <p>The hyphen &quot;-&quot; character is commonly used as a placeholder in Unix commands to represent the standard input (stdin) or standard output (stdout) streams. From the <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html" rel="nofollow noreferrer">POSIX specification</a>:</p>
  <blockquote>
  <p><strong>Guideline 13:</strong></p>
  <p>For utilities that use operands to represent files to be opened for
  either reading or writing, the '-' operand should be used to mean only
  standard input (or standard output when it is clear from context that
  an output file is being specified) or a file named -.</p>
  </blockquote>
  <p>In your pipeline, the hyphen after the index file name indicates that the output of the <code>bwa mem</code> command should be piped to <a href="https://www.htslib.org/doc/samtools-view.html" rel="nofollow noreferrer"><code>samtools view</code></a>, which will then read it from the standard input stream. The hyphen after the <a href="https://www.htslib.org/doc/samtools-sort.html" rel="nofollow noreferrer"><code>samtools sort</code></a> command, is not strictly necessary since an input SAM/BAM/CRAM is entirely optional. Note that recent versions of <code>samtools</code> let you coordinate sort the SAM and write a sorted BAM, so you can do away with the call to <code>samtools view</code> entirely. For example:</p>
  <pre><code>set -euo pipefail
  
  bwa mem \
      ../01.data/GCF_000146045.2_R64_genomic.fna \
      &quot;../01.data/<span class="math-container">${sra}_1.fastq.gz" \
      "../01.data/$</span>{sra}_2.fastq.gz&quot; |
  samtools sort \
      -o &quot;<span class="math-container">${sra}.sorted.bam"
  samtools index \
      "$</span>{sra}.sorted.bam&quot;
  </code></pre>
  <p>If you wanted to, you could replace the hyphens with the <code>/dev/stdin</code> special file, which represents the standard input streams. Sometimes this can make more complex commands a bit more readable, but note that not all Unix systems support the <code>/dev/stdin</code> and <code>/dev/stdout</code> special files. Most modern Unix systems do however.</p>
  <p>The above uses <code>set -eo pipefail</code> to ensure pipeline errors are not masked. This exits immediately if any command in the pipeline exits with a non-zero status.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T13:03:01.297Z
Answers.LastActivityDate: 2023-03-24T13:28:50.690Z
Answers.LastEditDate: 2023-03-24T13:28:50.690Z
Answers.UserDisplayName: Steve
Answers.Body: <p>It allows you to chain commands together to avoid creating intermediate files and simplify your workflow:</p>
  <blockquote>
  <p>Samtools is designed to work on a stream. It regards an input file `-'
  as the standard input (stdin) and an output file `-' as the standard
  output (stdout). Several commands can thus be combined with Unix
  pipes. Samtools always output warning and error messages to the
  standard error output (stderr).</p>
  </blockquote>
  <p>This is a common idiom in the *nix world, and is actually part of the <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html" rel="nofollow noreferrer">POSIX specification</a>:</p>
  <blockquote>
  <p><strong>Guideline 13:</strong></p>
  <p>For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T11:13:49.880Z
Answers.LastActivityDate: 2023-03-24T13:01:56.060Z
Answers.LastEditDate: 2023-03-24T13:01:56.060Z
Answers.UserDisplayName: user438383
Body: <pre><code>bwa mem ../01.data/GCF_000146045.2_R64_genomic.fna ../01.data/${sra}_1.fastq.gz ../01.data/${sra}_2.fastq.gz | \
      samtools view -S -b -t ../01.data/GCF_000146045.2_R64_genomic.fna.fai - | \
      samtools sort - &gt;$sra.sorted.bam
  samtools index $sra.sorted.bam
  </code></pre>
  <p>I am mapping the downloaded yeast sequences in sra format to the reference genome in a bash shell script.  And I am using samtools to change to bam file.  Then I try to sort the resulted bam file.</p>
  <p>I could follow the steps but I could not understand the use of hyphen after the .fai (index file) and the use of this hyphen before the pipe.  Moreover, what is the use of the hyphen after the samtools sort?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-03-24T14:51:32.443Z
Comments.Text: could take a look at:  https://www.baeldung.com/linux/dash-in-command-line-parameters
Comments.UserDisplayName: envs_h_gang_5
Tags: <samtools>
Title: Problem with samtools command with a final hyphen
ThreadText: # Problem with samtools command with a final hyphen
  
  (Asked by: username_0 on 2023-03-24)
  
  ```
  bwa mem ../01.data/GCF_000146045.2_R64_genomic.fna ../01.data/${sra}_1.fastq.gz ../01.data/${sra}_2.fastq.gz | \
      samtools view -S -b -t ../01.data/GCF_000146045.2_R64_genomic.fna.fai - | \
      samtools sort - >$sra.sorted.bam
  samtools index $sra.sorted.bam
  
  ```
  
  I am mapping the downloaded yeast sequences in sra format to the reference genome in a bash shell script. And I am using samtools to change to bam file. Then I try to sort the resulted bam file.
  
  I could follow the steps but I could not understand the use of hyphen after the .fai (index file) and the use of this hyphen before the pipe. Moreover, what is the use of the hyphen after the samtools sort?
  
  **Tags:** samtools
  
  > **Comments:**
  > **username_1**: could take a look at:  https://www.baeldung.com/linux/dash-in-command-line-parameters  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2023-03-24. Score: 4 | Accepted answer
  
  It allows you to chain commands together to avoid creating intermediate files and simplify your workflow:
  
  > Samtools is designed to work on a stream. It regards an input file `-'
  > as the standard input (stdin) and an output file `-' as the standard
  > output (stdout). Several commands can thus be combined with Unix
  > pipes. Samtools always output warning and error messages to the
  > standard error output (stderr).
  
  This is a common idiom in the \*nix world, and is actually part of the [POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html):
  
  > **Guideline 13:**
  >
  > For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -.
  
  
  ---
  
  ### Answer by username_2 on 2023-03-24. Score: 4
  
  The hyphen "-" character is commonly used as a placeholder in Unix commands to represent the standard input (stdin) or standard output (stdout) streams. From the [POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html):
  
  > **Guideline 13:**
  >
  > For utilities that use operands to represent files to be opened for
  > either reading or writing, the '-' operand should be used to mean only
  > standard input (or standard output when it is clear from context that
  > an output file is being specified) or a file named -.
  
  In your pipeline, the hyphen after the index file name indicates that the output of the `bwa mem` command should be piped to [`samtools view`](https://www.htslib.org/doc/samtools-view.html), which will then read it from the standard input stream. The hyphen after the [`samtools sort`](https://www.htslib.org/doc/samtools-sort.html) command, is not strictly necessary since an input SAM/BAM/CRAM is entirely optional. Note that recent versions of `samtools` let you coordinate sort the SAM and write a sorted BAM, so you can do away with the call to `samtools view` entirely. For example:
  
  ```
  set -euo pipefail
  
  bwa mem \
      ../01.data/GCF_000146045.2_R64_genomic.fna \
      "../01.data/${sra}_1.fastq.gz" \
      "../01.data/${sra}_2.fastq.gz" |
  samtools sort \
      -o "${sra}.sorted.bam"
  samtools index \
      "${sra}.sorted.bam"
  
  ```
  
  If you wanted to, you could replace the hyphens with the `/dev/stdin` special file, which represents the standard input streams. Sometimes this can make more complex commands a bit more readable, but note that not all Unix systems support the `/dev/stdin` and `/dev/stdout` special files. Most modern Unix systems do however.
  
  The above uses `set -eo pipefail` to ensure pipeline errors are not masked. This exits immediately if any command in the pipeline exits with a non-zero status.
  
  
  ---
  
  