<p>I think that the easiest way is to work with query-name sorted groups of reads. In that case, mates will be adjacent in the sort and you can use that to extract the paired CIGARs.</p>
<p>If you are depending on position sorting to extract the alignments of interest efficiently, you can maybe do the following:</p>
<ol>
<li><p>extract alignments in the region of interest plus a lot of padding to ensure that you are also getting the mates (this will obviously only work with standard short-insert shotgun libraries).</p>
</li>
<li><p>Query-name sort the list iterator using built-in <code>sort()</code> or <code>sorted()</code> with a key function that returns the query name.</p>
</li>
<li><p>Iterate over the pairs using adjacency in the iterator to extract paired CIGARs. Discard unpaired reads (first reads whose query name does not match supposed mates).</p>
</li>
</ol>
<p>Code would look something like this (I have not run or tested this in any way!):</p>
<pre class="lang-py prettyprint-override"><code>import pysam

def get_query_key(read):
  &quot;&quot;&quot;read is pysam.AlignedSegment&quot;&quot;&quot;
  return read.query_name

def get_paired_cigars(bam, chr, start, end, padding=1000):
  &quot;&quot;&quot;bam is pysam.AlignmentFile&quot;&quot;&quot;
  reads = list(bam.fetch(bam, chr, start-padding, end+padding))  # may fail at chr ends
  reads.sort(key=get_query_key)
  first_cigar = None
  mate_cigar = None
  cigars = list()
  qn = &quot;&quot;
  for read in reads:
    if first_cigar is None:
      first_cigar = read.cigarstring
      qn = read.query_name
    elif mate_cigar is None:
      if read.query_name != qn:
        first_cigar = read.cigarstring
        qn = read.query_name
      else:
        mate_cigar = read.cigarstring
    if first_cigar is not None and mate_cigar is not None:
      cigars.append((first_cigar, mate_cigar))
      first_cigar = None
      mate_cigar = None
  return cigars
</code></pre>
<p>I agree that I would like this to be simpler! Unfortunately BAM files are not terribly well-designed for using pairing information at that level of detail IMO, I have always had to use something like this. I would be interested to see a straightforward alternative to this approach from someone with more experience.</p>

CC BY-SA 4.0
2021-06-05T19:00:06.703Z
2021-08-12T03:55:49.087Z
2021-08-12T03:55:49.087Z
Maximilian Press
<p>I am working with bam files and I have to check if reads of a specific position or their mates are soft clipped. So, I am looking for a fast way to extract the read pairs from a bam file in python. So far, I use pysam and fetch reads of a given position. From those reads, I have access to the CIGAR string and can check them for soft clipping. My problem is that I cannot access the CIGAR of the read mates. Is there a way to access information of read mates with pysam or another tool, especially CIGAR strings, without iterating over the whole .bam file?</p>
<p>Unlike in this question <a href="https://bioinformatics.stackexchange.com/questions/3838/extracting-the-cigar-string-from-a-bam-via-python">Extracting the CIGAR string from a BAM via Python?</a> I am looking for the CIGAR of both reads of a pair.</p>

<sam><pysam><cigar>
Is there an efficient way to extract CIGAR strings for read pairs from bam files with python?
# Is there an efficient way to extract CIGAR strings for read pairs from bam files with python?

(Asked by: username_0 on 2021-06-05)

I am working with bam files and I have to check if reads of a specific position or their mates are soft clipped. So, I am looking for a fast way to extract the read pairs from a bam file in python. So far, I use pysam and fetch reads of a given position. From those reads, I have access to the CIGAR string and can check them for soft clipping. My problem is that I cannot access the CIGAR of the read mates. Is there a way to access information of read mates with pysam or another tool, especially CIGAR strings, without iterating over the whole .bam file?

Unlike in this question [Extracting the CIGAR string from a BAM via Python?](https://bioinformatics.stackexchange.com/questions/3838/extracting-the-cigar-string-from-a-bam-via-python) I am looking for the CIGAR of both reads of a pair.

**Tags:** sam, pysam, cigar

---

## 1 answers

### Answer by username_1 on 2021-06-05. Score: 5

I think that the easiest way is to work with query-name sorted groups of reads. In that case, mates will be adjacent in the sort and you can use that to extract the paired CIGARs.
If you are depending on position sorting to extract the alignments of interest efficiently, you can maybe do the following:

extract alignments in the region of interest plus a lot of padding to ensure that you are also getting the mates (this will obviously only work with standard short-insert shotgun libraries).

Query-name sort the list iterator using built-in sort() or sorted() with a key function that returns the query name.

Iterate over the pairs using adjacency in the iterator to extract paired CIGARs. Discard unpaired reads (first reads whose query name does not match supposed mates).


Code would look something like this (I have not run or tested this in any way!):
import pysam

def get_query_key(read):
  """read is pysam.AlignedSegment"""
  return read.query_name

def get_paired_cigars(bam, chr, start, end, padding=1000):
  """bam is pysam.AlignmentFile"""
  reads = list(bam.fetch(bam, chr, start-padding, end+padding))  # may fail at chr ends
  reads.sort(key=get_query_key)
  first_cigar = None
  mate_cigar = None
  cigars = list()
  qn = ""
  for read in reads:
    if first_cigar is None:
      first_cigar = read.cigarstring
      qn = read.query_name
    elif mate_cigar is None:
      if read.query_name != qn:
        first_cigar = read.cigarstring
        qn = read.query_name
      else:
        mate_cigar = read.cigarstring
    if first_cigar is not None and mate_cigar is not None:
      cigars.append((first_cigar, mate_cigar))
      first_cigar = None
      mate_cigar = None
  return cigars

I agree that I would like this to be simpler! Unfortunately BAM files are not terribly well-designed for using pairing information at that level of detail IMO, I have always had to use something like this. I would be interested to see a straightforward alternative to this approach from someone with more experience.


---

