sentence1 stringlengths 52 3.87M | sentence2 stringlengths 1 47.2k | label stringclasses 1 value |
|---|---|---|
def load_multiple(filters="*.*", text='Select some files, FACEFACE!', default_directory='default_directory'):
"""
Pops up a dialog for opening more than one file. Returns a list of string paths or None.
"""
# make sure the filters contains "*.*" as an option!
if not '*' in filters.split(';'): filters = filters + ";;All files (*)"
# if this type of pref doesn't exist, we need to make a new one
if default_directory in _settings.keys(): default = _settings[default_directory]
else: default = ""
# pop up the dialog
results = _qtw.QFileDialog.getOpenFileNames(None,text,default,filters)
# If Qt5, take the zeroth element
if _s._qt.VERSION_INFO[0:5] == "PyQt5": results = results[0]
# Make sure it's a string
result = []
for r in results: result.append(str(r))
if len(result)==0: return
else:
_settings[default_directory] = _os.path.split(result[0])[0]
return result | Pops up a dialog for opening more than one file. Returns a list of string paths or None. | entailment |
def Dump(self):
"""
Dumps the current prefs to the preferences.txt file
"""
prefs_file = open(self.prefs_path, 'w')
for n in range(0,len(self.prefs)):
if len(list(self.prefs.items())[n]) > 1:
prefs_file.write(str(list(self.prefs.items())[n][0]) + ' = ' +
str(list(self.prefs.items())[n][1]) + '\n')
prefs_file.close() | Dumps the current prefs to the preferences.txt file | entailment |
def link(self):
"""Resolve and link all types in the scope."""
type_specs = {}
types = []
for name, type_spec in self.scope.type_specs.items():
type_spec = type_spec.link(self.scope)
type_specs[name] = type_spec
if type_spec.surface is not None:
self.scope.add_surface(name, type_spec.surface)
types.append(type_spec.surface)
self.scope.type_specs = type_specs
self.scope.add_surface('__types__', tuple(types)) | Resolve and link all types in the scope. | entailment |
def install(path, name=None):
"""Compiles a Thrift file and installs it as a submodule of the caller.
Given a tree organized like so::
foo/
__init__.py
bar.py
my_service.thrift
You would do,
.. code-block:: python
my_service = thriftrw.install('my_service.thrift')
To install ``my_service`` as a submodule of the module from which you made
the call. If the call was made in ``foo/bar.py``, the compiled
Thrift file will be installed as ``foo.bar.my_service``. If the call was
made in ``foo/__init__.py``, the compiled Thrift file will be installed as
``foo.my_service``. This allows other modules to import ``from`` the
compiled module like so,
.. code-block:: python
from foo.my_service import MyService
.. versionadded:: 0.2
:param path:
Path of the Thrift file. This may be an absolute path, or a path
relative to the Python module making the call.
:param str name:
Name of the submodule. Defaults to the basename of the Thrift file.
:returns:
The compiled module
"""
if name is None:
name = os.path.splitext(os.path.basename(path))[0]
callermod = inspect.getmodule(inspect.stack()[1][0])
name = '%s.%s' % (callermod.__name__, name)
if name in sys.modules:
return sys.modules[name]
if not os.path.isabs(path):
callerfile = callermod.__file__
path = os.path.normpath(
os.path.join(os.path.dirname(callerfile), path)
)
sys.modules[name] = mod = load(path, name=name)
return mod | Compiles a Thrift file and installs it as a submodule of the caller.
Given a tree organized like so::
foo/
__init__.py
bar.py
my_service.thrift
You would do,
.. code-block:: python
my_service = thriftrw.install('my_service.thrift')
To install ``my_service`` as a submodule of the module from which you made
the call. If the call was made in ``foo/bar.py``, the compiled
Thrift file will be installed as ``foo.bar.my_service``. If the call was
made in ``foo/__init__.py``, the compiled Thrift file will be installed as
``foo.my_service``. This allows other modules to import ``from`` the
compiled module like so,
.. code-block:: python
from foo.my_service import MyService
.. versionadded:: 0.2
:param path:
Path of the Thrift file. This may be an absolute path, or a path
relative to the Python module making the call.
:param str name:
Name of the submodule. Defaults to the basename of the Thrift file.
:returns:
The compiled module | entailment |
def loads(self, name, document):
"""Parse and compile the given Thrift document.
:param str name:
Name of the Thrift document.
:param str document:
The Thrift IDL as a string.
"""
return self.compiler.compile(name, document).link().surface | Parse and compile the given Thrift document.
:param str name:
Name of the Thrift document.
:param str document:
The Thrift IDL as a string. | entailment |
def load(self, path, name=None):
"""Load and compile the given Thrift file.
:param str path:
Path to the ``.thrift`` file.
:param str name:
Name of the generated module. Defaults to the base name of the
file.
:returns:
The compiled module.
"""
if name is None:
name = os.path.splitext(os.path.basename(path))[0]
# TODO do we care if the file extension is .thrift?
with open(path, 'r') as f:
document = f.read()
return self.compiler.compile(name, document, path).link().surface | Load and compile the given Thrift file.
:param str path:
Path to the ``.thrift`` file.
:param str name:
Name of the generated module. Defaults to the base name of the
file.
:returns:
The compiled module. | entailment |
def get_url_implicit_flow_user(client_id, scope,
redirect_uri='https://oauth.vk.com/blank.html', display='page',
response_type='token', version=None, state=None, revoke=1):
"""
https://vk.com/dev/implicit_flow_user
:return: url
"""
url = "https://oauth.vk.com/authorize"
params = {
"client_id": client_id,
"scope": scope,
"redirect_uri": redirect_uri,
"display": display,
"response_type": response_type,
"version": version,
"state": state,
"revoke": revoke
}
params = {key: value for key, value in params.items() if value is not None}
return u"{url}?{params}".format(url=url, params=urlencode(params)) | https://vk.com/dev/implicit_flow_user
:return: url | entailment |
def get_url_authcode_flow_user(client_id, redirect_uri, display="page", scope=None, state=None):
"""Authorization Code Flow for User Access Token
Use Authorization Code Flow to run VK API methods from the server side of an application.
Access token received this way is not bound to an ip address but set of permissions that can be granted is limited for security reasons.
Args:
client_id (int): Application id.
redirect_uri (str): Address to redirect user after authorization.
display (str): Sets authorization page appearance.
Sets: {`page`, `popup`, `mobile`}
Defaults to `page`
scope (:obj:`str`, optional): Permissions bit mask, to check on authorization and request if necessary.
More scope: https://vk.com/dev/permissions
state (:obj:`str`, optional): An arbitrary string that will be returned together with authorization result.
Returns:
str: Url
Examples:
>>> vk.get_url_authcode_flow_user(1, 'http://example.com/', scope="wall,email")
'https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=http://example.com/&scope=wall,email&response_type=code
.. _Docs:
https://vk.com/dev/authcode_flow_user
"""
url = "https://oauth.vk.com/authorize"
params = {
"client_id": client_id,
"redirect_uri": redirect_uri,
"display": display,
"response_type": "code"
}
if scope:
params['scope'] = scope
if state:
params['state'] = state
return u"{url}?{params}".format(url=url, params=urlencode(params)) | Authorization Code Flow for User Access Token
Use Authorization Code Flow to run VK API methods from the server side of an application.
Access token received this way is not bound to an ip address but set of permissions that can be granted is limited for security reasons.
Args:
client_id (int): Application id.
redirect_uri (str): Address to redirect user after authorization.
display (str): Sets authorization page appearance.
Sets: {`page`, `popup`, `mobile`}
Defaults to `page`
scope (:obj:`str`, optional): Permissions bit mask, to check on authorization and request if necessary.
More scope: https://vk.com/dev/permissions
state (:obj:`str`, optional): An arbitrary string that will be returned together with authorization result.
Returns:
str: Url
Examples:
>>> vk.get_url_authcode_flow_user(1, 'http://example.com/', scope="wall,email")
'https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=http://example.com/&scope=wall,email&response_type=code
.. _Docs:
https://vk.com/dev/authcode_flow_user | entailment |
def get_fake_data(*a):
"""
Called whenever someone presses the "fire" button.
"""
# add columns of data to the databox
d['x'] = _n.linspace(0,10,100)
d['y'] = _n.cos(d['x']) + 0.1*_n.random.rand(100)
# update the curve
c.setData(d['x'], d['y']) | Called whenever someone presses the "fire" button. | entailment |
def setOpts(self, **opts):
"""
Changes the behavior of the SpinBox. Accepts most of the arguments
allowed in :func:`__init__ <pyqtgraph.SpinBox.__init__>`.
"""
#print opts
for k in opts:
if k == 'bounds':
#print opts[k]
self.setMinimum(opts[k][0], update=False)
self.setMaximum(opts[k][1], update=False)
#for i in [0,1]:
#if opts[k][i] is None:
#self.opts[k][i] = None
#else:
#self.opts[k][i] = D(unicode(opts[k][i]))
elif k in ['step', 'minStep']:
self.opts[k] = D(asUnicode(opts[k]))
elif k == 'value':
pass ## don't set value until bounds have been set
else:
self.opts[k] = opts[k]
if 'value' in opts:
self.setValue(opts['value'])
## If bounds have changed, update value to match
if 'bounds' in opts and 'value' not in opts:
self.setValue()
## sanity checks:
if self.opts['int']:
if 'step' in opts:
step = opts['step']
## not necessary..
#if int(step) != step:
#raise Exception('Integer SpinBox must have integer step size.')
else:
self.opts['step'] = int(self.opts['step'])
if 'minStep' in opts:
step = opts['minStep']
if int(step) != step:
raise Exception('Integer SpinBox must have integer minStep size.')
else:
ms = int(self.opts.get('minStep', 1))
if ms < 1:
ms = 1
self.opts['minStep'] = ms
if 'delay' in opts:
self.proxy.setDelay(opts['delay'])
self.updateText() | Changes the behavior of the SpinBox. Accepts most of the arguments
allowed in :func:`__init__ <pyqtgraph.SpinBox.__init__>`. | entailment |
def setMaximum(self, m, update=True):
"""Set the maximum allowed value (or None for no limit)"""
if m is not None:
m = D(asUnicode(m))
self.opts['bounds'][1] = m
if update:
self.setValue() | Set the maximum allowed value (or None for no limit) | entailment |
def selectNumber(self):
"""
Select the numerical portion of the text to allow quick editing by the user.
"""
le = self.lineEdit()
text = asUnicode(le.text())
if self.opts['suffix'] == '':
le.setSelection(0, len(text))
else:
try:
index = text.index(' ')
except ValueError:
return
le.setSelection(0, index) | Select the numerical portion of the text to allow quick editing by the user. | entailment |
def value(self):
"""
Return the value of this SpinBox.
"""
if self.opts['int']:
return int(self.val)
else:
return float(self.val) | Return the value of this SpinBox. | entailment |
def setValue(self, value=None, update=True, delaySignal=False):
"""
Set the value of this spin.
If the value is out of bounds, it will be clipped to the nearest boundary.
If the spin is integer type, the value will be coerced to int.
Returns the actual value set.
If value is None, then the current value is used (this is for resetting
the value after bounds, etc. have changed)
"""
if value is None:
value = self.value()
bounds = self.opts['bounds']
if bounds[0] is not None and value < bounds[0]:
value = bounds[0]
if bounds[1] is not None and value > bounds[1]:
value = bounds[1]
if self.opts['int']:
value = int(value)
value = D(asUnicode(value))
if value == self.val:
return
prev = self.val
self.val = value
if update:
self.updateText(prev=prev)
self.sigValueChanging.emit(self, float(self.val)) ## change will be emitted in 300ms if there are no subsequent changes.
if not delaySignal:
self.emitChanged()
return value | Set the value of this spin.
If the value is out of bounds, it will be clipped to the nearest boundary.
If the spin is integer type, the value will be coerced to int.
Returns the actual value set.
If value is None, then the current value is used (this is for resetting
the value after bounds, etc. have changed) | entailment |
def interpret(self):
"""Return value of text. Return False if text is invalid, raise exception if text is intermediate"""
strn = self.lineEdit().text()
suf = self.opts['suffix']
if len(suf) > 0:
if strn[-len(suf):] != suf:
return False
#raise Exception("Units are invalid.")
strn = strn[:-len(suf)]
try:
val = fn.siEval(strn)
except:
#sys.excepthook(*sys.exc_info())
#print "invalid"
return False
#print val
return val | Return value of text. Return False if text is invalid, raise exception if text is intermediate | entailment |
def editingFinishedEvent(self):
"""Edit has finished; set value."""
#print "Edit finished."
if asUnicode(self.lineEdit().text()) == self.lastText:
#print "no text change."
return
try:
val = self.interpret()
except:
return
if val is False:
#print "value invalid:", str(self.lineEdit().text())
return
if val == self.val:
#print "no value change:", val, self.val
return
self.setValue(val, delaySignal=False) | Edit has finished; set value. | entailment |
def make_factory(self, cls, count):
""" Get the generators from the Scaffolding class within the model.
"""
field_names = cls._meta.get_all_field_names()
fields = {}
text = []
finalizer = None
scaffold = scaffolding.scaffold_for_model(cls)
for field_name in field_names:
generator = getattr(scaffold, field_name, None)
if generator:
if hasattr(generator, 'set_up'):
generator.set_up(cls, count)
fields[field_name] = generator
text.append(u'%s: %s; ' % (field_name, fields[field_name]))
try:
self.stdout.write(u'Generator for %s: %s\n' % (cls, u''.join(text)))
except models.ObjectDoesNotExist:
self.stdout.write(u'Generator for %s\n' % u''.join(text))
if hasattr(scaffold, 'finalize') and hasattr(scaffold.finalize, '__call__'):
finalizer = scaffold.finalize
return fields, finalizer | Get the generators from the Scaffolding class within the model. | entailment |
def compute_indel_length(fs_df):
"""Computes the indel length accounting for wether it is an insertion or
deletion.
Parameters
----------
fs_df : pd.DataFrame
mutation input as dataframe only containing indel mutations
Returns
-------
indel_len : pd.Series
length of indels
"""
indel_len = pd.Series(index=fs_df.index)
indel_len[fs_df['Reference_Allele']=='-'] = fs_df['Tumor_Allele'][fs_df['Reference_Allele']=='-'].str.len()
indel_len[fs_df['Tumor_Allele']=='-'] = fs_df['Reference_Allele'][fs_df['Tumor_Allele']=='-'].str.len()
indel_len = indel_len.fillna(0).astype(int)
return indel_len | Computes the indel length accounting for wether it is an insertion or
deletion.
Parameters
----------
fs_df : pd.DataFrame
mutation input as dataframe only containing indel mutations
Returns
-------
indel_len : pd.Series
length of indels | entailment |
def keep_indels(mut_df,
indel_len_col=True,
indel_type_col=True):
"""Filters out all mutations that are not indels.
Requires that one of the alleles have '-' indicating either an insertion
or deletion depending if found in reference allele or somatic allele
columns, respectively.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
indel_len_col : bool
whether or not to add a column indicating the length of the indel
Returns
-------
mut_df : pd.DataFrame
mutations with only frameshift mutations kept
"""
# keep only frameshifts
mut_df = mut_df[is_indel_annotation(mut_df)]
if indel_len_col:
# calculate length
mut_df.loc[:, 'indel len'] = compute_indel_length(mut_df)
if indel_type_col:
is_ins = mut_df['Reference_Allele']=='-'
is_del = mut_df['Tumor_Allele']=='-'
mut_df['indel type'] = ''
mut_df.loc[is_ins, 'indel type'] = 'INS'
mut_df.loc[is_del, 'indel type'] = 'DEL'
return mut_df | Filters out all mutations that are not indels.
Requires that one of the alleles have '-' indicating either an insertion
or deletion depending if found in reference allele or somatic allele
columns, respectively.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
indel_len_col : bool
whether or not to add a column indicating the length of the indel
Returns
-------
mut_df : pd.DataFrame
mutations with only frameshift mutations kept | entailment |
def keep_frameshifts(mut_df,
indel_len_col=True):
"""Filters out all mutations that are not frameshift indels.
Requires that one of the alleles have '-' indicating either an insertion
or deletion depending if found in reference allele or somatic allele
columns, respectively.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
indel_len_col : bool
whether or not to add a column indicating the length of the frameshift
Returns
-------
mut_df : pd.DataFrame
mutations with only frameshift mutations kept
"""
# keep only frameshifts
mut_df = mut_df[is_frameshift_annotation(mut_df)]
if indel_len_col:
# calculate length
mut_df.loc[:, 'indel len'] = compute_indel_length(mut_df)
return mut_df | Filters out all mutations that are not frameshift indels.
Requires that one of the alleles have '-' indicating either an insertion
or deletion depending if found in reference allele or somatic allele
columns, respectively.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
indel_len_col : bool
whether or not to add a column indicating the length of the frameshift
Returns
-------
mut_df : pd.DataFrame
mutations with only frameshift mutations kept | entailment |
def is_frameshift_len(mut_df):
"""Simply returns a series indicating whether each corresponding mutation
is a frameshift.
This is based on the length of the indel. Thus may be fooled by frameshifts
at exon-intron boundaries or other odd cases.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
Returns
-------
is_fs : pd.Series
pandas series indicating if mutaitons are frameshifts
"""
# calculate length, 0-based coordinates
#indel_len = mut_df['End_Position'] - mut_df['Start_Position']
if 'indel len' in mut_df.columns:
indel_len = mut_df['indel len']
else:
indel_len = compute_indel_length(mut_df)
# only non multiples of 3 are frameshifts
is_fs = (indel_len%3)>0
# make sure no single base substitutions are counted
is_indel = (mut_df['Reference_Allele']=='-') | (mut_df['Tumor_Allele']=='-')
is_fs[~is_indel] = False
return is_fs | Simply returns a series indicating whether each corresponding mutation
is a frameshift.
This is based on the length of the indel. Thus may be fooled by frameshifts
at exon-intron boundaries or other odd cases.
Parameters
----------
mut_df : pd.DataFrame
mutation input file as a dataframe in standard format
Returns
-------
is_fs : pd.Series
pandas series indicating if mutaitons are frameshifts | entailment |
def get_frameshift_lengths(num_bins):
"""Simple function that returns the lengths for each frameshift category
if `num_bins` number of frameshift categories are requested.
"""
fs_len = []
i = 1
tmp_bins = 0
while(tmp_bins<num_bins):
if i%3:
fs_len.append(i)
tmp_bins += 1
i += 1
return fs_len | Simple function that returns the lengths for each frameshift category
if `num_bins` number of frameshift categories are requested. | entailment |
def _init_context(self, gene_seq):
"""Initializes attributes defining mutation contexts and their position.
The self.context2pos and self.pos2context dictionaries map from
sequence context to sequence position and sequence position to
sequence context, respectively. These attributes allow for randomly
sampling of mutation positions while respecting sequence context in the
randomization-based test.
Parameters
----------
gene_seq : GeneSequence
GeneSequence object from the gene_sequence module
"""
self.context2pos, self.pos2context = {}, {}
gene_len = len(gene_seq.exon_seq) # get length of CDS
five_ss_len = 2*len(gene_seq.five_prime_seq) # total length of 5' splice sites
three_ss_len = 2*len(gene_seq.three_prime_seq) # total length of 3' splice sites
if gene_seq.nuc_context in [1, 2]:
# case where context matters
index_context = int(gene_seq.nuc_context) - 1 # subtract 1 since python is zero-based index
for i in range(index_context, gene_len):
nucs = gene_seq.exon_seq[i-index_context:i+1]
self.context2pos.setdefault(nucs, [])
self.context2pos[nucs].append(i)
self.pos2context[i] = nucs
# sequence context for five prime splice site
for i, five_ss in enumerate(gene_seq.five_prime_seq):
first_nucs = five_ss[1-index_context:1+1]
second_nucs = five_ss[2-index_context:2+1]
first_pos = 2*i + gene_len
second_pos = 2*i + gene_len + 1
self.context2pos.setdefault(first_nucs, [])
self.context2pos[first_nucs].append(first_pos)
self.context2pos.setdefault(second_nucs, [])
self.context2pos[second_nucs].append(second_pos)
self.pos2context[first_pos] = first_nucs
self.pos2context[second_pos] = second_nucs
# sequence context for three prime splice site
for i, three_ss in enumerate(gene_seq.three_prime_seq):
first_nucs = three_ss[1-index_context:1+1]
second_nucs = three_ss[2-index_context:2+1]
first_pos = 2*i + gene_len + five_ss_len
second_pos = 2*i + gene_len + five_ss_len + 1
self.context2pos.setdefault(first_nucs, [])
self.context2pos[first_nucs].append(first_pos)
self.context2pos.setdefault(second_nucs, [])
self.context2pos[second_nucs].append(second_pos)
self.pos2context[first_pos] = first_nucs
self.pos2context[second_pos] = second_nucs
# hack solution for context for first nuc
if gene_seq.exon_seq and gene_seq.nuc_context > 1:
self.pos2context[0] = gene_seq.exon_seq[0] * 2
self.context2pos.setdefault(gene_seq.exon_seq[0]*2, [])
self.context2pos[gene_seq.exon_seq[0]*2].append(0)
elif gene_seq.nuc_context in [1.5, 3]:
# use the nucleotide context from chasm if nuc
# context is 1.5 otherwise always use a three
# nucleotide context
ncontext = gene_seq.nuc_context
for i in range(1, len(gene_seq.exon_seq)-1):
nucs = gene_seq.exon_seq[i-1:i+2]
if ncontext == 1.5:
context = prob2020.python.mutation_context.get_chasm_context(nucs)
else:
context = nucs
self.context2pos.setdefault(context, [])
self.context2pos[context].append(i)
self.pos2context[i] = context
# sequence context for five prime splice site
for i, five_ss in enumerate(gene_seq.five_prime_seq):
first_nucs = five_ss[:3]
second_nucs = five_ss[1:4]
first_pos = 2*i + gene_len
second_pos = 2*i + gene_len + 1
if ncontext == 1.5:
first_context = prob2020.python.mutation_context.get_chasm_context(first_nucs)
second_context = prob2020.python.mutation_context.get_chasm_context(second_nucs)
else:
first_context = first_nucs
second_context = second_nucs
self.context2pos.setdefault(first_context, [])
self.context2pos[first_context].append(first_pos)
self.context2pos.setdefault(second_context, [])
self.context2pos[second_context].append(second_pos)
self.pos2context[first_pos] = first_context
self.pos2context[second_pos] = second_context
# sequence context for three prime splice site
for i, three_ss in enumerate(gene_seq.three_prime_seq):
first_nucs = three_ss[:3]
second_nucs = three_ss[1:4]
first_pos = 2*i + gene_len + five_ss_len
second_pos = 2*i + gene_len + five_ss_len + 1
if ncontext == 1.5:
first_context = prob2020.python.mutation_context.get_chasm_context(first_nucs)
second_context = prob2020.python.mutation_context.get_chasm_context(second_nucs)
else:
first_context = first_nucs
second_context = second_nucs
self.context2pos.setdefault(first_context, [])
self.context2pos[first_context].append(first_pos)
self.context2pos.setdefault(second_context, [])
self.context2pos[second_context].append(second_pos)
self.pos2context[first_pos] = first_context
self.pos2context[second_pos] = second_context
# hack solution for context for first nuc
if gene_seq.exon_seq:
first_nuc = gene_seq.exon_seq[0] + gene_seq.exon_seq[:2]
if ncontext == 1.5:
first_context = prob2020.python.mutation_context.get_chasm_context(first_nuc)
else:
first_context = first_nuc
self.pos2context[0] = first_context
self.context2pos.setdefault(first_context, [])
self.context2pos[first_context].append(0)
last_nuc = gene_seq.exon_seq[-2:] + gene_seq.exon_seq[-1]
if ncontext == 1.5:
last_context = prob2020.python.mutation_context.get_chasm_context(last_nuc)
else:
last_context = last_nuc
last_pos = len(gene_seq.exon_seq) - 1
self.pos2context[last_pos] = first_context
self.context2pos.setdefault(last_context, [])
self.context2pos[last_context].append(last_pos)
else:
# case where there is no context,
# mutations occur with uniform probability at each
# position
for i in range(gene_len + five_ss_len + three_ss_len):
self.pos2context[i] = 'None'
self.context2pos['None'] = range(gene_len + five_ss_len + three_ss_len) | Initializes attributes defining mutation contexts and their position.
The self.context2pos and self.pos2context dictionaries map from
sequence context to sequence position and sequence position to
sequence context, respectively. These attributes allow for randomly
sampling of mutation positions while respecting sequence context in the
randomization-based test.
Parameters
----------
gene_seq : GeneSequence
GeneSequence object from the gene_sequence module | entailment |
def random_context_pos(self, num, num_permutations, context):
"""Samples with replacement available positions matching the
sequence context.
Note: this method does random sampling only for an individual
sequence context.
Parameters
----------
num : int
Number of positions to sample for each permutation. This
is the number of actually observed mutations having the
matching sequence context for this gene.
num_permutations : int
Number of permutations for permutation test.
context : str
Sequence context.
Returns
-------
random_pos : np.array
num_permutations X num sized array that represents the
randomly sampled positions for a specific context.
"""
# make sure provide context is valid
if not self.is_valid_context(context):
error_msg = 'Context ({0}) was never seen in sequence.'.format(context)
raise ValueError(error_msg)
# make sure sampling is a positive integer
if num < 1:
error_msg = ('There must be at least one sample (specified {0}) '
'for a context'.format(num))
raise ValueError(error_msg)
# randomly select from available positions that fit the specified context
available_pos = self.context2pos[context]
random_pos = self.prng_dict[context].choice(available_pos, (num_permutations, num))
return random_pos | Samples with replacement available positions matching the
sequence context.
Note: this method does random sampling only for an individual
sequence context.
Parameters
----------
num : int
Number of positions to sample for each permutation. This
is the number of actually observed mutations having the
matching sequence context for this gene.
num_permutations : int
Number of permutations for permutation test.
context : str
Sequence context.
Returns
-------
random_pos : np.array
num_permutations X num sized array that represents the
randomly sampled positions for a specific context. | entailment |
def random_pos(self, context_iterable, num_permutations):
"""Obtains random positions w/ replacement which match sequence context.
Parameters
----------
context_iterable: iterable containing two element tuple
Records number of mutations in each context. context_iterable
should be something like [('AA', 5), ...].
num_permutations : int
Number of permutations used in the permutation test.
Returns
-------
position_list : list
Contains context string and the randomly chosen positions
for that context.
"""
position_list = []
for contxt, n in context_iterable:
pos_array = self.random_context_pos(n, num_permutations, contxt)
position_list.append([contxt, pos_array])
return position_list | Obtains random positions w/ replacement which match sequence context.
Parameters
----------
context_iterable: iterable containing two element tuple
Records number of mutations in each context. context_iterable
should be something like [('AA', 5), ...].
num_permutations : int
Number of permutations used in the permutation test.
Returns
-------
position_list : list
Contains context string and the randomly chosen positions
for that context. | entailment |
def multiprocess_permutation(bed_dict, mut_df, opts, indel_df=None):
"""Handles parallelization of permutations by splitting work
by chromosome.
"""
chroms = sorted(bed_dict.keys(), key=lambda x: len(bed_dict[x]), reverse=True)
multiprocess_flag = opts['processes']>0
if multiprocess_flag:
num_processes = opts['processes']
else:
num_processes = 1
#file_handle = open(opts['output'], 'w')
file_handle = opts['handle']
mywriter = csv.writer(file_handle, delimiter='\t', lineterminator='\n')
if opts['maf'] and opts['num_iterations']:
header = ['Gene', 'strand', 'Chromosome', 'Start_Position',
'End_Position', 'Reference_Allele', 'Tumor_Allele',
'Context', 'DNA_Change', 'Protein_Change', 'Variant_Classification']
elif opts['maf']:
header = ['Gene', 'strand', 'Chromosome', 'Start_Position',
'End_Position', 'Reference_Allele', 'Tumor_Allele',
'DNA_Change', 'Protein_Change', 'Variant_Classification',
'Tumor_Sample', 'Tumor_Type']
else:
header = ['Gene', 'ID', 'gene length', 'non-silent snv', 'silent snv', 'nonsense', 'lost stop',
'splice site', 'lost start', 'missense', 'recurrent missense',
'normalized missense position entropy',]
# add column header for scores, is user provided one
if opts['score_dir']:
header += ['Total Missense MGAEntropy', 'Total Missense VEST Score']
# add indel columns
header += ['frameshift indel', 'inframe indel', 'normalized mutation entropy']
mywriter.writerow(header)
num_iterations = opts['num_iterations']
# simulate indel counts
if opts['summary'] and num_iterations:
fs_cts, inframe_cts, gene_names = indel.simulate_indel_counts(indel_df,
bed_dict,
num_iterations,
opts['seed'])
name2ix = {gene_names[z]: z for z in range(len(gene_names))}
# just count observed indels
elif opts['summary']:
# get gene names
gene_names = [mybed.gene_name
for chrom in bed_dict
for mybed in bed_dict[chrom]]
name2ix = {gene_names[z]: z for z in range(len(gene_names))}
# initiate count vectors
inframe_cts = np.zeros((1, len(gene_names)))
fs_cts = np.zeros((1, len(gene_names)))
# populate observed counts
indel_cts_dict = indel_df['Gene'].value_counts().to_dict()
fs_cts_dict = indel_df[indel.is_frameshift_annotation(indel_df)]['Gene'].value_counts().to_dict()
for mygene in indel_cts_dict:
if mygene in name2ix:
# gene should be found in BED file annotation
ix = name2ix[mygene]
fs_cts[0, ix] = 0 if mygene not in fs_cts_dict else fs_cts_dict[mygene]
inframe_cts[0, ix] = indel_cts_dict[mygene] - fs_cts[0, ix]
# simulate snvs
obs_result = []
for i in range(0, len(chroms), num_processes):
if multiprocess_flag:
pool = Pool(processes=num_processes)
tmp_num_proc = len(chroms) - i if i + num_processes > len(chroms) else num_processes
info_repeat = ((bed_dict[chroms[tmp_ix]], mut_df, opts)
for tmp_ix in range(i, i+tmp_num_proc))
process_results = pool.imap(singleprocess_permutation, info_repeat)
process_results.next = utils.keyboard_exit_wrapper(process_results.next)
try:
# iterate through each chromosome result
for chrom_result in process_results:
# add columns for indels
if opts['summary']:
tmp_chrom_result = []
for gname, grp in it.groupby(chrom_result, lambda x: x[0]):
for l, row in enumerate(grp):
gene_ix = name2ix[gname]
fs_count = fs_cts[l, gene_ix]
inframe_count = inframe_cts[l, gene_ix]
missense_pos_ct = list(row.pop(-1).values()) # missense codon counts
silent_pos_ct = [1 for l in range(row[4])]
inactivating_ct = sum(row[5:9]) + fs_count
tmp_count_list = missense_pos_ct + silent_pos_ct + [inactivating_ct, inframe_count]
norm_ent = math.normalized_mutation_entropy(tmp_count_list)
tmp_chrom_result.append(row+[fs_count, inframe_count, norm_ent])
chrom_result = tmp_chrom_result
# write output to file
mywriter.writerows(chrom_result)
except KeyboardInterrupt:
pool.close()
pool.join()
logger.info('Exited by user. ctrl-c')
sys.exit(0)
pool.close()
pool.join()
else:
# perform simulation
info = (bed_dict[chroms[i]], mut_df, opts)
chrom_results = singleprocess_permutation(info)
# add indel columns
if opts['summary']:
tmp_chrom_result = []
for gname, grp in it.groupby(chrom_results, lambda x: x[0]):
for l, row in enumerate(grp):
gene_ix = name2ix[gname]
fs_count = fs_cts[l, gene_ix]
inframe_count = inframe_cts[l, gene_ix]
missense_pos_ct = list(row.pop(-1).values()) # missense codon counts
silent_pos_ct = [1 for l in range(row[4])]
inactivating_ct = sum(row[5:9]) + fs_count
tmp_count_list = missense_pos_ct + silent_pos_ct + [inactivating_ct, inframe_count]
norm_ent = math.normalized_mutation_entropy(tmp_count_list)
tmp_chrom_result.append(row+[fs_count, inframe_count, norm_ent])
chrom_results = tmp_chrom_result
# write to file
mywriter.writerows(chrom_results) | Handles parallelization of permutations by splitting work
by chromosome. | entailment |
def multiprocess_permutation(bed_dict, mut_df, opts):
"""Handles parallelization of permutations by splitting work
by chromosome.
"""
chroms = sorted(bed_dict.keys())
multiprocess_flag = opts['processes']>0
if multiprocess_flag:
num_processes = opts['processes']
else:
num_processes = 1
num_permutations = opts['num_permutations']
if not opts['by_sample']:
obs_result = []
else:
uniq_samp = mut_df['Tumor_Sample'].unique()
obs_result = pd.DataFrame(np.zeros((len(uniq_samp), len(cols))),
index=uniq_samp, columns=cols)
# initialize list containing output
if not opts['score_dir']:
result_list = [[0, 0, 0, 0, 0, 0, 0] for k in range(num_permutations)]
else:
result_list = [[0, 0, 0, 0, 0, 0, 0, 0, 0] for k in range(num_permutations)]
# iterate over each chromosome
for i in range(0, len(chroms), num_processes):
if multiprocess_flag:
pool = Pool(processes=num_processes)
tmp_num_proc = len(chroms) - i if i + num_processes > len(chroms) else num_processes
info_repeat = ((bed_dict[chroms[tmp_ix]], mut_df, opts)
for tmp_ix in range(i, i+tmp_num_proc))
process_results = pool.imap(singleprocess_permutation, info_repeat)
process_results.next = utils.keyboard_exit_wrapper(process_results.next)
try:
for chrom_result, obs_mutations in process_results:
for j in range(num_permutations):
result_list[j][0] += chrom_result[j][0]
result_list[j][1] += chrom_result[j][1]
result_list[j][2] += chrom_result[j][2]
result_list[j][3] += chrom_result[j][3]
result_list[j][4] += chrom_result[j][4]
result_list[j][5] += chrom_result[j][5]
result_list[j][6] += chrom_result[j][6]
if opts['score_dir']:
result_list[j][7] += chrom_result[j][7]
result_list[j][8] += chrom_result[j][8]
if not opts['by_sample']:
obs_result.append(obs_mutations)
else:
obs_result = obs_result + obs_mutations
except KeyboardInterrupt:
pool.close()
pool.join()
logger.info('Exited by user. ctrl-c')
sys.exit(0)
pool.close()
pool.join()
else:
info = (bed_dict[chroms[i]], mut_df, opts)
chrom_result, obs_mutations = singleprocess_permutation(info)
for j in range(num_permutations):
result_list[j][0] += chrom_result[j][0]
result_list[j][1] += chrom_result[j][1]
result_list[j][2] += chrom_result[j][2]
result_list[j][3] += chrom_result[j][3]
result_list[j][4] += chrom_result[j][4]
result_list[j][5] += chrom_result[j][5]
result_list[j][6] += chrom_result[j][6]
if opts['score_dir']:
result_list[j][7] += chrom_result[j][7]
result_list[j][8] += chrom_result[j][8]
if not opts['by_sample']:
obs_result.append(obs_mutations)
else:
obs_result = obs_result + obs_mutations
return result_list, obs_result | Handles parallelization of permutations by splitting work
by chromosome. | entailment |
def retrieve_scores(gname, sdir,
codon_pos, germ_aa, somatic_aa,
default_mga=5., default_vest=0,
no_file_flag=-1):
"""Retrieves scores from pickle files.
Used by summary script.
"""
# get variant types
#var_class = cutils.get_variant_classification(germ_aa, somatic_aa, codon_pos)
# get information about MGA entropy
mga_path = os.path.join(sdir, gname+".mgaentropy.pickle")
if os.path.exists(mga_path):
if sys.version_info < (3,):
# python 2.7 way
with open(mga_path) as handle:
mga_ent = pickle.load(handle)
else:
# python 3.X way
with open(mga_path, 'rb') as handle:
mga_ent = pickle.load(handle, encoding='latin-1')
else:
mga_ent = None
missense_pos = [p for i, p in enumerate(codon_pos)
if (germ_aa[i]!=somatic_aa[i]) and
(germ_aa[i] not in ['-', '*', 'Splice_Site']) and
(somatic_aa[i] not in ['-', '*', 'Splice_Site'])]
total_mga_ent = compute_mga_entropy_stat(mga_ent, missense_pos, sum, default_mga)
#mga_ent_ixs = [codon_pos[i] for i in range(len(var_class))
#if var_class[i] == 'Missense_Mutation']
#len_mga_ent = len(mga_ent)
#mga_ent_scores = [mga_ent[ix] for ix in mga_ent_ixs if ix < len_mga_ent]
#if mga_ent_scores:
#total_mga_ent = sum(mga_ent_scores)
#else:
#total_mga_ent = default_mga
#else:
#total_mga_ent = no_file_flag
# get information about VEST scores
vest_path = os.path.join(sdir, gname+".vest.pickle")
if os.path.exists(vest_path):
if sys.version_info < (3,):
# python 2.7 way
with open(vest_path) as handle:
vest_score = pickle.load(handle)
else:
# python 3.X way
with open(vest_path, 'rb') as handle:
vest_score = pickle.load(handle, encoding='latin-1')
else:
vest_score = None
total_vest = compute_vest_stat(vest_score,
germ_aa, somatic_aa, codon_pos,
stat_func=sum, default_val=default_vest)
#vest_scores = [vest_score.get(codon_pos[i]+1, {}).get(germ_aa[i], {}).get(somatic_aa[i], default_vest)
#for i in range(len(var_class))
#if var_class[i] == 'Missense_Mutation']
#total_vest = sum(vest_scores)
#else:
#total_vest = no_file_flag
return total_mga_ent, total_vest | Retrieves scores from pickle files.
Used by summary script. | entailment |
def read_vest_pickle(gname, score_dir):
"""Read in VEST scores for given gene.
Parameters
----------
gname : str
name of gene
score_dir : str
directory containing vest scores
Returns
-------
gene_vest : dict or None
dict containing vest scores for gene. Returns None if not found.
"""
vest_path = os.path.join(score_dir, gname+".vest.pickle")
if os.path.exists(vest_path):
if sys.version_info < (3,):
with open(vest_path) as handle:
gene_vest = pickle.load(handle)
else:
with open(vest_path, 'rb') as handle:
gene_vest = pickle.load(handle, encoding='latin-1')
return gene_vest
else:
return None | Read in VEST scores for given gene.
Parameters
----------
gname : str
name of gene
score_dir : str
directory containing vest scores
Returns
-------
gene_vest : dict or None
dict containing vest scores for gene. Returns None if not found. | entailment |
def compute_vest_stat(vest_dict, ref_aa, somatic_aa, codon_pos,
stat_func=np.mean,
default_val=0.0):
"""Compute missense VEST score statistic.
Note: non-missense mutations are intentially not filtered out and will take
a default value of zero.
Parameters
----------
vest_dict : dict
dictionary containing vest scores across the gene of interest
ref_aa: list of str
list of reference amino acids
somatic_aa: list of str
somatic mutation aa
codon_pos : list of int
position of codon in protein sequence
stat_func : function, default=np.mean
function that calculates a statistic
default_val : float
default value to return if there are no mutations
Returns
-------
score_stat : float
vest score statistic for provided mutation list
"""
# return default value if VEST scores are missing
if vest_dict is None:
return default_val
# fetch scores
myscores = fetch_vest_scores(vest_dict, ref_aa, somatic_aa, codon_pos)
# calculate mean score
if myscores:
score_stat = stat_func(myscores)
else:
score_stat = default_val
return score_stat | Compute missense VEST score statistic.
Note: non-missense mutations are intentially not filtered out and will take
a default value of zero.
Parameters
----------
vest_dict : dict
dictionary containing vest scores across the gene of interest
ref_aa: list of str
list of reference amino acids
somatic_aa: list of str
somatic mutation aa
codon_pos : list of int
position of codon in protein sequence
stat_func : function, default=np.mean
function that calculates a statistic
default_val : float
default value to return if there are no mutations
Returns
-------
score_stat : float
vest score statistic for provided mutation list | entailment |
def compute_mga_entropy_stat(mga_vec, codon_pos,
stat_func=np.mean,
default_val=0.0):
"""Compute MGA entropy conservation statistic
Parameters
----------
mga_vec : np.array
numpy vector containing MGA Entropy conservation scores for residues
codon_pos : list of int
position of codon in protein sequence
stat_func : function, default=np.mean
function that calculates a statistic
default_val : float
default value to return if there are no mutations
Returns
-------
score_stat : float
MGA entropy score statistic for provided mutation list
"""
# return default value if VEST scores are missing
if mga_vec is None:
return default_val
# fetch scores
myscores = fetch_mga_scores(mga_vec, codon_pos)
# calculate mean score
if myscores is not None and len(myscores):
score_stat = stat_func(myscores)
else:
score_stat = default_val
return score_stat | Compute MGA entropy conservation statistic
Parameters
----------
mga_vec : np.array
numpy vector containing MGA Entropy conservation scores for residues
codon_pos : list of int
position of codon in protein sequence
stat_func : function, default=np.mean
function that calculates a statistic
default_val : float
default value to return if there are no mutations
Returns
-------
score_stat : float
MGA entropy score statistic for provided mutation list | entailment |
def fetch_vest_scores(vest_dict,
ref_aa, somatic_aa, codon_pos,
default_vest=0.0):
"""Get VEST scores from pre-computed scores in dictionary.
Note: either all mutations should be missense or non-missense intended
to have value equal to default.
Parameters
----------
vest_dict : dict
dictionary containing vest scores across the gene of interest
ref_aa: list of str
list of reference amino acids
somatic_aa: list of str
somatic mutation aa
codon_pos: list of int
position of codon in protein sequence
default_vest: float, default=0.0
value to use if VEST score not available for a given mutation
Returns
-------
vest_score_list: list
score results for mutations
"""
vest_score_list = []
for i in range(len(somatic_aa)):
# make sure position is valid
if codon_pos[i] is not None:
tmp_score = vest_dict.get(codon_pos[i]+1, {}).get(ref_aa[i], {}).get(somatic_aa[i], default_vest)
else:
tmp_score = 0.0
vest_score_list.append(tmp_score)
return vest_score_list | Get VEST scores from pre-computed scores in dictionary.
Note: either all mutations should be missense or non-missense intended
to have value equal to default.
Parameters
----------
vest_dict : dict
dictionary containing vest scores across the gene of interest
ref_aa: list of str
list of reference amino acids
somatic_aa: list of str
somatic mutation aa
codon_pos: list of int
position of codon in protein sequence
default_vest: float, default=0.0
value to use if VEST score not available for a given mutation
Returns
-------
vest_score_list: list
score results for mutations | entailment |
def fetch_mga_scores(mga_vec,
codon_pos,
default_mga=None):
"""Get MGAEntropy scores from pre-computed scores in array.
Parameters
----------
mga_vec : np.array
numpy vector containing MGA Entropy conservation scores for residues
codon_pos: list of int
position of codon in protein sequence
default_mga: float or None, default=None
value to use if MGA entropy score not available for a given mutation.
Drop mutations if no default specified.
Returns
-------
mga_ent_scores : np.array
score results for MGA entropy conservation
"""
# keep only positions in range of MGAEntropy scores
len_mga = len(mga_vec)
good_codon_pos = [p for p in codon_pos if p < len_mga]
# get MGAEntropy scores
if good_codon_pos:
mga_ent_scores = mga_vec[good_codon_pos]
else:
mga_ent_scores = None
return mga_ent_scores | Get MGAEntropy scores from pre-computed scores in array.
Parameters
----------
mga_vec : np.array
numpy vector containing MGA Entropy conservation scores for residues
codon_pos: list of int
position of codon in protein sequence
default_mga: float or None, default=None
value to use if MGA entropy score not available for a given mutation.
Drop mutations if no default specified.
Returns
-------
mga_ent_scores : np.array
score results for MGA entropy conservation | entailment |
def read_neighbor_graph_pickle(gname, graph_dir):
"""Read in neighbor graph for given gene.
Parameters
----------
gname : str
name of gene
graph_dir : str
directory containing gene graphs
Returns
-------
gene_graph : dict or None
neighbor graph as dict for gene. Returns None if not found.
"""
graph_path = os.path.join(graph_dir, gname+".pickle")
if os.path.exists(graph_path):
with open(graph_path) as handle:
gene_graph = pickle.load(handle)
return gene_graph
else:
return None | Read in neighbor graph for given gene.
Parameters
----------
gname : str
name of gene
graph_dir : str
directory containing gene graphs
Returns
-------
gene_graph : dict or None
neighbor graph as dict for gene. Returns None if not found. | entailment |
def compute_ng_stat(gene_graph, pos_ct, alpha=.5):
"""Compute the clustering score for the gene on its neighbor graph.
Parameters
----------
gene_graph : dict
Graph of spatially near codons. keys = nodes, edges = key -> value.
pos_ct : dict
missense mutation count for each codon
alpha : float
smoothing factor
Returns
-------
graph_score : float
score measuring the clustering of missense mutations in the graph
coverage : int
number of nodes that received non-zero weight
"""
# skip if there are no missense mutations
if not len(pos_ct):
return 1.0, 0
max_pos = max(gene_graph)
codon_vals = np.zeros(max_pos+1)
# smooth out mutation counts
for pos in pos_ct:
mut_count = pos_ct[pos]
# update neighbor values
neighbors = list(gene_graph[pos])
num_neighbors = len(neighbors)
codon_vals[neighbors] += alpha*mut_count
# update self-value
codon_vals[pos] += (1-alpha)*mut_count
# compute the normalized entropy
#total_cts = float(np.count_nonzero(codon_vals))
#graph_score = mymath.normalized_mutation_entropy(codon_vals, total_cts=total_cts)
# compute regular entropy
p = codon_vals / np.sum(codon_vals)
graph_score = mymath.shannon_entropy(p)
# get coverage
coverage = np.count_nonzero(p)
return graph_score, coverage | Compute the clustering score for the gene on its neighbor graph.
Parameters
----------
gene_graph : dict
Graph of spatially near codons. keys = nodes, edges = key -> value.
pos_ct : dict
missense mutation count for each codon
alpha : float
smoothing factor
Returns
-------
graph_score : float
score measuring the clustering of missense mutations in the graph
coverage : int
number of nodes that received non-zero weight | entailment |
def count_frameshift_total(mut_df,
bed_path,
use_unmapped=False,
to_zero_based=False):
"""Count frameshifts for each gene.
Parameters
----------
mut_df : pd.DataFrame
mutation input
bed_path : str
path to BED file containing reference tx for genes
use_unmapped : Bool
flag indicating whether to include frameshifts not mapping
to reference tx
to_zero_based : Bool
whether to convert end-coordinate to zero based for analysis
Returns
-------
fs_cts_df : pd.DataFrame
contains both total frameshift counts and frameshift counts
not mappable to the reference transcript.
"""
if to_zero_based:
mut_df['Start_Position'] = mut_df['Start_Position'] - 1
fs_cts = {} # frameshift count information for each gene
fs_df = indel.keep_frameshifts(mut_df)
for bed in utils.bed_generator(bed_path):
gene_df = fs_df[fs_df['Gene']==bed.gene_name]
# find it frameshift actually is on gene annotation
fs_pos = []
for ix, row in gene_df.iterrows():
indel_pos = [row['Start_Position'], row['End_Position']]
coding_pos = bed.query_position(bed.strand, row['Chromosome'], indel_pos)
fs_pos.append(coding_pos)
# mark frameshifts that could not be mapped to reference tx
gene_df['unmapped'] = [(1 if x is None else 0) for x in fs_pos]
total_fs = len(gene_df)
unmapped_fs = len(gene_df[gene_df['unmapped']==1])
# filter out frameshifts that did not match reference tx
if not use_unmapped:
gene_df = gene_df[gene_df['unmapped']==0]
total_fs -= unmapped_fs
info = [total_fs, unmapped_fs,]
fs_cts[bed.gene_name] = info
# prepare counts into a dataframe
fs_cts_df = pd.DataFrame.from_dict(fs_cts, orient='index')
cols = ['total', 'unmapped',]
fs_cts_df.columns = cols
return fs_cts_df | Count frameshifts for each gene.
Parameters
----------
mut_df : pd.DataFrame
mutation input
bed_path : str
path to BED file containing reference tx for genes
use_unmapped : Bool
flag indicating whether to include frameshifts not mapping
to reference tx
to_zero_based : Bool
whether to convert end-coordinate to zero based for analysis
Returns
-------
fs_cts_df : pd.DataFrame
contains both total frameshift counts and frameshift counts
not mappable to the reference transcript. | entailment |
def _fetch_3ss_fasta(fasta, gene_name, exon_num,
chrom, strand, start, end):
"""Retreives the 3' SS sequence flanking the specified exon.
Returns a string in fasta format with the first line containing
a ">" and the second line contains the two base pairs of 3' SS.
Parameters
----------
fasta : pysam.Fastafile
fasta object from pysam
gene_name : str
gene name used for fasta seq id
exon_num : int
the `exon_num` exon, used for seq id
chrom : str
chromsome
strand : str
strand, {'+', '-'}
start : int
0-based start position
end : int
0-based end position
Returns
-------
ss_fasta : str
string in fasta format with first line being seq id
"""
if strand == '-':
ss_seq = fasta.fetch(reference=chrom,
start=end-1,
end=end+3)
ss_seq = utils.rev_comp(ss_seq)
elif strand == '+':
ss_seq = fasta.fetch(reference=chrom,
start=start-3,
end=start+1)
ss_fasta = '>{0};exon{1};3SS\n{2}\n'.format(gene_name,
exon_num,
ss_seq.upper())
return ss_fasta | Retreives the 3' SS sequence flanking the specified exon.
Returns a string in fasta format with the first line containing
a ">" and the second line contains the two base pairs of 3' SS.
Parameters
----------
fasta : pysam.Fastafile
fasta object from pysam
gene_name : str
gene name used for fasta seq id
exon_num : int
the `exon_num` exon, used for seq id
chrom : str
chromsome
strand : str
strand, {'+', '-'}
start : int
0-based start position
end : int
0-based end position
Returns
-------
ss_fasta : str
string in fasta format with first line being seq id | entailment |
def fetch_gene_fasta(gene_bed, fasta_obj):
"""Retreive gene sequences in FASTA format.
Parameters
----------
gene_bed : BedLine
BedLine object representing a single gene
fasta_obj : pysam.Fastafile
fasta object for index retreival of sequence
Returns
-------
gene_fasta : str
sequence of gene in FASTA format
"""
gene_fasta = ''
strand = gene_bed.strand
exons = gene_bed.get_exons()
if strand == '-':
exons.reverse() # order exons 5' to 3', so reverse if '-' strand
# iterate over exons
for i, exon in enumerate(exons):
exon_seq = fasta_obj.fetch(reference=gene_bed.chrom,
start=exon[0],
end=exon[1]).upper()
if strand == '-':
exon_seq = utils.rev_comp(exon_seq)
exon_fasta = '>{0};exon{1}\n{2}\n'.format(gene_bed.gene_name,
i, exon_seq)
# get splice site sequence
if len(exons) == 1:
# splice sites don't matter if there is no splicing
ss_fasta = ''
elif i == 0:
# first exon only, get 3' SS
ss_fasta = _fetch_5ss_fasta(fasta_obj, gene_bed.gene_name, i,
gene_bed.chrom, strand, exon[0], exon[1])
elif i == (len(exons) - 1):
# last exon only, get 5' SS
ss_fasta = _fetch_3ss_fasta(fasta_obj, gene_bed.gene_name, i,
gene_bed.chrom, strand, exon[0], exon[1])
else:
# middle exon, get bot 5' and 3' SS
fasta_3ss = _fetch_3ss_fasta(fasta_obj, gene_bed.gene_name, i,
gene_bed.chrom, strand, exon[0], exon[1])
fasta_5ss = _fetch_5ss_fasta(fasta_obj, gene_bed.gene_name, i,
gene_bed.chrom, strand, exon[0], exon[1])
ss_fasta = fasta_5ss + fasta_3ss
gene_fasta += exon_fasta + ss_fasta
return gene_fasta | Retreive gene sequences in FASTA format.
Parameters
----------
gene_bed : BedLine
BedLine object representing a single gene
fasta_obj : pysam.Fastafile
fasta object for index retreival of sequence
Returns
-------
gene_fasta : str
sequence of gene in FASTA format | entailment |
def _reset_seq(self):
"""Updates attributes for gene represented in the self.bed attribute.
Sequences are always upper case.
"""
exon_seq_list, five_ss_seq_list, three_ss_seq_list = self._fetch_seq()
self.exon_seq = ''.join(exon_seq_list)
self.three_prime_seq = three_ss_seq_list
self.five_prime_seq = five_ss_seq_list
self._to_upper() | Updates attributes for gene represented in the self.bed attribute.
Sequences are always upper case. | entailment |
def add_germline_variants(self, germline_nucs, coding_pos):
"""Add potential germline variants into the nucleotide sequence.
Sequenced individuals may potentially have a SNP at a somatic mutation position.
Therefore they may differ from the reference genome. This method updates the gene
germline gene sequence to match the actual individual.
Parameters
----------
germline_nucs : list of str
list of DNA nucleotides containing the germline letter
coding_pos : int
0-based nucleotide position in coding sequence
NOTE: the self.exon_seq attribute is updated, no return value
"""
if len(germline_nucs) != len(coding_pos):
raise ValueError('Each germline nucleotide should have a coding position')
es = list(self.exon_seq)
for i in range(len(germline_nucs)):
gl_nuc, cpos = germline_nucs[i].upper(), coding_pos[i]
if not utils.is_valid_nuc(gl_nuc):
raise ValueError('{0} is not a valid nucleotide'.format(gl_nuc))
if cpos >= 0:
es[cpos] = gl_nuc
self.exon_seq = ''.join(es) | Add potential germline variants into the nucleotide sequence.
Sequenced individuals may potentially have a SNP at a somatic mutation position.
Therefore they may differ from the reference genome. This method updates the gene
germline gene sequence to match the actual individual.
Parameters
----------
germline_nucs : list of str
list of DNA nucleotides containing the germline letter
coding_pos : int
0-based nucleotide position in coding sequence
NOTE: the self.exon_seq attribute is updated, no return value | entailment |
def _to_upper(self):
"""Convert sequences to upper case."""
self.exon_seq = self.exon_seq.upper()
self.three_prime_seq = [s.upper() for s in self.three_prime_seq]
self.five_prime_seq = [s.upper() for s in self.five_prime_seq] | Convert sequences to upper case. | entailment |
def _fetch_seq(self):
"""Fetches gene sequence from PySAM fasta object.
Returns
-------
exons : list of str
list of exon nucleotide sequences
five_prime_ss : list of str
list of 5' splice site sequences
three_prime_ss : list of str
list of 3' splice site sequences
"""
exons = []
three_prime_ss = []
five_prime_ss = []
num_exons = self.bed.get_num_exons()
for i in range(num_exons):
# add exon sequence
tmp_id = '{0};exon{1}'.format(self.bed.gene_name, i)
tmp_exon = self.fasta.fetch(reference=tmp_id)
exons.append(tmp_exon)
# add splice site sequence
tmp_id_3ss = '{0};3SS'.format(tmp_id)
tmp_id_5ss = '{0};5SS'.format(tmp_id)
if num_exons == 1:
pass
elif i == 0:
tmp_5ss = self.fasta.fetch(tmp_id_5ss)
five_prime_ss.append(tmp_5ss)
elif i == (num_exons - 1):
tmp_3ss = self.fasta.fetch(tmp_id_3ss)
three_prime_ss.append(tmp_3ss)
else:
tmp_3ss = self.fasta.fetch(tmp_id_3ss)
tmp_5ss = self.fasta.fetch(tmp_id_5ss)
three_prime_ss.append(tmp_3ss)
five_prime_ss.append(tmp_5ss)
return exons, five_prime_ss, three_prime_ss | Fetches gene sequence from PySAM fasta object.
Returns
-------
exons : list of str
list of exon nucleotide sequences
five_prime_ss : list of str
list of 5' splice site sequences
three_prime_ss : list of str
list of 3' splice site sequences | entailment |
def correct_chrom_names(chroms):
"""Make sure chromosome names follow UCSC chr convention."""
chrom_list = []
for chrom in chroms:
# fix chrom numbering
chrom = str(chrom)
chrom = chrom.replace('23', 'X')
chrom = chrom.replace('24', 'Y')
chrom = chrom.replace('25', 'Mt')
if not chrom.startswith('chr'):
chrom = 'chr' + chrom
chrom_list.append(chrom)
return chrom_list | Make sure chromosome names follow UCSC chr convention. | entailment |
def fishers_method(pvals):
"""Fisher's method for combining independent p-values."""
pvals = np.asarray(pvals)
degrees_of_freedom = 2 * pvals.size
chisq_stat = np.sum(-2*np.log(pvals))
fishers_pval = stats.chi2.sf(chisq_stat, degrees_of_freedom)
return fishers_pval | Fisher's method for combining independent p-values. | entailment |
def cummin(x):
"""A python implementation of the cummin function in R"""
for i in range(1, len(x)):
if x[i-1] < x[i]:
x[i] = x[i-1]
return x | A python implementation of the cummin function in R | entailment |
def bh_fdr(pval):
"""A python implementation of the Benjamani-Hochberg FDR method.
This code should always give precisely the same answer as using
p.adjust(pval, method="BH") in R.
Parameters
----------
pval : list or array
list/array of p-values
Returns
-------
pval_adj : np.array
adjusted p-values according the benjamani-hochberg method
"""
pval_array = np.array(pval)
sorted_order = np.argsort(pval_array)
original_order = np.argsort(sorted_order)
pval_array = pval_array[sorted_order]
# calculate the needed alpha
n = float(len(pval))
pval_adj = np.zeros(int(n))
i = np.arange(1, int(n)+1, dtype=float)[::-1] # largest to smallest
pval_adj = np.minimum(1, cummin(n/i * pval_array[::-1]))[::-1]
return pval_adj[original_order] | A python implementation of the Benjamani-Hochberg FDR method.
This code should always give precisely the same answer as using
p.adjust(pval, method="BH") in R.
Parameters
----------
pval : list or array
list/array of p-values
Returns
-------
pval_adj : np.array
adjusted p-values according the benjamani-hochberg method | entailment |
def calc_deleterious_p_value(mut_info,
unmapped_mut_info,
sc,
gs,
bed,
num_permutations,
stop_thresh,
del_threshold,
pseudo_count,
seed=None):
"""Calculates the p-value for the number of inactivating SNV mutations.
Calculates p-value based on how many simulations exceed the observed value.
Parameters
----------
mut_info : dict
contains codon and amino acid residue information for mutations mappable
to provided reference tx.
unmapped_mut_info : dict
contains codon/amino acid residue info for mutations that are NOT mappable
to provided reference tx.
fs_ct : int
number of frameshifts for gene
prob_inactive : float
proportion of inactivating mutations out of total over all genes
sc : SequenceContext
object contains the nucleotide contexts for a gene such that new random
positions can be obtained while respecting nucleotide context.
gs : GeneSequence
contains gene sequence
bed : BedLine
just used to return gene name
num_permutations : int
number of permutations to perform to estimate p-value. more permutations
means more precision on the p-value.
seed : int (Default: None)
seed number to random number generator (None to be randomly set)
"""
#prng = np.random.RandomState(seed)
if len(mut_info) > 0:
mut_info['Coding Position'] = mut_info['Coding Position'].astype(int)
mut_info['Context'] = mut_info['Coding Position'].apply(lambda x: sc.pos2context[x])
# group mutations by context
cols = ['Context', 'Tumor_Allele']
unmapped_mut_df = pd.DataFrame(unmapped_mut_info)
tmp_df = pd.concat([mut_info[cols], unmapped_mut_df[cols]])
context_cts = tmp_df['Context'].value_counts()
context_to_mutations = dict((name, group['Tumor_Allele'])
for name, group in tmp_df.groupby('Context'))
# get deleterious info for actual mutations
aa_mut_info = mc.get_aa_mut_info(mut_info['Coding Position'],
mut_info['Tumor_Allele'].tolist(),
gs)
ref_aa = aa_mut_info['Reference AA'] + unmapped_mut_info['Reference AA']
somatic_aa = aa_mut_info['Somatic AA'] + unmapped_mut_info['Somatic AA']
codon_pos = aa_mut_info['Codon Pos'] + unmapped_mut_info['Codon Pos']
num_del = cutils.calc_deleterious_info(ref_aa, somatic_aa, codon_pos)
#num_del = fs_ct + num_snv_del
# skip permutation test if number of deleterious mutations is not at
# least meet some user-specified threshold
if num_del >= del_threshold:
# perform permutations
del_p_value = pm.deleterious_permutation(num_del,
context_cts,
context_to_mutations,
sc, # sequence context obj
gs, # gene sequence obj
num_permutations,
stop_thresh,
pseudo_count)
else:
del_p_value = None
else:
num_del = 0
del_p_value = None
result = [bed.gene_name, num_del, del_p_value]
return result | Calculates the p-value for the number of inactivating SNV mutations.
Calculates p-value based on how many simulations exceed the observed value.
Parameters
----------
mut_info : dict
contains codon and amino acid residue information for mutations mappable
to provided reference tx.
unmapped_mut_info : dict
contains codon/amino acid residue info for mutations that are NOT mappable
to provided reference tx.
fs_ct : int
number of frameshifts for gene
prob_inactive : float
proportion of inactivating mutations out of total over all genes
sc : SequenceContext
object contains the nucleotide contexts for a gene such that new random
positions can be obtained while respecting nucleotide context.
gs : GeneSequence
contains gene sequence
bed : BedLine
just used to return gene name
num_permutations : int
number of permutations to perform to estimate p-value. more permutations
means more precision on the p-value.
seed : int (Default: None)
seed number to random number generator (None to be randomly set) | entailment |
def calc_protein_p_value(mut_info,
unmapped_mut_info,
sc,
gs,
bed,
graph_dir,
num_permutations,
stop_thresh,
min_recurrent,
min_fraction):
"""Computes the p-value for clustering on a neighbor graph composed
of codons connected with edges if they are spatially near in 3D protein
structure.
Parameters
----------
Returns
-------
"""
if len(mut_info) > 0:
mut_info['Coding Position'] = mut_info['Coding Position'].astype(int)
mut_info['Context'] = mut_info['Coding Position'].apply(lambda x: sc.pos2context[x])
# group mutations by context
cols = ['Context', 'Tumor_Allele']
unmapped_mut_df = pd.DataFrame(unmapped_mut_info)
tmp_df = pd.concat([mut_info[cols], unmapped_mut_df[cols]])
context_cts = tmp_df['Context'].value_counts()
context_to_mutations = dict((name, group['Tumor_Allele'])
for name, group in tmp_df.groupby('Context'))
# get vest scores for gene if directory provided
if graph_dir:
gene_graph = scores.read_neighbor_graph_pickle(bed.gene_name, graph_dir)
if gene_graph is None:
logger.warning('Could not find neighbor graph for {0}, skipping . . .'.format(bed.gene_name))
else:
gene_graph = None
# get recurrent info for actual mutations
aa_mut_info = mc.get_aa_mut_info(mut_info['Coding Position'],
mut_info['Tumor_Allele'].tolist(),
gs)
codon_pos = aa_mut_info['Codon Pos'] + unmapped_mut_info['Codon Pos']
ref_aa = aa_mut_info['Reference AA'] + unmapped_mut_info['Reference AA']
somatic_aa = aa_mut_info['Somatic AA'] + unmapped_mut_info['Somatic AA']
num_recurrent, pos_ent, delta_pos_ent, pos_ct = cutils.calc_pos_info(codon_pos,
ref_aa,
somatic_aa,
min_frac=min_fraction,
min_recur=min_recurrent)
try:
# get vest score for actual mutations
graph_score, coverage = scores.compute_ng_stat(gene_graph, pos_ct)
# perform simulations to get p-value
protein_p_value, norm_graph_score = pm.protein_permutation(
graph_score, len(pos_ct), context_cts,
context_to_mutations,
sc, # sequence context obj
gs, # gene sequence obj
gene_graph, num_permutations, stop_thresh
)
except Exception as err:
exc_info = sys.exc_info()
norm_graph_score = 0.0
protein_p_value = 1.0
logger.warning('Codon numbering problem with '+bed.gene_name)
else:
norm_graph_score = 0.0
protein_p_value = 1.0
num_recurrent = 0
result = [bed.gene_name, num_recurrent, norm_graph_score, protein_p_value]
return result | Computes the p-value for clustering on a neighbor graph composed
of codons connected with edges if they are spatially near in 3D protein
structure.
Parameters
----------
Returns
------- | entailment |
def shannon_entropy(p):
"""Calculates shannon entropy in bits.
Parameters
----------
p : np.array
array of probabilities
Returns
-------
shannon entropy in bits
"""
return -np.sum(np.where(p!=0, p * np.log2(p), 0)) | Calculates shannon entropy in bits.
Parameters
----------
p : np.array
array of probabilities
Returns
-------
shannon entropy in bits | entailment |
def normalized_mutation_entropy(counts, total_cts=None):
"""Calculate the normalized mutation entropy based on a list/array
of mutation counts.
Note: Any grouping of mutation counts together should be done before hand
Parameters
----------
counts : np.array_like
array/list of mutation counts
Returns
-------
norm_ent : float
normalized entropy of mutation count distribution.
"""
cts = np.asarray(counts, dtype=float)
if total_cts is None:
total_cts = np.sum(cts)
if total_cts > 1:
p = cts / total_cts
ent = shannon_entropy(p)
max_ent = max_shannon_entropy(total_cts)
norm_ent = ent / max_ent
else:
norm_ent = 1.0
return norm_ent | Calculate the normalized mutation entropy based on a list/array
of mutation counts.
Note: Any grouping of mutation counts together should be done before hand
Parameters
----------
counts : np.array_like
array/list of mutation counts
Returns
-------
norm_ent : float
normalized entropy of mutation count distribution. | entailment |
def kl_divergence(p, q):
"""Compute the Kullback-Leibler (KL) divergence for discrete distributions.
Parameters
----------
p : np.array
"Ideal"/"true" Probability distribution
q : np.array
Approximation of probability distribution p
Returns
-------
kl : float
KL divergence of approximating p with the distribution q
"""
# make sure numpy arrays are floats
p = p.astype(float)
q = q.astype(float)
# compute kl divergence
kl = np.sum(np.where(p!=0, p*np.log2(p/q), 0))
return kl | Compute the Kullback-Leibler (KL) divergence for discrete distributions.
Parameters
----------
p : np.array
"Ideal"/"true" Probability distribution
q : np.array
Approximation of probability distribution p
Returns
-------
kl : float
KL divergence of approximating p with the distribution q | entailment |
def js_divergence(p, q):
"""Compute the Jensen-Shannon Divergence between two discrete distributions.
Parameters
----------
p : np.array
probability mass array (sums to 1)
q : np.array
probability mass array (sums to 1)
Returns
-------
js_div : float
js divergence between the two distrubtions
"""
m = .5 * (p+q)
js_div = .5*kl_divergence(p, m) + .5*kl_divergence(q, m)
return js_div | Compute the Jensen-Shannon Divergence between two discrete distributions.
Parameters
----------
p : np.array
probability mass array (sums to 1)
q : np.array
probability mass array (sums to 1)
Returns
-------
js_div : float
js divergence between the two distrubtions | entailment |
def js_distance(p, q):
"""Compute the Jensen-Shannon distance between two discrete distributions.
NOTE: JS divergence is not a metric but the sqrt of JS divergence is a
metric and is called the JS distance.
Parameters
----------
p : np.array
probability mass array (sums to 1)
q : np.array
probability mass array (sums to 1)
Returns
-------
js_dist : float
Jensen-Shannon distance between two discrete distributions
"""
js_dist = np.sqrt(js_divergence(p, q))
return js_dist | Compute the Jensen-Shannon distance between two discrete distributions.
NOTE: JS divergence is not a metric but the sqrt of JS divergence is a
metric and is called the JS distance.
Parameters
----------
p : np.array
probability mass array (sums to 1)
q : np.array
probability mass array (sums to 1)
Returns
-------
js_dist : float
Jensen-Shannon distance between two discrete distributions | entailment |
def _filter_utr(self, ex):
"""Filter out UTR regions from the exon list (ie retain only coding regions).
Coding regions are defined by the thickStart and thickEnd attributes.
Parameters
----------
ex : list of tuples
list of exon positions, [(ex1_start, ex1_end), ...]
Returns
-------
filtered_exons : list of tuples
exons with UTR regions "chopped" out
"""
# define coding region
coding_start = int(self.bed_tuple.thickStart)
coding_end = int(self.bed_tuple.thickEnd)
if (coding_end - coding_start) < 3:
# coding regions should have at least one codon, otherwise the
# region is invalid and does not indicate an actually coding region
logger.debug('{0} has an invalid coding region specified by thickStart '
'and thickEnd (only {1} bps long). This gene is possibly either '
'a non-coding transcript or a pseudo gene.'.format(self.gene_name,
coding_end-coding_start))
return []
filtered_exons = []
for exon in ex:
if exon[0] > coding_end and exon[1] > coding_end:
# exon has no coding region
pass
elif exon[0] < coding_start and exon[1] < coding_start:
# exon has no coding region
pass
elif exon[0] <= coding_start and exon[1] >= coding_end:
# coding region entirely contained within one exon
filtered_exons.append((coding_start, coding_end))
elif exon[0] <= coding_start and exon[1] < coding_end:
# only beginning of exon contains UTR
filtered_exons.append((coding_start, exon[1]))
elif exon[0] > coding_start and exon[1] >= coding_end:
# only end part of exon contains UTR
filtered_exons.append((exon[0], coding_end))
elif exon[0] > coding_start and exon[1] < coding_end:
# entire exon is coding
filtered_exons.append(exon)
else:
# exon is only a UTR
pass
return filtered_exons | Filter out UTR regions from the exon list (ie retain only coding regions).
Coding regions are defined by the thickStart and thickEnd attributes.
Parameters
----------
ex : list of tuples
list of exon positions, [(ex1_start, ex1_end), ...]
Returns
-------
filtered_exons : list of tuples
exons with UTR regions "chopped" out | entailment |
def _init_exons(self):
"""Sets a list of position intervals for each exon.
Only coding regions as defined by thickStart and thickEnd are kept.
Exons are stored in the self.exons attribute.
"""
exon_starts = [self.chrom_start + int(s)
for s in self.bed_tuple.blockStarts.strip(',').split(',')]
exon_sizes = list(map(int, self.bed_tuple.blockSizes.strip(',').split(',')))
# get chromosome intervals
exons = [(exon_starts[i], exon_starts[i] + exon_sizes[i])
for i in range(len(exon_starts))]
no_utr_exons = self._filter_utr(exons)
self.exons = no_utr_exons
self.exon_lens = [e[1] - e[0] for e in self.exons]
self.num_exons = len(self.exons)
self.cds_len = sum(self.exon_lens)
self.five_ss_len = 2*(self.num_exons-1)
self.three_ss_len = 2*(self.num_exons-1)
self._init_splice_site_pos() | Sets a list of position intervals for each exon.
Only coding regions as defined by thickStart and thickEnd are kept.
Exons are stored in the self.exons attribute. | entailment |
def init_genome_coordinates(self) :
"""Creates the self.seqpos2genome dictionary that converts positions
relative to the sequence to genome coordinates."""
self.seqpos2genome = {}
# record genome positions for each sequence position
seq_pos = 0
for estart, eend in self.exons:
for genome_pos in range(estart, eend):
if self.strand == '+':
self.seqpos2genome[seq_pos] = genome_pos
elif self.strand == '-':
tmp = self.cds_len - seq_pos - 1
self.seqpos2genome[tmp] = genome_pos
seq_pos += 1
# recode 5' splice site locations
for i in range(0, self.five_ss_len):
seq_pos = self.cds_len + i
ss_ix = i // 2 # the ss_ix'th 5'ss starting from upstream tx
pos_in_ss = i % 2 # whether first/second nuc in splice site
# determine genome coordinates for 5' splice site
if self.strand == '+':
self.seqpos2genome[seq_pos] = self.exons[ss_ix][1] + pos_in_ss
else:
exon_pos = -1 - ss_ix
self.seqpos2genome[seq_pos] = self.exons[exon_pos][0] - pos_in_ss - 1
# recode 3' splice site locations
for i in range(0, self.three_ss_len):
seq_pos = self.cds_len + self.five_ss_len + i
ss_ix = i // 2 # the ss_ix'th 3'ss starting from upstream tx
pos_in_ss = i % 2 # whether first/second nuc in splice site
# determine genome coordinates for 3' splice site
if self.strand == '+':
self.seqpos2genome[seq_pos] = self.exons[ss_ix+1][0] - 2 + pos_in_ss
else:
exon_pos = -1 - ss_ix
self.seqpos2genome[seq_pos] = self.exons[exon_pos-1][1] + 1 - pos_in_ss | Creates the self.seqpos2genome dictionary that converts positions
relative to the sequence to genome coordinates. | entailment |
def query_position(self, strand, chr, genome_coord):
"""Provides the relative position on the coding sequence for a given
genomic position.
Parameters
----------
chr : str
chromosome, provided to check validity of query
genome_coord : int
0-based position for mutation, actually used to get relative coding pos
Returns
-------
pos : int or None
position of mutation in coding sequence, returns None if mutation
does not match region found in self.exons
"""
# first check if valid
pos = None # initialize to invalid pos
if chr != self.chrom:
#logger.debug('Wrong chromosome queried. You provided {0} but gene is '
#'on {1}.'.format(chr, self.chrom))
# return pos
pass
if type(genome_coord) is list:
# handle case for indels
pos_left = self.query_position(strand, chr, genome_coord[0])
pos_right = self.query_position(strand, chr, genome_coord[1])
if pos_left is not None or pos_right is not None:
return [pos_left, pos_right]
else:
return None
# return position if contained within coding region or splice site
for i, (estart, eend) in enumerate(self.exons):
# in coding region
if estart <= genome_coord < eend:
if strand == '+':
prev_lens = sum(self.exon_lens[:i]) # previous exon lengths
pos = prev_lens + (genome_coord - estart)
elif strand == '-':
prev_lens = sum(self.exon_lens[:i]) # previous exon lengths
pos = prev_lens + (genome_coord - estart)
pos = self.cds_len - pos - 1 # flip coords because neg strand
return pos
# in splice site
elif (eend <= genome_coord < eend + 2) and i != self.num_exons-1:
if strand == '+':
pos = self.cds_len + 2*i + (genome_coord - eend)
elif strand == '-':
pos = self.cds_len + self.five_ss_len + 2*(self.num_exons-(i+2)) + (genome_coord - eend)
return pos
# in splice site
elif (estart - 2 <= genome_coord < estart) and i != 0:
if strand == '-':
pos = self.cds_len + 2*(self.num_exons-(i+2)) + (genome_coord - (estart - 2))
elif strand == '+':
pos = self.cds_len + self.five_ss_len + 2*(i-1) + (genome_coord - (estart - 2))
return pos
return pos | Provides the relative position on the coding sequence for a given
genomic position.
Parameters
----------
chr : str
chromosome, provided to check validity of query
genome_coord : int
0-based position for mutation, actually used to get relative coding pos
Returns
-------
pos : int or None
position of mutation in coding sequence, returns None if mutation
does not match region found in self.exons | entailment |
def start_logging(log_file='', log_level='INFO', verbose=False):
"""Start logging information into the log directory.
If os.devnull is specified as the log_file then the log file will
not actually be written to a file.
"""
if not log_file:
# create log directory if it doesn't exist
log_dir = os.path.abspath('log') + '/'
if not os.path.isdir(log_dir):
os.mkdir(log_dir)
# path to new log file
log_file = log_dir + 'log.run.' + str(datetime.datetime.now()).replace(':', '.') + '.txt'
# logger options
lvl = logging.DEBUG if log_level.upper() == 'DEBUG' else logging.INFO
# ignore warnings if not in debug
if log_level.upper() != 'DEBUG':
warnings.filterwarnings('ignore')
# define logging format
if verbose:
myformat = '%(asctime)s - %(name)s - %(levelname)s \n>>> %(message)s'
else:
myformat = '%(message)s'
# create logger
if not log_file == 'stdout':
# normal logging to a regular file
logging.basicConfig(level=lvl,
format=myformat,
filename=log_file,
filemode='w')
else:
# logging to stdout
root = logging.getLogger()
root.setLevel(lvl)
stdout_stream = logging.StreamHandler(sys.stdout)
stdout_stream.setLevel(lvl)
formatter = logging.Formatter(myformat)
stdout_stream.setFormatter(formatter)
root.addHandler(stdout_stream)
root.propagate = True | Start logging information into the log directory.
If os.devnull is specified as the log_file then the log file will
not actually be written to a file. | entailment |
def log_error_decorator(f):
"""Writes exception to log file if occured in decorated function.
This decorator wrapper is needed for multiprocess logging since otherwise
the python multiprocessing module will obscure the actual line of the error.
"""
@wraps(f)
def wrapper(*args, **kwds):
try:
result = f(*args, **kwds)
return result
except KeyboardInterrupt:
logger.info('Ctrl-C stopped a process.')
except Exception as e:
logger.exception(e)
raise
return wrapper | Writes exception to log file if occured in decorated function.
This decorator wrapper is needed for multiprocess logging since otherwise
the python multiprocessing module will obscure the actual line of the error. | entailment |
def filter_list(mylist, bad_ixs):
"""Removes indices from a list.
All elements in bad_ixs will be removed from the list.
Parameters
----------
mylist : list
list to filter out specific indices
bad_ixs : list of ints
indices to remove from list
Returns
-------
mylist : list
list with elements filtered out
"""
# indices need to be in reverse order for filtering
# to prevent .pop() from yielding eroneous results
bad_ixs = sorted(bad_ixs, reverse=True)
for i in bad_ixs:
mylist.pop(i)
return mylist | Removes indices from a list.
All elements in bad_ixs will be removed from the list.
Parameters
----------
mylist : list
list to filter out specific indices
bad_ixs : list of ints
indices to remove from list
Returns
-------
mylist : list
list with elements filtered out | entailment |
def rev_comp(seq):
"""Get reverse complement of sequence.
rev_comp will maintain the case of the sequence.
Parameters
----------
seq : str
nucleotide sequence. valid {a, c, t, g, n}
Returns
-------
rev_comp_seq : str
reverse complement of sequence
"""
rev_seq = seq[::-1]
rev_comp_seq = ''.join([base_pairing[s] for s in rev_seq])
return rev_comp_seq | Get reverse complement of sequence.
rev_comp will maintain the case of the sequence.
Parameters
----------
seq : str
nucleotide sequence. valid {a, c, t, g, n}
Returns
-------
rev_comp_seq : str
reverse complement of sequence | entailment |
def bed_generator(bed_path):
"""Iterates through a BED file yielding parsed BED lines.
Parameters
----------
bed_path : str
path to BED file
Yields
------
BedLine(line) : BedLine
A BedLine object which has parsed the individual line in
a BED file.
"""
with open(bed_path) as handle:
bed_reader = csv.reader(handle, delimiter='\t')
for line in bed_reader:
yield BedLine(line) | Iterates through a BED file yielding parsed BED lines.
Parameters
----------
bed_path : str
path to BED file
Yields
------
BedLine(line) : BedLine
A BedLine object which has parsed the individual line in
a BED file. | entailment |
def read_bed(file_path, restricted_genes=None):
"""Reads BED file and populates a dictionary separating genes
by chromosome.
Parameters
----------
file_path : str
path to BED file
filtered_genes: list
list of gene names to not use
Returns
-------
bed_dict: dict
dictionary mapping chromosome keys to a list of BED lines
"""
# read in entire bed file into a dict with keys as chromsomes
bed_dict = OrderedDict()
for bed_row in bed_generator(file_path):
is_restrict_flag = restricted_genes is None or bed_row.gene_name in restricted_genes
if is_restrict_flag:
bed_dict.setdefault(bed_row.chrom, [])
bed_dict[bed_row.chrom].append(bed_row)
sort_chroms = sorted(bed_dict.keys(), key=lambda x: len(bed_dict[x]), reverse=True)
bed_dict = OrderedDict((chrom, bed_dict[chrom]) for chrom in sort_chroms)
return bed_dict | Reads BED file and populates a dictionary separating genes
by chromosome.
Parameters
----------
file_path : str
path to BED file
filtered_genes: list
list of gene names to not use
Returns
-------
bed_dict: dict
dictionary mapping chromosome keys to a list of BED lines | entailment |
def _fix_mutation_df(mutation_df, only_unique=False):
"""Drops invalid mutations and corrects for 1-based coordinates.
TODO: Be smarter about what coordinate system is put in the provided
mutations.
Parameters
----------
mutation_df : pd.DataFrame
user provided mutations
only_unique : bool
flag indicating whether only unique mutations for each tumor sample
should be kept. This avoids issues when the same mutation has
duplicate reportings.
Returns
-------
mutation_df : pd.DataFrame
mutations filtered for being valid and correct mutation type. Also
converted 1-base coordinates to 0-based.
"""
# only keep allowed mutation types
orig_len = len(mutation_df) # number of mutations before filtering
mutation_df = mutation_df[mutation_df.Variant_Classification.isin(variant_snv)] # only keep SNV
type_len = len(mutation_df) # number of mutations after filtering based on mut type
# log the number of dropped mutations
log_msg = ('Dropped {num_dropped} mutations after only keeping '
'{mut_types}. Indels are processed separately.'.format(num_dropped=orig_len-type_len,
mut_types=', '.join(variant_snv)))
logger.info(log_msg)
# check if mutations are valid SNVs
valid_nuc_flag = (mutation_df['Reference_Allele'].apply(is_valid_nuc) & \
mutation_df['Tumor_Allele'].apply(is_valid_nuc))
mutation_df = mutation_df[valid_nuc_flag] # filter bad lines
mutation_df = mutation_df[mutation_df['Tumor_Allele'].apply(lambda x: len(x)==1)]
mutation_df = mutation_df[mutation_df['Reference_Allele'].apply(lambda x: len(x)==1)]
valid_len = len(mutation_df)
# log the number of dropped mutations
log_msg = ('Dropped {num_dropped} mutations after only keeping '
'valid SNVs'.format(num_dropped=type_len-valid_len))
logger.info(log_msg)
# drop duplicate mutations
if only_unique:
dup_cols = ['Tumor_Sample', 'Chromosome', 'Start_Position',
'End_Position', 'Reference_Allele', 'Tumor_Allele']
mutation_df = mutation_df.drop_duplicates(subset=dup_cols)
# log results of de-duplication
dedup_len = len(mutation_df)
log_msg = ('Dropped {num_dropped} mutations when removing '
'duplicates'.format(num_dropped=valid_len-dedup_len))
logger.info(log_msg)
# add dummy Protein_Change or Tumor_Type columns if not provided
# in file
if 'Tumor_Type' not in mutation_df.columns:
mutation_df['Tumor_Type'] = ''
if 'Protein_Change' not in mutation_df.columns:
mutation_df['Protein_Change'] = ''
# correct for 1-based coordinates
mutation_df['Start_Position'] = mutation_df['Start_Position'].astype(int) - 1
return mutation_df | Drops invalid mutations and corrects for 1-based coordinates.
TODO: Be smarter about what coordinate system is put in the provided
mutations.
Parameters
----------
mutation_df : pd.DataFrame
user provided mutations
only_unique : bool
flag indicating whether only unique mutations for each tumor sample
should be kept. This avoids issues when the same mutation has
duplicate reportings.
Returns
-------
mutation_df : pd.DataFrame
mutations filtered for being valid and correct mutation type. Also
converted 1-base coordinates to 0-based. | entailment |
def calc_windowed_sum(aa_mut_pos,
germ_aa,
somatic_aa,
window=[3]):
"""Calculate the sum of mutations within a window around a particular mutated
codon.
Parameters
----------
aa_mut_pos : list
list of mutated amino acid positions
germ_aa : list
Reference amino acid
somatic_aa : list
Somatic amino acid (if missense)
window : list
List of windows to calculate for
Returns
-------
pos_ctr : dict
dictionary of mutated positions (key) with associated counts (value)
pos_sum : dict of dict
Window size as first key points to dictionary of mutated positions (key)
with associated mutation count within the window size (value)
"""
pos_ctr, pos_sum = {}, {w: {} for w in window}
num_pos = len(aa_mut_pos)
# figure out the missense mutations
for i in range(num_pos):
pos = aa_mut_pos[i]
# make sure mutation is missense
if germ_aa[i] and somatic_aa[i] and germ_aa[i] != '*' and \
somatic_aa[i] != '*' and germ_aa[i] != somatic_aa[i]:
# should have a position, but if not skip it
if pos is not None:
pos_ctr.setdefault(pos, 0)
pos_ctr[pos] += 1
# calculate windowed sum
pos_list = sorted(pos_ctr.keys())
max_window = max(window)
for ix, pos in enumerate(pos_list):
tmp_sum = {w: 0 for w in window}
# go through the same and lower positions
for k in reversed(range(ix+1)):
pos2 = pos_list[k]
if pos2 < pos-max_window: break
for w in window:
if pos-w <= pos2:
tmp_sum[w] += pos_ctr[pos2]
# go though the higher positions
for l in range(ix+1, len(pos_list)):
pos2 = pos_list[l]
if pos2 > pos+max_window: break
for w in window:
if pos2 <= pos+w:
tmp_sum[w] += pos_ctr[pos2]
# iterate through all other positions
#for pos2 in pos_list:
#for w in window:
#if pos-w <= pos2 <= pos+w:
#tmp_sum[w] += pos_ctr[pos2]
# update windowed counts
for w in window:
pos_sum[w][pos] = tmp_sum[w]
return pos_ctr, pos_sum | Calculate the sum of mutations within a window around a particular mutated
codon.
Parameters
----------
aa_mut_pos : list
list of mutated amino acid positions
germ_aa : list
Reference amino acid
somatic_aa : list
Somatic amino acid (if missense)
window : list
List of windows to calculate for
Returns
-------
pos_ctr : dict
dictionary of mutated positions (key) with associated counts (value)
pos_sum : dict of dict
Window size as first key points to dictionary of mutated positions (key)
with associated mutation count within the window size (value) | entailment |
def get_all_context_names(context_num):
"""Based on the nucleotide base context number, return
a list of strings representing each context.
Parameters
----------
context_num : int
number representing the amount of nucleotide base context to use.
Returns
-------
a list of strings containing the names of the base contexts
"""
if context_num == 0:
return ['None']
elif context_num == 1:
return ['A', 'C', 'T', 'G']
elif context_num == 1.5:
return ['C*pG', 'CpG*', 'TpC*', 'G*pA',
'A', 'C', 'T', 'G']
elif context_num == 2:
dinucs = list(set(
[d1+d2
for d1 in 'ACTG'
for d2 in 'ACTG']
))
return dinucs
elif context_num == 3:
trinucs = list(set(
[t1+t2+t3
for t1 in 'ACTG'
for t2 in 'ACTG'
for t3 in 'ACTG']
))
return trinucs | Based on the nucleotide base context number, return
a list of strings representing each context.
Parameters
----------
context_num : int
number representing the amount of nucleotide base context to use.
Returns
-------
a list of strings containing the names of the base contexts | entailment |
def get_chasm_context(tri_nuc):
"""Returns the mutation context acording to CHASM.
For more information about CHASM's mutation context, look
at http://wiki.chasmsoftware.org/index.php/CHASM_Overview.
Essentially CHASM uses a few specified di-nucleotide contexts
followed by single nucleotide context.
Parameters
----------
tri_nuc : str
three nucleotide string with mutated base in the middle.
Returns
-------
chasm context : str
a string representing the context used in CHASM
"""
# check if string is correct length
if len(tri_nuc) != 3:
raise ValueError('Chasm context requires a three nucleotide string '
'(Provided: "{0}")'.format(tri_nuc))
# try dinuc context if found
if tri_nuc[1:] == 'CG':
return 'C*pG'
elif tri_nuc[:2] == 'CG':
return 'CpG*'
elif tri_nuc[:2] == 'TC':
return 'TpC*'
elif tri_nuc[1:] == 'GA':
return 'G*pA'
else:
# just return single nuc context
return tri_nuc[1] | Returns the mutation context acording to CHASM.
For more information about CHASM's mutation context, look
at http://wiki.chasmsoftware.org/index.php/CHASM_Overview.
Essentially CHASM uses a few specified di-nucleotide contexts
followed by single nucleotide context.
Parameters
----------
tri_nuc : str
three nucleotide string with mutated base in the middle.
Returns
-------
chasm context : str
a string representing the context used in CHASM | entailment |
def get_aa_mut_info(coding_pos, somatic_base, gene_seq):
"""Retrieves relevant information about the effect of a somatic
SNV on the amino acid of a gene.
Information includes the germline codon, somatic codon, codon
position, germline AA, and somatic AA.
Parameters
----------
coding_pos : iterable of ints
Contains the base position (0-based) of the mutations
somatic_base : list of str
Contains the somatic nucleotide for the mutations
gene_seq : GeneSequence
gene sequence
Returns
-------
aa_info : dict
information about the somatic mutation effect on AA's
"""
# if no mutations return empty result
if not somatic_base:
aa_info = {'Reference Codon': [],
'Somatic Codon': [],
'Codon Pos': [],
'Reference Nuc': [],
'Reference AA': [],
'Somatic AA': []}
return aa_info
# get codon information into three lists
ref_codon, codon_pos, pos_in_codon, ref_nuc = zip(*[cutils.pos_to_codon(gene_seq, p)
for p in coding_pos])
ref_codon, codon_pos, pos_in_codon, ref_nuc = list(ref_codon), list(codon_pos), list(pos_in_codon), list(ref_nuc)
# construct codons for mutations
mut_codon = [(list(x) if x != 'Splice_Site' else []) for x in ref_codon]
for i in range(len(mut_codon)):
# splice site mutations are not in a codon, so skip such mutations to
# prevent an error
if pos_in_codon[i] is not None:
pc = pos_in_codon[i]
mut_codon[i][pc] = somatic_base[i]
mut_codon = [(''.join(x) if x else 'Splice_Site') for x in mut_codon]
# output resulting info
aa_info = {'Reference Codon': ref_codon,
'Somatic Codon': mut_codon,
'Codon Pos': codon_pos,
'Reference Nuc': ref_nuc,
'Reference AA': [(utils.codon_table[r] if (r in utils.codon_table) else None)
for r in ref_codon],
'Somatic AA': [(utils.codon_table[s] if (s in utils.codon_table) else None)
for s in mut_codon]}
return aa_info | Retrieves relevant information about the effect of a somatic
SNV on the amino acid of a gene.
Information includes the germline codon, somatic codon, codon
position, germline AA, and somatic AA.
Parameters
----------
coding_pos : iterable of ints
Contains the base position (0-based) of the mutations
somatic_base : list of str
Contains the somatic nucleotide for the mutations
gene_seq : GeneSequence
gene sequence
Returns
-------
aa_info : dict
information about the somatic mutation effect on AA's | entailment |
def handle_tsg_results(permutation_result):
"""Handles result from TSG results.
Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save
"""
permutation_df = pd.DataFrame(sorted(permutation_result, key=lambda x: x[2] if x[2] is not None else 1.1),
columns=['gene', 'inactivating count', 'inactivating p-value',
'Total SNV Mutations', 'SNVs Unmapped to Ref Tx'])
permutation_df['inactivating p-value'] = permutation_df['inactivating p-value'].astype('float')
tmp_df = permutation_df[permutation_df['inactivating p-value'].notnull()]
# get benjamani hochberg adjusted p-values
permutation_df['inactivating BH q-value'] = np.nan
permutation_df.loc[tmp_df.index, 'inactivating BH q-value'] = mypval.bh_fdr(tmp_df['inactivating p-value'])
# sort output by p-value. due to no option to specify NaN order in
# sort, the df needs to sorted descendingly and then flipped
permutation_df = permutation_df.sort_values(by='inactivating p-value', ascending=False)
permutation_df = permutation_df.reindex(index=permutation_df.index[::-1])
# order result
permutation_df = permutation_df.set_index('gene', drop=False)
col_order = ['gene', 'Total SNV Mutations', 'SNVs Unmapped to Ref Tx',
#'Total Frameshift Mutations', 'Frameshifts Unmapped to Ref Tx',
'inactivating count', 'inactivating p-value',
'inactivating BH q-value']
return permutation_df[col_order] | Handles result from TSG results.
Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save | entailment |
def handle_oncogene_results(permutation_result, num_permutations):
"""Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save
"""
mycols = ['gene', 'num recurrent', 'position entropy',
'mean vest score', 'entropy p-value',
'vest p-value', 'Total Mutations', 'Unmapped to Ref Tx']
permutation_df = pd.DataFrame(permutation_result, columns=mycols)
# get benjamani hochberg adjusted p-values
permutation_df['entropy BH q-value'] = mypval.bh_fdr(permutation_df['entropy p-value'])
permutation_df['vest BH q-value'] = mypval.bh_fdr(permutation_df['vest p-value'])
# combine p-values
permutation_df['tmp entropy p-value'] = permutation_df['entropy p-value']
permutation_df['tmp vest p-value'] = permutation_df['vest p-value']
permutation_df.loc[permutation_df['entropy p-value']==0, 'tmp entropy p-value'] = 1. / num_permutations
permutation_df.loc[permutation_df['vest p-value']==0, 'tmp vest p-value'] = 1. / num_permutations
permutation_df['combined p-value'] = permutation_df[['entropy p-value', 'vest p-value']].apply(mypval.fishers_method, axis=1)
permutation_df['combined BH q-value'] = mypval.bh_fdr(permutation_df['combined p-value'])
del permutation_df['tmp vest p-value']
del permutation_df['tmp entropy p-value']
# order output
permutation_df = permutation_df.set_index('gene', drop=False) # make sure genes are indices
permutation_df['num recurrent'] = permutation_df['num recurrent'].fillna(-1).astype(int) # fix dtype isssue
col_order = ['gene', 'Total Mutations', 'Unmapped to Ref Tx',
'num recurrent', 'position entropy',
'mean vest score', 'entropy p-value',
'vest p-value', 'combined p-value', 'entropy BH q-value',
'vest BH q-value', 'combined BH q-value']
permutation_df = permutation_df.sort_values(by=['combined p-value'])
return permutation_df[col_order] | Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save | entailment |
def handle_hotmaps_results(permutation_result):
"""Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save
"""
if len(permutation_result[0]) == 6:
mycols = ['gene', 'window length', 'codon position', 'mutation count',
'windowed sum', 'p-value']
else:
mycols = ['gene', 'window length', 'codon position', 'index', 'mutation count',
'windowed sum', 'p-value']
permutation_df = pd.DataFrame(permutation_result, columns=mycols)
# get benjamani hochberg adjusted p-values
permutation_df['q-value'] = 1
for w in permutation_df['window length'].unique():
is_window = permutation_df['window length'] == w
permutation_df.loc[is_window, 'q-value'] = mypval.bh_fdr(permutation_df.loc[is_window, 'p-value'])
#permutation_df['q-value'] = mypval.bh_fdr(permutation_df['p-value'])
# order output
#permutation_df = permutation_df.set_index('gene', drop=False) # make sure genes are indices
col_order = mycols + ['q-value']
permutation_df = permutation_df.sort_values(by=['window length', 'p-value'])
return permutation_df[col_order] | Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save | entailment |
def handle_protein_results(permutation_result):
"""Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save
"""
mycols = ['gene', 'num recurrent', 'normalized graph-smoothed position entropy',
'normalized graph-smoothed position entropy p-value',
'Total Mutations', 'Unmapped to Ref Tx']
permutation_df = pd.DataFrame(permutation_result, columns=mycols)
# get benjamani hochberg adjusted p-values
permutation_df['normalized graph-smoothed position entropy BH q-value'] = mypval.bh_fdr(permutation_df['normalized graph-smoothed position entropy p-value'])
# order output
permutation_df = permutation_df.set_index('gene', drop=False) # make sure genes are indices
col_order = ['gene', 'Total Mutations', 'Unmapped to Ref Tx',
'num recurrent',
'normalized graph-smoothed position entropy',
'normalized graph-smoothed position entropy p-value',
'normalized graph-smoothed position entropy BH q-value']
permutation_df = permutation_df.sort_values(by=['normalized graph-smoothed position entropy p-value'])
return permutation_df[col_order] | Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save | entailment |
def handle_effect_results(permutation_result):
"""Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save
"""
mycols = ['gene', 'num recurrent', 'num inactivating', 'entropy-on-effect',
'entropy-on-effect p-value',
'Total Mutations', 'Unmapped to Ref Tx']
permutation_df = pd.DataFrame(sorted(permutation_result, key=lambda x: x[4] if x[4] is not None else 1.1),
columns=mycols)
# get benjamani hochberg adjusted p-values
permutation_df['entropy-on-effect BH q-value'] = mypval.bh_fdr(permutation_df['entropy-on-effect p-value'])
# order output
permutation_df = permutation_df.set_index('gene', drop=False) # make sure genes are indices
permutation_df['num recurrent'] = permutation_df['num recurrent'].fillna(-1).astype(int) # fix dtype isssue
col_order = ['gene', 'Total Mutations', 'Unmapped to Ref Tx',
'num recurrent', 'num inactivating', 'entropy-on-effect',
'entropy-on-effect p-value', 'entropy-on-effect BH q-value']
return permutation_df[col_order] | Takes in output from multiprocess_permutation function and converts to
a better formatted dataframe.
Parameters
----------
permutation_result : list
output from multiprocess_permutation
Returns
-------
permutation_df : pd.DataFrame
formatted output suitable to save | entailment |
def get_frameshift_info(fs_df, bins):
"""Counts frameshifts stratified by a given length.
Parameters
----------
fs_df : pd.DataFrame
indel mutations from non-coding portion
bins : int
number of different length categories for frameshifts
Returns
-------
indel_len : list
length of specific frameshift length category
num_indels : list
number of frameshifts matchin indel_len
"""
fs_df = compute_indel_length(fs_df)
# count the number INDELs with length non-dividable by 3
num_indels = []
indel_len = []
num_categories = 0
i = 1
while(num_categories<bins):
# not inframe length indel
if i%3:
if num_categories != bins-1:
tmp_num = len(fs_df[fs_df['indel len']==i])
else:
tmp_num = len(fs_df[(fs_df['indel len']>=i) & ((fs_df['indel len']%3)>0)])
num_indels.append(tmp_num)
indel_len.append(i)
num_categories += 1
i += 1
return indel_len, num_indels | Counts frameshifts stratified by a given length.
Parameters
----------
fs_df : pd.DataFrame
indel mutations from non-coding portion
bins : int
number of different length categories for frameshifts
Returns
-------
indel_len : list
length of specific frameshift length category
num_indels : list
number of frameshifts matchin indel_len | entailment |
def set_mutation_type(self, mut_type=''):
"""Sets the mutation type attribute to a single label based on
attribute flags.
Kwargs:
mut_type (str): value to set self.mut_type
"""
if mut_type:
# user specifies a mutation type
self.mutation_type = mut_type
else:
# mutation type is taken from object attributes
if not self.is_valid:
# does not correctly fall into a category
self.mutation_type = 'not valid'
elif self.unknown_effect:
self.mutation_type = 'unknown effect'
elif self.is_no_protein:
self.mutation_type = 'no protein'
elif self.is_missing_info:
# mutation has a ?
self.mutation_type = 'missing'
else:
# valid mutation type to be counted
if self.is_lost_stop:
self.mutation_type = 'Nonstop_Mutation'
elif self.is_lost_start:
self.mutation_type = 'Translation_Start_Site'
elif self.is_synonymous:
# synonymous must go before missense since mutations
# can be categorized as synonymous and missense. Although
# in reality such cases are actually synonymous and not
# missense mutations.
self.mutation_type = 'Silent'
elif self.is_missense:
self.mutation_type = 'Missense_Mutation'
elif self.is_indel:
self.mutation_type = 'In_Frame_Indel'
elif self.is_nonsense_mutation:
self.mutation_type = 'Nonsense_Mutation'
elif self.is_frame_shift:
self.mutation_type = 'Frame_Shift_Indel' | Sets the mutation type attribute to a single label based on
attribute flags.
Kwargs:
mut_type (str): value to set self.mut_type | entailment |
def set_amino_acid(self, aa):
"""Set amino acid change and position."""
aa = aa.upper() # make sure it is upper case
aa = aa[2:] if aa.startswith('P.') else aa # strip "p."
self.__set_mutation_status() # set flags detailing the type of mutation
self.__parse_hgvs_syntax(aa) | Set amino acid change and position. | entailment |
def __set_mutation_type(self, hgvs_string):
"""Interpret the mutation type (missense, etc.) and set appropriate flags.
Args:
hgvs_string (str): hgvs syntax with "p." removed
"""
self.__set_lost_stop_status(hgvs_string)
self.__set_lost_start_status(hgvs_string)
self.__set_missense_status(hgvs_string) # missense mutations
self.__set_indel_status() # indel mutations
self.__set_frame_shift_status() # check for fs
self.__set_premature_stop_codon_status(hgvs_string) | Interpret the mutation type (missense, etc.) and set appropriate flags.
Args:
hgvs_string (str): hgvs syntax with "p." removed | entailment |
def __set_missense_status(self, hgvs_string):
"""Sets the self.is_missense flag."""
# set missense status
if re.search('^[A-Z?]\d+[A-Z?]$', hgvs_string):
self.is_missense = True
self.is_non_silent = True
else:
self.is_missense = False | Sets the self.is_missense flag. | entailment |
def __set_lost_start_status(self, hgvs_string):
"""Sets the self.is_lost_start flag."""
# set is lost start status
mymatch = re.search('^([A-Z?])(\d+)([A-Z?])$', hgvs_string)
if mymatch:
grps = mymatch.groups()
if int(grps[1]) == 1 and grps[0] != grps[2]:
self.is_lost_start = True
self.is_non_silent = True
else:
self.is_lost_start = False
else:
self.is_lost_start = False | Sets the self.is_lost_start flag. | entailment |
def __set_frame_shift_status(self):
"""Check for frame shift and set the self.is_frame_shift flag."""
if 'fs' in self.hgvs_original:
self.is_frame_shift = True
self.is_non_silent = True
elif re.search('[A-Z]\d+[A-Z]+\*', self.hgvs_original):
# it looks like some mutations dont follow the convention
# of using 'fs' to indicate frame shift
self.is_frame_shift = True
self.is_non_silent = True
else:
self.is_frame_shift = False | Check for frame shift and set the self.is_frame_shift flag. | entailment |
def __set_lost_stop_status(self, hgvs_string):
"""Check if the stop codon was mutated to something other than
a stop codon."""
lost_stop_pattern = '^\*\d+[A-Z?]+\*?$'
if re.search(lost_stop_pattern, hgvs_string):
self.is_lost_stop = True
self.is_non_silent = True
else:
self.is_lost_stop = False | Check if the stop codon was mutated to something other than
a stop codon. | entailment |
def __set_premature_stop_codon_status(self, hgvs_string):
"""Set whether there is a premature stop codon."""
if re.search('.+\*(\d+)?$', hgvs_string):
self.is_premature_stop_codon = True
self.is_non_silent = True
# check if it is also a nonsense mutation
if hgvs_string.endswith('*'):
self.is_nonsense_mutation = True
else:
self.is_nonsense_mutation = False
else:
self.is_premature_stop_codon = False
self.is_nonsense_mutation = False | Set whether there is a premature stop codon. | entailment |
def __set_indel_status(self):
"""Sets flags related to the mutation being an indel."""
# set indel status
if "ins" in self.hgvs_original:
# mutation is insertion
self.is_insertion = True
self.is_deletion = False
self.is_indel = True
self.is_non_silent = True
elif "del" in self.hgvs_original:
# mutation is deletion
self.is_deletion = True
self.is_insertion = False
self.is_indel = True
self.is_non_silent = True
else:
# not an indel
self.is_deletion = False
self.is_insertion = False
self.is_indel = False | Sets flags related to the mutation being an indel. | entailment |
def __set_unkown_effect(self, hgvs_string):
"""Sets a flag for unkown effect according to HGVS syntax. The
COSMIC database also uses unconventional questionmarks to denote
missing information.
Args:
hgvs_string (str): hgvs syntax with "p." removed
"""
# Standard use by HGVS of indicating unknown effect.
unknown_effect_list = ['?', '(=)', '='] # unknown effect symbols
if hgvs_string in unknown_effect_list:
self.unknown_effect = True
elif "(" in hgvs_string:
# parethesis in HGVS indicate expected outcomes
self.unknown_effect = True
else:
self.unknown_effect = False
# detect if there are missing information. commonly COSMIC will
# have insertions with p.?_?ins? or deleteions with ?del indicating
# missing information.
if "?" in hgvs_string:
self.is_missing_info = True
else:
self.is_missing_info = False | Sets a flag for unkown effect according to HGVS syntax. The
COSMIC database also uses unconventional questionmarks to denote
missing information.
Args:
hgvs_string (str): hgvs syntax with "p." removed | entailment |
def __set_no_protein(self, hgvs_string):
"""Set a flag for no protein expected. ("p.0" or "p.0?")
Args:
hgvs_string (str): hgvs syntax with "p." removed
"""
no_protein_list = ['0', '0?'] # no protein symbols
if hgvs_string in no_protein_list:
self.is_no_protein = True
self.is_non_silent = True
else:
self.is_no_protein = False | Set a flag for no protein expected. ("p.0" or "p.0?")
Args:
hgvs_string (str): hgvs syntax with "p." removed | entailment |
def __parse_hgvs_syntax(self, aa_hgvs):
"""Convert HGVS syntax for amino acid change into attributes.
Specific details of the mutation are stored in attributes like
self.intial (prior to mutation), sel.pos (mutation position),
self.mutated (mutation), and self.stop_pos (position of stop codon,
if any).
Args:
aa_hgvs (str): amino acid string following HGVS syntax
"""
self.is_valid = True # assume initially the syntax is legitimate
self.is_synonymous = False # assume not synonymous until proven
if self.unknown_effect or self.is_no_protein:
# unknown effect from mutation. usually denoted as p.?
self.pos = None
pass
elif self.is_lost_stop:
self.initial = aa_hgvs[0]
self.mutated = re.findall('([A-Z?*]+)$', aa_hgvs)[0]
self.pos = int(re.findall('^\*(\d+)', aa_hgvs)[0])
self.stop_pos = None
elif self.is_lost_start:
self.initial = aa_hgvs[0]
self.mutated = aa_hgvs[-1]
self.pos = int(aa_hgvs[1:-1])
elif self.is_missense:
self.initial = aa_hgvs[0]
self.mutated = aa_hgvs[-1]
self.pos = int(aa_hgvs[1:-1])
self.stop_pos = None # not a nonsense mutation
if self.initial == self.mutated:
self.is_synonymous = True
self.is_non_silent = False
elif self.mutated == '*':
self.is_nonsense_mutation = True
elif self.is_indel:
if self.is_insertion:
if not self.is_missing_info:
self.initial = re.findall('([A-Z])\d+', aa_hgvs)[:2] # first two
self.pos = tuple(map(int, re.findall('[A-Z](\d+)', aa_hgvs)[:2])) # first two
self.mutated = re.findall('(?<=INS)[A-Z0-9?*]+', aa_hgvs)[0]
self.mutated = self.mutated.strip('?') # remove the missing info '?'
else:
self.initial = ''
self.pos = tuple()
self.mutated = ''
elif self.is_deletion:
if not self.is_missing_info:
self.initial = re.findall('([A-Z])\d+', aa_hgvs)
self.pos = tuple(map(int, re.findall('[A-Z](\d+)', aa_hgvs)))
self.mutated = re.findall('(?<=DEL)[A-Z]*', aa_hgvs)[0]
else:
self.initial = ''
self.pos = tuple()
self.mutated = ''
elif self.is_frame_shift:
self.initial = aa_hgvs[0]
self.mutated = ''
try:
self.pos = int(re.findall('[A-Z*](\d+)', aa_hgvs)[0])
if self.is_premature_stop_codon:
self.stop_pos = int(re.findall('\*>?(\d+)$', aa_hgvs)[0])
else:
self.stop_pos = None
except IndexError:
# unconventional usage of indicating frameshifts will cause
# index errors. For example, in some cases 'fs' is not used.
# In other cases, either amino acids were not included or
# just designated as a '?'
self.logger.debug('(Parsing-Problem) frame shift hgvs string: "%s"' % aa_hgvs)
self.pos = None
self.stop_pos = None
self.is_missing_info = True
elif self.is_nonsense_mutation:
self.initial = aa_hgvs[0]
self.mutated = '*' # there is actually a stop codon
self.stop_pos = 0 # indicates same position is stop codon
try:
self.pos = int(aa_hgvs[1:-1])
except ValueError:
# wierd error of p.E217>D*
self.is_valid = False
self.pos = None
self.logger.debug('(Parsing-Problem) Invalid HGVS Amino Acid '
'syntax: ' + aa_hgvs)
if self.initial == self.mutated:
# classify nonsense-to-nonsense mutations as synonymous
self.is_synonymous = True
self.is_non_silent = False
else:
self.is_valid = False # did not match any of the possible cases
self.logger.debug('(Parsing-Problem) Invalid HGVS Amino Acid '
'syntax: ' + aa_hgvs) | Convert HGVS syntax for amino acid change into attributes.
Specific details of the mutation are stored in attributes like
self.intial (prior to mutation), sel.pos (mutation position),
self.mutated (mutation), and self.stop_pos (position of stop codon,
if any).
Args:
aa_hgvs (str): amino acid string following HGVS syntax | entailment |
def deleterious_permutation(obs_del,
context_counts,
context_to_mut,
seq_context,
gene_seq,
num_permutations=10000,
stop_criteria=100,
pseudo_count=0,
max_batch=25000):
"""Performs null-permutations for deleterious mutation statistics
in a single gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
pseudo_count : int, default: 0
Pseudo-count for number of deleterious mutations for each
permutation of the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
del_count_list : list
list of deleterious mutation counts under the null
"""
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# calculate the # of batches for simulations
max_batch = min(num_permutations, max_batch)
num_batches = num_permutations // max_batch
remainder = num_permutations % max_batch
batch_sizes = [max_batch] * num_batches
if remainder:
batch_sizes += [remainder]
num_sim = 0
null_del_ct = 0
for j, batch_size in enumerate(batch_sizes):
# stop iterations if reached sufficient precision
if null_del_ct >= stop_criteria:
#j = j - 1
break
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
batch_size)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# determine result of random positions
for i, row in enumerate(tmp_mut_pos):
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calc deleterious mutation info
tmp_del_count = cutils.calc_deleterious_info(tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
tmp_mut_info['Codon Pos'])
# update empricial null distribution
if tmp_del_count >= obs_del: null_del_ct += 1
# stop if reach sufficient precision on p-value
if null_del_ct >= stop_criteria:
break
# update number of simulations
num_sim += i + 1
#num_sim = j*max_batch + i+1
del_pval = float(null_del_ct) / (num_sim)
return del_pval | Performs null-permutations for deleterious mutation statistics
in a single gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
pseudo_count : int, default: 0
Pseudo-count for number of deleterious mutations for each
permutation of the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
del_count_list : list
list of deleterious mutation counts under the null | entailment |
def position_permutation(obs_stat,
context_counts,
context_to_mut,
seq_context,
gene_seq,
gene_vest=None,
num_permutations=10000,
stop_criteria=100,
pseudo_count=0,
max_batch=25000):
"""Performs null-permutations for position-based mutation statistics
in a single gene.
Parameters
----------
obs_stat : tuple, (recur ct, entropy, delta entropy, mean vest)
tuple containing the observed statistics
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
pseudo_count : int, default: 0
Pseudo-count for number of recurrent missense mutations for each
permutation for the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
num_recur_list : list
list of recurrent mutation counts under the null
entropy_list : list
list of position entropy values under the null
"""
# get contexts and somatic base
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# calculate the # of batches for simulations
max_batch = min(num_permutations, max_batch)
num_batches = num_permutations // max_batch
remainder = num_permutations % max_batch
batch_sizes = [max_batch] * num_batches
if remainder:
batch_sizes += [remainder]
obs_recur, obs_ent, obs_delta_ent, obs_vest = obs_stat
num_sim = 0 # number of simulations
null_num_recur_ct, null_entropy_ct, null_delta_entropy_ct, null_vest_ct = 0, 0, 0, 0
for j, batch_size in enumerate(batch_sizes):
# stop iterations if reached sufficient precision
if null_vest_ct >= stop_criteria and null_entropy_ct >= stop_criteria:
break
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
batch_size)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# calculate position-based statistics as a result of random positions
for i, row in enumerate(tmp_mut_pos):
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calculate position info
tmp_recur_ct, tmp_entropy, tmp_delta_entropy, _ = cutils.calc_pos_info(tmp_mut_info['Codon Pos'],
tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
pseudo_count=pseudo_count,
is_obs=0)
# get vest scores
if gene_vest:
tmp_vest = scores.compute_vest_stat(gene_vest,
tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
tmp_mut_info['Codon Pos'])
else:
tmp_vest = 0.0
# update empirical null distribution counts
if tmp_entropy-utils.epsilon <= obs_ent: null_entropy_ct += 1
if tmp_vest+utils.epsilon >= obs_vest: null_vest_ct += 1
# stop iterations if reached sufficient precision
if null_vest_ct >= stop_criteria and null_entropy_ct >= stop_criteria:
break
# update the number of simulations
num_sim += i+1
# calculate p-value from empirical null-distribution
ent_pval = float(null_entropy_ct) / (num_sim)
vest_pval = float(null_vest_ct) / (num_sim)
return ent_pval, vest_pval | Performs null-permutations for position-based mutation statistics
in a single gene.
Parameters
----------
obs_stat : tuple, (recur ct, entropy, delta entropy, mean vest)
tuple containing the observed statistics
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
pseudo_count : int, default: 0
Pseudo-count for number of recurrent missense mutations for each
permutation for the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
num_recur_list : list
list of recurrent mutation counts under the null
entropy_list : list
list of position entropy values under the null | entailment |
def hotmaps_permutation(obs_stat,
context_counts,
context_to_mut,
seq_context,
gene_seq,
window,
num_permutations=10000,
stop_criteria=100,
max_batch=25000,
null_save_path=None):
"""Performs null-permutations for position-based mutation statistics
in a single gene.
Parameters
----------
obs_stat : dict
dictionary mapping codons to the sum of mutations in a window
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
window : int
Number of codons to the left/right of a mutated position to consider
in the window
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
max_batch : int
maximum number of whole gene simulations to do at once.
For large number of simulations holding a matrix of M x N,
where M is the number of mutations and N is the number of simulations,
can get quite large.
null_save_path : str or None
File path to save null distribution. If None, don't save it.
Returns
-------
pvals : dict
Maps mutated codon position to the calculated p-value
"""
# get contexts and somatic base
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# calculate the # of batches for simulations
max_batch = min(num_permutations, max_batch)
num_batches = num_permutations // max_batch
remainder = num_permutations % max_batch
batch_sizes = [max_batch] * num_batches
if remainder:
batch_sizes += [remainder]
# figure out which position has highest value
max_key = {w: max(obs_stat[w], key=(lambda key: obs_stat[w][key]))
for w in window}
# setup null dist counts
null_cts = {w: {k: 0 for k in obs_stat[w]}
for w in window }
# empirical null distribution (saved if file path provided)
empirical_null = {w: {} for w in window}
num_sim = 0 # number of simulations
for j, batch_size in enumerate(batch_sizes):
# stop iterations if reached sufficient precision
# stop iterations if reached sufficient precision
stop_flag = [(null_cts[w][max_key[w]]>=stop_criteria)
for w in window]
if all(stop_flag):
break
#if null_cts[max_key] >= stop_criteria:
#break
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
batch_size)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# calculate position-based statistics as a result of random positions
for i, row in enumerate(tmp_mut_pos):
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calculate position info
tmp_pos, tmp_sim = utils.calc_windowed_sum(tmp_mut_info['Codon Pos'],
tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
window)
# update the counts when the empirical null passes the observed
for tmp_w in tmp_sim:
for tmp_key in tmp_sim[tmp_w]:
# get mutation count for simulation
val = tmp_sim[tmp_w][tmp_key]
# add to empirical null distribution
empirical_null[tmp_w].setdefault(val, 0)
empirical_null[tmp_w][val] += 1
# update counts used for p-value
for key in null_cts[tmp_w]:
if val >= obs_stat[tmp_w][key]:
null_cts[tmp_w][key] += 1
# update the number of simulations
num_sim += len(tmp_pos)
# stop iterations if reached sufficient precision
stop_flag = [(null_cts[w][max_key[w]]>=stop_criteria)
for w in window]
if all(stop_flag):
break
# calculate p-value from empirical null-distribution
pvals = {w: {k: float(null_cts[w][k]) / (num_sim) for k in obs_stat[w]}
for w in window}
# save empirical distribution
if null_save_path:
for w in window:
# create null distribution
output = [['mutation_count', 'p-value']]
sorted_cts = sorted(empirical_null[w].keys())
tmp_sum = 0
for i in range(len(sorted_cts)):
tmp_sum += empirical_null[w][sorted_cts[-(i+1)]]
tmp_pval = tmp_sum / float(num_sim)
output.append([sorted_cts[-(i+1)], tmp_pval])
# save output
with open(null_save_path.format(w), 'w') as handle:
mywriter = csv.writer(handle, delimiter='\t', lineterminator='\n')
mywriter.writerows(output)
return pvals | Performs null-permutations for position-based mutation statistics
in a single gene.
Parameters
----------
obs_stat : dict
dictionary mapping codons to the sum of mutations in a window
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
window : int
Number of codons to the left/right of a mutated position to consider
in the window
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
max_batch : int
maximum number of whole gene simulations to do at once.
For large number of simulations holding a matrix of M x N,
where M is the number of mutations and N is the number of simulations,
can get quite large.
null_save_path : str or None
File path to save null distribution. If None, don't save it.
Returns
-------
pvals : dict
Maps mutated codon position to the calculated p-value | entailment |
def protein_permutation(graph_score,
num_codons_obs,
context_counts,
context_to_mut,
seq_context,
gene_seq,
gene_graph,
num_permutations=10000,
stop_criteria=100,
pseudo_count=0):
"""Performs null-simulations for position-based mutation statistics
in a single gene.
Parameters
----------
graph_score : float
clustering score for observed data
num_codons_obs : int
number of codons with missense mutation in observed data
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
Returns
-------
protein_pval : float
p-value for clustering in neighbor graph constructure from protein
structures
"""
# get contexts and somatic base
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
num_permutations)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# calculate position-based statistics as a result of random positions
null_graph_entropy_ct = 0
coverage_list = []
num_mut_list = []
graph_entropy_list = []
for i, row in enumerate(tmp_mut_pos):
# calculate the expected value of the relative increase in coverage
if i == stop_criteria-1:
rel_inc = [coverage_list[k] / float(num_mut_list[k])
for k in range(stop_criteria-1)
if coverage_list[k]]
exp_rel_inc = np.mean(rel_inc)
# calculate observed statistic
if num_codons_obs:
obs_stat = graph_score / np.log2(exp_rel_inc*num_codons_obs)
else:
obs_stat = 1.0
# calculate statistics for simulated data
sim_stat_list = [ent / np.log2(exp_rel_inc*num_mut_list[l])
for l, ent in enumerate(graph_entropy_list)]
null_graph_entropy_ct = len([s for s in sim_stat_list
if s-utils.epsilon <= obs_stat])
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calculate position info
tmp_tuple = cutils.calc_pos_info(tmp_mut_info['Codon Pos'],
tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
pseudo_count=pseudo_count,
is_obs=0)
_, _, _, tmp_pos_ct = tmp_tuple
# record num of mut codons
if i < stop_criteria-1:
tmp_num_mut_codons = len(tmp_pos_ct)
num_mut_list.append(tmp_num_mut_codons)
# get entropy on graph-smoothed probability distribution
tmp_graph_entropy, tmp_coverage = scores.compute_ng_stat(gene_graph, tmp_pos_ct)
# record the "coverage" in the graph
if i < stop_criteria-1:
coverage_list.append(tmp_coverage)
graph_entropy_list.append(tmp_graph_entropy)
# update empirical null distribution counts
if i >= stop_criteria:
#if tmp_graph_entropy-utils.epsilon <= graph_score:
if tmp_num_mut_codons:
sim_stat = tmp_graph_entropy / np.log2(exp_rel_inc*tmp_num_mut_codons)
else:
sim_stat = 1.0
# add count
if sim_stat-utils.epsilon <= obs_stat:
null_graph_entropy_ct += 1
# stop iterations if reached sufficient precision
if null_graph_entropy_ct >= stop_criteria:
break
# calculate p-value from empirical null-distribution
protein_pval = float(null_graph_entropy_ct) / (i+1)
return protein_pval, obs_stat | Performs null-simulations for position-based mutation statistics
in a single gene.
Parameters
----------
graph_score : float
clustering score for observed data
num_codons_obs : int
number of codons with missense mutation in observed data
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
stop_criteria : int
stop after stop_criteria iterations are more significant
then the observed statistic.
Returns
-------
protein_pval : float
p-value for clustering in neighbor graph constructure from protein
structures | entailment |
def effect_permutation(context_counts,
context_to_mut,
seq_context,
gene_seq,
num_permutations=10000,
pseudo_count=0):
"""Performs null-permutations for effect-based mutation statistics
in a single gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
pseudo_count : int, default: 0
Pseudo-count for number of recurrent missense mutations for each
permutation for the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
effect_entropy_list : list
list of entropy of effect values under the null
recur_list : list
number of recurrent missense mutations
inactivating_list : list
number of inactivating mutations
"""
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
num_permutations)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# calculate position-based statistics as a result of random positions
effect_entropy_list, recur_list, inactivating_list = [], [], []
for row in tmp_mut_pos:
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calculate position info
tmp_entropy, tmp_recur, tmp_inactivating = cutils.calc_effect_info(tmp_mut_info['Codon Pos'],
tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
pseudo_count=pseudo_count,
is_obs=0)
effect_entropy_list.append(tmp_entropy)
recur_list.append(tmp_recur)
inactivating_list.append(tmp_inactivating)
return effect_entropy_list, recur_list, inactivating_list | Performs null-permutations for effect-based mutation statistics
in a single gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
pseudo_count : int, default: 0
Pseudo-count for number of recurrent missense mutations for each
permutation for the null distribution. Increasing pseudo_count
makes the statistical test more stringent.
Returns
-------
effect_entropy_list : list
list of entropy of effect values under the null
recur_list : list
number of recurrent missense mutations
inactivating_list : list
number of inactivating mutations | entailment |
def non_silent_ratio_permutation(context_counts,
context_to_mut,
seq_context,
gene_seq,
num_permutations=10000):
"""Performs null-permutations for non-silent ratio across all genes.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
Returns
-------
non_silent_count_list : list of tuples
list of non-silent and silent mutation counts under the null
"""
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
num_permutations)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# determine result of random positions
non_silent_count_list = []
for row in tmp_mut_pos:
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# calc deleterious mutation info
tmp_non_silent = cutils.calc_non_silent_info(tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
tmp_mut_info['Codon Pos'])
non_silent_count_list.append(tmp_non_silent)
return non_silent_count_list | Performs null-permutations for non-silent ratio across all genes.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
Returns
-------
non_silent_count_list : list of tuples
list of non-silent and silent mutation counts under the null | entailment |
def summary_permutation(context_counts,
context_to_mut,
seq_context,
gene_seq,
score_dir,
num_permutations=10000,
min_frac=0.0,
min_recur=2,
drop_silent=False):
"""Performs null-permutations and summarizes the results as features over
the gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
drop_silent : bool, default=False
Flage on whether to drop all silent mutations. Some data sources
do not report silent mutations, and the simulations should match this.
Returns
-------
summary_info_list : list of lists
list of non-silent and silent mutation counts under the null along
with information on recurrent missense counts and missense positional
entropy.
"""
mycontexts = context_counts.index.tolist()
somatic_base = [base
for one_context in mycontexts
for base in context_to_mut[one_context]]
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
num_permutations)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# determine result of random positions
gene_name = gene_seq.bed.gene_name
gene_len = gene_seq.bed.cds_len
summary_info_list = []
for i, row in enumerate(tmp_mut_pos):
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# Get all metrics summarizing each gene
tmp_summary = cutils.calc_summary_info(tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
tmp_mut_info['Codon Pos'],
gene_name,
score_dir,
min_frac=min_frac,
min_recur=min_recur)
# drop silent if needed
if drop_silent:
# silent mutation count is index 1
tmp_summary[1] = 0
# limit the precision of floats
#pos_ent = tmp_summary[-1]
#tmp_summary[-1] = '{0:.5f}'.format(pos_ent)
summary_info_list.append([gene_name, i+1, gene_len]+tmp_summary)
return summary_info_list | Performs null-permutations and summarizes the results as features over
the gene.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
drop_silent : bool, default=False
Flage on whether to drop all silent mutations. Some data sources
do not report silent mutations, and the simulations should match this.
Returns
-------
summary_info_list : list of lists
list of non-silent and silent mutation counts under the null along
with information on recurrent missense counts and missense positional
entropy. | entailment |
def maf_permutation(context_counts,
context_to_mut,
seq_context,
gene_seq,
num_permutations=10000,
drop_silent=False):
"""Performs null-permutations across all genes and records the results in
a format like a MAF file. This could be useful for examining the null
permutations because the alternative approaches always summarize the results.
With the simulated null-permutations, novel metrics can be applied to create
an empirical null-distribution.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
drop_silent : bool, default=False
Flage on whether to drop all silent mutations. Some data sources
do not report silent mutations, and the simulations should match this.
Returns
-------
maf_list : list of tuples
list of null mutations with mutation info in a MAF like format
"""
mycontexts = context_counts.index.tolist()
somatic_base, base_context = zip(*[(base, one_context)
for one_context in mycontexts
for base in context_to_mut[one_context]])
# get random positions determined by sequence context
tmp_contxt_pos = seq_context.random_pos(context_counts.iteritems(),
num_permutations)
tmp_mut_pos = np.hstack(pos_array for base, pos_array in tmp_contxt_pos)
# info about gene
gene_name = gene_seq.bed.gene_name
strand = gene_seq.bed.strand
chrom = gene_seq.bed.chrom
gene_seq.bed.init_genome_coordinates() # map seq pos to genome
# determine result of random positions
maf_list = []
for row in tmp_mut_pos:
# get genome coordinate
pos2genome = np.vectorize(lambda x: gene_seq.bed.seqpos2genome[x]+1)
genome_coord = pos2genome(row)
# get info about mutations
tmp_mut_info = mc.get_aa_mut_info(row,
somatic_base,
gene_seq)
# get string describing variant
var_class = cutils.get_variant_classification(tmp_mut_info['Reference AA'],
tmp_mut_info['Somatic AA'],
tmp_mut_info['Codon Pos'])
# prepare output
for k, mysomatic_base in enumerate(somatic_base):
# format DNA change
ref_nuc = tmp_mut_info['Reference Nuc'][k]
nuc_pos = row[k]
dna_change = 'c.{0}{1}>{2}'.format(ref_nuc, nuc_pos, mysomatic_base)
# format protein change
ref_aa = tmp_mut_info['Reference AA'][k]
somatic_aa = tmp_mut_info['Somatic AA'][k]
codon_pos = tmp_mut_info['Codon Pos'][k]
protein_change = 'p.{0}{1}{2}'.format(ref_aa, codon_pos, somatic_aa)
# reverse complement if on negative strand
if strand == '-':
ref_nuc = utils.rev_comp(ref_nuc)
mysomatic_base = utils.rev_comp(mysomatic_base)
# append results
if drop_silent and var_class[k].decode() == 'Silent': continue
maf_line = [gene_name, strand, chrom, genome_coord[k], genome_coord[k],
ref_nuc, mysomatic_base, base_context[k], dna_change,
protein_change, var_class[k].decode()]
maf_list.append(maf_line)
return maf_list | Performs null-permutations across all genes and records the results in
a format like a MAF file. This could be useful for examining the null
permutations because the alternative approaches always summarize the results.
With the simulated null-permutations, novel metrics can be applied to create
an empirical null-distribution.
Parameters
----------
context_counts : pd.Series
number of mutations for each context
context_to_mut : dict
dictionary mapping nucleotide context to a list of observed
somatic base changes.
seq_context : SequenceContext
Sequence context for the entire gene sequence (regardless
of where mutations occur). The nucleotide contexts are
identified at positions along the gene.
gene_seq : GeneSequence
Sequence of gene of interest
num_permutations : int, default: 10000
number of permutations to create for null
drop_silent : bool, default=False
Flage on whether to drop all silent mutations. Some data sources
do not report silent mutations, and the simulations should match this.
Returns
-------
maf_list : list of tuples
list of null mutations with mutation info in a MAF like format | entailment |
def markdown(value, extensions=settings.MARKDOWN_EXTENSIONS,
extension_configs=settings.MARKDOWN_EXTENSION_CONFIGS,
safe=False):
""" Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
:returns: A rendered markdown
"""
return mark_safe(markdown_module.markdown(
force_text(value), extensions=extensions,
extension_configs=extension_configs, safe_mode=safe)) | Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
:returns: A rendered markdown | entailment |
def editor_js_initialization(selector, **extra_settings):
""" Return script tag with initialization code. """
init_template = loader.get_template(
settings.MARKDOWN_EDITOR_INIT_TEMPLATE)
options = dict(
previewParserPath=reverse('django_markdown_preview'),
**settings.MARKDOWN_EDITOR_SETTINGS)
options.update(extra_settings)
ctx = dict(
selector=selector,
extra_settings=simplejson.dumps(options)
)
return init_template.render(ctx) | Return script tag with initialization code. | entailment |
def preview(request):
""" Render preview page.
:returns: A rendered preview
"""
if settings.MARKDOWN_PROTECT_PREVIEW:
user = getattr(request, 'user', None)
if not user or not user.is_staff:
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(request.get_full_path())
return render(
request, settings.MARKDOWN_PREVIEW_TEMPLATE, dict(
content=request.POST.get('data', 'No content posted'),
css=settings.MARKDOWN_STYLE
)) | Render preview page.
:returns: A rendered preview | entailment |
def register():
""" Register markdown for flatpages. """
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, LocalFlatPageAdmin) | Register markdown for flatpages. | entailment |
def main(argv=None):
"""Command line options."""
program_name = __programm_name__
program_version = "v%s" % __version__
program_descrption = __programm_description__
try:
# Setup argument parser
parser = ArgumentParser(prog=program_name, description=program_descrption)
parser.add_argument("-v", "--verbose", dest="verbose", action="count", help="run in verbose mode (-vvv for more, -vvvv to enable connection debugging)")
parser.add_argument("-s", "--sudo", action="store_true", help="run supervisorctl actions with sudo (nopasswd))")
parser.add_argument("-V", "--version", action="version", version=program_version)
parser.add_argument("host-pattern", help="A host-pattern usually refers to a group of hosts. For more details, see Ansible documentation about Patterns.")
#parser.add_argument("supervisorctl-action", help="A supervisorctl action (and optional argument). For more details, see Supervisor documentation about the available supervisorctl actions.")
subparsers = parser.add_subparsers(help="One of the available supervisorctl actions.", dest="supervisorctl-action")
subparsers.add_parser("status", help="Get status info of all processes.")
subparsers.add_parser("reread", help="Reread the configuration files of supervisord")
subparsers.add_parser("reload", help="Restart remote supervisord")
subparsers.add_parser("update", help="Reload the configuration files of supervisord and add/remove processes as necessary")
start_subparser = subparsers.add_parser("start", help="Start a process by name")
start_subparser.add_argument("process-name", help="Name of the process")
stop_subparser = subparsers.add_parser("stop", help="Stop a process by name")
stop_subparser.add_argument("process-name", help="Name of the process")
restart_subparser = subparsers.add_parser("restart", help="Restart a process by name")
restart_subparser.add_argument("process-name", help="Name of the process")
remove_subparser = subparsers.add_parser("remove", help="Remove a process by name")
remove_subparser.add_argument("process-name", help="Name of the process")
# Process arguments
args = parser.parse_args(argv)
verbose = args.verbose
host_pattern = getattr(args, "host-pattern")
supervisorctl_action = getattr(args, "supervisorctl-action")
sudo = args.sudo
ansible_executable = "ansible"
supervisorctl_executable = "supervisorctl"
ansible_action_option = "-a"
if sudo:
supervisorctl_command = "sudo " + supervisorctl_executable + " " + supervisorctl_action
else:
supervisorctl_command = supervisorctl_executable + " " + supervisorctl_action
if supervisorctl_action not in ['status', 'reread', 'update', 'reload']:
supervisorctl_argument = getattr(args, "process-name")
supervisorctl_command = supervisorctl_command + ' ' + supervisorctl_argument
if verbose >0:
verbose_level = "-"+ "v"*verbose
print("Verbose mode on: " + verbose_level)
print "Parsed arguments:"
print args
retcode = call([ansible_executable, host_pattern, verbose_level, ansible_action_option, supervisorctl_command,])
else:
retcode = call([ansible_executable, host_pattern, ansible_action_option, supervisorctl_command])
return retcode
except KeyboardInterrupt:
### handle keyboard interrupt ###
return 0 | Command line options. | entailment |
def markdown(value, arg=None):
""" Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
Syntax: ::
{{value|markdown}}
{{value|markdown:"tables,codehilite"}}
:returns: A rendered markdown
"""
extensions = (arg and arg.split(',')) or settings.MARKDOWN_EXTENSIONS
return _markdown(value, extensions=extensions, safe=False) | Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
Syntax: ::
{{value|markdown}}
{{value|markdown:"tables,codehilite"}}
:returns: A rendered markdown | entailment |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.