Does anyone know of a commonly used method that tr...
# random
c
Does anyone know of a commonly used method that tries to determine where exactly in the genome a given insertion occurred? For example, a drug resistance cassette. I do this by eye at the moment by appending the expected sequence to the fasta and looking at it in IGV. I haven't had enough throughput that I needed to automate it, but it would make sense to me to pull out supplemental alignments and check those -- if there are any reads that align to both the expected locus and the marker, it is where it should be more or less. I think that is likely the only way, but this seems like a common enough task that I thought I'd ask if there are tools, or anyone has alternatives?
t
There are structural variant callers that can find "break points", which should be able to help you here. (It might even work without the insertion sequence, but you can add it as an extra contig/chromosome to you reference genome.) I am currently working on an experiment where we are looking for gene fusions in WGS, so I am interested in the general topic, but don't have practical experience yet. In the #sarek pipeline, there are tools like Manta, TIDDIT and ASCAT, in which I am putting some hope, and there are several other tools not in sarek.
c
yeah -- i have tried Manta and TIDDIT and didn't have consistent results, though it is complicated by the fact that there is a promoter/terminator on either side of the insertion that is from elsewhere in the same genome. There is multi-mapping to contend with, as a result. But, you're right, the answer to my question is to use an SV caller and look at the breakpoints -- thank you!
👍 1
t
Let me know if you find something really good. 🤞
c
For what it is worth, right now im just appending flanking regions to the insertion and then counting reads that overlap the junction between the insertion and the flank. In R:
Copy code
# Example usage
# bam_file <- "TH0003_sorted_markdups_tagged.bam"
# region <- "TH_C8_NAT_IR6:301"
# min_alignment_score <- 0  # no filter
# reads_df <- extract_reads_by_alignment_score('sample1',bam_file, region, min_alignment_score)

# Define the function
extract_reads_by_alignment_score <- function(sample, bam_file, region, min_alignment_score) {
  # Parse the region
  chrom <- gsub(":.*", "", region)
  pos <- as.numeric(gsub(".*:", "", region))
  
  # Ensure only valid fields are requested
  valid_fields <- scanBamWhat()
  requested_fields <- c("qname", "pos", "cigar", "qual")
  what_fields <- requested_fields[requested_fields %in% valid_fields]
  
  # Define the scan parameters, ensuring only valid fields are included
  param <- ScanBamParam(
    which = GRanges(chrom, IRanges(start = pos, end = pos)),
    what = what_fields,
    tag = c("AS", "NM")
  )
  
  # Read the BAM file
  aln <- scanBam(bam_file, param = param)[[1]]
  
  match_length_left_right <- parse_cigar_for_coordinate(aln$pos, aln$cigar, pos)
  
  tibble(sample=sample,
         chr=chrom,
         pos=pos,
         qname = aln$qname,
         cigar=aln$cigar,
         align_qual=aln$tag$AS,
         edit_dist=aln$tag$NM) %>%
    bind_cols(match_length_left_right)
}
output:
Copy code
sample,chr,pos,qname,cigar,align_qual,edit_dist,length_aln_left,length_aln_right
SLB0003,SB_C8_NAT,2245,<qname1>,150M,150,0,113,36
SLB0003,SB_C8_NAT,2245,<qname2>,150M,150,0,63,86
...
where this would be an example of the right junction (2245) with coverage, but the left (which was something like 550) without
you can use the
length_aln_left
and
_right
to remove reads that cover due to soft clipping (filter out a record if either of those is 0)
and i suppose i should include this function for completeness:
Copy code
parse_cigar_for_coordinate <- function(positions, cigars, coordinate) {
  results <- lapply(seq_along(cigars), function(i) {
    pos <- positions[i]
    cigar <- cigars[i]
    
    # Split CIGAR string into operations and lengths
    ops <- gregexpr("[A-Z]", cigar)[[1]]
    lengths <- as.integer(unlist(regmatches(cigar, gregexpr("[0-9]+", cigar))))
    operations <- regmatches(cigar, gregexpr("[A-Z]", cigar))[[1]]
    
    aln_left <- 0
    aln_right <- 0
    reached_coordinate <- FALSE
    
    for (j in seq_along(operations)) {
      op <- operations[j]
      len <- lengths[j]
      
      # For alignment length calculation, consider match, deletion, and mismatch
      if(op %in% c("M", "D", "N", "=", "X")) {
        if (pos + len <= coordinate) {
          aln_left <- aln_left + len
        } else if (pos > coordinate) {
          aln_right <- aln_right + len
        } else {
          aln_left <- aln_left + (coordinate - pos)
          aln_right <- aln_right + (pos + len - coordinate - 1)
          reached_coordinate <- TRUE
          break
        }
        pos <- pos + len
      }
      
      if (reached_coordinate) break
    }
    
    return(c(length_aln_left = aln_left, length_aln_right = aln_right))
  })
  
  return(bind_rows(results))
}
🙏 1
👍 1