Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_pysam_version(min_pysam_ver="0.8.4"):
"""Checks that the imported version of pysam is greater than
or equal to provided version. Returns 0 if version is high enough,
raises ImportWarning otherwise."""
import pysam
min_ver = [int(x) for x in min_pysam_ver.split(".")]
pysam_ver = [int(x) for x in pysam.__version__.split(".")]
n_ver = min(len(pysam_ver), len(min_pysam_ver))
for i in range(n_ver):
if pysam_ver[i] < min_ver[i]:
raise ImportWarning("pysam version is %s, but pysam version %s "
"or greater is required" % (pysam.__version__,
min_pysam_ver))
if pysam_ver[i] > min_ver[i]:
# version like 1.0 beats version like 0.8
break
return 0
TODO:
- add tests
- use argparse
- allow user defined tag as well as overwriting of tag
"""
import sys
import os
import pysam
PYSAM_VERSION = [int(x) for x in pysam.__version__.split(".")]
assert PYSAM_VERSION >= [0, 8, 0]
IDENT_TAG = 'Xi'
def calc_ident(r, ref):
"""Computes pairwise identity of read r
"""
if r.is_unmapped:
return -1.0
op_counts = {'indel': 0, 'match': 0, 'mismatch': 0}
for (qpos, rpos) in r.aligned_pairs:
if qpos is None or rpos is None:
op = 'indel'
# Iterating on MPBSs
intFile = open(intFileName,"r")
spr = 0.0
counter = 0.0
for line in intFile:
# Fetching signal
ll = line.strip().split("\t")
mLen = int(ll[2]) - int(ll[1])
mid = (int(ll[1])+int(ll[2]))/2
p1 = max(mid - halfWindow,0)
p2 = mid + halfWindow
# Fetch raw signal
pileup_region = PileupRegion(p1,p2,1)
if(ps_version == "0.7.5"):
bam.fetch(reference=ll[0], start=p1, end=p2, callback = pileup_region)
else:
iter = bam.fetch(reference=ll[0], start=p1, end=p2)
for alignment in iter: pileup_region.__call__(alignment)
raw_signal = array([min(e,initial_clip) for e in pileup_region.vector])
# Std-based clipping
mean = raw_signal.mean()
std = raw_signal.std()
clip_signal = [min(e, mean + (10 * std)) for e in raw_signal]
# Bias Correction
correctedSignal = bias_correction(bam, clip_signal, biasTableF, biasTableR, genomeFileName, ll[0], p1, p2)
# Summing min value to signal
stdzSignal = [e+minValue for e in correctedSignal]
chrom_sizes[chrom] = end
# load the reference chromosomes
genome_chromosomes = set()
for s in FastaReader(genome_file):
if s.name in genome_chromosomes:
raise ValueError("{} is duplicate in {}".format(s.name, genome_file))
genome_chromosomes.add(s.name)
# check that the reference is compliant with the chromosome size file
if genome_chromosomes != set(chrom_sizes):
raise ValueError("reference and chromosome size file do not agree")
# load bam files chromosome information
if pysam.__version__.split(".")[1] > 8:
idxstats_1 = filter(None, pysam.idxstats(bamfile1).split("\n"))
idxstats_2 = filter(None, pysam.idxstats(bamfile2).split("\n"))
else:
idxstats_1 = pysam.idxstats(bamfile1)
idxstats_2 = pysam.idxstats(bamfile2)
chrom_bams_1 = dict(map(lambda x: x.split('\t')[0:2], idxstats_1))
chrom_bams_2 = dict(map(lambda x: x.split('\t')[0:2], idxstats_2))
# now check the chromosomes
for chrom, end in chrom_bams_1.items():
if chrom == "*":
continue
if chrom not in chrom_sizes:
raise ValueError("{} has chromosome {} but it's not in reference".format(bamfile1, chrom))
def __init__(self, file_path, parser=pysam.asVCF()):
self.vcf_file_path = file_path
self.tabix_file = pysam.TabixFile(file_path, parser=parser)
self.sample_names = self.read_sample_names()
self.clens = self.contig_lengths()
self.indexDelta = -1 if tuple(map(int, pysam.__version__.split('.'))) > (0,5,0) else 0
def _write_intro_and_overview(self, output_handle):
output_handle.write(_SECTION_HEADER.format(name="intro", title="Introduction"))
output_handle.write(_INTRODUCTION)
output_handle.write(
_SECTION_HEADER.format(name="overview", title="Analysis overview")
)
revision = self._data.settings["Revision"]
overview = _OVERVIEW_HEADER.format(
DATABASE=revision,
PYSAM=pysam.__version__,
SAMTOOLS=_fmt_v(SAMTOOLS_VERSION),
PLINK=_fmt_v(nuclear.PLINK_VERSION),
RSCRIPT=_fmt_v(RSCRIPT_VERSION),
)
output_handle.write(overview)
if self._has_nuc:
summary = self._report.snp_summary()
output_handle.write(_OVERVIEW_NUCLEAR % summary)
if self._has_mt:
summary = self._report.mito_summary()
output_handle.write(_OVERVIEW_MITOCHONDRIA % summary)
if self._has_nuc:
output_handle.write(_OVERVIEW_NUCLEAR_COVERAGE)
def _get_pysam_wrapper():
pysam_wrappers = [_Pysam14Wrapper(),
_Pysam10_11_12_13Wrapper(),
_Pysam9Wrapper(),
_Pysam8Wrapper()]
for wrapper in pysam_wrappers:
if wrapper.is_compatible_version(pysam.__version__):
return wrapper
msg = 'no wrapper compatible with pysam version {}'.format(pysam.__version__)
raise RuntimeError(msg)
formatter.end_section()
return formatter.format_help()
parser = CustomArgumentParser (
formatter_class=argparse.RawTextHelpFormatter,
description=
"Program: {} ({})\n".format(PROGRAM, DESC)+
"Version: {}\n".format(VERSION, pysam.__version__) +
"Author: Karel Brinda ",
)
parser._optionals.title = 'Basic options'
parser.add_argument('-v', '--version',
action='version',
version='{} {} (using pysam {})'.format(PROGRAM, VERSION, pysam.__version__),
)
parser.add_argument('-i',
type=str,
metavar='FILE',
help="input SAM/BAM file [-]",
dest='in_sam_fn',
default='-',
required=False,
)
parser.add_argument('-o',
type=str,
metavar='FILE',
help="output SAM/BAM file [-]",
dest='out_sam_fn',
formatter.add_text(self.epilog)
# positionals, optionals and user-defined groups
for action_group in self._action_groups:
formatter.start_section(action_group.title)
formatter.add_text(action_group.description)
formatter.add_arguments(action_group._group_actions)
formatter.end_section()
return formatter.format_help()
parser = CustomArgumentParser (
formatter_class=argparse.RawTextHelpFormatter,
description=
"Program: {} ({})\n".format(PROGRAM, DESC)+
"Version: {}\n".format(VERSION, pysam.__version__) +
"Author: Karel Brinda ",
)
parser._optionals.title = 'Basic options'
parser.add_argument('-v', '--version',
action='version',
version='{} {} (using pysam {})'.format(PROGRAM, VERSION, pysam.__version__),
)
parser.add_argument('-i',
type=str,
metavar='FILE',
help="input SAM/BAM file [-]",
dest='in_sam_fn',
default='-',
required=False,