Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def drop_duplicate_positions(self, strand=None, **kwargs):
from pyranges.methods.drop_duplicates import _drop_duplicate_positions
if strand is None:
strand = self.stranded
kwargs["sparse"] = {"self": False}
kwargs = fill_kwargs(kwargs)
kwargs["strand"] = strand and self.stranded
return PyRanges(
pyrange_apply_single(_drop_duplicate_positions, self, strand,
kwargs))
def set_columns(self, value):
assert len(value) == len(
self.columns), "New and old columns must be same length"
def _columns(df):
df.columns = value
return df
return pr.PyRanges(
pyrange_apply_single(
_columns,
self,
strand=None,
kwargs={"sparse": {
"self": False
}}))
def slack(self, slack):
if isinstance(slack, dict):
assert self.stranded, "PyRanges must be stranded to add 5/3-end specific slack."
kwargs = fill_kwargs({"slack": slack})
prg = PyRanges(
pyrange_apply_single(_slack, self, self.stranded, kwargs))
return prg
def tile(self, tile_size, strand=None, **kwargs):
from pyranges.methods.windows import _tiles
if strand is None:
strand = self.stranded
kwargs["sparse"] = {"self": False}
kwargs["tile_size"] = tile_size
df = pyrange_apply_single(_tiles, self, strand, kwargs)
return PyRanges(df)
def five_end(self, slack=0):
assert self.stranded, "Need stranded pyrange to find 5'."
kwargs = fill_kwargs({"slack": slack})
return PyRanges(
pyrange_apply_single(_tss, self, self.stranded, kwargs))
kwargs["sparse"] = {"self": False}
_stranded = self.stranded
if not strand and _stranded:
# print(" WOOO " * 100)
self.Strand2 = self.Strand
self = self.unstrand()
if not by:
from pyranges.methods.cluster import _cluster
df = pyrange_apply_single(_cluster, self, strand, kwargs)
else:
from pyranges.methods.cluster import _cluster_by
kwargs["by"] = by
df = pyrange_apply_single(_cluster_by, self, strand, kwargs)
gr = PyRanges(df)
# each cluster got same ids (0 to len). Need to make unique!
new_dfs = {}
first = True
max_id = 0
for k, v in gr.items():
if first:
max_id = v.Cluster.max()
new_dfs[k] = v
first = False
continue
v.loc[:, "Cluster"] += max_id
max_id = v.Cluster.max()
def assign(self, col, function, strand=None, **kwargs):
if strand is None:
strand = self.stranded
kwargs = fill_kwargs(kwargs)
result = pyrange_apply_single(function, self, strand, kwargs)
first_result = next(iter(result.values()))
assert isinstance(
first_result, pd.Series
), "result of assign function must be Series, but is {}".format(
type(first_result))
# do a deepcopy of object
new_self = pr.PyRanges({k: v.copy() for k, v in self.items()})
new_self.__setattr__(col, result)
return new_self
def merge(self, strand=None, count=False, **kwargs):
if strand is None:
strand = self.stranded
if not ("by" in kwargs):
kwargs["sparse"] = {"self": True}
from pyranges.methods.merge import _merge
df = pyrange_apply_single(_merge, self, strand, kwargs)
else:
kwargs["sparse"] = {"self": False}
from pyranges.methods.merge import _merge_by
df = pyrange_apply_single(_merge_by, self, strand, kwargs)
if not count:
df = {k: v.drop("Count", axis=1) for k, v in df.items()}
return PyRanges(df)
def new_position(self, new_pos, strand=None, **kwargs):
from pyranges.methods.new_position import _new_position
kwargs["sparse"] = {"self": False}
kwargs["new_pos"] = new_pos
kwargs = fill_kwargs(kwargs)
if strand is None:
strand = self.stranded
dfs = pyrange_apply_single(_new_position, self, strand, kwargs)
return pr.PyRanges(dfs)