id int32 0 252k | repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1
value | code stringlengths 51 19.8k | code_tokens list | docstring stringlengths 3 17.3k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 87 242 |
|---|---|---|---|---|---|---|---|---|---|---|---|
236,800 | kieferk/dfply | dfply/summary_functions.py | mean | def mean(series):
"""
Returns the mean of a series.
Args:
series (pandas.Series): column to summarize.
"""
if np.issubdtype(series.dtype, np.number):
return series.mean()
else:
return np.nan | python | def mean(series):
if np.issubdtype(series.dtype, np.number):
return series.mean()
else:
return np.nan | [
"def",
"mean",
"(",
"series",
")",
":",
"if",
"np",
".",
"issubdtype",
"(",
"series",
".",
"dtype",
",",
"np",
".",
"number",
")",
":",
"return",
"series",
".",
"mean",
"(",
")",
"else",
":",
"return",
"np",
".",
"nan"
] | Returns the mean of a series.
Args:
series (pandas.Series): column to summarize. | [
"Returns",
"the",
"mean",
"of",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L11-L22 |
236,801 | kieferk/dfply | dfply/summary_functions.py | first | def first(series, order_by=None):
"""
Returns the first value of a series.
Args:
series (pandas.Series): column to summarize.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization.
"""
if order_by is not None:
series = order_series_by(series, order_by)
first_s = series.iloc[0]
return first_s | python | def first(series, order_by=None):
if order_by is not None:
series = order_series_by(series, order_by)
first_s = series.iloc[0]
return first_s | [
"def",
"first",
"(",
"series",
",",
"order_by",
"=",
"None",
")",
":",
"if",
"order_by",
"is",
"not",
"None",
":",
"series",
"=",
"order_series_by",
"(",
"series",
",",
"order_by",
")",
"first_s",
"=",
"series",
".",
"iloc",
"[",
"0",
"]",
"return",
... | Returns the first value of a series.
Args:
series (pandas.Series): column to summarize.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization. | [
"Returns",
"the",
"first",
"value",
"of",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L26-L41 |
236,802 | kieferk/dfply | dfply/summary_functions.py | last | def last(series, order_by=None):
"""
Returns the last value of a series.
Args:
series (pandas.Series): column to summarize.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization.
"""
if order_by is not None:
series = order_series_by(series, order_by)
last_s = series.iloc[series.size - 1]
return last_s | python | def last(series, order_by=None):
if order_by is not None:
series = order_series_by(series, order_by)
last_s = series.iloc[series.size - 1]
return last_s | [
"def",
"last",
"(",
"series",
",",
"order_by",
"=",
"None",
")",
":",
"if",
"order_by",
"is",
"not",
"None",
":",
"series",
"=",
"order_series_by",
"(",
"series",
",",
"order_by",
")",
"last_s",
"=",
"series",
".",
"iloc",
"[",
"series",
".",
"size",
... | Returns the last value of a series.
Args:
series (pandas.Series): column to summarize.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization. | [
"Returns",
"the",
"last",
"value",
"of",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L45-L60 |
236,803 | kieferk/dfply | dfply/summary_functions.py | nth | def nth(series, n, order_by=None):
"""
Returns the nth value of a series.
Args:
series (pandas.Series): column to summarize.
n (integer): position of desired value. Returns `NaN` if out of range.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization.
"""
if order_by is not None:
series = order_series_by(series, order_by)
try:
return series.iloc[n]
except:
return np.nan | python | def nth(series, n, order_by=None):
if order_by is not None:
series = order_series_by(series, order_by)
try:
return series.iloc[n]
except:
return np.nan | [
"def",
"nth",
"(",
"series",
",",
"n",
",",
"order_by",
"=",
"None",
")",
":",
"if",
"order_by",
"is",
"not",
"None",
":",
"series",
"=",
"order_series_by",
"(",
"series",
",",
"order_by",
")",
"try",
":",
"return",
"series",
".",
"iloc",
"[",
"n",
... | Returns the nth value of a series.
Args:
series (pandas.Series): column to summarize.
n (integer): position of desired value. Returns `NaN` if out of range.
Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization. | [
"Returns",
"the",
"nth",
"value",
"of",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L64-L82 |
236,804 | kieferk/dfply | dfply/summary_functions.py | median | def median(series):
"""
Returns the median value of a series.
Args:
series (pandas.Series): column to summarize.
"""
if np.issubdtype(series.dtype, np.number):
return series.median()
else:
return np.nan | python | def median(series):
if np.issubdtype(series.dtype, np.number):
return series.median()
else:
return np.nan | [
"def",
"median",
"(",
"series",
")",
":",
"if",
"np",
".",
"issubdtype",
"(",
"series",
".",
"dtype",
",",
"np",
".",
"number",
")",
":",
"return",
"series",
".",
"median",
"(",
")",
"else",
":",
"return",
"np",
".",
"nan"
] | Returns the median value of a series.
Args:
series (pandas.Series): column to summarize. | [
"Returns",
"the",
"median",
"value",
"of",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L153-L164 |
236,805 | kieferk/dfply | dfply/summary_functions.py | var | def var(series):
"""
Returns the variance of values in a series.
Args:
series (pandas.Series): column to summarize.
"""
if np.issubdtype(series.dtype, np.number):
return series.var()
else:
return np.nan | python | def var(series):
if np.issubdtype(series.dtype, np.number):
return series.var()
else:
return np.nan | [
"def",
"var",
"(",
"series",
")",
":",
"if",
"np",
".",
"issubdtype",
"(",
"series",
".",
"dtype",
",",
"np",
".",
"number",
")",
":",
"return",
"series",
".",
"var",
"(",
")",
"else",
":",
"return",
"np",
".",
"nan"
] | Returns the variance of values in a series.
Args:
series (pandas.Series): column to summarize. | [
"Returns",
"the",
"variance",
"of",
"values",
"in",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L168-L178 |
236,806 | kieferk/dfply | dfply/summary_functions.py | sd | def sd(series):
"""
Returns the standard deviation of values in a series.
Args:
series (pandas.Series): column to summarize.
"""
if np.issubdtype(series.dtype, np.number):
return series.std()
else:
return np.nan | python | def sd(series):
if np.issubdtype(series.dtype, np.number):
return series.std()
else:
return np.nan | [
"def",
"sd",
"(",
"series",
")",
":",
"if",
"np",
".",
"issubdtype",
"(",
"series",
".",
"dtype",
",",
"np",
".",
"number",
")",
":",
"return",
"series",
".",
"std",
"(",
")",
"else",
":",
"return",
"np",
".",
"nan"
] | Returns the standard deviation of values in a series.
Args:
series (pandas.Series): column to summarize. | [
"Returns",
"the",
"standard",
"deviation",
"of",
"values",
"in",
"a",
"series",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/summary_functions.py#L182-L193 |
236,807 | kieferk/dfply | dfply/join.py | get_join_parameters | def get_join_parameters(join_kwargs):
"""
Convenience function to determine the columns to join the right and
left DataFrames on, as well as any suffixes for the columns.
"""
by = join_kwargs.get('by', None)
suffixes = join_kwargs.get('suffixes', ('_x', '_y'))
if isinstance(by, tuple):
left_on, right_on = by
elif isinstance(by, list):
by = [x if isinstance(x, tuple) else (x, x) for x in by]
left_on, right_on = (list(x) for x in zip(*by))
else:
left_on, right_on = by, by
return left_on, right_on, suffixes | python | def get_join_parameters(join_kwargs):
by = join_kwargs.get('by', None)
suffixes = join_kwargs.get('suffixes', ('_x', '_y'))
if isinstance(by, tuple):
left_on, right_on = by
elif isinstance(by, list):
by = [x if isinstance(x, tuple) else (x, x) for x in by]
left_on, right_on = (list(x) for x in zip(*by))
else:
left_on, right_on = by, by
return left_on, right_on, suffixes | [
"def",
"get_join_parameters",
"(",
"join_kwargs",
")",
":",
"by",
"=",
"join_kwargs",
".",
"get",
"(",
"'by'",
",",
"None",
")",
"suffixes",
"=",
"join_kwargs",
".",
"get",
"(",
"'suffixes'",
",",
"(",
"'_x'",
",",
"'_y'",
")",
")",
"if",
"isinstance",
... | Convenience function to determine the columns to join the right and
left DataFrames on, as well as any suffixes for the columns. | [
"Convenience",
"function",
"to",
"determine",
"the",
"columns",
"to",
"join",
"the",
"right",
"and",
"left",
"DataFrames",
"on",
"as",
"well",
"as",
"any",
"suffixes",
"for",
"the",
"columns",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/join.py#L8-L23 |
236,808 | kieferk/dfply | dfply/join.py | inner_join | def inner_join(df, other, **kwargs):
"""
Joins on values present in both DataFrames.
Args:
df (pandas.DataFrame): Left DataFrame (passed in via pipe)
other (pandas.DataFrame): Right DataFrame
Kwargs:
by (str or list): Columns to join on. If a single string, will join
on that column. If a list of lists which contain strings or
integers, the right/left columns to join on.
suffixes (list): String suffixes to append to column names in left
and right DataFrames.
Example:
a >> inner_join(b, by='x1')
x1 x2 x3
0 A 1 True
1 B 2 False
"""
left_on, right_on, suffixes = get_join_parameters(kwargs)
joined = df.merge(other, how='inner', left_on=left_on,
right_on=right_on, suffixes=suffixes)
return joined | python | def inner_join(df, other, **kwargs):
left_on, right_on, suffixes = get_join_parameters(kwargs)
joined = df.merge(other, how='inner', left_on=left_on,
right_on=right_on, suffixes=suffixes)
return joined | [
"def",
"inner_join",
"(",
"df",
",",
"other",
",",
"*",
"*",
"kwargs",
")",
":",
"left_on",
",",
"right_on",
",",
"suffixes",
"=",
"get_join_parameters",
"(",
"kwargs",
")",
"joined",
"=",
"df",
".",
"merge",
"(",
"other",
",",
"how",
"=",
"'inner'",
... | Joins on values present in both DataFrames.
Args:
df (pandas.DataFrame): Left DataFrame (passed in via pipe)
other (pandas.DataFrame): Right DataFrame
Kwargs:
by (str or list): Columns to join on. If a single string, will join
on that column. If a list of lists which contain strings or
integers, the right/left columns to join on.
suffixes (list): String suffixes to append to column names in left
and right DataFrames.
Example:
a >> inner_join(b, by='x1')
x1 x2 x3
0 A 1 True
1 B 2 False | [
"Joins",
"on",
"values",
"present",
"in",
"both",
"DataFrames",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/join.py#L27-L53 |
236,809 | kieferk/dfply | dfply/join.py | anti_join | def anti_join(df, other, **kwargs):
"""
Returns all of the rows in the left DataFrame that do not have a
match in the right DataFrame.
Args:
df (pandas.DataFrame): Left DataFrame (passed in via pipe)
other (pandas.DataFrame): Right DataFrame
Kwargs:
by (str or list): Columns to join on. If a single string, will join
on that column. If a list of lists which contain strings or
integers, the right/left columns to join on.
Example:
a >> anti_join(b, by='x1')
x1 x2
2 C 3
"""
left_on, right_on, suffixes = get_join_parameters(kwargs)
if not right_on:
right_on = [col_name for col_name in df.columns.values.tolist() if col_name in other.columns.values.tolist()]
left_on = right_on
elif not isinstance(right_on, (list, tuple)):
right_on = [right_on]
other_reduced = other[right_on].drop_duplicates()
joined = df.merge(other_reduced, how='left', left_on=left_on,
right_on=right_on, suffixes=('', '_y'),
indicator=True).query('_merge=="left_only"')[df.columns.values.tolist()]
return joined | python | def anti_join(df, other, **kwargs):
left_on, right_on, suffixes = get_join_parameters(kwargs)
if not right_on:
right_on = [col_name for col_name in df.columns.values.tolist() if col_name in other.columns.values.tolist()]
left_on = right_on
elif not isinstance(right_on, (list, tuple)):
right_on = [right_on]
other_reduced = other[right_on].drop_duplicates()
joined = df.merge(other_reduced, how='left', left_on=left_on,
right_on=right_on, suffixes=('', '_y'),
indicator=True).query('_merge=="left_only"')[df.columns.values.tolist()]
return joined | [
"def",
"anti_join",
"(",
"df",
",",
"other",
",",
"*",
"*",
"kwargs",
")",
":",
"left_on",
",",
"right_on",
",",
"suffixes",
"=",
"get_join_parameters",
"(",
"kwargs",
")",
"if",
"not",
"right_on",
":",
"right_on",
"=",
"[",
"col_name",
"for",
"col_name"... | Returns all of the rows in the left DataFrame that do not have a
match in the right DataFrame.
Args:
df (pandas.DataFrame): Left DataFrame (passed in via pipe)
other (pandas.DataFrame): Right DataFrame
Kwargs:
by (str or list): Columns to join on. If a single string, will join
on that column. If a list of lists which contain strings or
integers, the right/left columns to join on.
Example:
a >> anti_join(b, by='x1')
x1 x2
2 C 3 | [
"Returns",
"all",
"of",
"the",
"rows",
"in",
"the",
"left",
"DataFrame",
"that",
"do",
"not",
"have",
"a",
"match",
"in",
"the",
"right",
"DataFrame",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/join.py#L219-L250 |
236,810 | kieferk/dfply | dfply/join.py | bind_rows | def bind_rows(df, other, join='outer', ignore_index=False):
"""
Binds DataFrames "vertically", stacking them together. This is equivalent
to `pd.concat` with `axis=0`.
Args:
df (pandas.DataFrame): Top DataFrame (passed in via pipe).
other (pandas.DataFrame): Bottom DataFrame.
Kwargs:
join (str): One of `"outer"` or `"inner"`. Outer join will preserve
columns not present in both DataFrames, whereas inner joining will
drop them.
ignore_index (bool): Indicates whether to consider pandas indices as
part of the concatenation (defaults to `False`).
"""
df = pd.concat([df, other], join=join, ignore_index=ignore_index, axis=0)
return df | python | def bind_rows(df, other, join='outer', ignore_index=False):
df = pd.concat([df, other], join=join, ignore_index=ignore_index, axis=0)
return df | [
"def",
"bind_rows",
"(",
"df",
",",
"other",
",",
"join",
"=",
"'outer'",
",",
"ignore_index",
"=",
"False",
")",
":",
"df",
"=",
"pd",
".",
"concat",
"(",
"[",
"df",
",",
"other",
"]",
",",
"join",
"=",
"join",
",",
"ignore_index",
"=",
"ignore_in... | Binds DataFrames "vertically", stacking them together. This is equivalent
to `pd.concat` with `axis=0`.
Args:
df (pandas.DataFrame): Top DataFrame (passed in via pipe).
other (pandas.DataFrame): Bottom DataFrame.
Kwargs:
join (str): One of `"outer"` or `"inner"`. Outer join will preserve
columns not present in both DataFrames, whereas inner joining will
drop them.
ignore_index (bool): Indicates whether to consider pandas indices as
part of the concatenation (defaults to `False`). | [
"Binds",
"DataFrames",
"vertically",
"stacking",
"them",
"together",
".",
"This",
"is",
"equivalent",
"to",
"pd",
".",
"concat",
"with",
"axis",
"=",
"0",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/join.py#L258-L276 |
236,811 | kieferk/dfply | dfply/reshape.py | arrange | def arrange(df, *args, **kwargs):
"""Calls `pandas.DataFrame.sort_values` to sort a DataFrame according to
criteria.
See:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html
For a list of specific keyword arguments for sort_values (which will be
the same in arrange).
Args:
*args: Symbolic, string, integer or lists of those types indicating
columns to sort the DataFrame by.
Kwargs:
**kwargs: Any keyword arguments will be passed through to the pandas
`DataFrame.sort_values` function.
"""
flat_args = [a for a in flatten(args)]
series = [df[arg] if isinstance(arg, str) else
df.iloc[:, arg] if isinstance(arg, int) else
pd.Series(arg) for arg in flat_args]
sorter = pd.concat(series, axis=1).reset_index(drop=True)
sorter = sorter.sort_values(sorter.columns.tolist(), **kwargs)
return df.iloc[sorter.index, :] | python | def arrange(df, *args, **kwargs):
flat_args = [a for a in flatten(args)]
series = [df[arg] if isinstance(arg, str) else
df.iloc[:, arg] if isinstance(arg, int) else
pd.Series(arg) for arg in flat_args]
sorter = pd.concat(series, axis=1).reset_index(drop=True)
sorter = sorter.sort_values(sorter.columns.tolist(), **kwargs)
return df.iloc[sorter.index, :] | [
"def",
"arrange",
"(",
"df",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"flat_args",
"=",
"[",
"a",
"for",
"a",
"in",
"flatten",
"(",
"args",
")",
"]",
"series",
"=",
"[",
"df",
"[",
"arg",
"]",
"if",
"isinstance",
"(",
"arg",
",",
... | Calls `pandas.DataFrame.sort_values` to sort a DataFrame according to
criteria.
See:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html
For a list of specific keyword arguments for sort_values (which will be
the same in arrange).
Args:
*args: Symbolic, string, integer or lists of those types indicating
columns to sort the DataFrame by.
Kwargs:
**kwargs: Any keyword arguments will be passed through to the pandas
`DataFrame.sort_values` function. | [
"Calls",
"pandas",
".",
"DataFrame",
".",
"sort_values",
"to",
"sort",
"a",
"DataFrame",
"according",
"to",
"criteria",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L10-L37 |
236,812 | kieferk/dfply | dfply/reshape.py | rename | def rename(df, **kwargs):
"""Renames columns, where keyword argument values are the current names
of columns and keys are the new names.
Args:
df (:obj:`pandas.DataFrame`): DataFrame passed in via `>>` pipe.
Kwargs:
**kwargs: key:value pairs where keys are new names for columns and
values are current names of columns.
"""
return df.rename(columns={v: k for k, v in kwargs.items()}) | python | def rename(df, **kwargs):
return df.rename(columns={v: k for k, v in kwargs.items()}) | [
"def",
"rename",
"(",
"df",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"df",
".",
"rename",
"(",
"columns",
"=",
"{",
"v",
":",
"k",
"for",
"k",
",",
"v",
"in",
"kwargs",
".",
"items",
"(",
")",
"}",
")"
] | Renames columns, where keyword argument values are the current names
of columns and keys are the new names.
Args:
df (:obj:`pandas.DataFrame`): DataFrame passed in via `>>` pipe.
Kwargs:
**kwargs: key:value pairs where keys are new names for columns and
values are current names of columns. | [
"Renames",
"columns",
"where",
"keyword",
"argument",
"values",
"are",
"the",
"current",
"names",
"of",
"columns",
"and",
"keys",
"are",
"the",
"new",
"names",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L46-L58 |
236,813 | kieferk/dfply | dfply/reshape.py | convert_type | def convert_type(df, columns):
"""
Helper function that attempts to convert columns into their appropriate
data type.
"""
# taken in part from the dplython package
out_df = df.copy()
for col in columns:
column_values = pd.Series(out_df[col].unique())
column_values = column_values[~column_values.isnull()]
# empty
if len(column_values) == 0:
continue
# boolean
if set(column_values.values) < {'True', 'False'}:
out_df[col] = out_df[col].map({'True': True, 'False': False})
continue
# numeric
if pd.to_numeric(column_values, errors='coerce').isnull().sum() == 0:
out_df[col] = pd.to_numeric(out_df[col], errors='ignore')
continue
# datetime
if pd.to_datetime(column_values, errors='coerce').isnull().sum() == 0:
out_df[col] = pd.to_datetime(out_df[col], errors='ignore',
infer_datetime_format=True)
continue
return out_df | python | def convert_type(df, columns):
# taken in part from the dplython package
out_df = df.copy()
for col in columns:
column_values = pd.Series(out_df[col].unique())
column_values = column_values[~column_values.isnull()]
# empty
if len(column_values) == 0:
continue
# boolean
if set(column_values.values) < {'True', 'False'}:
out_df[col] = out_df[col].map({'True': True, 'False': False})
continue
# numeric
if pd.to_numeric(column_values, errors='coerce').isnull().sum() == 0:
out_df[col] = pd.to_numeric(out_df[col], errors='ignore')
continue
# datetime
if pd.to_datetime(column_values, errors='coerce').isnull().sum() == 0:
out_df[col] = pd.to_datetime(out_df[col], errors='ignore',
infer_datetime_format=True)
continue
return out_df | [
"def",
"convert_type",
"(",
"df",
",",
"columns",
")",
":",
"# taken in part from the dplython package",
"out_df",
"=",
"df",
".",
"copy",
"(",
")",
"for",
"col",
"in",
"columns",
":",
"column_values",
"=",
"pd",
".",
"Series",
"(",
"out_df",
"[",
"col",
"... | Helper function that attempts to convert columns into their appropriate
data type. | [
"Helper",
"function",
"that",
"attempts",
"to",
"convert",
"columns",
"into",
"their",
"appropriate",
"data",
"type",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L111-L138 |
236,814 | kieferk/dfply | dfply/reshape.py | spread | def spread(df, key, values, convert=False):
"""
Transforms a "long" DataFrame into a "wide" format using a key and value
column.
If you have a mixed datatype column in your long-format DataFrame then the
default behavior is for the spread columns to be of type `object`, or
string. If you want to try to convert dtypes when spreading, you can set
the convert keyword argument in spread to True.
Args:
key (str, int, or symbolic): Label for the key column.
values (str, int, or symbolic): Label for the values column.
Kwargs:
convert (bool): Boolean indicating whether or not to try and convert
the spread columns to more appropriate data types.
Example:
widened = elongated >> spread(X.variable, X.value)
widened >> head(5)
_ID carat clarity color cut depth price table x y z
0 0 0.23 SI2 E Ideal 61.5 326 55 3.95 3.98 2.43
1 1 0.21 SI1 E Premium 59.8 326 61 3.89 3.84 2.31
2 10 0.3 SI1 J Good 64 339 55 4.25 4.28 2.73
3 100 0.75 SI1 D Very Good 63.2 2760 56 5.8 5.75 3.65
4 1000 0.75 SI1 D Ideal 62.3 2898 55 5.83 5.8 3.62
"""
# Taken mostly from dplython package
columns = df.columns.tolist()
id_cols = [col for col in columns if not col in [key, values]]
temp_index = ['' for i in range(len(df))]
for id_col in id_cols:
temp_index += df[id_col].map(str)
out_df = df.assign(temp_index=temp_index)
out_df = out_df.set_index('temp_index')
spread_data = out_df[[key, values]]
if not all(spread_data.groupby([spread_data.index, key]).agg(
'count').reset_index()[values] < 2):
raise ValueError('Duplicate identifiers')
spread_data = spread_data.pivot(columns=key, values=values)
if convert and (out_df[values].dtype.kind in 'OSaU'):
columns_to_convert = [col for col in spread_data if col not in columns]
spread_data = convert_type(spread_data, columns_to_convert)
out_df = out_df[id_cols].drop_duplicates()
out_df = out_df.merge(spread_data, left_index=True, right_index=True).reset_index(drop=True)
out_df = (out_df >> arrange(id_cols)).reset_index(drop=True)
return out_df | python | def spread(df, key, values, convert=False):
# Taken mostly from dplython package
columns = df.columns.tolist()
id_cols = [col for col in columns if not col in [key, values]]
temp_index = ['' for i in range(len(df))]
for id_col in id_cols:
temp_index += df[id_col].map(str)
out_df = df.assign(temp_index=temp_index)
out_df = out_df.set_index('temp_index')
spread_data = out_df[[key, values]]
if not all(spread_data.groupby([spread_data.index, key]).agg(
'count').reset_index()[values] < 2):
raise ValueError('Duplicate identifiers')
spread_data = spread_data.pivot(columns=key, values=values)
if convert and (out_df[values].dtype.kind in 'OSaU'):
columns_to_convert = [col for col in spread_data if col not in columns]
spread_data = convert_type(spread_data, columns_to_convert)
out_df = out_df[id_cols].drop_duplicates()
out_df = out_df.merge(spread_data, left_index=True, right_index=True).reset_index(drop=True)
out_df = (out_df >> arrange(id_cols)).reset_index(drop=True)
return out_df | [
"def",
"spread",
"(",
"df",
",",
"key",
",",
"values",
",",
"convert",
"=",
"False",
")",
":",
"# Taken mostly from dplython package",
"columns",
"=",
"df",
".",
"columns",
".",
"tolist",
"(",
")",
"id_cols",
"=",
"[",
"col",
"for",
"col",
"in",
"columns... | Transforms a "long" DataFrame into a "wide" format using a key and value
column.
If you have a mixed datatype column in your long-format DataFrame then the
default behavior is for the spread columns to be of type `object`, or
string. If you want to try to convert dtypes when spreading, you can set
the convert keyword argument in spread to True.
Args:
key (str, int, or symbolic): Label for the key column.
values (str, int, or symbolic): Label for the values column.
Kwargs:
convert (bool): Boolean indicating whether or not to try and convert
the spread columns to more appropriate data types.
Example:
widened = elongated >> spread(X.variable, X.value)
widened >> head(5)
_ID carat clarity color cut depth price table x y z
0 0 0.23 SI2 E Ideal 61.5 326 55 3.95 3.98 2.43
1 1 0.21 SI1 E Premium 59.8 326 61 3.89 3.84 2.31
2 10 0.3 SI1 J Good 64 339 55 4.25 4.28 2.73
3 100 0.75 SI1 D Very Good 63.2 2760 56 5.8 5.75 3.65
4 1000 0.75 SI1 D Ideal 62.3 2898 55 5.83 5.8 3.62 | [
"Transforms",
"a",
"long",
"DataFrame",
"into",
"a",
"wide",
"format",
"using",
"a",
"key",
"and",
"value",
"column",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L143-L201 |
236,815 | kieferk/dfply | dfply/reshape.py | separate | def separate(df, column, into, sep="[\W_]+", remove=True, convert=False,
extra='drop', fill='right'):
"""
Splits columns into multiple columns.
Args:
df (pandas.DataFrame): DataFrame passed in through the pipe.
column (str, symbolic): Label of column to split.
into (list): List of string names for new columns.
Kwargs:
sep (str or list): If a string, the regex string used to split the
column. If a list, a list of integer positions to split strings
on.
remove (bool): Boolean indicating whether to remove the original column.
convert (bool): Boolean indicating whether the new columns should be
converted to the appropriate type.
extra (str): either `'drop'`, where split pieces beyond the specified
new columns are dropped, or `'merge'`, where the final split piece
contains the remainder of the original column.
fill (str): either `'right'`, where `np.nan` values are filled in the
right-most columns for missing pieces, or `'left'` where `np.nan`
values are filled in the left-most columns.
"""
assert isinstance(into, (tuple, list))
if isinstance(sep, (tuple, list)):
inds = [0] + list(sep)
if len(inds) > len(into):
if extra == 'drop':
inds = inds[:len(into) + 1]
elif extra == 'merge':
inds = inds[:len(into)] + [None]
else:
inds = inds + [None]
splits = df[column].map(lambda x: [str(x)[slice(inds[i], inds[i + 1])]
if i < len(inds) - 1 else np.nan
for i in range(len(into))])
else:
maxsplit = len(into) - 1 if extra == 'merge' else 0
splits = df[column].map(lambda x: re.split(sep, x, maxsplit))
right_filler = lambda x: x + [np.nan for i in range(len(into) - len(x))]
left_filler = lambda x: [np.nan for i in range(len(into) - len(x))] + x
if fill == 'right':
splits = [right_filler(x) for x in splits]
elif fill == 'left':
splits = [left_filler(x) for x in splits]
for i, split_col in enumerate(into):
df[split_col] = [x[i] if not x[i] == '' else np.nan for x in splits]
if convert:
df = convert_type(df, into)
if remove:
df.drop(column, axis=1, inplace=True)
return df | python | def separate(df, column, into, sep="[\W_]+", remove=True, convert=False,
extra='drop', fill='right'):
assert isinstance(into, (tuple, list))
if isinstance(sep, (tuple, list)):
inds = [0] + list(sep)
if len(inds) > len(into):
if extra == 'drop':
inds = inds[:len(into) + 1]
elif extra == 'merge':
inds = inds[:len(into)] + [None]
else:
inds = inds + [None]
splits = df[column].map(lambda x: [str(x)[slice(inds[i], inds[i + 1])]
if i < len(inds) - 1 else np.nan
for i in range(len(into))])
else:
maxsplit = len(into) - 1 if extra == 'merge' else 0
splits = df[column].map(lambda x: re.split(sep, x, maxsplit))
right_filler = lambda x: x + [np.nan for i in range(len(into) - len(x))]
left_filler = lambda x: [np.nan for i in range(len(into) - len(x))] + x
if fill == 'right':
splits = [right_filler(x) for x in splits]
elif fill == 'left':
splits = [left_filler(x) for x in splits]
for i, split_col in enumerate(into):
df[split_col] = [x[i] if not x[i] == '' else np.nan for x in splits]
if convert:
df = convert_type(df, into)
if remove:
df.drop(column, axis=1, inplace=True)
return df | [
"def",
"separate",
"(",
"df",
",",
"column",
",",
"into",
",",
"sep",
"=",
"\"[\\W_]+\"",
",",
"remove",
"=",
"True",
",",
"convert",
"=",
"False",
",",
"extra",
"=",
"'drop'",
",",
"fill",
"=",
"'right'",
")",
":",
"assert",
"isinstance",
"(",
"into... | Splits columns into multiple columns.
Args:
df (pandas.DataFrame): DataFrame passed in through the pipe.
column (str, symbolic): Label of column to split.
into (list): List of string names for new columns.
Kwargs:
sep (str or list): If a string, the regex string used to split the
column. If a list, a list of integer positions to split strings
on.
remove (bool): Boolean indicating whether to remove the original column.
convert (bool): Boolean indicating whether the new columns should be
converted to the appropriate type.
extra (str): either `'drop'`, where split pieces beyond the specified
new columns are dropped, or `'merge'`, where the final split piece
contains the remainder of the original column.
fill (str): either `'right'`, where `np.nan` values are filled in the
right-most columns for missing pieces, or `'left'` where `np.nan`
values are filled in the left-most columns. | [
"Splits",
"columns",
"into",
"multiple",
"columns",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L210-L272 |
236,816 | kieferk/dfply | dfply/reshape.py | unite | def unite(df, colname, *args, **kwargs):
"""
Does the inverse of `separate`, joining columns together by a specified
separator.
Any columns that are not strings will be converted to strings.
Args:
df (pandas.DataFrame): DataFrame passed in through the pipe.
colname (str): the name of the new joined column.
*args: list of columns to be joined, which can be strings, symbolic, or
integer positions.
Kwargs:
sep (str): the string separator to join the columns with.
remove (bool): Boolean indicating whether or not to remove the
original columns.
na_action (str): can be one of `'maintain'` (the default),
'`ignore'`, or `'as_string'`. The default will make the new column
row a `NaN` value if any of the original column cells at that
row contained `NaN`. '`ignore'` will treat any `NaN` value as an
empty string during joining. `'as_string'` will convert any `NaN`
value to the string `'nan'` prior to joining.
"""
to_unite = list([a for a in flatten(args)])
sep = kwargs.get('sep', '_')
remove = kwargs.get('remove', True)
# possible na_action values
# ignore: empty string
# maintain: keep as np.nan (default)
# as_string: becomes string 'nan'
na_action = kwargs.get('na_action', 'maintain')
# print(to_unite, sep, remove, na_action)
if na_action == 'maintain':
df[colname] = df[to_unite].apply(lambda x: np.nan if any(x.isnull())
else sep.join(x.map(str)), axis=1)
elif na_action == 'ignore':
df[colname] = df[to_unite].apply(lambda x: sep.join(x[~x.isnull()].map(str)),
axis=1)
elif na_action == 'as_string':
df[colname] = df[to_unite].astype(str).apply(lambda x: sep.join(x), axis=1)
if remove:
df.drop(to_unite, axis=1, inplace=True)
return df | python | def unite(df, colname, *args, **kwargs):
to_unite = list([a for a in flatten(args)])
sep = kwargs.get('sep', '_')
remove = kwargs.get('remove', True)
# possible na_action values
# ignore: empty string
# maintain: keep as np.nan (default)
# as_string: becomes string 'nan'
na_action = kwargs.get('na_action', 'maintain')
# print(to_unite, sep, remove, na_action)
if na_action == 'maintain':
df[colname] = df[to_unite].apply(lambda x: np.nan if any(x.isnull())
else sep.join(x.map(str)), axis=1)
elif na_action == 'ignore':
df[colname] = df[to_unite].apply(lambda x: sep.join(x[~x.isnull()].map(str)),
axis=1)
elif na_action == 'as_string':
df[colname] = df[to_unite].astype(str).apply(lambda x: sep.join(x), axis=1)
if remove:
df.drop(to_unite, axis=1, inplace=True)
return df | [
"def",
"unite",
"(",
"df",
",",
"colname",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"to_unite",
"=",
"list",
"(",
"[",
"a",
"for",
"a",
"in",
"flatten",
"(",
"args",
")",
"]",
")",
"sep",
"=",
"kwargs",
".",
"get",
"(",
"'sep'",
"... | Does the inverse of `separate`, joining columns together by a specified
separator.
Any columns that are not strings will be converted to strings.
Args:
df (pandas.DataFrame): DataFrame passed in through the pipe.
colname (str): the name of the new joined column.
*args: list of columns to be joined, which can be strings, symbolic, or
integer positions.
Kwargs:
sep (str): the string separator to join the columns with.
remove (bool): Boolean indicating whether or not to remove the
original columns.
na_action (str): can be one of `'maintain'` (the default),
'`ignore'`, or `'as_string'`. The default will make the new column
row a `NaN` value if any of the original column cells at that
row contained `NaN`. '`ignore'` will treat any `NaN` value as an
empty string during joining. `'as_string'` will convert any `NaN`
value to the string `'nan'` prior to joining. | [
"Does",
"the",
"inverse",
"of",
"separate",
"joining",
"columns",
"together",
"by",
"a",
"specified",
"separator",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/reshape.py#L281-L329 |
236,817 | kieferk/dfply | dfply/set_ops.py | validate_set_ops | def validate_set_ops(df, other):
"""
Helper function to ensure that DataFrames are valid for set operations.
Columns must be the same name in the same order, and indices must be of the
same dimension with the same names.
"""
if df.columns.values.tolist() != other.columns.values.tolist():
not_in_df = [col for col in other.columns if col not in df.columns]
not_in_other = [col for col in df.columns if col not in other.columns]
error_string = 'Error: not compatible.'
if len(not_in_df):
error_string += ' Cols in y but not x: ' + str(not_in_df) + '.'
if len(not_in_other):
error_string += ' Cols in x but not y: ' + str(not_in_other) + '.'
raise ValueError(error_string)
if len(df.index.names) != len(other.index.names):
raise ValueError('Index dimension mismatch')
if df.index.names != other.index.names:
raise ValueError('Index mismatch')
else:
return | python | def validate_set_ops(df, other):
if df.columns.values.tolist() != other.columns.values.tolist():
not_in_df = [col for col in other.columns if col not in df.columns]
not_in_other = [col for col in df.columns if col not in other.columns]
error_string = 'Error: not compatible.'
if len(not_in_df):
error_string += ' Cols in y but not x: ' + str(not_in_df) + '.'
if len(not_in_other):
error_string += ' Cols in x but not y: ' + str(not_in_other) + '.'
raise ValueError(error_string)
if len(df.index.names) != len(other.index.names):
raise ValueError('Index dimension mismatch')
if df.index.names != other.index.names:
raise ValueError('Index mismatch')
else:
return | [
"def",
"validate_set_ops",
"(",
"df",
",",
"other",
")",
":",
"if",
"df",
".",
"columns",
".",
"values",
".",
"tolist",
"(",
")",
"!=",
"other",
".",
"columns",
".",
"values",
".",
"tolist",
"(",
")",
":",
"not_in_df",
"=",
"[",
"col",
"for",
"col"... | Helper function to ensure that DataFrames are valid for set operations.
Columns must be the same name in the same order, and indices must be of the
same dimension with the same names. | [
"Helper",
"function",
"to",
"ensure",
"that",
"DataFrames",
"are",
"valid",
"for",
"set",
"operations",
".",
"Columns",
"must",
"be",
"the",
"same",
"name",
"in",
"the",
"same",
"order",
"and",
"indices",
"must",
"be",
"of",
"the",
"same",
"dimension",
"wi... | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/set_ops.py#L6-L27 |
236,818 | kieferk/dfply | dfply/set_ops.py | union | def union(df, other, index=False, keep='first'):
"""
Returns rows that appear in either DataFrame.
Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.
Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`.
"""
validate_set_ops(df, other)
stacked = df.append(other)
if index:
stacked_reset_indexes = stacked.reset_index()
index_cols = [col for col in stacked_reset_indexes.columns if col not in df.columns]
index_name = df.index.names
return_df = stacked_reset_indexes.drop_duplicates(keep=keep).set_index(index_cols)
return_df.index.names = index_name
return return_df
else:
return stacked.drop_duplicates(keep=keep) | python | def union(df, other, index=False, keep='first'):
validate_set_ops(df, other)
stacked = df.append(other)
if index:
stacked_reset_indexes = stacked.reset_index()
index_cols = [col for col in stacked_reset_indexes.columns if col not in df.columns]
index_name = df.index.names
return_df = stacked_reset_indexes.drop_duplicates(keep=keep).set_index(index_cols)
return_df.index.names = index_name
return return_df
else:
return stacked.drop_duplicates(keep=keep) | [
"def",
"union",
"(",
"df",
",",
"other",
",",
"index",
"=",
"False",
",",
"keep",
"=",
"'first'",
")",
":",
"validate_set_ops",
"(",
"df",
",",
"other",
")",
"stacked",
"=",
"df",
".",
"append",
"(",
"other",
")",
"if",
"index",
":",
"stacked_reset_i... | Returns rows that appear in either DataFrame.
Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.
Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`. | [
"Returns",
"rows",
"that",
"appear",
"in",
"either",
"DataFrame",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/set_ops.py#L35-L60 |
236,819 | kieferk/dfply | dfply/set_ops.py | intersect | def intersect(df, other, index=False, keep='first'):
"""
Returns rows that appear in both DataFrames.
Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.
Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`.
"""
validate_set_ops(df, other)
if index:
df_reset_index = df.reset_index()
other_reset_index = other.reset_index()
index_cols = [col for col in df_reset_index.columns if col not in df.columns]
df_index_names = df.index.names
return_df = (pd.merge(df_reset_index, other_reset_index,
how='inner',
left_on=df_reset_index.columns.values.tolist(),
right_on=df_reset_index.columns.values.tolist())
.set_index(index_cols))
return_df.index.names = df_index_names
return_df = return_df.drop_duplicates(keep=keep)
return return_df
else:
return_df = pd.merge(df, other,
how='inner',
left_on=df.columns.values.tolist(),
right_on=df.columns.values.tolist())
return_df = return_df.drop_duplicates(keep=keep)
return return_df | python | def intersect(df, other, index=False, keep='first'):
validate_set_ops(df, other)
if index:
df_reset_index = df.reset_index()
other_reset_index = other.reset_index()
index_cols = [col for col in df_reset_index.columns if col not in df.columns]
df_index_names = df.index.names
return_df = (pd.merge(df_reset_index, other_reset_index,
how='inner',
left_on=df_reset_index.columns.values.tolist(),
right_on=df_reset_index.columns.values.tolist())
.set_index(index_cols))
return_df.index.names = df_index_names
return_df = return_df.drop_duplicates(keep=keep)
return return_df
else:
return_df = pd.merge(df, other,
how='inner',
left_on=df.columns.values.tolist(),
right_on=df.columns.values.tolist())
return_df = return_df.drop_duplicates(keep=keep)
return return_df | [
"def",
"intersect",
"(",
"df",
",",
"other",
",",
"index",
"=",
"False",
",",
"keep",
"=",
"'first'",
")",
":",
"validate_set_ops",
"(",
"df",
",",
"other",
")",
"if",
"index",
":",
"df_reset_index",
"=",
"df",
".",
"reset_index",
"(",
")",
"other_rese... | Returns rows that appear in both DataFrames.
Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.
Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`. | [
"Returns",
"rows",
"that",
"appear",
"in",
"both",
"DataFrames",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/set_ops.py#L69-L105 |
236,820 | kieferk/dfply | dfply/transform.py | transmute | def transmute(df, *keep_columns, **kwargs):
"""
Creates columns and then returns those new columns and optionally specified
original columns from the DataFrame.
This works like `mutate`, but designed to discard the original columns used
to create the new ones.
Args:
*keep_columns: Column labels to keep. Can be string, symbolic, or
integer position.
Kwargs:
**kwargs: keys are the names of the new columns, values indicate
what the new column values will be.
Example:
diamonds >> transmute(x_plus_y=X.x + X.y, y_div_z=(X.y / X.z)) >> head(3)
y_div_z x_plus_y
0 1.637860 7.93
1 1.662338 7.73
2 1.761905 8.12
"""
keep_cols = []
for col in flatten(keep_columns):
try:
keep_cols.append(col.name)
except:
if isinstance(col, str):
keep_cols.append(col)
elif isinstance(col, int):
keep_cols.append(df.columns[col])
df = df.assign(**kwargs)
columns = [k for k in kwargs.keys()] + list(keep_cols)
return df[columns] | python | def transmute(df, *keep_columns, **kwargs):
keep_cols = []
for col in flatten(keep_columns):
try:
keep_cols.append(col.name)
except:
if isinstance(col, str):
keep_cols.append(col)
elif isinstance(col, int):
keep_cols.append(df.columns[col])
df = df.assign(**kwargs)
columns = [k for k in kwargs.keys()] + list(keep_cols)
return df[columns] | [
"def",
"transmute",
"(",
"df",
",",
"*",
"keep_columns",
",",
"*",
"*",
"kwargs",
")",
":",
"keep_cols",
"=",
"[",
"]",
"for",
"col",
"in",
"flatten",
"(",
"keep_columns",
")",
":",
"try",
":",
"keep_cols",
".",
"append",
"(",
"col",
".",
"name",
"... | Creates columns and then returns those new columns and optionally specified
original columns from the DataFrame.
This works like `mutate`, but designed to discard the original columns used
to create the new ones.
Args:
*keep_columns: Column labels to keep. Can be string, symbolic, or
integer position.
Kwargs:
**kwargs: keys are the names of the new columns, values indicate
what the new column values will be.
Example:
diamonds >> transmute(x_plus_y=X.x + X.y, y_div_z=(X.y / X.z)) >> head(3)
y_div_z x_plus_y
0 1.637860 7.93
1 1.662338 7.73
2 1.761905 8.12 | [
"Creates",
"columns",
"and",
"then",
"returns",
"those",
"new",
"columns",
"and",
"optionally",
"specified",
"original",
"columns",
"from",
"the",
"DataFrame",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/transform.py#L64-L101 |
236,821 | kieferk/dfply | dfply/vector.py | coalesce | def coalesce(*series):
"""
Takes the first non-NaN value in order across the specified series,
returning a new series. Mimics the coalesce function in dplyr and SQL.
Args:
*series: Series objects, typically represented in their symbolic form
(like X.series).
Example:
df = pd.DataFrame({
'a':[1,np.nan,np.nan,np.nan,np.nan],
'b':[2,3,np.nan,np.nan,np.nan],
'c':[np.nan,np.nan,4,5,np.nan],
'd':[6,7,8,9,np.nan]
})
df >> transmute(coal=coalesce(X.a, X.b, X.c, X.d))
coal
0 1
1 3
2 4
3 5
4 np.nan
"""
series = [pd.Series(s) for s in series]
coalescer = pd.concat(series, axis=1)
min_nonna = np.argmin(pd.isnull(coalescer).values, axis=1)
min_nonna = [coalescer.columns[i] for i in min_nonna]
return coalescer.lookup(np.arange(coalescer.shape[0]), min_nonna) | python | def coalesce(*series):
series = [pd.Series(s) for s in series]
coalescer = pd.concat(series, axis=1)
min_nonna = np.argmin(pd.isnull(coalescer).values, axis=1)
min_nonna = [coalescer.columns[i] for i in min_nonna]
return coalescer.lookup(np.arange(coalescer.shape[0]), min_nonna) | [
"def",
"coalesce",
"(",
"*",
"series",
")",
":",
"series",
"=",
"[",
"pd",
".",
"Series",
"(",
"s",
")",
"for",
"s",
"in",
"series",
"]",
"coalescer",
"=",
"pd",
".",
"concat",
"(",
"series",
",",
"axis",
"=",
"1",
")",
"min_nonna",
"=",
"np",
... | Takes the first non-NaN value in order across the specified series,
returning a new series. Mimics the coalesce function in dplyr and SQL.
Args:
*series: Series objects, typically represented in their symbolic form
(like X.series).
Example:
df = pd.DataFrame({
'a':[1,np.nan,np.nan,np.nan,np.nan],
'b':[2,3,np.nan,np.nan,np.nan],
'c':[np.nan,np.nan,4,5,np.nan],
'd':[6,7,8,9,np.nan]
})
df >> transmute(coal=coalesce(X.a, X.b, X.c, X.d))
coal
0 1
1 3
2 4
3 5
4 np.nan | [
"Takes",
"the",
"first",
"non",
"-",
"NaN",
"value",
"in",
"order",
"across",
"the",
"specified",
"series",
"returning",
"a",
"new",
"series",
".",
"Mimics",
"the",
"coalesce",
"function",
"in",
"dplyr",
"and",
"SQL",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/vector.py#L80-L110 |
236,822 | kieferk/dfply | dfply/vector.py | case_when | def case_when(*conditions):
"""
Functions as a switch statement, creating a new series out of logical
conditions specified by 2-item lists where the left-hand item is the
logical condition and the right-hand item is the value where that
condition is true.
Conditions should go from the most specific to the most general. A
conditional that appears earlier in the series will "overwrite" one that
appears later. Think of it like a series of if-else statements.
The logicals and values of the condition pairs must be all the same
length, or length 1. Logicals can be vectors of booleans or a single
boolean (`True`, for example, can be the logical statement for the
final conditional to catch all remaining.).
Args:
*conditions: Each condition should be a list with two values. The first
value is a boolean or vector of booleans that specify indices in
which the condition is met. The second value is a vector of values
or single value specifying the outcome where that condition is met.
Example:
df = pd.DataFrame({
'num':np.arange(16)
})
df >> mutate(strnum=case_when([X.num % 15 == 0, 'fizzbuzz'],
[X.num % 3 == 0, 'fizz'],
[X.num % 5 == 0, 'buzz'],
[True, X.num.astype(str)]))
num strnum
0 0 fizzbuzz
1 1 1
2 2 2
3 3 fizz
4 4 4
5 5 buzz
6 6 fizz
7 7 7
8 8 8
9 9 fizz
10 10 buzz
11 11 11
12 12 fizz
13 13 13
14 14 14
15 15 fizzbuzz
"""
lengths = []
for logical, outcome in conditions:
if isinstance(logical, collections.Iterable):
lengths.append(len(logical))
if isinstance(outcome, collections.Iterable) and not isinstance(outcome, str):
lengths.append(len(outcome))
unique_lengths = np.unique(lengths)
assert len(unique_lengths) == 1
output_len = unique_lengths[0]
output = []
for logical, outcome in conditions:
if isinstance(logical, bool):
logical = np.repeat(logical, output_len)
if isinstance(logical, pd.Series):
logical = logical.values
if not isinstance(outcome, collections.Iterable) or isinstance(outcome, str):
outcome = pd.Series(np.repeat(outcome, output_len))
outcome[~logical] = np.nan
output.append(outcome)
return coalesce(*output) | python | def case_when(*conditions):
lengths = []
for logical, outcome in conditions:
if isinstance(logical, collections.Iterable):
lengths.append(len(logical))
if isinstance(outcome, collections.Iterable) and not isinstance(outcome, str):
lengths.append(len(outcome))
unique_lengths = np.unique(lengths)
assert len(unique_lengths) == 1
output_len = unique_lengths[0]
output = []
for logical, outcome in conditions:
if isinstance(logical, bool):
logical = np.repeat(logical, output_len)
if isinstance(logical, pd.Series):
logical = logical.values
if not isinstance(outcome, collections.Iterable) or isinstance(outcome, str):
outcome = pd.Series(np.repeat(outcome, output_len))
outcome[~logical] = np.nan
output.append(outcome)
return coalesce(*output) | [
"def",
"case_when",
"(",
"*",
"conditions",
")",
":",
"lengths",
"=",
"[",
"]",
"for",
"logical",
",",
"outcome",
"in",
"conditions",
":",
"if",
"isinstance",
"(",
"logical",
",",
"collections",
".",
"Iterable",
")",
":",
"lengths",
".",
"append",
"(",
... | Functions as a switch statement, creating a new series out of logical
conditions specified by 2-item lists where the left-hand item is the
logical condition and the right-hand item is the value where that
condition is true.
Conditions should go from the most specific to the most general. A
conditional that appears earlier in the series will "overwrite" one that
appears later. Think of it like a series of if-else statements.
The logicals and values of the condition pairs must be all the same
length, or length 1. Logicals can be vectors of booleans or a single
boolean (`True`, for example, can be the logical statement for the
final conditional to catch all remaining.).
Args:
*conditions: Each condition should be a list with two values. The first
value is a boolean or vector of booleans that specify indices in
which the condition is met. The second value is a vector of values
or single value specifying the outcome where that condition is met.
Example:
df = pd.DataFrame({
'num':np.arange(16)
})
df >> mutate(strnum=case_when([X.num % 15 == 0, 'fizzbuzz'],
[X.num % 3 == 0, 'fizz'],
[X.num % 5 == 0, 'buzz'],
[True, X.num.astype(str)]))
num strnum
0 0 fizzbuzz
1 1 1
2 2 2
3 3 fizz
4 4 4
5 5 buzz
6 6 fizz
7 7 7
8 8 8
9 9 fizz
10 10 buzz
11 11 11
12 12 fizz
13 13 13
14 14 14
15 15 fizzbuzz | [
"Functions",
"as",
"a",
"switch",
"statement",
"creating",
"a",
"new",
"series",
"out",
"of",
"logical",
"conditions",
"specified",
"by",
"2",
"-",
"item",
"lists",
"where",
"the",
"left",
"-",
"hand",
"item",
"is",
"the",
"logical",
"condition",
"and",
"t... | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/vector.py#L118-L189 |
236,823 | kieferk/dfply | dfply/vector.py | if_else | def if_else(condition, when_true, otherwise):
"""
Wraps creation of a series based on if-else conditional logic into a function
call.
Provide a boolean vector condition, value(s) when true, and value(s)
when false, and a vector will be returned the same length as the conditional
vector according to the logical statement.
Args:
condition: A boolean vector representing the condition. This is often
a logical statement with a symbolic series.
when_true: A vector the same length as the condition vector or a single
value to apply when the condition is `True`.
otherwise: A vector the same length as the condition vector or a single
value to apply when the condition is `False`.
Example:
df = pd.DataFrame
"""
if not isinstance(when_true, collections.Iterable) or isinstance(when_true, str):
when_true = np.repeat(when_true, len(condition))
if not isinstance(otherwise, collections.Iterable) or isinstance(otherwise, str):
otherwise = np.repeat(otherwise, len(condition))
assert (len(condition) == len(when_true)) and (len(condition) == len(otherwise))
if isinstance(when_true, pd.Series):
when_true = when_true.values
if isinstance(otherwise, pd.Series):
otherwise = otherwise.values
output = np.array([when_true[i] if c else otherwise[i]
for i, c in enumerate(condition)])
return output | python | def if_else(condition, when_true, otherwise):
if not isinstance(when_true, collections.Iterable) or isinstance(when_true, str):
when_true = np.repeat(when_true, len(condition))
if not isinstance(otherwise, collections.Iterable) or isinstance(otherwise, str):
otherwise = np.repeat(otherwise, len(condition))
assert (len(condition) == len(when_true)) and (len(condition) == len(otherwise))
if isinstance(when_true, pd.Series):
when_true = when_true.values
if isinstance(otherwise, pd.Series):
otherwise = otherwise.values
output = np.array([when_true[i] if c else otherwise[i]
for i, c in enumerate(condition)])
return output | [
"def",
"if_else",
"(",
"condition",
",",
"when_true",
",",
"otherwise",
")",
":",
"if",
"not",
"isinstance",
"(",
"when_true",
",",
"collections",
".",
"Iterable",
")",
"or",
"isinstance",
"(",
"when_true",
",",
"str",
")",
":",
"when_true",
"=",
"np",
"... | Wraps creation of a series based on if-else conditional logic into a function
call.
Provide a boolean vector condition, value(s) when true, and value(s)
when false, and a vector will be returned the same length as the conditional
vector according to the logical statement.
Args:
condition: A boolean vector representing the condition. This is often
a logical statement with a symbolic series.
when_true: A vector the same length as the condition vector or a single
value to apply when the condition is `True`.
otherwise: A vector the same length as the condition vector or a single
value to apply when the condition is `False`.
Example:
df = pd.DataFrame | [
"Wraps",
"creation",
"of",
"a",
"series",
"based",
"on",
"if",
"-",
"else",
"conditional",
"logic",
"into",
"a",
"function",
"call",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/vector.py#L197-L231 |
236,824 | kieferk/dfply | dfply/vector.py | na_if | def na_if(series, *values):
"""
If values in a series match a specified value, change them to `np.nan`.
Args:
series: Series or vector, often symbolic.
*values: Value(s) to convert to `np.nan` in the series.
"""
series = pd.Series(series)
series[series.isin(values)] = np.nan
return series | python | def na_if(series, *values):
series = pd.Series(series)
series[series.isin(values)] = np.nan
return series | [
"def",
"na_if",
"(",
"series",
",",
"*",
"values",
")",
":",
"series",
"=",
"pd",
".",
"Series",
"(",
"series",
")",
"series",
"[",
"series",
".",
"isin",
"(",
"values",
")",
"]",
"=",
"np",
".",
"nan",
"return",
"series"
] | If values in a series match a specified value, change them to `np.nan`.
Args:
series: Series or vector, often symbolic.
*values: Value(s) to convert to `np.nan` in the series. | [
"If",
"values",
"in",
"a",
"series",
"match",
"a",
"specified",
"value",
"change",
"them",
"to",
"np",
".",
"nan",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/vector.py#L239-L250 |
236,825 | kieferk/dfply | dfply/window_functions.py | between | def between(series, a, b, inclusive=False):
"""
Returns a boolean series specifying whether rows of the input series
are between values `a` and `b`.
Args:
series: column to compare, typically symbolic.
a: value series must be greater than (or equal to if `inclusive=True`)
for the output series to be `True` at that position.
b: value series must be less than (or equal to if `inclusive=True`) for
the output series to be `True` at that position.
Kwargs:
inclusive (bool): If `True`, comparison is done with `>=` and `<=`.
If `False` (the default), comparison uses `>` and `<`.
"""
if inclusive == True:
met_condition = (series >= a) & (series <= b)
elif inclusive == False:
met_condition = (series > a) & (series < b)
return met_condition | python | def between(series, a, b, inclusive=False):
if inclusive == True:
met_condition = (series >= a) & (series <= b)
elif inclusive == False:
met_condition = (series > a) & (series < b)
return met_condition | [
"def",
"between",
"(",
"series",
",",
"a",
",",
"b",
",",
"inclusive",
"=",
"False",
")",
":",
"if",
"inclusive",
"==",
"True",
":",
"met_condition",
"=",
"(",
"series",
">=",
"a",
")",
"&",
"(",
"series",
"<=",
"b",
")",
"elif",
"inclusive",
"==",... | Returns a boolean series specifying whether rows of the input series
are between values `a` and `b`.
Args:
series: column to compare, typically symbolic.
a: value series must be greater than (or equal to if `inclusive=True`)
for the output series to be `True` at that position.
b: value series must be less than (or equal to if `inclusive=True`) for
the output series to be `True` at that position.
Kwargs:
inclusive (bool): If `True`, comparison is done with `>=` and `<=`.
If `False` (the default), comparison uses `>` and `<`. | [
"Returns",
"a",
"boolean",
"series",
"specifying",
"whether",
"rows",
"of",
"the",
"input",
"series",
"are",
"between",
"values",
"a",
"and",
"b",
"."
] | 6a858f066602735a90f8b6b85106bc39ceadc282 | https://github.com/kieferk/dfply/blob/6a858f066602735a90f8b6b85106bc39ceadc282/dfply/window_functions.py#L43-L64 |
236,826 | euske/pdfminer | pdfminer/psparser.py | PSBaseParser.seek | def seek(self, pos):
"""Seeks the parser to the given position.
"""
if self.debug:
logging.debug('seek: %r' % pos)
self.fp.seek(pos)
# reset the status for nextline()
self.bufpos = pos
self.buf = b''
self.charpos = 0
# reset the status for nexttoken()
self._parse1 = self._parse_main
self._curtoken = b''
self._curtokenpos = 0
self._tokens = []
return | python | def seek(self, pos):
if self.debug:
logging.debug('seek: %r' % pos)
self.fp.seek(pos)
# reset the status for nextline()
self.bufpos = pos
self.buf = b''
self.charpos = 0
# reset the status for nexttoken()
self._parse1 = self._parse_main
self._curtoken = b''
self._curtokenpos = 0
self._tokens = []
return | [
"def",
"seek",
"(",
"self",
",",
"pos",
")",
":",
"if",
"self",
".",
"debug",
":",
"logging",
".",
"debug",
"(",
"'seek: %r'",
"%",
"pos",
")",
"self",
".",
"fp",
".",
"seek",
"(",
"pos",
")",
"# reset the status for nextline()",
"self",
".",
"bufpos",... | Seeks the parser to the given position. | [
"Seeks",
"the",
"parser",
"to",
"the",
"given",
"position",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/psparser.py#L191-L206 |
236,827 | euske/pdfminer | pdfminer/psparser.py | PSBaseParser.nextline | def nextline(self):
"""Fetches a next line that ends either with \\r or \\n.
"""
linebuf = b''
linepos = self.bufpos + self.charpos
eol = False
while 1:
self.fillbuf()
if eol:
c = self.buf[self.charpos]
# handle b'\r\n'
if c == b'\n':
linebuf += c
self.charpos += 1
break
m = EOL.search(self.buf, self.charpos)
if m:
linebuf += self.buf[self.charpos:m.end(0)]
self.charpos = m.end(0)
if linebuf[-1] == b'\r':
eol = True
else:
break
else:
linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf)
if self.debug:
logging.debug('nextline: %r, %r' % (linepos, linebuf))
return (linepos, linebuf) | python | def nextline(self):
linebuf = b''
linepos = self.bufpos + self.charpos
eol = False
while 1:
self.fillbuf()
if eol:
c = self.buf[self.charpos]
# handle b'\r\n'
if c == b'\n':
linebuf += c
self.charpos += 1
break
m = EOL.search(self.buf, self.charpos)
if m:
linebuf += self.buf[self.charpos:m.end(0)]
self.charpos = m.end(0)
if linebuf[-1] == b'\r':
eol = True
else:
break
else:
linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf)
if self.debug:
logging.debug('nextline: %r, %r' % (linepos, linebuf))
return (linepos, linebuf) | [
"def",
"nextline",
"(",
"self",
")",
":",
"linebuf",
"=",
"b''",
"linepos",
"=",
"self",
".",
"bufpos",
"+",
"self",
".",
"charpos",
"eol",
"=",
"False",
"while",
"1",
":",
"self",
".",
"fillbuf",
"(",
")",
"if",
"eol",
":",
"c",
"=",
"self",
"."... | Fetches a next line that ends either with \\r or \\n. | [
"Fetches",
"a",
"next",
"line",
"that",
"ends",
"either",
"with",
"\\\\",
"r",
"or",
"\\\\",
"n",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/psparser.py#L219-L247 |
236,828 | euske/pdfminer | pdfminer/psparser.py | PSBaseParser.revreadlines | def revreadlines(self):
"""Fetches a next line backward.
This is used to locate the trailers at the end of a file.
"""
self.fp.seek(0, 2)
pos = self.fp.tell()
buf = b''
while 0 < pos:
prevpos = pos
pos = max(0, pos-self.BUFSIZ)
self.fp.seek(pos)
s = self.fp.read(prevpos-pos)
if not s:
break
while 1:
n = max(s.rfind(b'\r'), s.rfind(b'\n'))
if n == -1:
buf = s + buf
break
yield s[n:]+buf
s = s[:n]
buf = b''
return | python | def revreadlines(self):
self.fp.seek(0, 2)
pos = self.fp.tell()
buf = b''
while 0 < pos:
prevpos = pos
pos = max(0, pos-self.BUFSIZ)
self.fp.seek(pos)
s = self.fp.read(prevpos-pos)
if not s:
break
while 1:
n = max(s.rfind(b'\r'), s.rfind(b'\n'))
if n == -1:
buf = s + buf
break
yield s[n:]+buf
s = s[:n]
buf = b''
return | [
"def",
"revreadlines",
"(",
"self",
")",
":",
"self",
".",
"fp",
".",
"seek",
"(",
"0",
",",
"2",
")",
"pos",
"=",
"self",
".",
"fp",
".",
"tell",
"(",
")",
"buf",
"=",
"b''",
"while",
"0",
"<",
"pos",
":",
"prevpos",
"=",
"pos",
"pos",
"=",
... | Fetches a next line backward.
This is used to locate the trailers at the end of a file. | [
"Fetches",
"a",
"next",
"line",
"backward",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/psparser.py#L249-L272 |
236,829 | euske/pdfminer | pdfminer/psparser.py | PSStackParser.nextobject | def nextobject(self):
"""Yields a list of objects.
Returns keywords, literals, strings, numbers, arrays and dictionaries.
Arrays and dictionaries are represented as Python lists and dictionaries.
"""
while not self.results:
(pos, token) = self.nexttoken()
#print (pos,token), (self.curtype, self.curstack)
if isinstance(token, (int, long, float, bool, str, PSLiteral)):
# normal token
self.push((pos, token))
elif token == KEYWORD_ARRAY_BEGIN:
# begin array
self.start_type(pos, 'a')
elif token == KEYWORD_ARRAY_END:
# end array
try:
self.push(self.end_type('a'))
except PSTypeError:
if STRICT:
raise
elif token == KEYWORD_DICT_BEGIN:
# begin dictionary
self.start_type(pos, 'd')
elif token == KEYWORD_DICT_END:
# end dictionary
try:
(pos, objs) = self.end_type('d')
if len(objs) % 2 != 0:
raise PSSyntaxError('Invalid dictionary construct: %r' % (objs,))
# construct a Python dictionary.
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
self.push((pos, d))
except PSTypeError:
if STRICT:
raise
elif token == KEYWORD_PROC_BEGIN:
# begin proc
self.start_type(pos, 'p')
elif token == KEYWORD_PROC_END:
# end proc
try:
self.push(self.end_type('p'))
except PSTypeError:
if STRICT:
raise
else:
if self.debug:
logging.debug('do_keyword: pos=%r, token=%r, stack=%r' % \
(pos, token, self.curstack))
self.do_keyword(pos, token)
if self.context:
continue
else:
self.flush()
obj = self.results.pop(0)
if self.debug:
logging.debug('nextobject: %r' % (obj,))
return obj | python | def nextobject(self):
while not self.results:
(pos, token) = self.nexttoken()
#print (pos,token), (self.curtype, self.curstack)
if isinstance(token, (int, long, float, bool, str, PSLiteral)):
# normal token
self.push((pos, token))
elif token == KEYWORD_ARRAY_BEGIN:
# begin array
self.start_type(pos, 'a')
elif token == KEYWORD_ARRAY_END:
# end array
try:
self.push(self.end_type('a'))
except PSTypeError:
if STRICT:
raise
elif token == KEYWORD_DICT_BEGIN:
# begin dictionary
self.start_type(pos, 'd')
elif token == KEYWORD_DICT_END:
# end dictionary
try:
(pos, objs) = self.end_type('d')
if len(objs) % 2 != 0:
raise PSSyntaxError('Invalid dictionary construct: %r' % (objs,))
# construct a Python dictionary.
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
self.push((pos, d))
except PSTypeError:
if STRICT:
raise
elif token == KEYWORD_PROC_BEGIN:
# begin proc
self.start_type(pos, 'p')
elif token == KEYWORD_PROC_END:
# end proc
try:
self.push(self.end_type('p'))
except PSTypeError:
if STRICT:
raise
else:
if self.debug:
logging.debug('do_keyword: pos=%r, token=%r, stack=%r' % \
(pos, token, self.curstack))
self.do_keyword(pos, token)
if self.context:
continue
else:
self.flush()
obj = self.results.pop(0)
if self.debug:
logging.debug('nextobject: %r' % (obj,))
return obj | [
"def",
"nextobject",
"(",
"self",
")",
":",
"while",
"not",
"self",
".",
"results",
":",
"(",
"pos",
",",
"token",
")",
"=",
"self",
".",
"nexttoken",
"(",
")",
"#print (pos,token), (self.curtype, self.curstack)",
"if",
"isinstance",
"(",
"token",
",",
"(",
... | Yields a list of objects.
Returns keywords, literals, strings, numbers, arrays and dictionaries.
Arrays and dictionaries are represented as Python lists and dictionaries. | [
"Yields",
"a",
"list",
"of",
"objects",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/psparser.py#L567-L626 |
236,830 | euske/pdfminer | pdfminer/encodingdb.py | name2unicode | def name2unicode(name):
"""Converts Adobe glyph names to Unicode numbers."""
if name in glyphname2unicode:
return glyphname2unicode[name]
m = STRIP_NAME.search(name)
if not m:
raise KeyError(name)
return unichr(int(m.group(0))) | python | def name2unicode(name):
if name in glyphname2unicode:
return glyphname2unicode[name]
m = STRIP_NAME.search(name)
if not m:
raise KeyError(name)
return unichr(int(m.group(0))) | [
"def",
"name2unicode",
"(",
"name",
")",
":",
"if",
"name",
"in",
"glyphname2unicode",
":",
"return",
"glyphname2unicode",
"[",
"name",
"]",
"m",
"=",
"STRIP_NAME",
".",
"search",
"(",
"name",
")",
"if",
"not",
"m",
":",
"raise",
"KeyError",
"(",
"name",... | Converts Adobe glyph names to Unicode numbers. | [
"Converts",
"Adobe",
"glyph",
"names",
"to",
"Unicode",
"numbers",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/encodingdb.py#L13-L20 |
236,831 | euske/pdfminer | pdfminer/pdftypes.py | resolve1 | def resolve1(x, default=None):
"""Resolves an object.
If this is an array or dictionary, it may still contains
some indirect objects inside.
"""
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
return x | python | def resolve1(x, default=None):
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
return x | [
"def",
"resolve1",
"(",
"x",
",",
"default",
"=",
"None",
")",
":",
"while",
"isinstance",
"(",
"x",
",",
"PDFObjRef",
")",
":",
"x",
"=",
"x",
".",
"resolve",
"(",
"default",
"=",
"default",
")",
"return",
"x"
] | Resolves an object.
If this is an array or dictionary, it may still contains
some indirect objects inside. | [
"Resolves",
"an",
"object",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdftypes.py#L73-L81 |
236,832 | euske/pdfminer | pdfminer/pdftypes.py | resolve_all | def resolve_all(x, default=None):
"""Recursively resolves the given object and all the internals.
Make sure there is no indirect reference within the nested object.
This procedure might be slow.
"""
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
if isinstance(x, list):
x = [resolve_all(v, default=default) for v in x]
elif isinstance(x, dict):
for (k, v) in x.iteritems():
x[k] = resolve_all(v, default=default)
return x | python | def resolve_all(x, default=None):
while isinstance(x, PDFObjRef):
x = x.resolve(default=default)
if isinstance(x, list):
x = [resolve_all(v, default=default) for v in x]
elif isinstance(x, dict):
for (k, v) in x.iteritems():
x[k] = resolve_all(v, default=default)
return x | [
"def",
"resolve_all",
"(",
"x",
",",
"default",
"=",
"None",
")",
":",
"while",
"isinstance",
"(",
"x",
",",
"PDFObjRef",
")",
":",
"x",
"=",
"x",
".",
"resolve",
"(",
"default",
"=",
"default",
")",
"if",
"isinstance",
"(",
"x",
",",
"list",
")",
... | Recursively resolves the given object and all the internals.
Make sure there is no indirect reference within the nested object.
This procedure might be slow. | [
"Recursively",
"resolves",
"the",
"given",
"object",
"and",
"all",
"the",
"internals",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdftypes.py#L84-L97 |
236,833 | euske/pdfminer | pdfminer/pdftypes.py | decipher_all | def decipher_all(decipher, objid, genno, x):
"""Recursively deciphers the given object.
"""
if isinstance(x, str):
return decipher(objid, genno, x)
if isinstance(x, list):
x = [decipher_all(decipher, objid, genno, v) for v in x]
elif isinstance(x, dict):
for (k, v) in x.iteritems():
x[k] = decipher_all(decipher, objid, genno, v)
return x | python | def decipher_all(decipher, objid, genno, x):
if isinstance(x, str):
return decipher(objid, genno, x)
if isinstance(x, list):
x = [decipher_all(decipher, objid, genno, v) for v in x]
elif isinstance(x, dict):
for (k, v) in x.iteritems():
x[k] = decipher_all(decipher, objid, genno, v)
return x | [
"def",
"decipher_all",
"(",
"decipher",
",",
"objid",
",",
"genno",
",",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"str",
")",
":",
"return",
"decipher",
"(",
"objid",
",",
"genno",
",",
"x",
")",
"if",
"isinstance",
"(",
"x",
",",
"list",
... | Recursively deciphers the given object. | [
"Recursively",
"deciphers",
"the",
"given",
"object",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdftypes.py#L100-L110 |
236,834 | euske/pdfminer | pdfminer/pdfdocument.py | PDFDocument.find_xref | def find_xref(self, parser):
"""Internal function used to locate the first XRef."""
# search the last xref table by scanning the file backwards.
prev = None
for line in parser.revreadlines():
line = line.strip()
if self.debug:
logging.debug('find_xref: %r' % line)
if line == b'startxref':
break
if line:
prev = line
else:
raise PDFNoValidXRef('Unexpected EOF')
if self.debug:
logging.info('xref found: pos=%r' % prev)
return long(prev) | python | def find_xref(self, parser):
# search the last xref table by scanning the file backwards.
prev = None
for line in parser.revreadlines():
line = line.strip()
if self.debug:
logging.debug('find_xref: %r' % line)
if line == b'startxref':
break
if line:
prev = line
else:
raise PDFNoValidXRef('Unexpected EOF')
if self.debug:
logging.info('xref found: pos=%r' % prev)
return long(prev) | [
"def",
"find_xref",
"(",
"self",
",",
"parser",
")",
":",
"# search the last xref table by scanning the file backwards.",
"prev",
"=",
"None",
"for",
"line",
"in",
"parser",
".",
"revreadlines",
"(",
")",
":",
"line",
"=",
"line",
".",
"strip",
"(",
")",
"if",... | Internal function used to locate the first XRef. | [
"Internal",
"function",
"used",
"to",
"locate",
"the",
"first",
"XRef",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdfdocument.py#L755-L771 |
236,835 | euske/pdfminer | pdfminer/pdfdocument.py | PDFDocument.read_xref_from | def read_xref_from(self, parser, start, xrefs):
"""Reads XRefs from the given location."""
parser.seek(start)
parser.reset()
try:
(pos, token) = parser.nexttoken()
except PSEOF:
raise PDFNoValidXRef('Unexpected EOF')
if self.debug:
logging.info('read_xref_from: start=%d, token=%r' % (start, token))
if isinstance(token, int):
# XRefStream: PDF-1.5
parser.seek(pos)
parser.reset()
xref = PDFXRefStream()
xref.load(parser)
else:
if token is parser.KEYWORD_XREF:
parser.nextline()
xref = PDFXRef()
xref.load(parser)
xrefs.append(xref)
trailer = xref.get_trailer()
if self.debug:
logging.info('trailer: %r' % trailer)
if 'XRefStm' in trailer:
pos = int_value(trailer['XRefStm'])
self.read_xref_from(parser, pos, xrefs)
if 'Prev' in trailer:
# find previous xref
pos = int_value(trailer['Prev'])
self.read_xref_from(parser, pos, xrefs)
return | python | def read_xref_from(self, parser, start, xrefs):
parser.seek(start)
parser.reset()
try:
(pos, token) = parser.nexttoken()
except PSEOF:
raise PDFNoValidXRef('Unexpected EOF')
if self.debug:
logging.info('read_xref_from: start=%d, token=%r' % (start, token))
if isinstance(token, int):
# XRefStream: PDF-1.5
parser.seek(pos)
parser.reset()
xref = PDFXRefStream()
xref.load(parser)
else:
if token is parser.KEYWORD_XREF:
parser.nextline()
xref = PDFXRef()
xref.load(parser)
xrefs.append(xref)
trailer = xref.get_trailer()
if self.debug:
logging.info('trailer: %r' % trailer)
if 'XRefStm' in trailer:
pos = int_value(trailer['XRefStm'])
self.read_xref_from(parser, pos, xrefs)
if 'Prev' in trailer:
# find previous xref
pos = int_value(trailer['Prev'])
self.read_xref_from(parser, pos, xrefs)
return | [
"def",
"read_xref_from",
"(",
"self",
",",
"parser",
",",
"start",
",",
"xrefs",
")",
":",
"parser",
".",
"seek",
"(",
"start",
")",
"parser",
".",
"reset",
"(",
")",
"try",
":",
"(",
"pos",
",",
"token",
")",
"=",
"parser",
".",
"nexttoken",
"(",
... | Reads XRefs from the given location. | [
"Reads",
"XRefs",
"from",
"the",
"given",
"location",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdfdocument.py#L774-L806 |
236,836 | euske/pdfminer | pdfminer/utils.py | mult_matrix | def mult_matrix(m1, m0):
(a1, b1, c1, d1, e1, f1) = m1
(a0, b0, c0, d0, e0, f0) = m0
"""Returns the multiplication of two matrices."""
return (a0*a1+c0*b1, b0*a1+d0*b1,
a0*c1+c0*d1, b0*c1+d0*d1,
a0*e1+c0*f1+e0, b0*e1+d0*f1+f0) | python | def mult_matrix(m1, m0):
(a1, b1, c1, d1, e1, f1) = m1
(a0, b0, c0, d0, e0, f0) = m0
return (a0*a1+c0*b1, b0*a1+d0*b1,
a0*c1+c0*d1, b0*c1+d0*d1,
a0*e1+c0*f1+e0, b0*e1+d0*f1+f0) | [
"def",
"mult_matrix",
"(",
"m1",
",",
"m0",
")",
":",
"(",
"a1",
",",
"b1",
",",
"c1",
",",
"d1",
",",
"e1",
",",
"f1",
")",
"=",
"m1",
"(",
"a0",
",",
"b0",
",",
"c0",
",",
"d0",
",",
"e0",
",",
"f0",
")",
"=",
"m0",
"return",
"(",
"a0... | Returns the multiplication of two matrices. | [
"Returns",
"the",
"multiplication",
"of",
"two",
"matrices",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L57-L63 |
236,837 | euske/pdfminer | pdfminer/utils.py | uniq | def uniq(objs):
"""Eliminates duplicated elements."""
done = set()
for obj in objs:
if obj in done:
continue
done.add(obj)
yield obj
return | python | def uniq(objs):
done = set()
for obj in objs:
if obj in done:
continue
done.add(obj)
yield obj
return | [
"def",
"uniq",
"(",
"objs",
")",
":",
"done",
"=",
"set",
"(",
")",
"for",
"obj",
"in",
"objs",
":",
"if",
"obj",
"in",
"done",
":",
"continue",
"done",
".",
"add",
"(",
"obj",
")",
"yield",
"obj",
"return"
] | Eliminates duplicated elements. | [
"Eliminates",
"duplicated",
"elements",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L95-L103 |
236,838 | euske/pdfminer | pdfminer/utils.py | csort | def csort(objs, key):
"""Order-preserving sorting function."""
idxs = dict((obj, i) for (i, obj) in enumerate(objs))
return sorted(objs, key=lambda obj: (key(obj), idxs[obj])) | python | def csort(objs, key):
idxs = dict((obj, i) for (i, obj) in enumerate(objs))
return sorted(objs, key=lambda obj: (key(obj), idxs[obj])) | [
"def",
"csort",
"(",
"objs",
",",
"key",
")",
":",
"idxs",
"=",
"dict",
"(",
"(",
"obj",
",",
"i",
")",
"for",
"(",
"i",
",",
"obj",
")",
"in",
"enumerate",
"(",
"objs",
")",
")",
"return",
"sorted",
"(",
"objs",
",",
"key",
"=",
"lambda",
"o... | Order-preserving sorting function. | [
"Order",
"-",
"preserving",
"sorting",
"function",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L107-L110 |
236,839 | euske/pdfminer | pdfminer/utils.py | fsplit | def fsplit(pred, objs):
"""Split a list into two classes according to the predicate."""
t = []
f = []
for obj in objs:
if pred(obj):
t.append(obj)
else:
f.append(obj)
return (t, f) | python | def fsplit(pred, objs):
t = []
f = []
for obj in objs:
if pred(obj):
t.append(obj)
else:
f.append(obj)
return (t, f) | [
"def",
"fsplit",
"(",
"pred",
",",
"objs",
")",
":",
"t",
"=",
"[",
"]",
"f",
"=",
"[",
"]",
"for",
"obj",
"in",
"objs",
":",
"if",
"pred",
"(",
"obj",
")",
":",
"t",
".",
"append",
"(",
"obj",
")",
"else",
":",
"f",
".",
"append",
"(",
"... | Split a list into two classes according to the predicate. | [
"Split",
"a",
"list",
"into",
"two",
"classes",
"according",
"to",
"the",
"predicate",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L114-L123 |
236,840 | euske/pdfminer | pdfminer/utils.py | drange | def drange(v0, v1, d):
"""Returns a discrete range."""
assert v0 < v1
return xrange(int(v0)//d, int(v1+d)//d) | python | def drange(v0, v1, d):
assert v0 < v1
return xrange(int(v0)//d, int(v1+d)//d) | [
"def",
"drange",
"(",
"v0",
",",
"v1",
",",
"d",
")",
":",
"assert",
"v0",
"<",
"v1",
"return",
"xrange",
"(",
"int",
"(",
"v0",
")",
"//",
"d",
",",
"int",
"(",
"v1",
"+",
"d",
")",
"//",
"d",
")"
] | Returns a discrete range. | [
"Returns",
"a",
"discrete",
"range",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L127-L130 |
236,841 | euske/pdfminer | pdfminer/utils.py | get_bound | def get_bound(pts):
"""Compute a minimal rectangle that covers all the points."""
(x0, y0, x1, y1) = (INF, INF, -INF, -INF)
for (x, y) in pts:
x0 = min(x0, x)
y0 = min(y0, y)
x1 = max(x1, x)
y1 = max(y1, y)
return (x0, y0, x1, y1) | python | def get_bound(pts):
(x0, y0, x1, y1) = (INF, INF, -INF, -INF)
for (x, y) in pts:
x0 = min(x0, x)
y0 = min(y0, y)
x1 = max(x1, x)
y1 = max(y1, y)
return (x0, y0, x1, y1) | [
"def",
"get_bound",
"(",
"pts",
")",
":",
"(",
"x0",
",",
"y0",
",",
"x1",
",",
"y1",
")",
"=",
"(",
"INF",
",",
"INF",
",",
"-",
"INF",
",",
"-",
"INF",
")",
"for",
"(",
"x",
",",
"y",
")",
"in",
"pts",
":",
"x0",
"=",
"min",
"(",
"x0"... | Compute a minimal rectangle that covers all the points. | [
"Compute",
"a",
"minimal",
"rectangle",
"that",
"covers",
"all",
"the",
"points",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L134-L142 |
236,842 | euske/pdfminer | pdfminer/utils.py | choplist | def choplist(n, seq):
"""Groups every n elements of the list."""
r = []
for x in seq:
r.append(x)
if len(r) == n:
yield tuple(r)
r = []
return | python | def choplist(n, seq):
r = []
for x in seq:
r.append(x)
if len(r) == n:
yield tuple(r)
r = []
return | [
"def",
"choplist",
"(",
"n",
",",
"seq",
")",
":",
"r",
"=",
"[",
"]",
"for",
"x",
"in",
"seq",
":",
"r",
".",
"append",
"(",
"x",
")",
"if",
"len",
"(",
"r",
")",
"==",
"n",
":",
"yield",
"tuple",
"(",
"r",
")",
"r",
"=",
"[",
"]",
"re... | Groups every n elements of the list. | [
"Groups",
"every",
"n",
"elements",
"of",
"the",
"list",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L157-L165 |
236,843 | euske/pdfminer | pdfminer/utils.py | decode_text | def decode_text(s):
"""Decodes a PDFDocEncoding string to Unicode."""
if s.startswith(b'\xfe\xff'):
return unicode(s[2:], 'utf-16be', 'ignore')
else:
return ''.join(PDFDocEncoding[ord(c)] for c in s) | python | def decode_text(s):
if s.startswith(b'\xfe\xff'):
return unicode(s[2:], 'utf-16be', 'ignore')
else:
return ''.join(PDFDocEncoding[ord(c)] for c in s) | [
"def",
"decode_text",
"(",
"s",
")",
":",
"if",
"s",
".",
"startswith",
"(",
"b'\\xfe\\xff'",
")",
":",
"return",
"unicode",
"(",
"s",
"[",
"2",
":",
"]",
",",
"'utf-16be'",
",",
"'ignore'",
")",
"else",
":",
"return",
"''",
".",
"join",
"(",
"PDFD... | Decodes a PDFDocEncoding string to Unicode. | [
"Decodes",
"a",
"PDFDocEncoding",
"string",
"to",
"Unicode",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/utils.py#L223-L228 |
236,844 | euske/pdfminer | pdfminer/pdfparser.py | PDFParser.do_keyword | def do_keyword(self, pos, token):
"""Handles PDF-related keywords."""
if token in (self.KEYWORD_XREF, self.KEYWORD_STARTXREF):
self.add_results(*self.pop(1))
elif token is self.KEYWORD_ENDOBJ:
self.add_results(*self.pop(4))
elif token is self.KEYWORD_NULL:
# null object
self.push((pos, None))
elif token is self.KEYWORD_R:
# reference to indirect object
try:
((_, objid), (_, genno)) = self.pop(2)
(objid, genno) = (int(objid), int(genno))
obj = PDFObjRef(self.doc, objid, genno)
self.push((pos, obj))
except PSSyntaxError:
pass
elif token is self.KEYWORD_STREAM:
# stream object
((_, dic),) = self.pop(1)
dic = dict_value(dic)
objlen = 0
if not self.fallback:
try:
objlen = int_value(dic['Length'])
except KeyError:
if STRICT:
raise PDFSyntaxError('/Length is undefined: %r' % dic)
self.seek(pos)
try:
(_, line) = self.nextline() # 'stream'
except PSEOF:
if STRICT:
raise PDFSyntaxError('Unexpected EOF')
return
pos += len(line)
self.fp.seek(pos)
data = self.fp.read(objlen)
self.seek(pos+objlen)
while 1:
try:
(linepos, line) = self.nextline()
except PSEOF:
if STRICT:
raise PDFSyntaxError('Unexpected EOF')
break
if b'endstream' in line:
i = line.index(b'endstream')
objlen += i
if self.fallback:
data += line[:i]
break
objlen += len(line)
if self.fallback:
data += line
self.seek(pos+objlen)
# XXX limit objlen not to exceed object boundary
if self.debug:
logging.debug('Stream: pos=%d, objlen=%d, dic=%r, data=%r...' % \
(pos, objlen, dic, data[:10]))
obj = PDFStream(dic, data, self.doc.decipher)
self.push((pos, obj))
else:
# others
self.push((pos, token))
return | python | def do_keyword(self, pos, token):
if token in (self.KEYWORD_XREF, self.KEYWORD_STARTXREF):
self.add_results(*self.pop(1))
elif token is self.KEYWORD_ENDOBJ:
self.add_results(*self.pop(4))
elif token is self.KEYWORD_NULL:
# null object
self.push((pos, None))
elif token is self.KEYWORD_R:
# reference to indirect object
try:
((_, objid), (_, genno)) = self.pop(2)
(objid, genno) = (int(objid), int(genno))
obj = PDFObjRef(self.doc, objid, genno)
self.push((pos, obj))
except PSSyntaxError:
pass
elif token is self.KEYWORD_STREAM:
# stream object
((_, dic),) = self.pop(1)
dic = dict_value(dic)
objlen = 0
if not self.fallback:
try:
objlen = int_value(dic['Length'])
except KeyError:
if STRICT:
raise PDFSyntaxError('/Length is undefined: %r' % dic)
self.seek(pos)
try:
(_, line) = self.nextline() # 'stream'
except PSEOF:
if STRICT:
raise PDFSyntaxError('Unexpected EOF')
return
pos += len(line)
self.fp.seek(pos)
data = self.fp.read(objlen)
self.seek(pos+objlen)
while 1:
try:
(linepos, line) = self.nextline()
except PSEOF:
if STRICT:
raise PDFSyntaxError('Unexpected EOF')
break
if b'endstream' in line:
i = line.index(b'endstream')
objlen += i
if self.fallback:
data += line[:i]
break
objlen += len(line)
if self.fallback:
data += line
self.seek(pos+objlen)
# XXX limit objlen not to exceed object boundary
if self.debug:
logging.debug('Stream: pos=%d, objlen=%d, dic=%r, data=%r...' % \
(pos, objlen, dic, data[:10]))
obj = PDFStream(dic, data, self.doc.decipher)
self.push((pos, obj))
else:
# others
self.push((pos, token))
return | [
"def",
"do_keyword",
"(",
"self",
",",
"pos",
",",
"token",
")",
":",
"if",
"token",
"in",
"(",
"self",
".",
"KEYWORD_XREF",
",",
"self",
".",
"KEYWORD_STARTXREF",
")",
":",
"self",
".",
"add_results",
"(",
"*",
"self",
".",
"pop",
"(",
"1",
")",
"... | Handles PDF-related keywords. | [
"Handles",
"PDF",
"-",
"related",
"keywords",
"."
] | 8150458718e9024c80b00e74965510b20206e588 | https://github.com/euske/pdfminer/blob/8150458718e9024c80b00e74965510b20206e588/pdfminer/pdfparser.py#L60-L133 |
236,845 | eliangcs/http-prompt | http_prompt/execution.py | generate_help_text | def generate_help_text():
"""Return a formatted string listing commands, HTTPie options, and HTTP
actions.
"""
def generate_cmds_with_explanations(summary, cmds):
text = '{0}:\n'.format(summary)
for cmd, explanation in cmds:
text += '\t{0:<10}\t{1:<20}\n'.format(cmd, explanation)
return text + '\n'
text = generate_cmds_with_explanations('Commands', ROOT_COMMANDS.items())
text += generate_cmds_with_explanations('Options', OPTION_NAMES.items())
text += generate_cmds_with_explanations('Actions', ACTIONS.items())
text += generate_cmds_with_explanations('Headers', HEADER_NAMES.items())
return text | python | def generate_help_text():
def generate_cmds_with_explanations(summary, cmds):
text = '{0}:\n'.format(summary)
for cmd, explanation in cmds:
text += '\t{0:<10}\t{1:<20}\n'.format(cmd, explanation)
return text + '\n'
text = generate_cmds_with_explanations('Commands', ROOT_COMMANDS.items())
text += generate_cmds_with_explanations('Options', OPTION_NAMES.items())
text += generate_cmds_with_explanations('Actions', ACTIONS.items())
text += generate_cmds_with_explanations('Headers', HEADER_NAMES.items())
return text | [
"def",
"generate_help_text",
"(",
")",
":",
"def",
"generate_cmds_with_explanations",
"(",
"summary",
",",
"cmds",
")",
":",
"text",
"=",
"'{0}:\\n'",
".",
"format",
"(",
"summary",
")",
"for",
"cmd",
",",
"explanation",
"in",
"cmds",
":",
"text",
"+=",
"'... | Return a formatted string listing commands, HTTPie options, and HTTP
actions. | [
"Return",
"a",
"formatted",
"string",
"listing",
"commands",
"HTTPie",
"options",
"and",
"HTTP",
"actions",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/execution.py#L154-L168 |
236,846 | eliangcs/http-prompt | http_prompt/utils.py | colformat | def colformat(strings, num_sep_spaces=1, terminal_width=None):
"""Format a list of strings like ls does multi-column output."""
if terminal_width is None:
terminal_width = get_terminal_size().columns
if not strings:
return
num_items = len(strings)
max_len = max([len(strip_ansi_escapes(s)) for s in strings])
num_columns = min(
int((terminal_width + num_sep_spaces) / (max_len + num_sep_spaces)),
num_items)
num_columns = max(1, num_columns)
num_lines = int(math.ceil(float(num_items) / num_columns))
num_columns = int(math.ceil(float(num_items) / num_lines))
num_elements_last_column = num_items % num_lines
if num_elements_last_column == 0:
num_elements_last_column = num_lines
lines = []
for i in range(num_lines):
line_size = num_columns
if i >= num_elements_last_column:
line_size -= 1
lines.append([None] * line_size)
for i, line in enumerate(lines):
line_size = len(line)
for j in range(line_size):
k = i + num_lines * j
item = strings[k]
if j % line_size != line_size - 1:
item_len = len(strip_ansi_escapes(item))
item = item + ' ' * (max_len - item_len)
line[j] = item
sep = ' ' * num_sep_spaces
for line in lines:
yield sep.join(line) | python | def colformat(strings, num_sep_spaces=1, terminal_width=None):
if terminal_width is None:
terminal_width = get_terminal_size().columns
if not strings:
return
num_items = len(strings)
max_len = max([len(strip_ansi_escapes(s)) for s in strings])
num_columns = min(
int((terminal_width + num_sep_spaces) / (max_len + num_sep_spaces)),
num_items)
num_columns = max(1, num_columns)
num_lines = int(math.ceil(float(num_items) / num_columns))
num_columns = int(math.ceil(float(num_items) / num_lines))
num_elements_last_column = num_items % num_lines
if num_elements_last_column == 0:
num_elements_last_column = num_lines
lines = []
for i in range(num_lines):
line_size = num_columns
if i >= num_elements_last_column:
line_size -= 1
lines.append([None] * line_size)
for i, line in enumerate(lines):
line_size = len(line)
for j in range(line_size):
k = i + num_lines * j
item = strings[k]
if j % line_size != line_size - 1:
item_len = len(strip_ansi_escapes(item))
item = item + ' ' * (max_len - item_len)
line[j] = item
sep = ' ' * num_sep_spaces
for line in lines:
yield sep.join(line) | [
"def",
"colformat",
"(",
"strings",
",",
"num_sep_spaces",
"=",
"1",
",",
"terminal_width",
"=",
"None",
")",
":",
"if",
"terminal_width",
"is",
"None",
":",
"terminal_width",
"=",
"get_terminal_size",
"(",
")",
".",
"columns",
"if",
"not",
"strings",
":",
... | Format a list of strings like ls does multi-column output. | [
"Format",
"a",
"list",
"of",
"strings",
"like",
"ls",
"does",
"multi",
"-",
"column",
"output",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/utils.py#L47-L89 |
236,847 | eliangcs/http-prompt | http_prompt/contextio.py | load_context | def load_context(context, file_path=None):
"""Load a Context object in place from user data directory."""
if not file_path:
file_path = _get_context_filepath()
if os.path.exists(file_path):
with io.open(file_path, encoding='utf-8') as f:
for line in f:
execute(line, context) | python | def load_context(context, file_path=None):
if not file_path:
file_path = _get_context_filepath()
if os.path.exists(file_path):
with io.open(file_path, encoding='utf-8') as f:
for line in f:
execute(line, context) | [
"def",
"load_context",
"(",
"context",
",",
"file_path",
"=",
"None",
")",
":",
"if",
"not",
"file_path",
":",
"file_path",
"=",
"_get_context_filepath",
"(",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"file_path",
")",
":",
"with",
"io",
".",
"... | Load a Context object in place from user data directory. | [
"Load",
"a",
"Context",
"object",
"in",
"place",
"from",
"user",
"data",
"directory",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/contextio.py#L23-L30 |
236,848 | eliangcs/http-prompt | http_prompt/contextio.py | save_context | def save_context(context):
"""Save a Context object to user data directory."""
file_path = _get_context_filepath()
content = format_to_http_prompt(context, excluded_options=EXCLUDED_OPTIONS)
with io.open(file_path, 'w', encoding='utf-8') as f:
f.write(content) | python | def save_context(context):
file_path = _get_context_filepath()
content = format_to_http_prompt(context, excluded_options=EXCLUDED_OPTIONS)
with io.open(file_path, 'w', encoding='utf-8') as f:
f.write(content) | [
"def",
"save_context",
"(",
"context",
")",
":",
"file_path",
"=",
"_get_context_filepath",
"(",
")",
"content",
"=",
"format_to_http_prompt",
"(",
"context",
",",
"excluded_options",
"=",
"EXCLUDED_OPTIONS",
")",
"with",
"io",
".",
"open",
"(",
"file_path",
","... | Save a Context object to user data directory. | [
"Save",
"a",
"Context",
"object",
"to",
"user",
"data",
"directory",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/contextio.py#L33-L38 |
236,849 | eliangcs/http-prompt | http_prompt/context/transform.py | extract_args_for_httpie_main | def extract_args_for_httpie_main(context, method=None):
"""Transform a Context object to a list of arguments that can be passed to
HTTPie main function.
"""
args = _extract_httpie_options(context)
if method:
args.append(method.upper())
args.append(context.url)
args += _extract_httpie_request_items(context)
return args | python | def extract_args_for_httpie_main(context, method=None):
args = _extract_httpie_options(context)
if method:
args.append(method.upper())
args.append(context.url)
args += _extract_httpie_request_items(context)
return args | [
"def",
"extract_args_for_httpie_main",
"(",
"context",
",",
"method",
"=",
"None",
")",
":",
"args",
"=",
"_extract_httpie_options",
"(",
"context",
")",
"if",
"method",
":",
"args",
".",
"append",
"(",
"method",
".",
"upper",
"(",
")",
")",
"args",
".",
... | Transform a Context object to a list of arguments that can be passed to
HTTPie main function. | [
"Transform",
"a",
"Context",
"object",
"to",
"a",
"list",
"of",
"arguments",
"that",
"can",
"be",
"passed",
"to",
"HTTPie",
"main",
"function",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/context/transform.py#L73-L84 |
236,850 | eliangcs/http-prompt | http_prompt/context/transform.py | format_to_httpie | def format_to_httpie(context, method=None):
"""Format a Context object to an HTTPie command."""
cmd = ['http'] + _extract_httpie_options(context, quote=True,
join_key_value=True)
if method:
cmd.append(method.upper())
cmd.append(context.url)
cmd += _extract_httpie_request_items(context, quote=True)
return ' '.join(cmd) + '\n' | python | def format_to_httpie(context, method=None):
cmd = ['http'] + _extract_httpie_options(context, quote=True,
join_key_value=True)
if method:
cmd.append(method.upper())
cmd.append(context.url)
cmd += _extract_httpie_request_items(context, quote=True)
return ' '.join(cmd) + '\n' | [
"def",
"format_to_httpie",
"(",
"context",
",",
"method",
"=",
"None",
")",
":",
"cmd",
"=",
"[",
"'http'",
"]",
"+",
"_extract_httpie_options",
"(",
"context",
",",
"quote",
"=",
"True",
",",
"join_key_value",
"=",
"True",
")",
"if",
"method",
":",
"cmd... | Format a Context object to an HTTPie command. | [
"Format",
"a",
"Context",
"object",
"to",
"an",
"HTTPie",
"command",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/context/transform.py#L97-L105 |
236,851 | eliangcs/http-prompt | http_prompt/context/transform.py | format_to_http_prompt | def format_to_http_prompt(context, excluded_options=None):
"""Format a Context object to HTTP Prompt commands."""
cmds = _extract_httpie_options(context, quote=True, join_key_value=True,
excluded_keys=excluded_options)
cmds.append('cd ' + smart_quote(context.url))
cmds += _extract_httpie_request_items(context, quote=True)
return '\n'.join(cmds) + '\n' | python | def format_to_http_prompt(context, excluded_options=None):
cmds = _extract_httpie_options(context, quote=True, join_key_value=True,
excluded_keys=excluded_options)
cmds.append('cd ' + smart_quote(context.url))
cmds += _extract_httpie_request_items(context, quote=True)
return '\n'.join(cmds) + '\n' | [
"def",
"format_to_http_prompt",
"(",
"context",
",",
"excluded_options",
"=",
"None",
")",
":",
"cmds",
"=",
"_extract_httpie_options",
"(",
"context",
",",
"quote",
"=",
"True",
",",
"join_key_value",
"=",
"True",
",",
"excluded_keys",
"=",
"excluded_options",
... | Format a Context object to HTTP Prompt commands. | [
"Format",
"a",
"Context",
"object",
"to",
"HTTP",
"Prompt",
"commands",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/context/transform.py#L108-L114 |
236,852 | eliangcs/http-prompt | http_prompt/config.py | initialize | def initialize():
"""Initialize a default config file if it doesn't exist yet.
Returns:
tuple: A tuple of (copied, dst_path). `copied` is a bool indicating if
this function created the default config file. `dst_path` is the
path of the user config file.
"""
dst_path = get_user_config_path()
copied = False
if not os.path.exists(dst_path):
src_path = os.path.join(os.path.dirname(__file__), 'defaultconfig.py')
shutil.copyfile(src_path, dst_path)
copied = True
return copied, dst_path | python | def initialize():
dst_path = get_user_config_path()
copied = False
if not os.path.exists(dst_path):
src_path = os.path.join(os.path.dirname(__file__), 'defaultconfig.py')
shutil.copyfile(src_path, dst_path)
copied = True
return copied, dst_path | [
"def",
"initialize",
"(",
")",
":",
"dst_path",
"=",
"get_user_config_path",
"(",
")",
"copied",
"=",
"False",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"dst_path",
")",
":",
"src_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
... | Initialize a default config file if it doesn't exist yet.
Returns:
tuple: A tuple of (copied, dst_path). `copied` is a bool indicating if
this function created the default config file. `dst_path` is the
path of the user config file. | [
"Initialize",
"a",
"default",
"config",
"file",
"if",
"it",
"doesn",
"t",
"exist",
"yet",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/config.py#L17-L31 |
236,853 | eliangcs/http-prompt | http_prompt/config.py | load_user | def load_user():
"""Read user config file and return it as a dict."""
config_path = get_user_config_path()
config = {}
# TODO: This may be overkill and too slow just for reading a config file
with open(config_path) as f:
code = compile(f.read(), config_path, 'exec')
exec(code, config)
keys = list(six.iterkeys(config))
for k in keys:
if k.startswith('_'):
del config[k]
return config | python | def load_user():
config_path = get_user_config_path()
config = {}
# TODO: This may be overkill and too slow just for reading a config file
with open(config_path) as f:
code = compile(f.read(), config_path, 'exec')
exec(code, config)
keys = list(six.iterkeys(config))
for k in keys:
if k.startswith('_'):
del config[k]
return config | [
"def",
"load_user",
"(",
")",
":",
"config_path",
"=",
"get_user_config_path",
"(",
")",
"config",
"=",
"{",
"}",
"# TODO: This may be overkill and too slow just for reading a config file",
"with",
"open",
"(",
"config_path",
")",
"as",
"f",
":",
"code",
"=",
"compi... | Read user config file and return it as a dict. | [
"Read",
"user",
"config",
"file",
"and",
"return",
"it",
"as",
"a",
"dict",
"."
] | 189321f25e3526fa1b79a9dc38c317892c478986 | https://github.com/eliangcs/http-prompt/blob/189321f25e3526fa1b79a9dc38c317892c478986/http_prompt/config.py#L48-L63 |
236,854 | bcbio/bcbio-nextgen | bcbio/bam/fastq.py | filter_single_reads_by_length | def filter_single_reads_by_length(in_file, quality_format, min_length=20,
out_file=None):
"""
removes reads from a fastq file which are shorter than a minimum
length
"""
logger.info("Removing reads in %s thare are less than %d bases."
% (in_file, min_length))
in_iterator = SeqIO.parse(in_file, quality_format)
out_iterator = (record for record in in_iterator if
len(record.seq) > min_length)
with file_transaction(out_file) as tmp_out_file:
with open(tmp_out_file, "w") as out_handle:
SeqIO.write(out_iterator, out_handle, quality_format)
return out_file | python | def filter_single_reads_by_length(in_file, quality_format, min_length=20,
out_file=None):
logger.info("Removing reads in %s thare are less than %d bases."
% (in_file, min_length))
in_iterator = SeqIO.parse(in_file, quality_format)
out_iterator = (record for record in in_iterator if
len(record.seq) > min_length)
with file_transaction(out_file) as tmp_out_file:
with open(tmp_out_file, "w") as out_handle:
SeqIO.write(out_iterator, out_handle, quality_format)
return out_file | [
"def",
"filter_single_reads_by_length",
"(",
"in_file",
",",
"quality_format",
",",
"min_length",
"=",
"20",
",",
"out_file",
"=",
"None",
")",
":",
"logger",
".",
"info",
"(",
"\"Removing reads in %s thare are less than %d bases.\"",
"%",
"(",
"in_file",
",",
"min_... | removes reads from a fastq file which are shorter than a minimum
length | [
"removes",
"reads",
"from",
"a",
"fastq",
"file",
"which",
"are",
"shorter",
"than",
"a",
"minimum",
"length"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/fastq.py#L40-L55 |
236,855 | bcbio/bcbio-nextgen | bcbio/bam/fastq.py | filter_reads_by_length | def filter_reads_by_length(fq1, fq2, quality_format, min_length=20):
"""
removes reads from a pair of fastq files that are shorter than
a minimum length. removes both ends of a read if one end falls
below the threshold while maintaining the order of the reads
"""
logger.info("Removing reads in %s and %s that "
"are less than %d bases." % (fq1, fq2, min_length))
fq1_out = utils.append_stem(fq1, ".fixed")
fq2_out = utils.append_stem(fq2, ".fixed")
fq1_single = utils.append_stem(fq1, ".singles")
fq2_single = utils.append_stem(fq2, ".singles")
if all(map(utils.file_exists, [fq1_out, fq2_out, fq2_single, fq2_single])):
return [fq1_out, fq2_out]
fq1_in = SeqIO.parse(fq1, quality_format)
fq2_in = SeqIO.parse(fq2, quality_format)
out_files = [fq1_out, fq2_out, fq1_single, fq2_single]
with file_transaction(out_files) as tmp_out_files:
fq1_out_handle = open(tmp_out_files[0], "w")
fq2_out_handle = open(tmp_out_files[1], "w")
fq1_single_handle = open(tmp_out_files[2], "w")
fq2_single_handle = open(tmp_out_files[3], "w")
for fq1_record, fq2_record in zip(fq1_in, fq2_in):
if len(fq1_record.seq) >= min_length and len(fq2_record.seq) >= min_length:
fq1_out_handle.write(fq1_record.format(quality_format))
fq2_out_handle.write(fq2_record.format(quality_format))
else:
if len(fq1_record.seq) > min_length:
fq1_single_handle.write(fq1_record.format(quality_format))
if len(fq2_record.seq) > min_length:
fq2_single_handle.write(fq2_record.format(quality_format))
fq1_out_handle.close()
fq2_out_handle.close()
fq1_single_handle.close()
fq2_single_handle.close()
return [fq1_out, fq2_out] | python | def filter_reads_by_length(fq1, fq2, quality_format, min_length=20):
logger.info("Removing reads in %s and %s that "
"are less than %d bases." % (fq1, fq2, min_length))
fq1_out = utils.append_stem(fq1, ".fixed")
fq2_out = utils.append_stem(fq2, ".fixed")
fq1_single = utils.append_stem(fq1, ".singles")
fq2_single = utils.append_stem(fq2, ".singles")
if all(map(utils.file_exists, [fq1_out, fq2_out, fq2_single, fq2_single])):
return [fq1_out, fq2_out]
fq1_in = SeqIO.parse(fq1, quality_format)
fq2_in = SeqIO.parse(fq2, quality_format)
out_files = [fq1_out, fq2_out, fq1_single, fq2_single]
with file_transaction(out_files) as tmp_out_files:
fq1_out_handle = open(tmp_out_files[0], "w")
fq2_out_handle = open(tmp_out_files[1], "w")
fq1_single_handle = open(tmp_out_files[2], "w")
fq2_single_handle = open(tmp_out_files[3], "w")
for fq1_record, fq2_record in zip(fq1_in, fq2_in):
if len(fq1_record.seq) >= min_length and len(fq2_record.seq) >= min_length:
fq1_out_handle.write(fq1_record.format(quality_format))
fq2_out_handle.write(fq2_record.format(quality_format))
else:
if len(fq1_record.seq) > min_length:
fq1_single_handle.write(fq1_record.format(quality_format))
if len(fq2_record.seq) > min_length:
fq2_single_handle.write(fq2_record.format(quality_format))
fq1_out_handle.close()
fq2_out_handle.close()
fq1_single_handle.close()
fq2_single_handle.close()
return [fq1_out, fq2_out] | [
"def",
"filter_reads_by_length",
"(",
"fq1",
",",
"fq2",
",",
"quality_format",
",",
"min_length",
"=",
"20",
")",
":",
"logger",
".",
"info",
"(",
"\"Removing reads in %s and %s that \"",
"\"are less than %d bases.\"",
"%",
"(",
"fq1",
",",
"fq2",
",",
"min_lengt... | removes reads from a pair of fastq files that are shorter than
a minimum length. removes both ends of a read if one end falls
below the threshold while maintaining the order of the reads | [
"removes",
"reads",
"from",
"a",
"pair",
"of",
"fastq",
"files",
"that",
"are",
"shorter",
"than",
"a",
"minimum",
"length",
".",
"removes",
"both",
"ends",
"of",
"a",
"read",
"if",
"one",
"end",
"falls",
"below",
"the",
"threshold",
"while",
"maintaining"... | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/fastq.py#L57-L99 |
236,856 | bcbio/bcbio-nextgen | bcbio/bam/fastq.py | rstrip_extra | def rstrip_extra(fname):
"""Strip extraneous, non-discriminative filename info from the end of a file.
"""
to_strip = ("_R", ".R", "-R", "_", "fastq", ".", "-")
while fname.endswith(to_strip):
for x in to_strip:
if fname.endswith(x):
fname = fname[:len(fname) - len(x)]
break
return fname | python | def rstrip_extra(fname):
to_strip = ("_R", ".R", "-R", "_", "fastq", ".", "-")
while fname.endswith(to_strip):
for x in to_strip:
if fname.endswith(x):
fname = fname[:len(fname) - len(x)]
break
return fname | [
"def",
"rstrip_extra",
"(",
"fname",
")",
":",
"to_strip",
"=",
"(",
"\"_R\"",
",",
"\".R\"",
",",
"\"-R\"",
",",
"\"_\"",
",",
"\"fastq\"",
",",
"\".\"",
",",
"\"-\"",
")",
"while",
"fname",
".",
"endswith",
"(",
"to_strip",
")",
":",
"for",
"x",
"i... | Strip extraneous, non-discriminative filename info from the end of a file. | [
"Strip",
"extraneous",
"non",
"-",
"discriminative",
"filename",
"info",
"from",
"the",
"end",
"of",
"a",
"file",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/fastq.py#L101-L110 |
236,857 | bcbio/bcbio-nextgen | bcbio/bam/fastq.py | fast_combine_pairs | def fast_combine_pairs(files, force_single, full_name, separators):
"""
assume files that need to be paired are within 10 entries of each other, once the list is sorted
"""
files = sort_filenames(files)
chunks = tz.sliding_window(10, files)
pairs = [combine_pairs(chunk, force_single, full_name, separators) for chunk in chunks]
pairs = [y for x in pairs for y in x]
longest = defaultdict(list)
# for each file, save the longest pair it is in
for pair in pairs:
for file in pair:
if len(longest[file]) < len(pair):
longest[file] = pair
# keep only unique pairs
longest = {tuple(sort_filenames(x)) for x in longest.values()}
# ensure filenames are R1 followed by R2
return [sort_filenames(list(x)) for x in longest] | python | def fast_combine_pairs(files, force_single, full_name, separators):
files = sort_filenames(files)
chunks = tz.sliding_window(10, files)
pairs = [combine_pairs(chunk, force_single, full_name, separators) for chunk in chunks]
pairs = [y for x in pairs for y in x]
longest = defaultdict(list)
# for each file, save the longest pair it is in
for pair in pairs:
for file in pair:
if len(longest[file]) < len(pair):
longest[file] = pair
# keep only unique pairs
longest = {tuple(sort_filenames(x)) for x in longest.values()}
# ensure filenames are R1 followed by R2
return [sort_filenames(list(x)) for x in longest] | [
"def",
"fast_combine_pairs",
"(",
"files",
",",
"force_single",
",",
"full_name",
",",
"separators",
")",
":",
"files",
"=",
"sort_filenames",
"(",
"files",
")",
"chunks",
"=",
"tz",
".",
"sliding_window",
"(",
"10",
",",
"files",
")",
"pairs",
"=",
"[",
... | assume files that need to be paired are within 10 entries of each other, once the list is sorted | [
"assume",
"files",
"that",
"need",
"to",
"be",
"paired",
"are",
"within",
"10",
"entries",
"of",
"each",
"other",
"once",
"the",
"list",
"is",
"sorted"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/fastq.py#L187-L204 |
236,858 | bcbio/bcbio-nextgen | bcbio/bam/fastq.py | open_fastq | def open_fastq(in_file):
""" open a fastq file, using gzip if it is gzipped
"""
if objectstore.is_remote(in_file):
return objectstore.open_file(in_file)
else:
return utils.open_gzipsafe(in_file) | python | def open_fastq(in_file):
if objectstore.is_remote(in_file):
return objectstore.open_file(in_file)
else:
return utils.open_gzipsafe(in_file) | [
"def",
"open_fastq",
"(",
"in_file",
")",
":",
"if",
"objectstore",
".",
"is_remote",
"(",
"in_file",
")",
":",
"return",
"objectstore",
".",
"open_file",
"(",
"in_file",
")",
"else",
":",
"return",
"utils",
".",
"open_gzipsafe",
"(",
"in_file",
")"
] | open a fastq file, using gzip if it is gzipped | [
"open",
"a",
"fastq",
"file",
"using",
"gzip",
"if",
"it",
"is",
"gzipped"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/fastq.py#L309-L315 |
236,859 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | get_region_bed | def get_region_bed(region, items, out_file, want_gzip=True):
"""Retrieve BED file of regions to analyze, either single or multi-region.
"""
variant_regions = bedutils.population_variant_regions(items, merged=True)
target = shared.subset_variant_regions(variant_regions, region, out_file, items)
if not target:
raise ValueError("Need BED input for strelka2 regions: %s %s" % (region, target))
if not isinstance(target, six.string_types) or not os.path.isfile(target):
chrom, start, end = target
target = "%s-regions.bed" % utils.splitext_plus(out_file)[0]
with file_transaction(items[0], target) as tx_out_file:
with open(tx_out_file, "w") as out_handle:
out_handle.write("%s\t%s\t%s\n" % (chrom, start, end))
out_file = target
if want_gzip:
out_file = vcfutils.bgzip_and_index(out_file, items[0]["config"])
return out_file | python | def get_region_bed(region, items, out_file, want_gzip=True):
variant_regions = bedutils.population_variant_regions(items, merged=True)
target = shared.subset_variant_regions(variant_regions, region, out_file, items)
if not target:
raise ValueError("Need BED input for strelka2 regions: %s %s" % (region, target))
if not isinstance(target, six.string_types) or not os.path.isfile(target):
chrom, start, end = target
target = "%s-regions.bed" % utils.splitext_plus(out_file)[0]
with file_transaction(items[0], target) as tx_out_file:
with open(tx_out_file, "w") as out_handle:
out_handle.write("%s\t%s\t%s\n" % (chrom, start, end))
out_file = target
if want_gzip:
out_file = vcfutils.bgzip_and_index(out_file, items[0]["config"])
return out_file | [
"def",
"get_region_bed",
"(",
"region",
",",
"items",
",",
"out_file",
",",
"want_gzip",
"=",
"True",
")",
":",
"variant_regions",
"=",
"bedutils",
".",
"population_variant_regions",
"(",
"items",
",",
"merged",
"=",
"True",
")",
"target",
"=",
"shared",
"."... | Retrieve BED file of regions to analyze, either single or multi-region. | [
"Retrieve",
"BED",
"file",
"of",
"regions",
"to",
"analyze",
"either",
"single",
"or",
"multi",
"-",
"region",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L36-L52 |
236,860 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | coverage_interval_from_bed | def coverage_interval_from_bed(bed_file, per_chrom=True):
"""Calculate a coverage interval for the current region BED.
This helps correctly work with cases of uneven coverage across an analysis
genome. strelka2 and other model based callers have flags for targeted and non
which depend on the local context.
Checks coverage per chromosome, avoiding non-standard chromosomes, if per_chrom is set.
Otherwise does a global check over all regions. The global check performs better for
strelka2 but not for DeepVariant:
https://github.com/bcbio/bcbio_validations/tree/master/deepvariant#deepvariant-v06-release-strelka2-stratification-and-initial-gatk-cnn
"""
total_starts = {}
total_ends = {}
bed_bases = collections.defaultdict(int)
with utils.open_gzipsafe(bed_file) as in_handle:
for line in in_handle:
parts = line.split()
if len(parts) >= 3:
chrom, start, end = parts[:3]
if chromhacks.is_autosomal(chrom):
start = int(start)
end = int(end)
bed_bases[chrom] += (end - start)
total_starts[chrom] = min([start, total_starts.get(chrom, sys.maxsize)])
total_ends[chrom] = max([end, total_ends.get(chrom, 0)])
# can check per chromosome -- any one chromosome with larger, or over all regions
if per_chrom:
freqs = [float(bed_bases[c]) / float(total_ends[c] - total_starts[c]) for c in sorted(bed_bases.keys())]
elif len(bed_bases) > 0:
freqs = [sum([bed_bases[c] for c in sorted(bed_bases.keys())]) /
sum([float(total_ends[c] - total_starts[c]) for c in sorted(bed_bases.keys())])]
else:
freqs = []
# Should be importing GENOME_COV_THRESH but get circular imports
if any([f >= 0.40 for f in freqs]):
return "genome"
else:
return "targeted" | python | def coverage_interval_from_bed(bed_file, per_chrom=True):
total_starts = {}
total_ends = {}
bed_bases = collections.defaultdict(int)
with utils.open_gzipsafe(bed_file) as in_handle:
for line in in_handle:
parts = line.split()
if len(parts) >= 3:
chrom, start, end = parts[:3]
if chromhacks.is_autosomal(chrom):
start = int(start)
end = int(end)
bed_bases[chrom] += (end - start)
total_starts[chrom] = min([start, total_starts.get(chrom, sys.maxsize)])
total_ends[chrom] = max([end, total_ends.get(chrom, 0)])
# can check per chromosome -- any one chromosome with larger, or over all regions
if per_chrom:
freqs = [float(bed_bases[c]) / float(total_ends[c] - total_starts[c]) for c in sorted(bed_bases.keys())]
elif len(bed_bases) > 0:
freqs = [sum([bed_bases[c] for c in sorted(bed_bases.keys())]) /
sum([float(total_ends[c] - total_starts[c]) for c in sorted(bed_bases.keys())])]
else:
freqs = []
# Should be importing GENOME_COV_THRESH but get circular imports
if any([f >= 0.40 for f in freqs]):
return "genome"
else:
return "targeted" | [
"def",
"coverage_interval_from_bed",
"(",
"bed_file",
",",
"per_chrom",
"=",
"True",
")",
":",
"total_starts",
"=",
"{",
"}",
"total_ends",
"=",
"{",
"}",
"bed_bases",
"=",
"collections",
".",
"defaultdict",
"(",
"int",
")",
"with",
"utils",
".",
"open_gzips... | Calculate a coverage interval for the current region BED.
This helps correctly work with cases of uneven coverage across an analysis
genome. strelka2 and other model based callers have flags for targeted and non
which depend on the local context.
Checks coverage per chromosome, avoiding non-standard chromosomes, if per_chrom is set.
Otherwise does a global check over all regions. The global check performs better for
strelka2 but not for DeepVariant:
https://github.com/bcbio/bcbio_validations/tree/master/deepvariant#deepvariant-v06-release-strelka2-stratification-and-initial-gatk-cnn | [
"Calculate",
"a",
"coverage",
"interval",
"for",
"the",
"current",
"region",
"BED",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L54-L93 |
236,861 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | _is_targeted_region | def _is_targeted_region(cur_bed, data):
"""Calculate if we should process region as a targeted or WGS.
Currently always based on total coverage interval, as that validates best and
is consistent between CWL (larger blocks) and non-CWL runs (smaller blocks).
We can check core usage and provide a consistent report when moving to CWL
exclusively.
"""
cores = dd.get_num_cores(data)
if cores > 0: # Apply to all core setups now for consistency
return dd.get_coverage_interval(data) not in ["genome"]
else:
return coverage_interval_from_bed(cur_bed, per_chrom=False) == "targeted" | python | def _is_targeted_region(cur_bed, data):
cores = dd.get_num_cores(data)
if cores > 0: # Apply to all core setups now for consistency
return dd.get_coverage_interval(data) not in ["genome"]
else:
return coverage_interval_from_bed(cur_bed, per_chrom=False) == "targeted" | [
"def",
"_is_targeted_region",
"(",
"cur_bed",
",",
"data",
")",
":",
"cores",
"=",
"dd",
".",
"get_num_cores",
"(",
"data",
")",
"if",
"cores",
">",
"0",
":",
"# Apply to all core setups now for consistency",
"return",
"dd",
".",
"get_coverage_interval",
"(",
"d... | Calculate if we should process region as a targeted or WGS.
Currently always based on total coverage interval, as that validates best and
is consistent between CWL (larger blocks) and non-CWL runs (smaller blocks).
We can check core usage and provide a consistent report when moving to CWL
exclusively. | [
"Calculate",
"if",
"we",
"should",
"process",
"region",
"as",
"a",
"targeted",
"or",
"WGS",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L95-L107 |
236,862 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | _postprocess_somatic | def _postprocess_somatic(in_file, paired):
"""Post-process somatic calls to provide standard output.
- Converts SGT and NT into standard VCF GT fields
- Replace generic TUMOR NORMAL names in VCF with sample names.
"""
out_file = in_file.replace(".vcf.gz", "-fixed.vcf")
if not utils.file_exists(out_file) and not utils.file_exists(out_file + ".gz"):
with file_transaction(paired.tumor_data, out_file) as tx_out_file:
with utils.open_gzipsafe(in_file) as in_handle:
with open(tx_out_file, "w") as out_handle:
added_gt = False
normal_index, tumor_index = (None, None)
for line in in_handle:
if line.startswith("##FORMAT") and not added_gt:
added_gt = True
out_handle.write('##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">\n')
out_handle.write(line)
elif line.startswith("#CHROM"):
assert added_gt
parts = line.strip().split("\t")
normal_index = parts.index("NORMAL")
tumor_index = parts.index("TUMOR")
line = line.replace("NORMAL", paired.normal_name).replace("TUMOR", paired.tumor_name)
out_handle.write(line)
elif line.startswith("#"):
out_handle.write(line)
else:
parts = line.rstrip().split("\t")
tumor_gt, normal_gt = _tumor_normal_genotypes(parts[3], parts[4].split(","),
parts[7].split(";"), in_file, parts[:2])
parts[8] = "GT:%s" % parts[8]
parts[normal_index] = "%s:%s" % (normal_gt, parts[normal_index])
parts[tumor_index] = "%s:%s" % (tumor_gt, parts[tumor_index])
out_handle.write("\t".join(parts) + "\n")
return vcfutils.bgzip_and_index(out_file, paired.tumor_data["config"]) | python | def _postprocess_somatic(in_file, paired):
out_file = in_file.replace(".vcf.gz", "-fixed.vcf")
if not utils.file_exists(out_file) and not utils.file_exists(out_file + ".gz"):
with file_transaction(paired.tumor_data, out_file) as tx_out_file:
with utils.open_gzipsafe(in_file) as in_handle:
with open(tx_out_file, "w") as out_handle:
added_gt = False
normal_index, tumor_index = (None, None)
for line in in_handle:
if line.startswith("##FORMAT") and not added_gt:
added_gt = True
out_handle.write('##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">\n')
out_handle.write(line)
elif line.startswith("#CHROM"):
assert added_gt
parts = line.strip().split("\t")
normal_index = parts.index("NORMAL")
tumor_index = parts.index("TUMOR")
line = line.replace("NORMAL", paired.normal_name).replace("TUMOR", paired.tumor_name)
out_handle.write(line)
elif line.startswith("#"):
out_handle.write(line)
else:
parts = line.rstrip().split("\t")
tumor_gt, normal_gt = _tumor_normal_genotypes(parts[3], parts[4].split(","),
parts[7].split(";"), in_file, parts[:2])
parts[8] = "GT:%s" % parts[8]
parts[normal_index] = "%s:%s" % (normal_gt, parts[normal_index])
parts[tumor_index] = "%s:%s" % (tumor_gt, parts[tumor_index])
out_handle.write("\t".join(parts) + "\n")
return vcfutils.bgzip_and_index(out_file, paired.tumor_data["config"]) | [
"def",
"_postprocess_somatic",
"(",
"in_file",
",",
"paired",
")",
":",
"out_file",
"=",
"in_file",
".",
"replace",
"(",
"\".vcf.gz\"",
",",
"\"-fixed.vcf\"",
")",
"if",
"not",
"utils",
".",
"file_exists",
"(",
"out_file",
")",
"and",
"not",
"utils",
".",
... | Post-process somatic calls to provide standard output.
- Converts SGT and NT into standard VCF GT fields
- Replace generic TUMOR NORMAL names in VCF with sample names. | [
"Post",
"-",
"process",
"somatic",
"calls",
"to",
"provide",
"standard",
"output",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L272-L307 |
236,863 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | _run_workflow | def _run_workflow(data, workflow_file, work_dir):
"""Run Strelka2 analysis inside prepared workflow directory.
"""
utils.remove_safe(os.path.join(work_dir, "workspace"))
cmd = [utils.get_program_python("configureStrelkaGermlineWorkflow.py"),
workflow_file, "-m", "local", "-j", dd.get_num_cores(data), "--quiet"]
do.run(cmd, "Run Strelka2: %s" % dd.get_sample_name(data))
utils.remove_safe(os.path.join(work_dir, "workspace")) | python | def _run_workflow(data, workflow_file, work_dir):
utils.remove_safe(os.path.join(work_dir, "workspace"))
cmd = [utils.get_program_python("configureStrelkaGermlineWorkflow.py"),
workflow_file, "-m", "local", "-j", dd.get_num_cores(data), "--quiet"]
do.run(cmd, "Run Strelka2: %s" % dd.get_sample_name(data))
utils.remove_safe(os.path.join(work_dir, "workspace")) | [
"def",
"_run_workflow",
"(",
"data",
",",
"workflow_file",
",",
"work_dir",
")",
":",
"utils",
".",
"remove_safe",
"(",
"os",
".",
"path",
".",
"join",
"(",
"work_dir",
",",
"\"workspace\"",
")",
")",
"cmd",
"=",
"[",
"utils",
".",
"get_program_python",
... | Run Strelka2 analysis inside prepared workflow directory. | [
"Run",
"Strelka2",
"analysis",
"inside",
"prepared",
"workflow",
"directory",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L320-L327 |
236,864 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | run_gvcfgenotyper | def run_gvcfgenotyper(data, orig_region, vrn_files, out_file):
"""Merge strelka2 and Illumina compatible gVCFs with gvcfgenotyper.
https://github.com/Illumina/gvcfgenotyper
Also need to explore GLnexus (https://github.com/dnanexus-rnd/GLnexus)
"""
if not utils.file_exists(out_file):
with file_transaction(data, out_file) as tx_out_file:
regions = _find_gvcf_blocks(vrn_files[0], bamprep.region_to_gatk(orig_region),
os.path.dirname(tx_out_file))
if len(regions) == 1:
_run_gvcfgenotyper(data, regions[0], vrn_files, tx_out_file)
else:
split_outs = [_run_gvcfgenotyper(data, r, vrn_files,
"%s-%s.vcf.gz" % (utils.splitext_plus(out_file)[0],
r.replace(":", "_").replace("-", "_")))
for r in regions]
vcfutils.concat_variant_files(split_outs, tx_out_file, regions,
dd.get_ref_file(data), data["config"])
return vcfutils.bgzip_and_index(out_file, data["config"]) | python | def run_gvcfgenotyper(data, orig_region, vrn_files, out_file):
if not utils.file_exists(out_file):
with file_transaction(data, out_file) as tx_out_file:
regions = _find_gvcf_blocks(vrn_files[0], bamprep.region_to_gatk(orig_region),
os.path.dirname(tx_out_file))
if len(regions) == 1:
_run_gvcfgenotyper(data, regions[0], vrn_files, tx_out_file)
else:
split_outs = [_run_gvcfgenotyper(data, r, vrn_files,
"%s-%s.vcf.gz" % (utils.splitext_plus(out_file)[0],
r.replace(":", "_").replace("-", "_")))
for r in regions]
vcfutils.concat_variant_files(split_outs, tx_out_file, regions,
dd.get_ref_file(data), data["config"])
return vcfutils.bgzip_and_index(out_file, data["config"]) | [
"def",
"run_gvcfgenotyper",
"(",
"data",
",",
"orig_region",
",",
"vrn_files",
",",
"out_file",
")",
":",
"if",
"not",
"utils",
".",
"file_exists",
"(",
"out_file",
")",
":",
"with",
"file_transaction",
"(",
"data",
",",
"out_file",
")",
"as",
"tx_out_file",... | Merge strelka2 and Illumina compatible gVCFs with gvcfgenotyper.
https://github.com/Illumina/gvcfgenotyper
Also need to explore GLnexus (https://github.com/dnanexus-rnd/GLnexus) | [
"Merge",
"strelka2",
"and",
"Illumina",
"compatible",
"gVCFs",
"with",
"gvcfgenotyper",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L331-L351 |
236,865 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | _run_gvcfgenotyper | def _run_gvcfgenotyper(data, region, vrn_files, out_file):
"""Run gvcfgenotyper on a single gVCF region in input file.
"""
if not utils.file_exists(out_file):
with file_transaction(data, out_file) as tx_out_file:
input_file = "%s-inputs.txt" % utils.splitext_plus(tx_out_file)[0]
with open(input_file, "w") as out_handle:
out_handle.write("%s\n" % "\n".join(vrn_files))
cmd = ["gvcfgenotyper", "-f", dd.get_ref_file(data), "-l", input_file,
"-r", region, "-O", "z", "-o", tx_out_file]
do.run(cmd, "gvcfgenotyper: %s %s" % (dd.get_sample_name(data), region))
return out_file | python | def _run_gvcfgenotyper(data, region, vrn_files, out_file):
if not utils.file_exists(out_file):
with file_transaction(data, out_file) as tx_out_file:
input_file = "%s-inputs.txt" % utils.splitext_plus(tx_out_file)[0]
with open(input_file, "w") as out_handle:
out_handle.write("%s\n" % "\n".join(vrn_files))
cmd = ["gvcfgenotyper", "-f", dd.get_ref_file(data), "-l", input_file,
"-r", region, "-O", "z", "-o", tx_out_file]
do.run(cmd, "gvcfgenotyper: %s %s" % (dd.get_sample_name(data), region))
return out_file | [
"def",
"_run_gvcfgenotyper",
"(",
"data",
",",
"region",
",",
"vrn_files",
",",
"out_file",
")",
":",
"if",
"not",
"utils",
".",
"file_exists",
"(",
"out_file",
")",
":",
"with",
"file_transaction",
"(",
"data",
",",
"out_file",
")",
"as",
"tx_out_file",
"... | Run gvcfgenotyper on a single gVCF region in input file. | [
"Run",
"gvcfgenotyper",
"on",
"a",
"single",
"gVCF",
"region",
"in",
"input",
"file",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L353-L364 |
236,866 | bcbio/bcbio-nextgen | bcbio/variation/strelka2.py | _find_gvcf_blocks | def _find_gvcf_blocks(vcf_file, region, tmp_dir):
"""Retrieve gVCF blocks within our current evaluation region.
gvcfgenotyper does not support calling larger regions with individual
coverage blocks, so we split our big region into potentially multiple.
"""
region_file = os.path.join(tmp_dir, "cur_region.bed")
with open(region_file, "w") as out_handle:
chrom, coords = region.split(":")
start, end = coords.split("-")
out_handle.write("\t".join([chrom, start, end]) + "\n")
final_file = os.path.join(tmp_dir, "split_regions.bed")
cmd = "gvcf_regions.py {vcf_file} | bedtools intersect -a - -b {region_file} > {final_file}"
do.run(cmd.format(**locals()))
regions = []
with open(final_file) as in_handle:
for line in in_handle:
chrom, start, end = line.strip().split("\t")
regions.append("%s:%s-%s" % (chrom, start, end))
return regions | python | def _find_gvcf_blocks(vcf_file, region, tmp_dir):
region_file = os.path.join(tmp_dir, "cur_region.bed")
with open(region_file, "w") as out_handle:
chrom, coords = region.split(":")
start, end = coords.split("-")
out_handle.write("\t".join([chrom, start, end]) + "\n")
final_file = os.path.join(tmp_dir, "split_regions.bed")
cmd = "gvcf_regions.py {vcf_file} | bedtools intersect -a - -b {region_file} > {final_file}"
do.run(cmd.format(**locals()))
regions = []
with open(final_file) as in_handle:
for line in in_handle:
chrom, start, end = line.strip().split("\t")
regions.append("%s:%s-%s" % (chrom, start, end))
return regions | [
"def",
"_find_gvcf_blocks",
"(",
"vcf_file",
",",
"region",
",",
"tmp_dir",
")",
":",
"region_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"tmp_dir",
",",
"\"cur_region.bed\"",
")",
"with",
"open",
"(",
"region_file",
",",
"\"w\"",
")",
"as",
"out_hand... | Retrieve gVCF blocks within our current evaluation region.
gvcfgenotyper does not support calling larger regions with individual
coverage blocks, so we split our big region into potentially multiple. | [
"Retrieve",
"gVCF",
"blocks",
"within",
"our",
"current",
"evaluation",
"region",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/variation/strelka2.py#L366-L385 |
236,867 | bcbio/bcbio-nextgen | bcbio/hla/__init__.py | run | def run(samples, run_parallel):
"""Run HLA detection on the input samples.
"""
to_process = []
extras = []
for data in (xs[0] for xs in samples):
hlacaller = tz.get_in(["config", "algorithm", "hlacaller"], data)
if hlacaller:
to_process.append(data)
else:
extras.append([data])
processed = run_parallel("call_hla", ([x] for x in to_process))
return extras + processed | python | def run(samples, run_parallel):
to_process = []
extras = []
for data in (xs[0] for xs in samples):
hlacaller = tz.get_in(["config", "algorithm", "hlacaller"], data)
if hlacaller:
to_process.append(data)
else:
extras.append([data])
processed = run_parallel("call_hla", ([x] for x in to_process))
return extras + processed | [
"def",
"run",
"(",
"samples",
",",
"run_parallel",
")",
":",
"to_process",
"=",
"[",
"]",
"extras",
"=",
"[",
"]",
"for",
"data",
"in",
"(",
"xs",
"[",
"0",
"]",
"for",
"xs",
"in",
"samples",
")",
":",
"hlacaller",
"=",
"tz",
".",
"get_in",
"(",
... | Run HLA detection on the input samples. | [
"Run",
"HLA",
"detection",
"on",
"the",
"input",
"samples",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/hla/__init__.py#L20-L32 |
236,868 | bcbio/bcbio-nextgen | bcbio/ngsalign/bwa.py | align_bam | def align_bam(in_bam, ref_file, names, align_dir, data):
"""Perform direct alignment of an input BAM file with BWA using pipes.
This avoids disk IO by piping between processes:
- samtools sort of input BAM to queryname
- bedtools conversion to interleaved FASTQ
- bwa-mem alignment
- samtools conversion to BAM
- samtools sort to coordinate
"""
config = data["config"]
out_file = os.path.join(align_dir, "{0}-sort.bam".format(names["lane"]))
samtools = config_utils.get_program("samtools", config)
bedtools = config_utils.get_program("bedtools", config)
resources = config_utils.get_resources("samtools", config)
num_cores = config["algorithm"].get("num_cores", 1)
# adjust memory for samtools since used for input and output
max_mem = config_utils.adjust_memory(resources.get("memory", "1G"),
3, "decrease").upper()
if not utils.file_exists(out_file):
with tx_tmpdir(data) as work_dir:
with postalign.tobam_cl(data, out_file, bam.is_paired(in_bam)) as (tobam_cl, tx_out_file):
bwa_cmd = _get_bwa_mem_cmd(data, out_file, ref_file, "-")
tx_out_prefix = os.path.splitext(tx_out_file)[0]
prefix1 = "%s-in1" % tx_out_prefix
cmd = ("unset JAVA_HOME && "
"{samtools} sort -n -o -l 1 -@ {num_cores} -m {max_mem} {in_bam} {prefix1} "
"| {bedtools} bamtofastq -i /dev/stdin -fq /dev/stdout -fq2 /dev/stdout "
"| {bwa_cmd} | ")
cmd = cmd.format(**locals()) + tobam_cl
do.run(cmd, "bwa mem alignment from BAM: %s" % names["sample"], None,
[do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, in_bam)])
return out_file | python | def align_bam(in_bam, ref_file, names, align_dir, data):
config = data["config"]
out_file = os.path.join(align_dir, "{0}-sort.bam".format(names["lane"]))
samtools = config_utils.get_program("samtools", config)
bedtools = config_utils.get_program("bedtools", config)
resources = config_utils.get_resources("samtools", config)
num_cores = config["algorithm"].get("num_cores", 1)
# adjust memory for samtools since used for input and output
max_mem = config_utils.adjust_memory(resources.get("memory", "1G"),
3, "decrease").upper()
if not utils.file_exists(out_file):
with tx_tmpdir(data) as work_dir:
with postalign.tobam_cl(data, out_file, bam.is_paired(in_bam)) as (tobam_cl, tx_out_file):
bwa_cmd = _get_bwa_mem_cmd(data, out_file, ref_file, "-")
tx_out_prefix = os.path.splitext(tx_out_file)[0]
prefix1 = "%s-in1" % tx_out_prefix
cmd = ("unset JAVA_HOME && "
"{samtools} sort -n -o -l 1 -@ {num_cores} -m {max_mem} {in_bam} {prefix1} "
"| {bedtools} bamtofastq -i /dev/stdin -fq /dev/stdout -fq2 /dev/stdout "
"| {bwa_cmd} | ")
cmd = cmd.format(**locals()) + tobam_cl
do.run(cmd, "bwa mem alignment from BAM: %s" % names["sample"], None,
[do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, in_bam)])
return out_file | [
"def",
"align_bam",
"(",
"in_bam",
",",
"ref_file",
",",
"names",
",",
"align_dir",
",",
"data",
")",
":",
"config",
"=",
"data",
"[",
"\"config\"",
"]",
"out_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"align_dir",
",",
"\"{0}-sort.bam\"",
".",
"... | Perform direct alignment of an input BAM file with BWA using pipes.
This avoids disk IO by piping between processes:
- samtools sort of input BAM to queryname
- bedtools conversion to interleaved FASTQ
- bwa-mem alignment
- samtools conversion to BAM
- samtools sort to coordinate | [
"Perform",
"direct",
"alignment",
"of",
"an",
"input",
"BAM",
"file",
"with",
"BWA",
"using",
"pipes",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/ngsalign/bwa.py#L21-L53 |
236,869 | bcbio/bcbio-nextgen | bcbio/ngsalign/bwa.py | _get_bwa_mem_cmd | def _get_bwa_mem_cmd(data, out_file, ref_file, fastq1, fastq2=""):
"""Perform piped bwa mem mapping potentially with alternative alleles in GRCh38 + HLA typing.
Commands for HLA post-processing:
base=TEST
run-HLA $base.hla > $base.hla.top
cat $base.hla.HLA*.gt | grep ^GT | cut -f2- > $base.hla.all
rm -f $base.hla.HLA*gt
rm -f $base.hla.HLA*gz
"""
alt_file = ref_file + ".alt"
if utils.file_exists(alt_file) and dd.get_hlacaller(data):
bwakit_dir = os.path.dirname(os.path.realpath(utils.which("run-bwamem")))
hla_base = os.path.join(utils.safe_makedir(os.path.join(os.path.dirname(out_file), "hla")),
os.path.basename(out_file) + ".hla")
alt_cmd = (" | {bwakit_dir}/k8 {bwakit_dir}/bwa-postalt.js -p {hla_base} {alt_file}")
else:
alt_cmd = ""
if dd.get_aligner(data) == "sentieon-bwa":
bwa_exe = "sentieon-bwa"
exports = sentieon.license_export(data)
else:
bwa_exe = "bwa"
exports = ""
bwa = config_utils.get_program(bwa_exe, data["config"])
num_cores = data["config"]["algorithm"].get("num_cores", 1)
bwa_resources = config_utils.get_resources("bwa", data["config"])
bwa_params = (" ".join([str(x) for x in bwa_resources.get("options", [])])
if "options" in bwa_resources else "")
rg_info = novoalign.get_rg_info(data["rgnames"])
# For UMI runs, pass along consensus tags
c_tags = "-C" if "umi_bam" in data else ""
pairing = "-p" if not fastq2 else ""
# Restrict seed occurances to 1/2 of default, manage memory usage for centromere repeats in hg38
# https://sourceforge.net/p/bio-bwa/mailman/message/31514937/
# http://ehc.ac/p/bio-bwa/mailman/message/32268544/
mem_usage = "-c 250"
bwa_cmd = ("{exports}{bwa} mem {pairing} {c_tags} {mem_usage} -M -t {num_cores} {bwa_params} -R '{rg_info}' "
"-v 1 {ref_file} {fastq1} {fastq2} ")
return (bwa_cmd + alt_cmd).format(**locals()) | python | def _get_bwa_mem_cmd(data, out_file, ref_file, fastq1, fastq2=""):
alt_file = ref_file + ".alt"
if utils.file_exists(alt_file) and dd.get_hlacaller(data):
bwakit_dir = os.path.dirname(os.path.realpath(utils.which("run-bwamem")))
hla_base = os.path.join(utils.safe_makedir(os.path.join(os.path.dirname(out_file), "hla")),
os.path.basename(out_file) + ".hla")
alt_cmd = (" | {bwakit_dir}/k8 {bwakit_dir}/bwa-postalt.js -p {hla_base} {alt_file}")
else:
alt_cmd = ""
if dd.get_aligner(data) == "sentieon-bwa":
bwa_exe = "sentieon-bwa"
exports = sentieon.license_export(data)
else:
bwa_exe = "bwa"
exports = ""
bwa = config_utils.get_program(bwa_exe, data["config"])
num_cores = data["config"]["algorithm"].get("num_cores", 1)
bwa_resources = config_utils.get_resources("bwa", data["config"])
bwa_params = (" ".join([str(x) for x in bwa_resources.get("options", [])])
if "options" in bwa_resources else "")
rg_info = novoalign.get_rg_info(data["rgnames"])
# For UMI runs, pass along consensus tags
c_tags = "-C" if "umi_bam" in data else ""
pairing = "-p" if not fastq2 else ""
# Restrict seed occurances to 1/2 of default, manage memory usage for centromere repeats in hg38
# https://sourceforge.net/p/bio-bwa/mailman/message/31514937/
# http://ehc.ac/p/bio-bwa/mailman/message/32268544/
mem_usage = "-c 250"
bwa_cmd = ("{exports}{bwa} mem {pairing} {c_tags} {mem_usage} -M -t {num_cores} {bwa_params} -R '{rg_info}' "
"-v 1 {ref_file} {fastq1} {fastq2} ")
return (bwa_cmd + alt_cmd).format(**locals()) | [
"def",
"_get_bwa_mem_cmd",
"(",
"data",
",",
"out_file",
",",
"ref_file",
",",
"fastq1",
",",
"fastq2",
"=",
"\"\"",
")",
":",
"alt_file",
"=",
"ref_file",
"+",
"\".alt\"",
"if",
"utils",
".",
"file_exists",
"(",
"alt_file",
")",
"and",
"dd",
".",
"get_h... | Perform piped bwa mem mapping potentially with alternative alleles in GRCh38 + HLA typing.
Commands for HLA post-processing:
base=TEST
run-HLA $base.hla > $base.hla.top
cat $base.hla.HLA*.gt | grep ^GT | cut -f2- > $base.hla.all
rm -f $base.hla.HLA*gt
rm -f $base.hla.HLA*gz | [
"Perform",
"piped",
"bwa",
"mem",
"mapping",
"potentially",
"with",
"alternative",
"alleles",
"in",
"GRCh38",
"+",
"HLA",
"typing",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/ngsalign/bwa.py#L55-L94 |
236,870 | bcbio/bcbio-nextgen | bcbio/ngsalign/bwa.py | _align_mem | def _align_mem(fastq_file, pair_file, ref_file, out_file, names, rg_info, data):
"""Perform bwa-mem alignment on supported read lengths.
"""
with postalign.tobam_cl(data, out_file, pair_file != "") as (tobam_cl, tx_out_file):
cmd = ("unset JAVA_HOME && "
"%s | %s" % (_get_bwa_mem_cmd(data, out_file, ref_file, fastq_file, pair_file), tobam_cl))
do.run(cmd, "bwa mem alignment from fastq: %s" % names["sample"], None,
[do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, fastq_file)])
return out_file | python | def _align_mem(fastq_file, pair_file, ref_file, out_file, names, rg_info, data):
with postalign.tobam_cl(data, out_file, pair_file != "") as (tobam_cl, tx_out_file):
cmd = ("unset JAVA_HOME && "
"%s | %s" % (_get_bwa_mem_cmd(data, out_file, ref_file, fastq_file, pair_file), tobam_cl))
do.run(cmd, "bwa mem alignment from fastq: %s" % names["sample"], None,
[do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, fastq_file)])
return out_file | [
"def",
"_align_mem",
"(",
"fastq_file",
",",
"pair_file",
",",
"ref_file",
",",
"out_file",
",",
"names",
",",
"rg_info",
",",
"data",
")",
":",
"with",
"postalign",
".",
"tobam_cl",
"(",
"data",
",",
"out_file",
",",
"pair_file",
"!=",
"\"\"",
")",
"as"... | Perform bwa-mem alignment on supported read lengths. | [
"Perform",
"bwa",
"-",
"mem",
"alignment",
"on",
"supported",
"read",
"lengths",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/ngsalign/bwa.py#L174-L182 |
236,871 | bcbio/bcbio-nextgen | bcbio/ngsalign/bwa.py | _align_backtrack | def _align_backtrack(fastq_file, pair_file, ref_file, out_file, names, rg_info, data):
"""Perform a BWA alignment using 'aln' backtrack algorithm.
"""
bwa = config_utils.get_program("bwa", data["config"])
config = data["config"]
sai1_file = "%s_1.sai" % os.path.splitext(out_file)[0]
sai2_file = "%s_2.sai" % os.path.splitext(out_file)[0] if pair_file else ""
if not utils.file_exists(sai1_file):
with file_transaction(data, sai1_file) as tx_sai1_file:
_run_bwa_align(fastq_file, ref_file, tx_sai1_file, config)
if sai2_file and not utils.file_exists(sai2_file):
with file_transaction(data, sai2_file) as tx_sai2_file:
_run_bwa_align(pair_file, ref_file, tx_sai2_file, config)
with postalign.tobam_cl(data, out_file, pair_file != "") as (tobam_cl, tx_out_file):
align_type = "sampe" if sai2_file else "samse"
cmd = ("unset JAVA_HOME && {bwa} {align_type} -r '{rg_info}' {ref_file} {sai1_file} {sai2_file} "
"{fastq_file} {pair_file} | ")
cmd = cmd.format(**locals()) + tobam_cl
do.run(cmd, "bwa %s" % align_type, data)
return out_file | python | def _align_backtrack(fastq_file, pair_file, ref_file, out_file, names, rg_info, data):
bwa = config_utils.get_program("bwa", data["config"])
config = data["config"]
sai1_file = "%s_1.sai" % os.path.splitext(out_file)[0]
sai2_file = "%s_2.sai" % os.path.splitext(out_file)[0] if pair_file else ""
if not utils.file_exists(sai1_file):
with file_transaction(data, sai1_file) as tx_sai1_file:
_run_bwa_align(fastq_file, ref_file, tx_sai1_file, config)
if sai2_file and not utils.file_exists(sai2_file):
with file_transaction(data, sai2_file) as tx_sai2_file:
_run_bwa_align(pair_file, ref_file, tx_sai2_file, config)
with postalign.tobam_cl(data, out_file, pair_file != "") as (tobam_cl, tx_out_file):
align_type = "sampe" if sai2_file else "samse"
cmd = ("unset JAVA_HOME && {bwa} {align_type} -r '{rg_info}' {ref_file} {sai1_file} {sai2_file} "
"{fastq_file} {pair_file} | ")
cmd = cmd.format(**locals()) + tobam_cl
do.run(cmd, "bwa %s" % align_type, data)
return out_file | [
"def",
"_align_backtrack",
"(",
"fastq_file",
",",
"pair_file",
",",
"ref_file",
",",
"out_file",
",",
"names",
",",
"rg_info",
",",
"data",
")",
":",
"bwa",
"=",
"config_utils",
".",
"get_program",
"(",
"\"bwa\"",
",",
"data",
"[",
"\"config\"",
"]",
")",... | Perform a BWA alignment using 'aln' backtrack algorithm. | [
"Perform",
"a",
"BWA",
"alignment",
"using",
"aln",
"backtrack",
"algorithm",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/ngsalign/bwa.py#L184-L203 |
236,872 | bcbio/bcbio-nextgen | bcbio/pipeline/main.py | run_main | def run_main(workdir, config_file=None, fc_dir=None, run_info_yaml=None,
parallel=None, workflow=None):
"""Run variant analysis, handling command line options.
"""
# Set environment to standard to use periods for decimals and avoid localization
os.environ["LC_ALL"] = "C"
os.environ["LC"] = "C"
os.environ["LANG"] = "C"
workdir = utils.safe_makedir(os.path.abspath(workdir))
os.chdir(workdir)
config, config_file = config_utils.load_system_config(config_file, workdir)
if config.get("log_dir", None) is None:
config["log_dir"] = os.path.join(workdir, DEFAULT_LOG_DIR)
if parallel["type"] in ["local", "clusterk"]:
_setup_resources()
_run_toplevel(config, config_file, workdir, parallel,
fc_dir, run_info_yaml)
elif parallel["type"] == "ipython":
assert parallel["scheduler"] is not None, "IPython parallel requires a specified scheduler (-s)"
if parallel["scheduler"] != "sge":
assert parallel["queue"] is not None, "IPython parallel requires a specified queue (-q)"
elif not parallel["queue"]:
parallel["queue"] = ""
_run_toplevel(config, config_file, workdir, parallel,
fc_dir, run_info_yaml)
else:
raise ValueError("Unexpected type of parallel run: %s" % parallel["type"]) | python | def run_main(workdir, config_file=None, fc_dir=None, run_info_yaml=None,
parallel=None, workflow=None):
# Set environment to standard to use periods for decimals and avoid localization
os.environ["LC_ALL"] = "C"
os.environ["LC"] = "C"
os.environ["LANG"] = "C"
workdir = utils.safe_makedir(os.path.abspath(workdir))
os.chdir(workdir)
config, config_file = config_utils.load_system_config(config_file, workdir)
if config.get("log_dir", None) is None:
config["log_dir"] = os.path.join(workdir, DEFAULT_LOG_DIR)
if parallel["type"] in ["local", "clusterk"]:
_setup_resources()
_run_toplevel(config, config_file, workdir, parallel,
fc_dir, run_info_yaml)
elif parallel["type"] == "ipython":
assert parallel["scheduler"] is not None, "IPython parallel requires a specified scheduler (-s)"
if parallel["scheduler"] != "sge":
assert parallel["queue"] is not None, "IPython parallel requires a specified queue (-q)"
elif not parallel["queue"]:
parallel["queue"] = ""
_run_toplevel(config, config_file, workdir, parallel,
fc_dir, run_info_yaml)
else:
raise ValueError("Unexpected type of parallel run: %s" % parallel["type"]) | [
"def",
"run_main",
"(",
"workdir",
",",
"config_file",
"=",
"None",
",",
"fc_dir",
"=",
"None",
",",
"run_info_yaml",
"=",
"None",
",",
"parallel",
"=",
"None",
",",
"workflow",
"=",
"None",
")",
":",
"# Set environment to standard to use periods for decimals and ... | Run variant analysis, handling command line options. | [
"Run",
"variant",
"analysis",
"handling",
"command",
"line",
"options",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/pipeline/main.py#L29-L55 |
236,873 | bcbio/bcbio-nextgen | bcbio/pipeline/main.py | _setup_resources | def _setup_resources():
"""Attempt to increase resource limits up to hard limits.
This allows us to avoid out of file handle limits where we can
move beyond the soft limit up to the hard limit.
"""
target_procs = 10240
cur_proc, max_proc = resource.getrlimit(resource.RLIMIT_NPROC)
target_proc = min(max_proc, target_procs) if max_proc > 0 else target_procs
resource.setrlimit(resource.RLIMIT_NPROC, (max(cur_proc, target_proc), max_proc))
cur_hdls, max_hdls = resource.getrlimit(resource.RLIMIT_NOFILE)
target_hdls = min(max_hdls, target_procs) if max_hdls > 0 else target_procs
resource.setrlimit(resource.RLIMIT_NOFILE, (max(cur_hdls, target_hdls), max_hdls)) | python | def _setup_resources():
target_procs = 10240
cur_proc, max_proc = resource.getrlimit(resource.RLIMIT_NPROC)
target_proc = min(max_proc, target_procs) if max_proc > 0 else target_procs
resource.setrlimit(resource.RLIMIT_NPROC, (max(cur_proc, target_proc), max_proc))
cur_hdls, max_hdls = resource.getrlimit(resource.RLIMIT_NOFILE)
target_hdls = min(max_hdls, target_procs) if max_hdls > 0 else target_procs
resource.setrlimit(resource.RLIMIT_NOFILE, (max(cur_hdls, target_hdls), max_hdls)) | [
"def",
"_setup_resources",
"(",
")",
":",
"target_procs",
"=",
"10240",
"cur_proc",
",",
"max_proc",
"=",
"resource",
".",
"getrlimit",
"(",
"resource",
".",
"RLIMIT_NPROC",
")",
"target_proc",
"=",
"min",
"(",
"max_proc",
",",
"target_procs",
")",
"if",
"ma... | Attempt to increase resource limits up to hard limits.
This allows us to avoid out of file handle limits where we can
move beyond the soft limit up to the hard limit. | [
"Attempt",
"to",
"increase",
"resource",
"limits",
"up",
"to",
"hard",
"limits",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/pipeline/main.py#L57-L69 |
236,874 | bcbio/bcbio-nextgen | bcbio/pipeline/main.py | _wres | def _wres(parallel, progs, fresources=None, ensure_mem=None):
"""Add resource information to the parallel environment on required programs and files.
Enables spinning up required machines and operating in non-shared filesystem
environments.
progs -- Third party tools used in processing
fresources -- Required file-based resources needed. These will be transferred on non-shared
filesystems.
ensure_mem -- Dictionary of required minimum memory for programs used. Ensures
enough memory gets allocated on low-core machines.
"""
parallel = copy.deepcopy(parallel)
parallel["progs"] = progs
if fresources:
parallel["fresources"] = fresources
if ensure_mem:
parallel["ensure_mem"] = ensure_mem
return parallel | python | def _wres(parallel, progs, fresources=None, ensure_mem=None):
parallel = copy.deepcopy(parallel)
parallel["progs"] = progs
if fresources:
parallel["fresources"] = fresources
if ensure_mem:
parallel["ensure_mem"] = ensure_mem
return parallel | [
"def",
"_wres",
"(",
"parallel",
",",
"progs",
",",
"fresources",
"=",
"None",
",",
"ensure_mem",
"=",
"None",
")",
":",
"parallel",
"=",
"copy",
".",
"deepcopy",
"(",
"parallel",
")",
"parallel",
"[",
"\"progs\"",
"]",
"=",
"progs",
"if",
"fresources",
... | Add resource information to the parallel environment on required programs and files.
Enables spinning up required machines and operating in non-shared filesystem
environments.
progs -- Third party tools used in processing
fresources -- Required file-based resources needed. These will be transferred on non-shared
filesystems.
ensure_mem -- Dictionary of required minimum memory for programs used. Ensures
enough memory gets allocated on low-core machines. | [
"Add",
"resource",
"information",
"to",
"the",
"parallel",
"environment",
"on",
"required",
"programs",
"and",
"files",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/pipeline/main.py#L94-L112 |
236,875 | bcbio/bcbio-nextgen | bcbio/pipeline/main.py | rnaseq_prep_samples | def rnaseq_prep_samples(config, run_info_yaml, parallel, dirs, samples):
"""
organizes RNA-seq and small-RNAseq samples, converting from BAM if
necessary and trimming if necessary
"""
pipeline = dd.get_in_samples(samples, dd.get_analysis)
trim_reads_set = any([tz.get_in(["algorithm", "trim_reads"], d) for d in dd.sample_data_iterator(samples)])
resources = ["picard"]
needs_trimming = (_is_smallrnaseq(pipeline) or trim_reads_set)
if needs_trimming:
resources.append("atropos")
with prun.start(_wres(parallel, resources),
samples, config, dirs, "trimming",
max_multicore=1 if not needs_trimming else None) as run_parallel:
with profile.report("organize samples", dirs):
samples = run_parallel("organize_samples", [[dirs, config, run_info_yaml,
[x[0]["description"] for x in samples]]])
samples = run_parallel("prepare_sample", samples)
if needs_trimming:
with profile.report("adapter trimming", dirs):
if _is_smallrnaseq(pipeline):
samples = run_parallel("trim_srna_sample", samples)
else:
samples = run_parallel("trim_sample", samples)
return samples | python | def rnaseq_prep_samples(config, run_info_yaml, parallel, dirs, samples):
pipeline = dd.get_in_samples(samples, dd.get_analysis)
trim_reads_set = any([tz.get_in(["algorithm", "trim_reads"], d) for d in dd.sample_data_iterator(samples)])
resources = ["picard"]
needs_trimming = (_is_smallrnaseq(pipeline) or trim_reads_set)
if needs_trimming:
resources.append("atropos")
with prun.start(_wres(parallel, resources),
samples, config, dirs, "trimming",
max_multicore=1 if not needs_trimming else None) as run_parallel:
with profile.report("organize samples", dirs):
samples = run_parallel("organize_samples", [[dirs, config, run_info_yaml,
[x[0]["description"] for x in samples]]])
samples = run_parallel("prepare_sample", samples)
if needs_trimming:
with profile.report("adapter trimming", dirs):
if _is_smallrnaseq(pipeline):
samples = run_parallel("trim_srna_sample", samples)
else:
samples = run_parallel("trim_sample", samples)
return samples | [
"def",
"rnaseq_prep_samples",
"(",
"config",
",",
"run_info_yaml",
",",
"parallel",
",",
"dirs",
",",
"samples",
")",
":",
"pipeline",
"=",
"dd",
".",
"get_in_samples",
"(",
"samples",
",",
"dd",
".",
"get_analysis",
")",
"trim_reads_set",
"=",
"any",
"(",
... | organizes RNA-seq and small-RNAseq samples, converting from BAM if
necessary and trimming if necessary | [
"organizes",
"RNA",
"-",
"seq",
"and",
"small",
"-",
"RNAseq",
"samples",
"converting",
"from",
"BAM",
"if",
"necessary",
"and",
"trimming",
"if",
"necessary"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/pipeline/main.py#L394-L418 |
236,876 | bcbio/bcbio-nextgen | bcbio/pipeline/main.py | _pair_samples_with_pipelines | def _pair_samples_with_pipelines(run_info_yaml, config):
"""Map samples defined in input file to pipelines to run.
"""
samples = config_utils.load_config(run_info_yaml)
if isinstance(samples, dict):
resources = samples.pop("resources")
samples = samples["details"]
else:
resources = {}
ready_samples = []
for sample in samples:
if "files" in sample:
del sample["files"]
# add any resources to this item to recalculate global configuration
usample = copy.deepcopy(sample)
usample.pop("algorithm", None)
if "resources" not in usample:
usample["resources"] = {}
for prog, pkvs in resources.items():
if prog not in usample["resources"]:
usample["resources"][prog] = {}
if pkvs is not None:
for key, val in pkvs.items():
usample["resources"][prog][key] = val
config = config_utils.update_w_custom(config, usample)
sample["resources"] = {}
ready_samples.append(sample)
paired = [(x, _get_pipeline(x)) for x in ready_samples]
d = defaultdict(list)
for x in paired:
d[x[1]].append([x[0]])
return d, config | python | def _pair_samples_with_pipelines(run_info_yaml, config):
samples = config_utils.load_config(run_info_yaml)
if isinstance(samples, dict):
resources = samples.pop("resources")
samples = samples["details"]
else:
resources = {}
ready_samples = []
for sample in samples:
if "files" in sample:
del sample["files"]
# add any resources to this item to recalculate global configuration
usample = copy.deepcopy(sample)
usample.pop("algorithm", None)
if "resources" not in usample:
usample["resources"] = {}
for prog, pkvs in resources.items():
if prog not in usample["resources"]:
usample["resources"][prog] = {}
if pkvs is not None:
for key, val in pkvs.items():
usample["resources"][prog][key] = val
config = config_utils.update_w_custom(config, usample)
sample["resources"] = {}
ready_samples.append(sample)
paired = [(x, _get_pipeline(x)) for x in ready_samples]
d = defaultdict(list)
for x in paired:
d[x[1]].append([x[0]])
return d, config | [
"def",
"_pair_samples_with_pipelines",
"(",
"run_info_yaml",
",",
"config",
")",
":",
"samples",
"=",
"config_utils",
".",
"load_config",
"(",
"run_info_yaml",
")",
"if",
"isinstance",
"(",
"samples",
",",
"dict",
")",
":",
"resources",
"=",
"samples",
".",
"p... | Map samples defined in input file to pipelines to run. | [
"Map",
"samples",
"defined",
"in",
"input",
"file",
"to",
"pipelines",
"to",
"run",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/pipeline/main.py#L430-L461 |
236,877 | bcbio/bcbio-nextgen | bcbio/hla/bwakit.py | run | def run(data):
"""HLA typing with bwakit, parsing output from called genotype files.
"""
bwakit_dir = os.path.dirname(os.path.realpath(utils.which("run-bwamem")))
hla_fqs = tz.get_in(["hla", "fastq"], data, [])
if len(hla_fqs) > 0:
hla_base = os.path.commonprefix(hla_fqs)
while hla_base.endswith("."):
hla_base = hla_base[:-1]
out_file = hla_base + ".top"
if not utils.file_exists(out_file):
cmd = "{bwakit_dir}/run-HLA {hla_base}"
do.run(cmd.format(**locals()), "HLA typing with bwakit")
out_file = _organize_calls(out_file, hla_base, data)
data["hla"].update({"call_file": out_file,
"hlacaller": "bwakit"})
return data | python | def run(data):
bwakit_dir = os.path.dirname(os.path.realpath(utils.which("run-bwamem")))
hla_fqs = tz.get_in(["hla", "fastq"], data, [])
if len(hla_fqs) > 0:
hla_base = os.path.commonprefix(hla_fqs)
while hla_base.endswith("."):
hla_base = hla_base[:-1]
out_file = hla_base + ".top"
if not utils.file_exists(out_file):
cmd = "{bwakit_dir}/run-HLA {hla_base}"
do.run(cmd.format(**locals()), "HLA typing with bwakit")
out_file = _organize_calls(out_file, hla_base, data)
data["hla"].update({"call_file": out_file,
"hlacaller": "bwakit"})
return data | [
"def",
"run",
"(",
"data",
")",
":",
"bwakit_dir",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"realpath",
"(",
"utils",
".",
"which",
"(",
"\"run-bwamem\"",
")",
")",
")",
"hla_fqs",
"=",
"tz",
".",
"get_in",
"(",
"[",
"... | HLA typing with bwakit, parsing output from called genotype files. | [
"HLA",
"typing",
"with",
"bwakit",
"parsing",
"output",
"from",
"called",
"genotype",
"files",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/hla/bwakit.py#L18-L34 |
236,878 | bcbio/bcbio-nextgen | bcbio/hla/bwakit.py | _organize_calls | def _organize_calls(out_file, hla_base, data):
"""Prepare genotype calls, reporting best call along with quality metrics.
"""
hla_truth = get_hla_truthset(data)
sample = dd.get_sample_name(data)
with file_transaction(data, out_file) as tx_out_file:
with open(tx_out_file, "w") as out_handle:
writer = csv.writer(out_handle)
writer.writerow(["sample", "locus", "mismatches", "options", "alleles", "p-groups", "expected",
"validates"])
for genotype_file in glob.glob("%s.HLA-*.gt" % (hla_base)):
hla_locus = os.path.basename(genotype_file).replace(
"%s.HLA-" % os.path.basename(hla_base), "").replace(".gt", "")
with open(genotype_file) as in_handle:
total_options = set([])
for i, line in enumerate(in_handle):
_, aone, atwo, m = line.split("\t")[:4]
pgroups = (hla_groups.hla_protein(aone, data), hla_groups.hla_protein(atwo, data))
if i == 0:
call_alleles = [aone, atwo]
call_pgroups = pgroups
mismatches = m
total_options.add(pgroups)
if len(total_options) > 0:
truth_alleles = tz.get_in([sample, hla_locus], hla_truth, [])
writer.writerow([sample, hla_locus, mismatches, len(total_options),
";".join(call_alleles), ";".join(call_pgroups),
";".join(truth_alleles), matches_truth(call_alleles, truth_alleles, data)])
return out_file | python | def _organize_calls(out_file, hla_base, data):
hla_truth = get_hla_truthset(data)
sample = dd.get_sample_name(data)
with file_transaction(data, out_file) as tx_out_file:
with open(tx_out_file, "w") as out_handle:
writer = csv.writer(out_handle)
writer.writerow(["sample", "locus", "mismatches", "options", "alleles", "p-groups", "expected",
"validates"])
for genotype_file in glob.glob("%s.HLA-*.gt" % (hla_base)):
hla_locus = os.path.basename(genotype_file).replace(
"%s.HLA-" % os.path.basename(hla_base), "").replace(".gt", "")
with open(genotype_file) as in_handle:
total_options = set([])
for i, line in enumerate(in_handle):
_, aone, atwo, m = line.split("\t")[:4]
pgroups = (hla_groups.hla_protein(aone, data), hla_groups.hla_protein(atwo, data))
if i == 0:
call_alleles = [aone, atwo]
call_pgroups = pgroups
mismatches = m
total_options.add(pgroups)
if len(total_options) > 0:
truth_alleles = tz.get_in([sample, hla_locus], hla_truth, [])
writer.writerow([sample, hla_locus, mismatches, len(total_options),
";".join(call_alleles), ";".join(call_pgroups),
";".join(truth_alleles), matches_truth(call_alleles, truth_alleles, data)])
return out_file | [
"def",
"_organize_calls",
"(",
"out_file",
",",
"hla_base",
",",
"data",
")",
":",
"hla_truth",
"=",
"get_hla_truthset",
"(",
"data",
")",
"sample",
"=",
"dd",
".",
"get_sample_name",
"(",
"data",
")",
"with",
"file_transaction",
"(",
"data",
",",
"out_file"... | Prepare genotype calls, reporting best call along with quality metrics. | [
"Prepare",
"genotype",
"calls",
"reporting",
"best",
"call",
"along",
"with",
"quality",
"metrics",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/hla/bwakit.py#L36-L64 |
236,879 | bcbio/bcbio-nextgen | bcbio/hla/bwakit.py | matches_truth | def matches_truth(call_alleles, truth_alleles, data):
"""Flexibly check if truth and call alleles match, using p-groups.
"""
if not truth_alleles:
return ""
else:
def _remove_p(x):
return x[:-1] if x.endswith("P") else x
t_cmp = set([_remove_p(hla_groups.hla_protein(x, data)) for x in truth_alleles])
c_cmp = set([_remove_p(hla_groups.hla_protein(x, data)) for x in call_alleles])
return "yes" if len(t_cmp.intersection(c_cmp)) == len(t_cmp) else "no" | python | def matches_truth(call_alleles, truth_alleles, data):
if not truth_alleles:
return ""
else:
def _remove_p(x):
return x[:-1] if x.endswith("P") else x
t_cmp = set([_remove_p(hla_groups.hla_protein(x, data)) for x in truth_alleles])
c_cmp = set([_remove_p(hla_groups.hla_protein(x, data)) for x in call_alleles])
return "yes" if len(t_cmp.intersection(c_cmp)) == len(t_cmp) else "no" | [
"def",
"matches_truth",
"(",
"call_alleles",
",",
"truth_alleles",
",",
"data",
")",
":",
"if",
"not",
"truth_alleles",
":",
"return",
"\"\"",
"else",
":",
"def",
"_remove_p",
"(",
"x",
")",
":",
"return",
"x",
"[",
":",
"-",
"1",
"]",
"if",
"x",
"."... | Flexibly check if truth and call alleles match, using p-groups. | [
"Flexibly",
"check",
"if",
"truth",
"and",
"call",
"alleles",
"match",
"using",
"p",
"-",
"groups",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/hla/bwakit.py#L66-L76 |
236,880 | bcbio/bcbio-nextgen | bcbio/hla/bwakit.py | get_hla_truthset | def get_hla_truthset(data):
"""Retrieve expected truth calls for annotating HLA called output.
"""
val_csv = tz.get_in(["config", "algorithm", "hlavalidate"], data)
out = {}
if val_csv and utils.file_exists(val_csv):
with open(val_csv) as in_handle:
reader = csv.reader(in_handle)
next(reader) # header
for sample, locus, alleles in (l for l in reader if l):
out = tz.update_in(out, [sample, locus], lambda x: [x.strip() for x in alleles.split(";")])
return out | python | def get_hla_truthset(data):
val_csv = tz.get_in(["config", "algorithm", "hlavalidate"], data)
out = {}
if val_csv and utils.file_exists(val_csv):
with open(val_csv) as in_handle:
reader = csv.reader(in_handle)
next(reader) # header
for sample, locus, alleles in (l for l in reader if l):
out = tz.update_in(out, [sample, locus], lambda x: [x.strip() for x in alleles.split(";")])
return out | [
"def",
"get_hla_truthset",
"(",
"data",
")",
":",
"val_csv",
"=",
"tz",
".",
"get_in",
"(",
"[",
"\"config\"",
",",
"\"algorithm\"",
",",
"\"hlavalidate\"",
"]",
",",
"data",
")",
"out",
"=",
"{",
"}",
"if",
"val_csv",
"and",
"utils",
".",
"file_exists",... | Retrieve expected truth calls for annotating HLA called output. | [
"Retrieve",
"expected",
"truth",
"calls",
"for",
"annotating",
"HLA",
"called",
"output",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/hla/bwakit.py#L78-L89 |
236,881 | bcbio/bcbio-nextgen | scripts/utils/bam_to_fastq_region.py | bam_to_fastq_pair | def bam_to_fastq_pair(in_file, target_region, pair):
"""Generator to convert BAM files into name, seq, qual in a region.
"""
space, start, end = target_region
bam_file = pysam.Samfile(in_file, "rb")
for read in bam_file:
if (not read.is_unmapped and not read.mate_is_unmapped
and bam_file.getrname(read.tid) == space
and bam_file.getrname(read.mrnm) == space
and read.pos >= start and read.pos <= end
and read.mpos >= start and read.mpos <= end
and not read.is_secondary
and read.is_paired and getattr(read, "is_read%s" % pair)):
seq = Seq.Seq(read.seq)
qual = list(read.qual)
if read.is_reverse:
seq = seq.reverse_complement()
qual.reverse()
yield read.qname, str(seq), "".join(qual) | python | def bam_to_fastq_pair(in_file, target_region, pair):
space, start, end = target_region
bam_file = pysam.Samfile(in_file, "rb")
for read in bam_file:
if (not read.is_unmapped and not read.mate_is_unmapped
and bam_file.getrname(read.tid) == space
and bam_file.getrname(read.mrnm) == space
and read.pos >= start and read.pos <= end
and read.mpos >= start and read.mpos <= end
and not read.is_secondary
and read.is_paired and getattr(read, "is_read%s" % pair)):
seq = Seq.Seq(read.seq)
qual = list(read.qual)
if read.is_reverse:
seq = seq.reverse_complement()
qual.reverse()
yield read.qname, str(seq), "".join(qual) | [
"def",
"bam_to_fastq_pair",
"(",
"in_file",
",",
"target_region",
",",
"pair",
")",
":",
"space",
",",
"start",
",",
"end",
"=",
"target_region",
"bam_file",
"=",
"pysam",
".",
"Samfile",
"(",
"in_file",
",",
"\"rb\"",
")",
"for",
"read",
"in",
"bam_file",... | Generator to convert BAM files into name, seq, qual in a region. | [
"Generator",
"to",
"convert",
"BAM",
"files",
"into",
"name",
"seq",
"qual",
"in",
"a",
"region",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/scripts/utils/bam_to_fastq_region.py#L32-L50 |
236,882 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | sample_callable_bed | def sample_callable_bed(bam_file, ref_file, data):
"""Retrieve callable regions for a sample subset by defined analysis regions.
"""
from bcbio.heterogeneity import chromhacks
CovInfo = collections.namedtuple("CovInfo", "callable, raw_callable, depth_files")
noalt_calling = "noalt_calling" in dd.get_tools_on(data) or "altcontigs" in dd.get_exclude_regions(data)
def callable_chrom_filter(r):
"""Filter to callable region, potentially limiting by chromosomes.
"""
return r.name == "CALLABLE" and (not noalt_calling or chromhacks.is_nonalt(r.chrom))
out_file = "%s-callable_sample.bed" % os.path.splitext(bam_file)[0]
with shared.bedtools_tmpdir(data):
sv_bed = regions.get_sv_bed(data)
callable_bed, depth_files = coverage.calculate(bam_file, data, sv_bed)
input_regions_bed = dd.get_variant_regions(data)
if not utils.file_uptodate(out_file, callable_bed):
with file_transaction(data, out_file) as tx_out_file:
callable_regions = pybedtools.BedTool(callable_bed)
filter_regions = callable_regions.filter(callable_chrom_filter)
if input_regions_bed:
if not utils.file_uptodate(out_file, input_regions_bed):
input_regions = pybedtools.BedTool(input_regions_bed)
filter_regions.intersect(input_regions, nonamecheck=True).saveas(tx_out_file)
else:
filter_regions.saveas(tx_out_file)
return CovInfo(out_file, callable_bed, depth_files) | python | def sample_callable_bed(bam_file, ref_file, data):
from bcbio.heterogeneity import chromhacks
CovInfo = collections.namedtuple("CovInfo", "callable, raw_callable, depth_files")
noalt_calling = "noalt_calling" in dd.get_tools_on(data) or "altcontigs" in dd.get_exclude_regions(data)
def callable_chrom_filter(r):
"""Filter to callable region, potentially limiting by chromosomes.
"""
return r.name == "CALLABLE" and (not noalt_calling or chromhacks.is_nonalt(r.chrom))
out_file = "%s-callable_sample.bed" % os.path.splitext(bam_file)[0]
with shared.bedtools_tmpdir(data):
sv_bed = regions.get_sv_bed(data)
callable_bed, depth_files = coverage.calculate(bam_file, data, sv_bed)
input_regions_bed = dd.get_variant_regions(data)
if not utils.file_uptodate(out_file, callable_bed):
with file_transaction(data, out_file) as tx_out_file:
callable_regions = pybedtools.BedTool(callable_bed)
filter_regions = callable_regions.filter(callable_chrom_filter)
if input_regions_bed:
if not utils.file_uptodate(out_file, input_regions_bed):
input_regions = pybedtools.BedTool(input_regions_bed)
filter_regions.intersect(input_regions, nonamecheck=True).saveas(tx_out_file)
else:
filter_regions.saveas(tx_out_file)
return CovInfo(out_file, callable_bed, depth_files) | [
"def",
"sample_callable_bed",
"(",
"bam_file",
",",
"ref_file",
",",
"data",
")",
":",
"from",
"bcbio",
".",
"heterogeneity",
"import",
"chromhacks",
"CovInfo",
"=",
"collections",
".",
"namedtuple",
"(",
"\"CovInfo\"",
",",
"\"callable, raw_callable, depth_files\"",
... | Retrieve callable regions for a sample subset by defined analysis regions. | [
"Retrieve",
"callable",
"regions",
"for",
"a",
"sample",
"subset",
"by",
"defined",
"analysis",
"regions",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L32-L57 |
236,883 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | get_ref_bedtool | def get_ref_bedtool(ref_file, config, chrom=None):
"""Retrieve a pybedtool BedTool object with reference sizes from input reference.
"""
broad_runner = broad.runner_from_path("picard", config)
ref_dict = broad_runner.run_fn("picard_index_ref", ref_file)
ref_lines = []
with pysam.Samfile(ref_dict, "r") as ref_sam:
for sq in ref_sam.header["SQ"]:
if not chrom or sq["SN"] == chrom:
ref_lines.append("%s\t%s\t%s" % (sq["SN"], 0, sq["LN"]))
return pybedtools.BedTool("\n".join(ref_lines), from_string=True) | python | def get_ref_bedtool(ref_file, config, chrom=None):
broad_runner = broad.runner_from_path("picard", config)
ref_dict = broad_runner.run_fn("picard_index_ref", ref_file)
ref_lines = []
with pysam.Samfile(ref_dict, "r") as ref_sam:
for sq in ref_sam.header["SQ"]:
if not chrom or sq["SN"] == chrom:
ref_lines.append("%s\t%s\t%s" % (sq["SN"], 0, sq["LN"]))
return pybedtools.BedTool("\n".join(ref_lines), from_string=True) | [
"def",
"get_ref_bedtool",
"(",
"ref_file",
",",
"config",
",",
"chrom",
"=",
"None",
")",
":",
"broad_runner",
"=",
"broad",
".",
"runner_from_path",
"(",
"\"picard\"",
",",
"config",
")",
"ref_dict",
"=",
"broad_runner",
".",
"run_fn",
"(",
"\"picard_index_re... | Retrieve a pybedtool BedTool object with reference sizes from input reference. | [
"Retrieve",
"a",
"pybedtool",
"BedTool",
"object",
"with",
"reference",
"sizes",
"from",
"input",
"reference",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L59-L69 |
236,884 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _get_nblock_regions | def _get_nblock_regions(in_file, min_n_size, ref_regions):
"""Retrieve coordinates of regions in reference genome with no mapping.
These are potential breakpoints for parallelizing analysis.
"""
out_lines = []
called_contigs = set([])
with utils.open_gzipsafe(in_file) as in_handle:
for line in in_handle:
contig, start, end, ctype = line.rstrip().split()
called_contigs.add(contig)
if (ctype in ["REF_N", "NO_COVERAGE", "EXCESSIVE_COVERAGE", "LOW_COVERAGE"] and
int(end) - int(start) > min_n_size):
out_lines.append("%s\t%s\t%s\n" % (contig, start, end))
for refr in ref_regions:
if refr.chrom not in called_contigs:
out_lines.append("%s\t%s\t%s\n" % (refr.chrom, 0, refr.stop))
return pybedtools.BedTool("\n".join(out_lines), from_string=True) | python | def _get_nblock_regions(in_file, min_n_size, ref_regions):
out_lines = []
called_contigs = set([])
with utils.open_gzipsafe(in_file) as in_handle:
for line in in_handle:
contig, start, end, ctype = line.rstrip().split()
called_contigs.add(contig)
if (ctype in ["REF_N", "NO_COVERAGE", "EXCESSIVE_COVERAGE", "LOW_COVERAGE"] and
int(end) - int(start) > min_n_size):
out_lines.append("%s\t%s\t%s\n" % (contig, start, end))
for refr in ref_regions:
if refr.chrom not in called_contigs:
out_lines.append("%s\t%s\t%s\n" % (refr.chrom, 0, refr.stop))
return pybedtools.BedTool("\n".join(out_lines), from_string=True) | [
"def",
"_get_nblock_regions",
"(",
"in_file",
",",
"min_n_size",
",",
"ref_regions",
")",
":",
"out_lines",
"=",
"[",
"]",
"called_contigs",
"=",
"set",
"(",
"[",
"]",
")",
"with",
"utils",
".",
"open_gzipsafe",
"(",
"in_file",
")",
"as",
"in_handle",
":",... | Retrieve coordinates of regions in reference genome with no mapping.
These are potential breakpoints for parallelizing analysis. | [
"Retrieve",
"coordinates",
"of",
"regions",
"in",
"reference",
"genome",
"with",
"no",
"mapping",
".",
"These",
"are",
"potential",
"breakpoints",
"for",
"parallelizing",
"analysis",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L71-L87 |
236,885 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _combine_regions | def _combine_regions(all_regions, ref_regions):
"""Combine multiple BEDtools regions of regions into sorted final BEDtool.
"""
chrom_order = {}
for i, x in enumerate(ref_regions):
chrom_order[x.chrom] = i
def wchrom_key(x):
chrom, start, end = x
return (chrom_order[chrom], start, end)
all_intervals = []
for region_group in all_regions:
for region in region_group:
all_intervals.append((region.chrom, int(region.start), int(region.stop)))
all_intervals.sort(key=wchrom_key)
bed_lines = ["%s\t%s\t%s" % (c, s, e) for (c, s, e) in all_intervals]
return pybedtools.BedTool("\n".join(bed_lines), from_string=True) | python | def _combine_regions(all_regions, ref_regions):
chrom_order = {}
for i, x in enumerate(ref_regions):
chrom_order[x.chrom] = i
def wchrom_key(x):
chrom, start, end = x
return (chrom_order[chrom], start, end)
all_intervals = []
for region_group in all_regions:
for region in region_group:
all_intervals.append((region.chrom, int(region.start), int(region.stop)))
all_intervals.sort(key=wchrom_key)
bed_lines = ["%s\t%s\t%s" % (c, s, e) for (c, s, e) in all_intervals]
return pybedtools.BedTool("\n".join(bed_lines), from_string=True) | [
"def",
"_combine_regions",
"(",
"all_regions",
",",
"ref_regions",
")",
":",
"chrom_order",
"=",
"{",
"}",
"for",
"i",
",",
"x",
"in",
"enumerate",
"(",
"ref_regions",
")",
":",
"chrom_order",
"[",
"x",
".",
"chrom",
"]",
"=",
"i",
"def",
"wchrom_key",
... | Combine multiple BEDtools regions of regions into sorted final BEDtool. | [
"Combine",
"multiple",
"BEDtools",
"regions",
"of",
"regions",
"into",
"sorted",
"final",
"BEDtool",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L89-L104 |
236,886 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _add_config_regions | def _add_config_regions(nblock_regions, ref_regions, data):
"""Add additional nblock regions based on configured regions to call.
Identifies user defined regions which we should not be analyzing.
"""
input_regions_bed = dd.get_variant_regions(data)
if input_regions_bed:
input_regions = pybedtools.BedTool(input_regions_bed)
# work around problem with single region not subtracted correctly.
if len(input_regions) == 1:
str_regions = str(input_regions[0]).strip()
input_regions = pybedtools.BedTool("%s\n%s" % (str_regions, str_regions),
from_string=True)
input_nblock = ref_regions.subtract(input_regions, nonamecheck=True)
if input_nblock == ref_regions:
raise ValueError("Input variant_region file (%s) "
"excludes all genomic regions. Do the chromosome names "
"in the BED file match your genome (chr1 vs 1)?" % input_regions_bed)
all_intervals = _combine_regions([input_nblock, nblock_regions], ref_regions)
else:
all_intervals = nblock_regions
if "noalt_calling" in dd.get_tools_on(data) or "altcontigs" in dd.get_exclude_regions(data):
from bcbio.heterogeneity import chromhacks
remove_intervals = ref_regions.filter(lambda r: not chromhacks.is_nonalt(r.chrom))
all_intervals = _combine_regions([all_intervals, remove_intervals], ref_regions)
return all_intervals.merge() | python | def _add_config_regions(nblock_regions, ref_regions, data):
input_regions_bed = dd.get_variant_regions(data)
if input_regions_bed:
input_regions = pybedtools.BedTool(input_regions_bed)
# work around problem with single region not subtracted correctly.
if len(input_regions) == 1:
str_regions = str(input_regions[0]).strip()
input_regions = pybedtools.BedTool("%s\n%s" % (str_regions, str_regions),
from_string=True)
input_nblock = ref_regions.subtract(input_regions, nonamecheck=True)
if input_nblock == ref_regions:
raise ValueError("Input variant_region file (%s) "
"excludes all genomic regions. Do the chromosome names "
"in the BED file match your genome (chr1 vs 1)?" % input_regions_bed)
all_intervals = _combine_regions([input_nblock, nblock_regions], ref_regions)
else:
all_intervals = nblock_regions
if "noalt_calling" in dd.get_tools_on(data) or "altcontigs" in dd.get_exclude_regions(data):
from bcbio.heterogeneity import chromhacks
remove_intervals = ref_regions.filter(lambda r: not chromhacks.is_nonalt(r.chrom))
all_intervals = _combine_regions([all_intervals, remove_intervals], ref_regions)
return all_intervals.merge() | [
"def",
"_add_config_regions",
"(",
"nblock_regions",
",",
"ref_regions",
",",
"data",
")",
":",
"input_regions_bed",
"=",
"dd",
".",
"get_variant_regions",
"(",
"data",
")",
"if",
"input_regions_bed",
":",
"input_regions",
"=",
"pybedtools",
".",
"BedTool",
"(",
... | Add additional nblock regions based on configured regions to call.
Identifies user defined regions which we should not be analyzing. | [
"Add",
"additional",
"nblock",
"regions",
"based",
"on",
"configured",
"regions",
"to",
"call",
".",
"Identifies",
"user",
"defined",
"regions",
"which",
"we",
"should",
"not",
"be",
"analyzing",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L106-L130 |
236,887 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | block_regions | def block_regions(callable_bed, in_bam, ref_file, data):
"""Find blocks of regions for analysis from mapped input BAM file.
Identifies islands of callable regions, surrounding by regions
with no read support, that can be analyzed independently.
"""
min_n_size = int(data["config"]["algorithm"].get("nomap_split_size", 250))
with shared.bedtools_tmpdir(data):
nblock_bed = "%s-nblocks.bed" % utils.splitext_plus(callable_bed)[0]
callblock_bed = "%s-callableblocks.bed" % utils.splitext_plus(callable_bed)[0]
if not utils.file_uptodate(nblock_bed, callable_bed):
ref_regions = get_ref_bedtool(ref_file, data["config"])
nblock_regions = _get_nblock_regions(callable_bed, min_n_size, ref_regions)
nblock_regions = _add_config_regions(nblock_regions, ref_regions, data)
with file_transaction(data, nblock_bed, callblock_bed) as (tx_nblock_bed, tx_callblock_bed):
nblock_regions.filter(lambda r: len(r) > min_n_size).saveas(tx_nblock_bed)
if len(ref_regions.subtract(nblock_regions, nonamecheck=True)) > 0:
ref_regions.subtract(tx_nblock_bed, nonamecheck=True).merge(d=min_n_size).saveas(tx_callblock_bed)
else:
raise ValueError("No callable regions found in %s from BAM file %s. Some causes:\n "
" - Alignment regions do not overlap with regions found "
"in your `variant_regions` BED: %s\n"
" - There are no aligned reads in your BAM file that pass sanity checks "
" (mapping score > 1, non-duplicates, both ends of paired reads mapped)"
% (dd.get_sample_name(data), in_bam, dd.get_variant_regions(data)))
return callblock_bed, nblock_bed | python | def block_regions(callable_bed, in_bam, ref_file, data):
min_n_size = int(data["config"]["algorithm"].get("nomap_split_size", 250))
with shared.bedtools_tmpdir(data):
nblock_bed = "%s-nblocks.bed" % utils.splitext_plus(callable_bed)[0]
callblock_bed = "%s-callableblocks.bed" % utils.splitext_plus(callable_bed)[0]
if not utils.file_uptodate(nblock_bed, callable_bed):
ref_regions = get_ref_bedtool(ref_file, data["config"])
nblock_regions = _get_nblock_regions(callable_bed, min_n_size, ref_regions)
nblock_regions = _add_config_regions(nblock_regions, ref_regions, data)
with file_transaction(data, nblock_bed, callblock_bed) as (tx_nblock_bed, tx_callblock_bed):
nblock_regions.filter(lambda r: len(r) > min_n_size).saveas(tx_nblock_bed)
if len(ref_regions.subtract(nblock_regions, nonamecheck=True)) > 0:
ref_regions.subtract(tx_nblock_bed, nonamecheck=True).merge(d=min_n_size).saveas(tx_callblock_bed)
else:
raise ValueError("No callable regions found in %s from BAM file %s. Some causes:\n "
" - Alignment regions do not overlap with regions found "
"in your `variant_regions` BED: %s\n"
" - There are no aligned reads in your BAM file that pass sanity checks "
" (mapping score > 1, non-duplicates, both ends of paired reads mapped)"
% (dd.get_sample_name(data), in_bam, dd.get_variant_regions(data)))
return callblock_bed, nblock_bed | [
"def",
"block_regions",
"(",
"callable_bed",
",",
"in_bam",
",",
"ref_file",
",",
"data",
")",
":",
"min_n_size",
"=",
"int",
"(",
"data",
"[",
"\"config\"",
"]",
"[",
"\"algorithm\"",
"]",
".",
"get",
"(",
"\"nomap_split_size\"",
",",
"250",
")",
")",
"... | Find blocks of regions for analysis from mapped input BAM file.
Identifies islands of callable regions, surrounding by regions
with no read support, that can be analyzed independently. | [
"Find",
"blocks",
"of",
"regions",
"for",
"analysis",
"from",
"mapped",
"input",
"BAM",
"file",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L182-L207 |
236,888 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _analysis_block_stats | def _analysis_block_stats(regions, samples):
"""Provide statistics on sizes and number of analysis blocks.
"""
prev = None
between_sizes = []
region_sizes = []
for region in regions:
if prev and prev.chrom == region.chrom:
between_sizes.append(region.start - prev.end)
region_sizes.append(region.end - region.start)
prev = region
def descriptive_stats(xs):
if len(xs) < 2:
return xs
parts = ["min: %s" % min(xs),
"5%%: %s" % numpy.percentile(xs, 5),
"25%%: %s" % numpy.percentile(xs, 25),
"median: %s" % numpy.percentile(xs, 50),
"75%%: %s" % numpy.percentile(xs, 75),
"95%%: %s" % numpy.percentile(xs, 95),
"99%%: %s" % numpy.percentile(xs, 99),
"max: %s" % max(xs)]
return "\n".join([" " + x for x in parts])
logger.info("Identified %s parallel analysis blocks\n" % len(region_sizes) +
"Block sizes:\n%s\n" % descriptive_stats(region_sizes) +
"Between block sizes:\n%s\n" % descriptive_stats(between_sizes))
if len(region_sizes) == 0:
raise ValueError("No callable regions found in: %s" %
(", ".join([dd.get_sample_name(x) for x in samples]))) | python | def _analysis_block_stats(regions, samples):
prev = None
between_sizes = []
region_sizes = []
for region in regions:
if prev and prev.chrom == region.chrom:
between_sizes.append(region.start - prev.end)
region_sizes.append(region.end - region.start)
prev = region
def descriptive_stats(xs):
if len(xs) < 2:
return xs
parts = ["min: %s" % min(xs),
"5%%: %s" % numpy.percentile(xs, 5),
"25%%: %s" % numpy.percentile(xs, 25),
"median: %s" % numpy.percentile(xs, 50),
"75%%: %s" % numpy.percentile(xs, 75),
"95%%: %s" % numpy.percentile(xs, 95),
"99%%: %s" % numpy.percentile(xs, 99),
"max: %s" % max(xs)]
return "\n".join([" " + x for x in parts])
logger.info("Identified %s parallel analysis blocks\n" % len(region_sizes) +
"Block sizes:\n%s\n" % descriptive_stats(region_sizes) +
"Between block sizes:\n%s\n" % descriptive_stats(between_sizes))
if len(region_sizes) == 0:
raise ValueError("No callable regions found in: %s" %
(", ".join([dd.get_sample_name(x) for x in samples]))) | [
"def",
"_analysis_block_stats",
"(",
"regions",
",",
"samples",
")",
":",
"prev",
"=",
"None",
"between_sizes",
"=",
"[",
"]",
"region_sizes",
"=",
"[",
"]",
"for",
"region",
"in",
"regions",
":",
"if",
"prev",
"and",
"prev",
".",
"chrom",
"==",
"region"... | Provide statistics on sizes and number of analysis blocks. | [
"Provide",
"statistics",
"on",
"sizes",
"and",
"number",
"of",
"analysis",
"blocks",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L216-L244 |
236,889 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _needs_region_update | def _needs_region_update(out_file, samples):
"""Check if we need to update BED file of regions, supporting back compatibility.
"""
nblock_files = [x["regions"]["nblock"] for x in samples if "regions" in x]
# For older approaches and do not create a new set of analysis
# regions, since the new algorithm will re-do all BAM and variant
# steps with new regions
for nblock_file in nblock_files:
test_old = nblock_file.replace("-nblocks", "-analysisblocks")
if os.path.exists(test_old):
return False
# Check if any of the local files have changed so we need to refresh
for noblock_file in nblock_files:
if not utils.file_uptodate(out_file, noblock_file):
return True
return False | python | def _needs_region_update(out_file, samples):
nblock_files = [x["regions"]["nblock"] for x in samples if "regions" in x]
# For older approaches and do not create a new set of analysis
# regions, since the new algorithm will re-do all BAM and variant
# steps with new regions
for nblock_file in nblock_files:
test_old = nblock_file.replace("-nblocks", "-analysisblocks")
if os.path.exists(test_old):
return False
# Check if any of the local files have changed so we need to refresh
for noblock_file in nblock_files:
if not utils.file_uptodate(out_file, noblock_file):
return True
return False | [
"def",
"_needs_region_update",
"(",
"out_file",
",",
"samples",
")",
":",
"nblock_files",
"=",
"[",
"x",
"[",
"\"regions\"",
"]",
"[",
"\"nblock\"",
"]",
"for",
"x",
"in",
"samples",
"if",
"\"regions\"",
"in",
"x",
"]",
"# For older approaches and do not create ... | Check if we need to update BED file of regions, supporting back compatibility. | [
"Check",
"if",
"we",
"need",
"to",
"update",
"BED",
"file",
"of",
"regions",
"supporting",
"back",
"compatibility",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L246-L261 |
236,890 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | combine_sample_regions | def combine_sample_regions(*samples):
"""Create batch-level sets of callable regions for multi-sample calling.
Intersects all non-callable (nblock) regions from all samples in a batch,
producing a global set of callable regions.
"""
samples = utils.unpack_worlds(samples)
samples = cwlutils.unpack_tarballs(samples, samples[0])
# back compatibility -- global file for entire sample set
global_analysis_file = os.path.join(samples[0]["dirs"]["work"], "analysis_blocks.bed")
if utils.file_exists(global_analysis_file) and not _needs_region_update(global_analysis_file, samples):
global_no_analysis_file = os.path.join(os.path.dirname(global_analysis_file), "noanalysis_blocks.bed")
else:
global_analysis_file = None
out = []
analysis_files = []
batches = []
with shared.bedtools_tmpdir(samples[0]):
for batch, items in vmulti.group_by_batch(samples, require_bam=False).items():
batches.append(items)
if global_analysis_file:
analysis_file, no_analysis_file = global_analysis_file, global_no_analysis_file
else:
analysis_file, no_analysis_file = _combine_sample_regions_batch(batch, items)
for data in items:
vr_file = dd.get_variant_regions(data)
if analysis_file:
analysis_files.append(analysis_file)
data["config"]["algorithm"]["callable_regions"] = analysis_file
data["config"]["algorithm"]["non_callable_regions"] = no_analysis_file
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(analysis_file).count()
elif vr_file:
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(vr_file).count()
# attach a representative sample for calculating callable region
if not data.get("work_bam"):
for x in items:
if x.get("work_bam"):
data["work_bam_callable"] = x["work_bam"]
out.append([data])
# Ensure output order matches input order, consistency for CWL-based runs
assert len(out) == len(samples)
sample_indexes = {dd.get_sample_name(d): i for i, d in enumerate(samples)}
def by_input_index(xs):
return sample_indexes[dd.get_sample_name(xs[0])]
out.sort(key=by_input_index)
if len(analysis_files) > 0:
final_regions = pybedtools.BedTool(analysis_files[0])
_analysis_block_stats(final_regions, batches[0])
return out | python | def combine_sample_regions(*samples):
samples = utils.unpack_worlds(samples)
samples = cwlutils.unpack_tarballs(samples, samples[0])
# back compatibility -- global file for entire sample set
global_analysis_file = os.path.join(samples[0]["dirs"]["work"], "analysis_blocks.bed")
if utils.file_exists(global_analysis_file) and not _needs_region_update(global_analysis_file, samples):
global_no_analysis_file = os.path.join(os.path.dirname(global_analysis_file), "noanalysis_blocks.bed")
else:
global_analysis_file = None
out = []
analysis_files = []
batches = []
with shared.bedtools_tmpdir(samples[0]):
for batch, items in vmulti.group_by_batch(samples, require_bam=False).items():
batches.append(items)
if global_analysis_file:
analysis_file, no_analysis_file = global_analysis_file, global_no_analysis_file
else:
analysis_file, no_analysis_file = _combine_sample_regions_batch(batch, items)
for data in items:
vr_file = dd.get_variant_regions(data)
if analysis_file:
analysis_files.append(analysis_file)
data["config"]["algorithm"]["callable_regions"] = analysis_file
data["config"]["algorithm"]["non_callable_regions"] = no_analysis_file
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(analysis_file).count()
elif vr_file:
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(vr_file).count()
# attach a representative sample for calculating callable region
if not data.get("work_bam"):
for x in items:
if x.get("work_bam"):
data["work_bam_callable"] = x["work_bam"]
out.append([data])
# Ensure output order matches input order, consistency for CWL-based runs
assert len(out) == len(samples)
sample_indexes = {dd.get_sample_name(d): i for i, d in enumerate(samples)}
def by_input_index(xs):
return sample_indexes[dd.get_sample_name(xs[0])]
out.sort(key=by_input_index)
if len(analysis_files) > 0:
final_regions = pybedtools.BedTool(analysis_files[0])
_analysis_block_stats(final_regions, batches[0])
return out | [
"def",
"combine_sample_regions",
"(",
"*",
"samples",
")",
":",
"samples",
"=",
"utils",
".",
"unpack_worlds",
"(",
"samples",
")",
"samples",
"=",
"cwlutils",
".",
"unpack_tarballs",
"(",
"samples",
",",
"samples",
"[",
"0",
"]",
")",
"# back compatibility --... | Create batch-level sets of callable regions for multi-sample calling.
Intersects all non-callable (nblock) regions from all samples in a batch,
producing a global set of callable regions. | [
"Create",
"batch",
"-",
"level",
"sets",
"of",
"callable",
"regions",
"for",
"multi",
"-",
"sample",
"calling",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L263-L311 |
236,891 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | _combine_sample_regions_batch | def _combine_sample_regions_batch(batch, items):
"""Combine sample regions within a group of batched samples.
"""
config = items[0]["config"]
work_dir = utils.safe_makedir(os.path.join(items[0]["dirs"]["work"], "regions"))
analysis_file = os.path.join(work_dir, "%s-analysis_blocks.bed" % batch)
no_analysis_file = os.path.join(work_dir, "%s-noanalysis_blocks.bed" % batch)
if not utils.file_exists(analysis_file) or _needs_region_update(analysis_file, items):
# Combine all nblocks into a final set of intersecting regions
# without callable bases. HT @brentp for intersection approach
# https://groups.google.com/forum/?fromgroups#!topic/bedtools-discuss/qA9wK4zN8do
bed_regions = [pybedtools.BedTool(x["regions"]["nblock"])
for x in items if "regions" in x and x["regions"]["nblock"]]
if len(bed_regions) == 0:
analysis_file, no_analysis_file = None, None
else:
with file_transaction(items[0], analysis_file, no_analysis_file) as (tx_afile, tx_noafile):
def intersect_two(a, b):
return a.intersect(b, nonamecheck=True).saveas()
nblock_regions = reduce(intersect_two, bed_regions).saveas(
"%s-nblock%s" % utils.splitext_plus(tx_afile))
ref_file = tz.get_in(["reference", "fasta", "base"], items[0])
ref_regions = get_ref_bedtool(ref_file, config)
min_n_size = int(config["algorithm"].get("nomap_split_size", 250))
block_filter = NBlockRegionPicker(ref_regions, config, min_n_size)
final_nblock_regions = nblock_regions.filter(
block_filter.include_block).saveas().each(block_filter.expand_block).saveas(
"%s-nblockfinal%s" % utils.splitext_plus(tx_afile))
final_regions = ref_regions.subtract(final_nblock_regions, nonamecheck=True).\
saveas().merge(d=min_n_size)
_write_bed_regions(items[0], final_regions, tx_afile, tx_noafile)
if analysis_file and utils.file_exists(analysis_file):
return analysis_file, no_analysis_file
else:
return None, None | python | def _combine_sample_regions_batch(batch, items):
config = items[0]["config"]
work_dir = utils.safe_makedir(os.path.join(items[0]["dirs"]["work"], "regions"))
analysis_file = os.path.join(work_dir, "%s-analysis_blocks.bed" % batch)
no_analysis_file = os.path.join(work_dir, "%s-noanalysis_blocks.bed" % batch)
if not utils.file_exists(analysis_file) or _needs_region_update(analysis_file, items):
# Combine all nblocks into a final set of intersecting regions
# without callable bases. HT @brentp for intersection approach
# https://groups.google.com/forum/?fromgroups#!topic/bedtools-discuss/qA9wK4zN8do
bed_regions = [pybedtools.BedTool(x["regions"]["nblock"])
for x in items if "regions" in x and x["regions"]["nblock"]]
if len(bed_regions) == 0:
analysis_file, no_analysis_file = None, None
else:
with file_transaction(items[0], analysis_file, no_analysis_file) as (tx_afile, tx_noafile):
def intersect_two(a, b):
return a.intersect(b, nonamecheck=True).saveas()
nblock_regions = reduce(intersect_two, bed_regions).saveas(
"%s-nblock%s" % utils.splitext_plus(tx_afile))
ref_file = tz.get_in(["reference", "fasta", "base"], items[0])
ref_regions = get_ref_bedtool(ref_file, config)
min_n_size = int(config["algorithm"].get("nomap_split_size", 250))
block_filter = NBlockRegionPicker(ref_regions, config, min_n_size)
final_nblock_regions = nblock_regions.filter(
block_filter.include_block).saveas().each(block_filter.expand_block).saveas(
"%s-nblockfinal%s" % utils.splitext_plus(tx_afile))
final_regions = ref_regions.subtract(final_nblock_regions, nonamecheck=True).\
saveas().merge(d=min_n_size)
_write_bed_regions(items[0], final_regions, tx_afile, tx_noafile)
if analysis_file and utils.file_exists(analysis_file):
return analysis_file, no_analysis_file
else:
return None, None | [
"def",
"_combine_sample_regions_batch",
"(",
"batch",
",",
"items",
")",
":",
"config",
"=",
"items",
"[",
"0",
"]",
"[",
"\"config\"",
"]",
"work_dir",
"=",
"utils",
".",
"safe_makedir",
"(",
"os",
".",
"path",
".",
"join",
"(",
"items",
"[",
"0",
"]"... | Combine sample regions within a group of batched samples. | [
"Combine",
"sample",
"regions",
"within",
"a",
"group",
"of",
"batched",
"samples",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L313-L347 |
236,892 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | get_split_regions | def get_split_regions(bed_file, data):
"""Retrieve a set of split regions using the input BED for callable regions.
Provides a less inclusive hook for parallelizing over multiple regions.
"""
out_file = "%s-analysis_blocks.bed" % utils.splitext_plus(bed_file)[0]
with shared.bedtools_tmpdir(data):
if not utils.file_uptodate(out_file, bed_file):
ref_regions = get_ref_bedtool(dd.get_ref_file(data), data["config"])
nblock_regions = ref_regions.subtract(pybedtools.BedTool(bed_file)).saveas()
min_n_size = int(tz.get_in(["config", "algorithm", "nomap_split_size"], data, 250))
block_filter = NBlockRegionPicker(ref_regions, data["config"], min_n_size)
final_nblock_regions = nblock_regions.filter(
block_filter.include_block).saveas().each(block_filter.expand_block).saveas()
with file_transaction(data, out_file) as tx_out_file:
final_regions = ref_regions.subtract(final_nblock_regions, nonamecheck=True).\
saveas().merge(d=min_n_size).saveas(tx_out_file)
chroms = set([])
with shared.bedtools_tmpdir(data):
for r in pybedtools.BedTool(bed_file):
chroms.add(r.chrom)
out = []
for r in pybedtools.BedTool(out_file):
if r.chrom in chroms:
out.append((r.chrom, r.start, r.stop))
return out | python | def get_split_regions(bed_file, data):
out_file = "%s-analysis_blocks.bed" % utils.splitext_plus(bed_file)[0]
with shared.bedtools_tmpdir(data):
if not utils.file_uptodate(out_file, bed_file):
ref_regions = get_ref_bedtool(dd.get_ref_file(data), data["config"])
nblock_regions = ref_regions.subtract(pybedtools.BedTool(bed_file)).saveas()
min_n_size = int(tz.get_in(["config", "algorithm", "nomap_split_size"], data, 250))
block_filter = NBlockRegionPicker(ref_regions, data["config"], min_n_size)
final_nblock_regions = nblock_regions.filter(
block_filter.include_block).saveas().each(block_filter.expand_block).saveas()
with file_transaction(data, out_file) as tx_out_file:
final_regions = ref_regions.subtract(final_nblock_regions, nonamecheck=True).\
saveas().merge(d=min_n_size).saveas(tx_out_file)
chroms = set([])
with shared.bedtools_tmpdir(data):
for r in pybedtools.BedTool(bed_file):
chroms.add(r.chrom)
out = []
for r in pybedtools.BedTool(out_file):
if r.chrom in chroms:
out.append((r.chrom, r.start, r.stop))
return out | [
"def",
"get_split_regions",
"(",
"bed_file",
",",
"data",
")",
":",
"out_file",
"=",
"\"%s-analysis_blocks.bed\"",
"%",
"utils",
".",
"splitext_plus",
"(",
"bed_file",
")",
"[",
"0",
"]",
"with",
"shared",
".",
"bedtools_tmpdir",
"(",
"data",
")",
":",
"if",... | Retrieve a set of split regions using the input BED for callable regions.
Provides a less inclusive hook for parallelizing over multiple regions. | [
"Retrieve",
"a",
"set",
"of",
"split",
"regions",
"using",
"the",
"input",
"BED",
"for",
"callable",
"regions",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L349-L374 |
236,893 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | NBlockRegionPicker.include_block | def include_block(self, x):
"""Check for inclusion of block based on distance from previous.
"""
last_pos = self._chr_last_blocks.get(x.chrom, 0)
# Region excludes an entire chromosome, typically decoy/haplotypes
if last_pos <= self._end_buffer and x.stop >= self._ref_sizes.get(x.chrom, 0) - self._end_buffer:
return True
# Do not split on smaller decoy and haplotype chromosomes
elif self._ref_sizes.get(x.chrom, 0) <= self._target_size:
return False
elif (x.start - last_pos) > self._target_size:
self._chr_last_blocks[x.chrom] = x.stop
return True
else:
return False | python | def include_block(self, x):
last_pos = self._chr_last_blocks.get(x.chrom, 0)
# Region excludes an entire chromosome, typically decoy/haplotypes
if last_pos <= self._end_buffer and x.stop >= self._ref_sizes.get(x.chrom, 0) - self._end_buffer:
return True
# Do not split on smaller decoy and haplotype chromosomes
elif self._ref_sizes.get(x.chrom, 0) <= self._target_size:
return False
elif (x.start - last_pos) > self._target_size:
self._chr_last_blocks[x.chrom] = x.stop
return True
else:
return False | [
"def",
"include_block",
"(",
"self",
",",
"x",
")",
":",
"last_pos",
"=",
"self",
".",
"_chr_last_blocks",
".",
"get",
"(",
"x",
".",
"chrom",
",",
"0",
")",
"# Region excludes an entire chromosome, typically decoy/haplotypes",
"if",
"last_pos",
"<=",
"self",
".... | Check for inclusion of block based on distance from previous. | [
"Check",
"for",
"inclusion",
"of",
"block",
"based",
"on",
"distance",
"from",
"previous",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L155-L169 |
236,894 | bcbio/bcbio-nextgen | bcbio/bam/callable.py | NBlockRegionPicker.expand_block | def expand_block(self, feat):
"""Expand any blocks which are near the start or end of a contig.
"""
chrom_end = self._ref_sizes.get(feat.chrom)
if chrom_end:
if feat.start < self._end_buffer:
feat.start = 0
if feat.stop >= chrom_end - self._end_buffer:
feat.stop = chrom_end
return feat | python | def expand_block(self, feat):
chrom_end = self._ref_sizes.get(feat.chrom)
if chrom_end:
if feat.start < self._end_buffer:
feat.start = 0
if feat.stop >= chrom_end - self._end_buffer:
feat.stop = chrom_end
return feat | [
"def",
"expand_block",
"(",
"self",
",",
"feat",
")",
":",
"chrom_end",
"=",
"self",
".",
"_ref_sizes",
".",
"get",
"(",
"feat",
".",
"chrom",
")",
"if",
"chrom_end",
":",
"if",
"feat",
".",
"start",
"<",
"self",
".",
"_end_buffer",
":",
"feat",
".",... | Expand any blocks which are near the start or end of a contig. | [
"Expand",
"any",
"blocks",
"which",
"are",
"near",
"the",
"start",
"or",
"end",
"of",
"a",
"contig",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/bam/callable.py#L171-L180 |
236,895 | bcbio/bcbio-nextgen | bcbio/chipseq/__init__.py | _keep_assembled_chrom | def _keep_assembled_chrom(bam_file, genome, config):
"""Remove contigs from the BAM file"""
fai = "%s.fai" % genome
chrom = []
with open(fai) as inh:
for line in inh:
c = line.split("\t")[0]
if c.find("_") < 0:
chrom.append(c)
chroms = " ".join(chrom)
out_file = utils.append_stem(bam_file, '_chrom')
samtools = config_utils.get_program("samtools", config)
if not utils.file_exists(out_file):
with file_transaction(out_file) as tx_out:
cmd = "{samtools} view -b {bam_file} {chroms} > {tx_out}"
do.run(cmd.format(**locals()), "Remove contigs from %s" % bam_file)
bam.index(out_file, config)
return out_file | python | def _keep_assembled_chrom(bam_file, genome, config):
fai = "%s.fai" % genome
chrom = []
with open(fai) as inh:
for line in inh:
c = line.split("\t")[0]
if c.find("_") < 0:
chrom.append(c)
chroms = " ".join(chrom)
out_file = utils.append_stem(bam_file, '_chrom')
samtools = config_utils.get_program("samtools", config)
if not utils.file_exists(out_file):
with file_transaction(out_file) as tx_out:
cmd = "{samtools} view -b {bam_file} {chroms} > {tx_out}"
do.run(cmd.format(**locals()), "Remove contigs from %s" % bam_file)
bam.index(out_file, config)
return out_file | [
"def",
"_keep_assembled_chrom",
"(",
"bam_file",
",",
"genome",
",",
"config",
")",
":",
"fai",
"=",
"\"%s.fai\"",
"%",
"genome",
"chrom",
"=",
"[",
"]",
"with",
"open",
"(",
"fai",
")",
"as",
"inh",
":",
"for",
"line",
"in",
"inh",
":",
"c",
"=",
... | Remove contigs from the BAM file | [
"Remove",
"contigs",
"from",
"the",
"BAM",
"file"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/chipseq/__init__.py#L40-L57 |
236,896 | bcbio/bcbio-nextgen | bcbio/chipseq/__init__.py | _prepare_bam | def _prepare_bam(bam_file, bed_file, config):
"""Remove regions from bed files"""
if not bam_file or not bed_file:
return bam_file
out_file = utils.append_stem(bam_file, '_filter')
bedtools = config_utils.get_program("bedtools", config)
if not utils.file_exists(out_file):
with file_transaction(out_file) as tx_out:
cmd = "{bedtools} subtract -nonamecheck -A -a {bam_file} -b {bed_file} > {tx_out}"
do.run(cmd.format(**locals()), "Remove blacklist regions from %s" % bam_file)
return out_file | python | def _prepare_bam(bam_file, bed_file, config):
if not bam_file or not bed_file:
return bam_file
out_file = utils.append_stem(bam_file, '_filter')
bedtools = config_utils.get_program("bedtools", config)
if not utils.file_exists(out_file):
with file_transaction(out_file) as tx_out:
cmd = "{bedtools} subtract -nonamecheck -A -a {bam_file} -b {bed_file} > {tx_out}"
do.run(cmd.format(**locals()), "Remove blacklist regions from %s" % bam_file)
return out_file | [
"def",
"_prepare_bam",
"(",
"bam_file",
",",
"bed_file",
",",
"config",
")",
":",
"if",
"not",
"bam_file",
"or",
"not",
"bed_file",
":",
"return",
"bam_file",
"out_file",
"=",
"utils",
".",
"append_stem",
"(",
"bam_file",
",",
"'_filter'",
")",
"bedtools",
... | Remove regions from bed files | [
"Remove",
"regions",
"from",
"bed",
"files"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/chipseq/__init__.py#L60-L70 |
236,897 | bcbio/bcbio-nextgen | bcbio/chipseq/__init__.py | _bam_coverage | def _bam_coverage(name, bam_input, data):
"""Run bamCoverage from deeptools"""
cmd = ("{bam_coverage} -b {bam_input} -o {bw_output} "
"--binSize 20 --effectiveGenomeSize {size} "
"--smoothLength 60 --extendReads 150 --centerReads -p {cores}")
size = bam.fasta.total_sequence_length(dd.get_ref_file(data))
cores = dd.get_num_cores(data)
try:
bam_coverage = config_utils.get_program("bamCoverage", data)
except config_utils.CmdNotFound:
logger.info("No bamCoverage found, skipping bamCoverage.")
return None
resources = config_utils.get_resources("bamCoverage", data["config"])
if resources:
options = resources.get("options")
if options:
cmd += " %s" % " ".join([str(x) for x in options])
bw_output = os.path.join(os.path.dirname(bam_input), "%s.bw" % name)
if utils.file_exists(bw_output):
return bw_output
with file_transaction(bw_output) as out_tx:
do.run(cmd.format(**locals()), "Run bamCoverage in %s" % name)
return bw_output | python | def _bam_coverage(name, bam_input, data):
cmd = ("{bam_coverage} -b {bam_input} -o {bw_output} "
"--binSize 20 --effectiveGenomeSize {size} "
"--smoothLength 60 --extendReads 150 --centerReads -p {cores}")
size = bam.fasta.total_sequence_length(dd.get_ref_file(data))
cores = dd.get_num_cores(data)
try:
bam_coverage = config_utils.get_program("bamCoverage", data)
except config_utils.CmdNotFound:
logger.info("No bamCoverage found, skipping bamCoverage.")
return None
resources = config_utils.get_resources("bamCoverage", data["config"])
if resources:
options = resources.get("options")
if options:
cmd += " %s" % " ".join([str(x) for x in options])
bw_output = os.path.join(os.path.dirname(bam_input), "%s.bw" % name)
if utils.file_exists(bw_output):
return bw_output
with file_transaction(bw_output) as out_tx:
do.run(cmd.format(**locals()), "Run bamCoverage in %s" % name)
return bw_output | [
"def",
"_bam_coverage",
"(",
"name",
",",
"bam_input",
",",
"data",
")",
":",
"cmd",
"=",
"(",
"\"{bam_coverage} -b {bam_input} -o {bw_output} \"",
"\"--binSize 20 --effectiveGenomeSize {size} \"",
"\"--smoothLength 60 --extendReads 150 --centerReads -p {cores}\"",
")",
"size",
"... | Run bamCoverage from deeptools | [
"Run",
"bamCoverage",
"from",
"deeptools"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/chipseq/__init__.py#L72-L94 |
236,898 | bcbio/bcbio-nextgen | bcbio/structural/manta.py | _get_out_file | def _get_out_file(work_dir, paired):
"""Retrieve manta output variant file, depending on analysis.
"""
if paired:
if paired.normal_bam:
base_file = "somaticSV.vcf.gz"
else:
base_file = "tumorSV.vcf.gz"
else:
base_file = "diploidSV.vcf.gz"
return os.path.join(work_dir, "results", "variants", base_file) | python | def _get_out_file(work_dir, paired):
if paired:
if paired.normal_bam:
base_file = "somaticSV.vcf.gz"
else:
base_file = "tumorSV.vcf.gz"
else:
base_file = "diploidSV.vcf.gz"
return os.path.join(work_dir, "results", "variants", base_file) | [
"def",
"_get_out_file",
"(",
"work_dir",
",",
"paired",
")",
":",
"if",
"paired",
":",
"if",
"paired",
".",
"normal_bam",
":",
"base_file",
"=",
"\"somaticSV.vcf.gz\"",
"else",
":",
"base_file",
"=",
"\"tumorSV.vcf.gz\"",
"else",
":",
"base_file",
"=",
"\"dipl... | Retrieve manta output variant file, depending on analysis. | [
"Retrieve",
"manta",
"output",
"variant",
"file",
"depending",
"on",
"analysis",
"."
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/structural/manta.py#L71-L81 |
236,899 | bcbio/bcbio-nextgen | bcbio/structural/manta.py | _get_evidence_bam | def _get_evidence_bam(work_dir, data):
"""Retrieve evidence BAM for the sample if it exists
"""
evidence_bam = glob.glob(os.path.join(work_dir, "results", "evidence",
"evidence_*.%s*.bam" % (dd.get_sample_name(data))))
if evidence_bam:
return evidence_bam[0] | python | def _get_evidence_bam(work_dir, data):
evidence_bam = glob.glob(os.path.join(work_dir, "results", "evidence",
"evidence_*.%s*.bam" % (dd.get_sample_name(data))))
if evidence_bam:
return evidence_bam[0] | [
"def",
"_get_evidence_bam",
"(",
"work_dir",
",",
"data",
")",
":",
"evidence_bam",
"=",
"glob",
".",
"glob",
"(",
"os",
".",
"path",
".",
"join",
"(",
"work_dir",
",",
"\"results\"",
",",
"\"evidence\"",
",",
"\"evidence_*.%s*.bam\"",
"%",
"(",
"dd",
".",... | Retrieve evidence BAM for the sample if it exists | [
"Retrieve",
"evidence",
"BAM",
"for",
"the",
"sample",
"if",
"it",
"exists"
] | 6a9348c0054ccd5baffd22f1bb7d0422f6978b20 | https://github.com/bcbio/bcbio-nextgen/blob/6a9348c0054ccd5baffd22f1bb7d0422f6978b20/bcbio/structural/manta.py#L83-L89 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.