code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
# 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 =... | 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
i... | 4.753926 | 4.721913 | 1.00678 |
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 | 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_permut... | 3.773666 | 3.574734 | 1.055649 |
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 ... | def multiprocess_permutation(bed_dict, mut_df, opts) | Handles parallelization of permutations by splitting work
by chromosome. | 1.810291 | 1.809255 | 1.000572 |
# 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
... | 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. | 2.046449 | 2.063861 | 0.991563 |
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 =... | 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. | 1.895329 | 1.88679 | 1.004526 |
# 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_sta... | 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... | 2.886148 | 2.945785 | 0.979755 |
# 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:
scor... | 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 calculat... | 3.712401 | 3.721551 | 0.997541 |
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.ap... | 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
... | 2.159358 | 2.330405 | 0.926602 |
# 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 | 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 ... | 3.316526 | 2.980897 | 1.112593 |
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 | 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. | 2.180921 | 2.116139 | 1.030613 |
# 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_gr... | 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
-... | 3.844071 | 3.449 | 1.114547 |
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 fra... | 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_ba... | 3.194492 | 3.073259 | 1.039448 |
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,
... | 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 n... | 2.515972 | 2.424369 | 1.037784 |
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,
... | 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 | 2.087579 | 2.104424 | 0.991996 |
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() | def _reset_seq(self) | Updates attributes for gene represented in the self.bed attribute.
Sequences are always upper case. | 3.717751 | 3.165327 | 1.174523 |
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_v... | 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.
Parame... | 2.394924 | 2.360155 | 1.014732 |
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] | def _to_upper(self) | Convert sequences to upper case. | 2.426005 | 2.142308 | 1.132426 |
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)
... | 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 | 1.89952 | 1.7084 | 1.111871 |
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_l... | def correct_chrom_names(chroms) | Make sure chromosome names follow UCSC chr convention. | 2.135382 | 2.122339 | 1.006146 |
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 | def fishers_method(pvals) | Fisher's method for combining independent p-values. | 2.444413 | 2.45075 | 0.997414 |
for i in range(1, len(x)):
if x[i-1] < x[i]:
x[i] = x[i-1]
return x | def cummin(x) | A python implementation of the cummin function in R | 1.857548 | 2.03298 | 0.913707 |
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 t... | 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
adjuste... | 3.527563 | 3.684375 | 0.957439 |
#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... | def calc_deleterious_p_value(mut_info,
unmapped_mut_info,
sc,
gs,
bed,
num_permutations,
stop_thresh,
del_threshold,
... | 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.
... | 3.234441 | 3.11131 | 1.039575 |
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.DataFram... | def calc_protein_p_value(mut_info,
unmapped_mut_info,
sc,
gs,
bed,
graph_dir,
num_permutations,
stop_thresh,
min_recurre... | 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
------- | 3.665694 | 3.757232 | 0.975637 |
return -np.sum(np.where(p!=0, p * np.log2(p), 0)) | def shannon_entropy(p) | Calculates shannon entropy in bits.
Parameters
----------
p : np.array
array of probabilities
Returns
-------
shannon entropy in bits | 2.807511 | 4.778575 | 0.58752 |
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_e... | 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
nor... | 2.486424 | 2.885573 | 0.861674 |
# 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 | 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 w... | 2.591711 | 3.135802 | 0.826491 |
m = .5 * (p+q)
js_div = .5*kl_divergence(p, m) + .5*kl_divergence(q, m)
return js_div | 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 | 3.172861 | 3.582508 | 0.885654 |
js_dist = np.sqrt(js_divergence(p, q))
return js_dist | 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 ma... | 5.202021 | 5.096797 | 1.020645 |
# 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 ... | 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
-------
... | 2.836432 | 2.678349 | 1.059023 |
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_siz... | 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. | 2.678381 | 2.682771 | 0.998364 |
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
... | def init_genome_coordinates(self) | Creates the self.seqpos2genome dictionary that converts positions
relative to the sequence to genome coordinates. | 2.316622 | 2.223539 | 1.041863 |
# 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 typ... | 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
... | 2.686636 | 2.675242 | 1.004259 |
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(':', '.') + '.tx... | 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. | 2.399486 | 2.41876 | 0.992032 |
@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 | 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. | 2.765519 | 3.005681 | 0.920097 |
# 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 | 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 filte... | 5.314985 | 6.603494 | 0.804875 |
rev_seq = seq[::-1]
rev_comp_seq = ''.join([base_pairing[s] for s in rev_seq])
return rev_comp_seq | 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 | 2.872111 | 4.67189 | 0.614764 |
with open(bed_path) as handle:
bed_reader = csv.reader(handle, delimiter='\t')
for line in bed_reader:
yield BedLine(line) | 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. | 2.44282 | 2.55916 | 0.95454 |
# 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, [])
... | 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... | 2.478613 | 2.711614 | 0.914073 |
# 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... | 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 mutation... | 2.556937 | 2.51576 | 1.016368 |
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] != ... | 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
... | 2.48371 | 2.494027 | 0.995863 |
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 ... | 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 | 2.754045 | 2.810209 | 0.980015 |
# 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] == 'C... | 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
----------
tr... | 4.083389 | 3.890767 | 1.049508 |
# 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... | 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... | 2.627751 | 2.588983 | 1.014974 |
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'])
permutati... | 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
form... | 3.791451 | 3.91062 | 0.969527 |
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... | 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 | 2.696193 | 2.760003 | 0.976881 |
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']
pe... | 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 | 3.32995 | 3.406957 | 0.977397 |
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 adjuste... | 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 | 4.213128 | 4.38256 | 0.961339 |
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),
... | 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 | 4.723446 | 4.809519 | 0.982104 |
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:
t... | 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... | 4.063182 | 3.628702 | 1.119734 |
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 = ... | 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 | 3.447447 | 3.518338 | 0.979851 |
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) | def set_amino_acid(self, aa) | Set amino acid change and position. | 9.267467 | 8.335623 | 1.111791 |
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... | 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 | 3.462771 | 3.70113 | 0.935598 |
# 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 | def __set_missense_status(self, hgvs_string) | Sets the self.is_missense flag. | 3.742715 | 3.450045 | 1.084831 |
# 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
... | def __set_lost_start_status(self, hgvs_string) | Sets the self.is_lost_start flag. | 2.901336 | 2.722131 | 1.065833 |
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
... | def __set_frame_shift_status(self) | Check for frame shift and set the self.is_frame_shift flag. | 4.432447 | 4.021534 | 1.102178 |
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 | def __set_lost_stop_status(self, hgvs_string) | Check if the stop codon was mutated to something other than
a stop codon. | 3.556805 | 3.598703 | 0.988357 |
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:
... | def __set_premature_stop_codon_status(self, hgvs_string) | Set whether there is a premature stop codon. | 2.678462 | 2.564105 | 1.044599 |
# 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:
# mutatio... | def __set_indel_status(self) | Sets flags related to the mutation being an indel. | 2.01202 | 1.923238 | 1.046163 |
# 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 outcom... | 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 | 7.450627 | 7.086362 | 1.051404 |
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 | 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 | 3.71568 | 4.028877 | 0.922262 |
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_permutation... | def deleterious_permutation(obs_del,
context_counts,
context_to_mut,
seq_context,
gene_seq,
num_permutations=10000,
stop_criteria=100,
... | 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.
... | 4.389572 | 4.41595 | 0.994027 |
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(),
... | 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.
... | 4.344762 | 3.690628 | 1.177242 |
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(),
... | 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 : Seque... | 4.758523 | 4.565446 | 1.042291 |
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(),
... | def summary_permutation(context_counts,
context_to_mut,
seq_context,
gene_seq,
score_dir,
num_permutations=10000,
min_frac=0.0,
min_recur=2,
... | 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.
... | 4.910311 | 4.486738 | 1.094406 |
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
... | 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 empir... | 3.492712 | 3.418955 | 1.021573 |
return mark_safe(markdown_module.markdown(
force_text(value), extensions=extensions,
extension_configs=extension_configs, safe_mode=safe)) | 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 | 2.80089 | 6.308524 | 0.443985 |
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_setti... | def editor_js_initialization(selector, **extra_settings) | Return script tag with initialization code. | 4.108282 | 3.834177 | 1.07149 |
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_... | def preview(request) | Render preview page.
:returns: A rendered preview | 3.523036 | 3.740073 | 0.94197 |
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, LocalFlatPageAdmin) | def register() | Register markdown for flatpages. | 5.785335 | 3.943417 | 1.467087 |
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="verb... | def main(argv=None) | Command line options. | 2.660912 | 2.654294 | 1.002493 |
extensions = (arg and arg.split(',')) or settings.MARKDOWN_EXTENSIONS
return _markdown(value, extensions=extensions, safe=False) | 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 | 6.52211 | 9.773273 | 0.667341 |
extensions = (arg and arg.split(',')) or settings.MARKDOWN_EXTENSIONS
return _markdown(value, extensions=extensions, safe=True) | def markdown_safe(value, arg=None) | Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
Enables safe mode, which strips raw HTML and only returns HTML generated
by markdown.
:returns: A rendered markdown. | 6.178503 | 8.16984 | 0.756257 |
return dict(
selector=selector,
extra_settings=mark_safe(simplejson.dumps(
dict(previewParserPath=reverse('django_markdown_preview'))))) | def markdown_editor(selector) | Enable markdown editor for given textarea.
:returns: Editor template context. | 10.5602 | 13.980116 | 0.755373 |
return dict(
CSS_SET=posixpath.join(
settings.MARKDOWN_SET_PATH, settings.MARKDOWN_SET_NAME, 'style.css'
),
CSS_SKIN=posixpath.join(
'django_markdown', 'skins', settings.MARKDOWN_EDITOR_SKIN,
'style.css'
)
) | def markdown_media_css() | Add css requirements to HTML.
:returns: Editor template context. | 4.382306 | 4.984385 | 0.879207 |
return _convert(
_read_file, _process_file,
source, to,
format, extra_args,
encoding=encoding) | def convert(source, to, format=None, extra_args=(), encoding='utf-8') | Convert given `source` from `format` `to` another.
`source` may be either a file path or a string to be converted.
It's possible to pass `extra_args` if needed. In case `format` is not
provided, it will try to invert the format based on given `source`.
Raises OSError if pandoc is not found! Make sure... | 6.121706 | 9.42179 | 0.649739 |
try:
p = subprocess.Popen(
['pandoc', '-h'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
except OSError:
raise OSError("You probably do not have pandoc installed.")
help_text = p.communicate()[0].decode().splitlines(False)
txt = ' '.join(help_... | def get_pandoc_formats() | Dynamic preprocessor for Pandoc formats.
Return 2 lists. "from_formats" and "to_formats". | 3.449136 | 3.425832 | 1.006802 |
html = super(MarkdownWidget, self).render(name, value, attrs, renderer)
attrs = self.build_attrs(attrs)
html += editor_js_initialization("#%s" % attrs['id'])
return mark_safe(html) | def render(self, name, value, attrs=None, renderer=None) | Render widget.
:returns: A rendered HTML | 3.994495 | 5.154797 | 0.774908 |
md.registerExtension(self)
md.preprocessors.add('graphviz_block',
InlineGraphvizPreprocessor(md),
"_begin") | def extendMarkdown(self, md, md_globals) | Add InlineGraphvizPreprocessor to the Markdown instance. | 5.605569 | 3.338277 | 1.67918 |
text = "\n".join(lines)
while 1:
m = BLOCK_RE.search(text)
if m:
command = m.group('command')
# Whitelist command, prevent command injection.
if command not in SUPPORTED_COMMAMDS:
raise Exception('Comma... | def run(self, lines) | Match and generate dot code blocks. | 3.223029 | 3.084988 | 1.044746 |
now = calendar.timegm(datetime.datetime.now().timetuple())
if now > self.expiration:
auth = self.__open("/oauth/token", data=self.oauth)
self.__sethead(auth['access_token'])
return self.__open("%s%s" % (self.api, command),
headers=self.... | def post(self, command, data=None) | Post data to API. | 5.462879 | 5.055112 | 1.080664 |
self.access_token = access_token
now = calendar.timegm(datetime.datetime.now().timetuple())
self.expiration = now + 1800
self.head = {"Authorization": "Bearer %s" % access_token,
"User-Agent": self.user_agent
} | def __sethead(self, access_token) | Set HTTP header. | 2.961597 | 2.955369 | 1.002107 |
headers = headers or {}
if not baseurl:
baseurl = self.baseurl
req = Request("%s%s" % (baseurl, url), headers=headers)
_LOGGER.debug(url)
try:
req.data = urlencode(data).encode('utf-8')
except TypeError:
pass
opener = ... | def __open(self, url, headers=None, data=None, baseurl="") | Use raw urlopen command. | 2.773096 | 2.685262 | 1.03271 |
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_drive_params(self._id)
if data:
if not data['shift_state'] or data['shift_state'] == 'P':
self.__state = True
else:
self.__state = False | def update(self) | Update the parking brake sensor. | 6.351341 | 5.139165 | 1.23587 |
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_climate_params(self._id)
if data:
if time.time() - self.__manual_update_time > 60:
self.__is_auto_conditioning_on = (data
['... | def update(self) | Update the HVAC state. | 2.690922 | 2.460218 | 1.093774 |
temp = round(temp, 1)
self.__manual_update_time = time.time()
data = self._controller.command(self._id, 'set_temps',
{"driver_temp": temp,
"passenger_temp": temp},
wa... | def set_temperature(self, temp) | Set both the driver and passenger temperature to temp. | 5.422731 | 4.638 | 1.169196 |
self.__manual_update_time = time.time()
if enabled:
data = self._controller.command(self._id,
'auto_conditioning_start',
wake_if_asleep=True)
if data['response']['result']:
... | def set_status(self, enabled) | Enable or disable the HVAC. | 3.207923 | 2.919683 | 1.098723 |
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_climate_params(self._id)
if data:
self.__inside_temp = (data['inside_temp'] if data['inside_temp']
else self.__inside_temp)
self.__outside_temp =... | def update(self) | Update the temperature. | 3.760788 | 3.424766 | 1.098115 |
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
if data['charging_state'] != "Charging":
self.__charger_state = False
else:
... | def update(self) | Update the charging state of the Tesla Vehicle. | 5.303608 | 4.180938 | 1.268521 |
if not self.__charger_state:
data = self._controller.command(self._id, 'charge_start',
wake_if_asleep=True)
if data and data['response']['result']:
self.__charger_state = True
self.__manual_update_time = tim... | def start_charge(self) | Start charging the Tesla Vehicle. | 7.802256 | 6.896783 | 1.131289 |
if self.__charger_state:
data = self._controller.command(self._id, 'charge_stop',
wake_if_asleep=True)
if data and data['response']['result']:
self.__charger_state = False
self.__manual_update_time = time.ti... | def stop_charge(self) | Stop charging the Tesla Vehicle. | 7.964667 | 7.063333 | 1.127608 |
self._controller.update(self._id, wake_if_asleep=False)
data = self._controller.get_charging_params(self._id)
if data and (time.time() - self.__manual_update_time > 60):
self.__maxrange_state = data['charge_to_max_range'] | def update(self) | Update the status of the range setting. | 8.32191 | 7.4862 | 1.111633 |
if not self.__maxrange_state:
data = self._controller.command(self._id, 'charge_max_range',
wake_if_asleep=True)
if data['response']['result']:
self.__maxrange_state = True
self.__manual_update_time = time.t... | def set_max(self) | Set the charger to max range for trips. | 10.756185 | 8.385867 | 1.282656 |
if self.__maxrange_state:
data = self._controller.command(self._id, 'charge_standard',
wake_if_asleep=True)
if data and data['response']['result']:
self.__maxrange_state = False
self.__manual_update_time = t... | def set_standard(self) | Set the charger to standard range for daily commute. | 11.729804 | 9.326102 | 1.257739 |
if self.__lock_state:
data = self._controller.command(self._id, 'door_unlock',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = False
self.__manual_update_time = time.time() | def unlock(self) | Unlock the doors and extend handles where applicable. | 8.631649 | 7.802948 | 1.106204 |
if not self.__lock_state:
data = self._controller.command(self._id, 'charge_port_door_close',
wake_if_asleep=True)
if data['response']['result']:
self.__lock_state = True
self.__manual_update_time = time.tim... | def lock(self) | Close the charger door. | 10.424943 | 8.296243 | 1.256586 |
# pylint: disable=no-self-argument
# issue is use of wraps on classmethods which should be replaced:
# https://hynek.me/articles/decorators/
@wraps(func)
def wrapped(*args, **kwargs):
# pylint: disable=too-many-branches,protected-access, not-callable
... | def wake_up(func) | Wrap a API f so it will attempt to wake the vehicle if asleep.
The command f is run once if the vehicle_id was last reported
online. Assuming f returns None and wake_if_asleep is True, 5 attempts
will be made to wake the vehicle to reissue the command. In addition,
if there is a `could_... | 3.217613 | 2.883716 | 1.115787 |
# pylint: disable=unused-argument
data = data or {}
return self.__connection.post('vehicles/%i/%s' %
(vehicle_id, command), data) | def post(self, vehicle_id, command, data=None, wake_if_asleep=True) | Send post command to the vehicle_id.
This is a wrapped function by wake_up.
Parameters
----------
vehicle_id : string
Identifier for the car on the owner-api endpoint. Confusingly it
is not the vehicle_id field for identifying the car across
differen... | 6.471847 | 11.120853 | 0.581956 |
# pylint: disable=unused-argument
return self.__connection.get('vehicles/%i/%s' % (vehicle_id, command)) | def get(self, vehicle_id, command, wake_if_asleep=False) | Send get command to the vehicle_id.
This is a wrapped function by wake_up.
Parameters
----------
vehicle_id : string
Identifier for the car on the owner-api endpoint. Confusingly it
is not the vehicle_id field for identifying the car across
different... | 7.308344 | 12.984923 | 0.562833 |
return self.get(vehicle_id, 'vehicle_data/%s' % name,
wake_if_asleep=wake_if_asleep)['response'] | def data_request(self, vehicle_id, name, wake_if_asleep=False) | Get requested data from vehicle_id.
Parameters
----------
vehicle_id : string
Identifier for the car on the owner-api endpoint. Confusingly it
is not the vehicle_id field for identifying the car across
different endpoints.
https://tesla-api.timdor... | 4.140959 | 5.549803 | 0.746145 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.