text: user3684: When we get raw sequenced reads in FASTQ format, we usually need to do some pre-processing (QC, demultiplexing, etc.) prior to sequence alignment. The only way to register the results of this preprocessing is to add metadata to each read. This is best done by using the SAM format which has a well-defined system of metadata tags.
  However, it seems that not a single read mapper (except maybe for bwa aln with option -b) accepts SAM (or one of the binary equivalents BAM/CRAM) as input to make use of the metadata that has been added during preprocessing. This implies that one has to convert the unaligned reads back to FASTQ format. 
  In any case, whether one uses FASTQ throughout or converts back to FASTQ, a common annoyance with FASTQ is that metadata is registered as part of the query name - this is not standardized or even well-defined. After alignment, we again have to do some text processing to get the metadata out of the query name and into proper metadata tags defined by SAM. This back and forth hacking is highly unfortunate and error prone. I have seen this, for example, in some single-cell sequencing projects where we have highly multiplexed sequencing runs with reads that need to be QC'd and classified. The new SAM standard from May 2018 does indeed define proper tags for handling cell-multiplexed runs. SAM is the de-facto standard to store both unaligned and aligned reads for years to come so it should be standard for read mappers to accept it as input.
  I would be grateful for some hints and if we could collect here some read mappers that do accept SAM/BAM/CRAM as input format.
  
  user37: With bwa-mem or minimap2, the recommended way is
  samtools fastq -T BC,RX name-grouped.sam | bwa mem -C -p -
  
  This passes the BC and RX tags through bwa-mem and copies them to the SAM output. If you are running paired-end alignment and your BAM is coordinate sorted, you have to run samtools collate to group reads by name:
  samtools collate -Ol0 in.bam tmp | samtools fastq -T BC,RX - | bwa mem -pC -
  
  Here bwa mem -C or minimap2 -y copies FASTQ comments to SAM output. This strategy is much easier to deal with than SAM/BAM. A caveat is that it doesn't work well for read groups because each read group requires a separate header line. You have to manually modify the header.
  I am not sure which short-read RNA-seq mappers have implemented a similar feature. You might consider to send a feature request.
  
  user77: The common solution for scRNA-seq is to put cell barcodes and such in read headers and then post-process things with UMItools.
  But regarding your actual question, STAR can accept SAM/BAM as input with the --readFilesType SAM PE option (it's SAM for both that format and BAM). You can swap SE for PE if your data is single-end (or is effectively that way, as is the case in many scRNA-seq experiments). Note that in the case of PE experiments that mates must be next to each other. I really don't know what this does with auxiliary tags though, so you would need to see if they're lost before you start processing a lot of data).
  
original_text: 3684: <p>When we get raw sequenced reads in FASTQ format, we usually need to do some pre-processing (QC, demultiplexing, etc.) prior to sequence alignment. The only way to register the results of this preprocessing is to add metadata to each read. This is best done by using the SAM format which has a well-defined system of metadata tags.</p>
  
  <p>However, it seems that not a single read mapper (except maybe for <em>bwa aln</em> with option <em>-b</em>) accepts SAM (or one of the binary equivalents BAM/CRAM) as input to make use of the metadata that has been added during preprocessing. This implies that one has to convert the unaligned reads back to FASTQ format. </p>
  
  <p>In any case, whether one uses FASTQ throughout or converts back to FASTQ, a common annoyance with FASTQ is that metadata is registered as part of the query name - this is not standardized or even well-defined. After alignment, we again have to do some text processing to get the metadata out of the query name and into proper metadata tags defined by SAM. This back and forth hacking is highly unfortunate and error prone. I have seen this, for <strong>example</strong>, in some single-cell sequencing projects where we have highly multiplexed sequencing runs with reads that need to be QC'd and classified. The new SAM standard from May 2018 does indeed define proper tags for handling cell-multiplexed runs. SAM is the de-facto standard to store both unaligned and aligned reads for years to come so it should be standard for read mappers to accept it as input.</p>
  
  <p><strong>I would be grateful for some hints and if we could collect here some read mappers that do accept SAM/BAM/CRAM as input format.</strong></p>
   37: <p>With bwa-mem or minimap2, the recommended way is</p>
  
  <pre><code>samtools fastq -T BC,RX name-grouped.sam | bwa mem -C -p -
  </code></pre>
  
  <p>This passes the <code>BC</code> and <code>RX</code> tags through bwa-mem and copies them to the SAM output. If you are running paired-end alignment and your BAM is coordinate sorted, you have to run <code>samtools collate</code> to group reads by name:</p>
  
  <pre><code>samtools collate -Ol0 in.bam tmp | samtools fastq -T BC,RX - | bwa mem -pC -
  </code></pre>
  
  <p>Here <code>bwa mem -C</code> or <code>minimap2 -y</code> copies FASTQ comments to SAM output. This strategy is much easier to deal with than SAM/BAM. A caveat is that it doesn't work well for read groups because each read group requires a separate header line. You have to manually modify the header.</p>
  
  <p>I am not sure which short-read RNA-seq mappers have implemented a similar feature. You might consider to send a feature request.</p>
   77: <p>The common solution for scRNA-seq is to put cell barcodes and such in read headers and then post-process things with UMItools.</p>
  
  <p>But regarding your actual question, STAR can accept SAM/BAM as input with the <code>--readFilesType SAM PE</code> option (it's <code>SAM</code> for both that format and BAM). You can swap <code>SE</code> for <code>PE</code> if your data is single-end (or is effectively that way, as is the case in many scRNA-seq experiments). Note that in the case of PE experiments that mates <a href="https://github.com/alexdobin/STAR/issues/416" rel="noreferrer">must be next to each other</a>. I really don't know what this does with auxiliary tags though, so you would need to see if they're lost before you start processing a lot of data).</p>
  