Python pysam.AlignedRead() Examples

The following are 3 code examples of pysam.AlignedRead(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pysam , or try the search function .
Example #1
Source File: hg19util.py    From ViFi with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, line, start=-1, end=-1, strand=1,
        file_format='', bamfile=None, info=''):
        self.info = ""
        self.file_format = file_format
        if type(line) == pysam.AlignedRead or type(line) == pysam.AlignedSegment:
            self.load_pysamread(line, bamfile)
        elif start == -1:
            self.load_line(line, file_format)
        elif end == -1:
            self.load_pos(line, start, start, strand)
        else:
            self.load_pos(line, start, end, strand)
        if len(info) > 0:
            self.info = info 
Example #2
Source File: hg19util.py    From ViFi with GNU General Public License v3.0 5 votes vote down vote up
def load_pysamread(self, line, bamfile):
        if bamfile is None:
            raise "Interval of pysam AlignedRead without bamfile"
        self.chrom = line.reference_name
        self.start = line.reference_start
        self.end = line.reference_end
        if line.is_reverse:
            self.strand = -1
        else:
            self.strand = 1 
Example #3
Source File: dropest_bc_correct.py    From velocyto.py with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def dropest_bc_correct(bamfilepath: str, dropest_out: str, corrected_output: str) -> None:
    """Using the output of DropEst:
    (1) Corrects barcodes directly in the bam file
    (2) Produces a valid barcodes list

    BAMFILEPATH - bam file with sorted reads obtained running DropEst

    DROPEST-OUT - R dump `rds` file generated by DropEst
    """

    try:
        import rpy2.robjects as ro
        from velocyto.r_interface import convert_r_obj
    except:
        ImportError("A problem was encountered importing rpy2. To run this `velocyto tools` rpy2 and R need to be installed (the use conda is recommended)")

    # bamfilepath = sys.argv[1]
    # filename = os.path.join(parentpath, f"{bamfilename.split('_')[0]}_dropEst.rds")
    parentpath, bamfilename = os.path.split(bamfilepath)
    filename = dropest_out
    logging.info(f"Loading `merge_targets` from {filename} using R / rpy2")
    mapping = convert_r_obj(ro.r(f"rds <- readRDS('{filename}'); rds$merge_targets"))  # a dict

    output_path = os.path.join(parentpath, f"barcodes_{bamfilename.split('_')[0]}.tsv")
    logging.info(f"Generating {output_path}")
    with open(output_path, "w") as fout:
        unique_bcs = set(list(mapping.values()))
        str_ = '\n'.join(list(unique_bcs))
        fout.write(str_)

    infile = pysam.AlignmentFile(bamfilepath, mode="rb")
    if corrected_output is None:
        bam_out_path = os.path.join(parentpath, f"correct_{bamfilename}")
    else:
        bam_out_path = corrected_output

    outfile = pysam.AlignmentFile(bam_out_path, mode="wb", template=infile)

    for read in infile:
        # read: pysam.AlignedRead
        cb = read.get_tag("CB")
        if cb in mapping:
            read.set_tag("CB", mapping[cb], value_type="Z")
        outfile.write(read)
    infile.close()
    outfile.close()
    logging.info("Done")

    return