docstring
stringlengths
52
499
function
stringlengths
67
35.2k
__index_level_0__
int64
52.6k
1.16M
Convert variant information to a VCF formated string Args: variant(dict) variant_type(str) Returns: vcf_variant(str)
def format_variant(variant, variant_type='snv'): chrom = variant.get('chrom') pos = variant.get('start') ref = variant.get('ref') alt = variant.get('alt') if variant_type == 'sv': pos = int((variant['pos_left'] + variant['pos_right'])/2) ref = 'N' alt = f"<{var...
703,313
Creates an objective function and its derivative for M, given W and X Args: w (array): clusters x cells X (array): genes x cells
def _create_m_objective(w, X): clusters, cells = w.shape genes = X.shape[0] w_sum = w.sum(1) def objective(m): m = m.reshape((X.shape[0], w.shape[0])) d = m.dot(w)+eps temp = X/d w2 = w.dot(temp.T) deriv = w_sum - w2.T return np.sum(d - X*np.log(d))/g...
703,329
Creates a weight initialization matrix from Poisson clustering assignments. Args: assignments (array): 1D array of integers, of length cells k (int): number of states/clusters max_assign_weight (float, optional): between 0 and 1 - how much weight to assign to the highest cluster. Default: 0...
def initialize_from_assignments(assignments, k, max_assign_weight=0.75): cells = len(assignments) init_W = np.zeros((k, cells)) for i, a in enumerate(assignments): # entirely arbitrary... maybe it would be better to scale # the weights based on k? init_W[a, i] = max_assign_weigh...
703,330
Initializes the M matrix given the data and a set of cluster labels. Cluster centers are set to the mean of each cluster. Args: data (array): genes x cells clusters (array): 1d array of ints (0...k-1) k (int): number of clusters
def initialize_means(data, clusters, k): init_w = np.zeros((data.shape[0], k)) if sparse.issparse(data): for i in range(k): if data[:,clusters==i].shape[1]==0: point = np.random.randint(0, data.shape[1]) init_w[:,i] = data[:,point].toarray().flatten() ...
703,331
Runs an ensemble method on the list of M results... Args: data: genes x cells array k: number of classes n_runs (optional): number of random initializations of state estimation M_list (optional): list of M arrays from state estimation se_params (optional): optional poisson_e...
def state_estimation_ensemble(data, k, n_runs=10, M_list=[], **se_params): if len(M_list)==0: M_list = [] for i in range(n_runs): M, W, ll = poisson_estimate_state(data, k, **se_params) M_list.append(M) M_stacked = np.hstack(M_list) M_new, W_new, ll = poisson_est...
703,339
Runs an ensemble method on the list of NMF W matrices... Args: data: genes x cells array (should be log + cell-normalized) k: number of classes n_runs (optional): number of random initializations of state estimation M_list (optional): list of M arrays from state estimation s...
def nmf_ensemble(data, k, n_runs=10, W_list=[], **nmf_params): nmf = NMF(k) if len(W_list)==0: W_list = [] for i in range(n_runs): W = nmf.fit_transform(data) W_list.append(W) W_stacked = np.hstack(W_list) nmf_w = nmf.fit_transform(W_stacked) nmf_h = nmf....
703,340
Return ped_parser case from a family file Create a dictionary with case data. If no family file is given create from VCF Args: family_lines (iterator): The family lines family_type (str): The format of the family lines vcf_path(str): Path to VCF Returns: family...
def get_case(family_lines, family_type='ped', vcf_path=None): family = None LOG.info("Parsing family information") family_parser = FamilyParser(family_lines, family_type) families = list(family_parser.families.keys()) LOG.info("Found families {0}".format(', '.join(families))) ...
703,348
Update an existing case This will add paths to VCF files, individuals etc Args: case_obj(models.Case) existing_case(models.Case) Returns: updated_case(models.Case): Updated existing case
def update_case(case_obj, existing_case): variant_nrs = ['nr_variants', 'nr_sv_variants'] individuals = [('individuals','_inds'), ('sv_individuals','_sv_inds')] updated_case = deepcopy(existing_case) for i,file_name in enumerate(['vcf_path','vcf_sv_path']): variant_type = 'snv' ...
703,349
Convert a variant to a proper update Args: variant(dict) Returns: update(dict)
def _get_update(self, variant): update = { '$inc': { 'homozygote': variant.get('homozygote', 0), 'hemizygote': variant.get('hemizygote', 0), 'observations': 1 }, '$set': { 'ch...
703,357
Add a variant to the variant collection If the variant exists we update the count else we insert a new variant object. Args: variant (dict): A variant dictionary
def add_variant(self, variant): LOG.debug("Upserting variant: {0}".format(variant.get('_id'))) update = self._get_update(variant) message = self.db.variant.update_one( {'_id': variant['_id']}, update, upsert=True ) if...
703,358
Add a bulk of variants This could be used for faster inserts Args: variants(iterable(dict))
def add_variants(self, variants): operations = [] nr_inserted = 0 for i,variant in enumerate(variants, 1): # We need to check if there was any information returned # The variant could be excluded based on low gq or if no individiual was called ...
703,359
Make a batch search for variants in the database Args: variant_ids(list(str)): List of variant ids Returns: res(pymngo.Cursor(variant_obj)): The result
def search_variants(self, variant_ids): query = {'_id': {'$in': variant_ids}} return self.db.variant.find(query)
703,360
Return all variants in the database If no region is specified all variants will be returned. Args: chromosome(str) start(int) end(int) Returns: variants(Iterable(Variant))
def get_variants(self, chromosome=None, start=None, end=None): query = {} if chromosome: query['chrom'] = chromosome if start: query['start'] = {'$lte': end} query['end'] = {'$gte': start} LOG.info("Find all variants {}".format(query)) ...
703,361
Delete observation in database This means that we take down the observations variable with one. If 'observations' == 1 we remove the variant. If variant was homozygote we decrease 'homozygote' with one. Also remove the family from array 'families'. Args: variant (di...
def delete_variant(self, variant): mongo_variant = self.get_variant(variant) if mongo_variant: if mongo_variant['observations'] == 1: LOG.debug("Removing variant {0}".format( mongo_variant.get('_id') )) ...
703,362
Return a list of all chromosomes found in database Args: sv(bool): if sv variants should be choosen Returns: res(iterable(str)): An iterable with all chromosomes in the database
def get_chromosomes(self, sv=False): if sv: res = self.db.structural_variant.distinct('chrom') else: res = self.db.variant.distinct('chrom') return res
703,363
Get the last position observed on a chromosome in the database Args: chrom(str) Returns: end(int): The largest end position found
def get_max_position(self, chrom): res = self.db.variant.find({'chrom':chrom}, {'_id':0, 'end':1}).sort([('end', DESCENDING)]).limit(1) end = 0 for variant in res: end = variant['end'] return end
703,364
downsample the data by removing a given percentage of the reads. Args: data: genes x cells array or sparse matrix percent: float between 0 and 1
def downsample(data, percent): n_genes = data.shape[0] n_cells = data.shape[1] new_data = data.copy() total_count = float(data.sum()) to_remove = total_count*percent # sum of read counts per cell cell_sums = data.sum(0).astype(float) # probability of selecting genes per cell cel...
703,373
Creates an objective function and its derivative for W, given M and X (data) Args: m (array): genes x clusters X (array): genes x cells R (array): 1 x genes
def _create_w_objective(m, X, R): genes, clusters = m.shape cells = X.shape[1] R1 = R.reshape((genes, 1)).dot(np.ones((1, cells))) def objective(w): # convert w into a matrix first... because it's a vector for # optimization purposes w = w.reshape((m.shape[1], X.shape[1])) ...
703,374
Creates an objective function and its derivative for M, given W and X Args: w (array): clusters x cells X (array): genes x cells selected_genes (array): array of ints - genes to be selected
def poisson_objective(X, m, w): clusters, cells = w.shape genes = X.shape[0] #m = m.reshape((X.shape[0], w.shape[0])) d = m.dot(w)+eps #temp = X/d #w_sum = w.sum(1) #w2 = w.dot(temp.T) #deriv = w_sum - w2.T return np.sum(d - X*np.log(d))/genes
703,381
Check if a coordinate is in the PAR region Args: chrom(str) pos(int) Returns: par(bool)
def check_par(chrom, pos): par = False for interval in PAR.get(chrom,[]): if (pos >= interval[0] and pos <= interval[1]): par = True return par
703,391
Check if position a is greater than position b This will look at chromosome and position. For example a position where chrom = 2 and pos = 300 is greater than a position where chrom = 1 and pos = 1000 If any of the chromosomes is outside [1-22,X,Y,MT] we can not say which is biggest. ...
def is_greater(a,b): a_chrom = CHROM_TO_INT.get(a.chrom,0) b_chrom = CHROM_TO_INT.get(b.chrom,0) if (a_chrom == 0 or b_chrom == 0): return False if a_chrom > b_chrom: return True if a_chrom == b_chrom: if a.pos > b.pos: return True ...
703,393
Returns a dictionary with position information Args: variant(cyvcf2.Variant) Returns: coordinates(dict)
def get_coords(variant): coordinates = { 'chrom': None, 'end_chrom': None, 'sv_length': None, 'sv_type': None, 'pos': None, 'end': None, } chrom = variant.CHROM if chrom.startswith(('chr', 'CHR', 'Chr')): chrom = chrom[3:] coordinates['chr...
703,394
Return a Variant object Take a cyvcf2 formated variant line and return a models.Variant. If criterias are not fullfilled, eg. variant have no gt call or quality is below gq treshold then return None. Args: variant(cyvcf2.Variant) case_obj(Case): We need the case object to check indivi...
def build_variant(variant, case_obj, case_id=None, gq_treshold=None): variant_obj = None sv = False # Let cyvcf2 tell if it is a Structural Variant or not if variant.var_type == 'sv': sv = True # chrom_pos_ref_alt variant_id = get_variant_id(variant) ref = variant.REF # A...
703,395
Load a case to the database Args: adapter: Connection to database case_obj: dict update(bool): If existing case should be updated Returns: case_obj(models.Case)
def load_case(adapter, case_obj, update=False): # Check if the case already exists in database. existing_case = adapter.case(case_obj) if existing_case: if not update: raise CaseError("Case {0} already exists in database".format(case_obj['case_id'])) case_obj = update_case(c...
703,433
Load variants for a family into the database. Args: adapter (loqusdb.plugins.Adapter): initialized plugin case_obj(Case): dict with case information nr_variants(int) skip_case_id (bool): whether to include the case id on variant level or not gq_t...
def load_variants(adapter, vcf_obj, case_obj, skip_case_id=False, gq_treshold=None, max_window=3000, variant_type='snv'): if variant_type == 'snv': nr_variants = case_obj['nr_variants'] else: nr_variants = case_obj['nr_sv_variants'] nr_inserted = 0 case_id = case_...
703,434
Loads variants used for profiling Args: adapter (loqusdb.plugins.Adapter): initialized plugin variant_file(str): Path to variant file
def load_profile_variants(adapter, variant_file): vcf_info = check_vcf(variant_file) nr_variants = vcf_info['nr_variants'] variant_type = vcf_info['variant_type'] if variant_type != 'snv': LOG.critical('Variants used for profiling must be SNVs only') raise VcfError vcf = get...
703,435
This function identifies the genes that have the max variance across a number of bins sorted by mean. Args: data (array): genes x cells nbins (int): number of bins to sort genes by mean expression level. Default: 10. frac (float): fraction of genes to return per bin - between 0 and 1. D...
def max_variance_genes(data, nbins=5, frac=0.2): # TODO: profile, make more efficient for large matrices # 8000 cells: 0.325 seconds # top time: sparse.csc_tocsr, csc_matvec, astype, copy, mul_scalar # 73233 cells: 5.347 seconds, 4.762 s in sparse_var # csc_tocsr: 1.736 s # copy: 1.028 s ...
703,437
Return a dictionary with individual positions Args: individuals(list): A list with vcf individuals in correct order Returns: ind_pos(dict): Map from ind_id -> index position
def get_individual_positions(individuals): ind_pos = {} if individuals: for i, ind in enumerate(individuals): ind_pos[ind] = i return ind_pos
703,440
Generates poisson-distributed data, given a set of means for each cluster. Args: centers (array): genes x clusters matrix n_cells (int): number of output cells cluster_probs (array): prior probability for each cluster. Default: uniform. Returns: output - array with ...
def generate_poisson_data(centers, n_cells, cluster_probs=None): genes, clusters = centers.shape output = np.zeros((genes, n_cells)) if cluster_probs is None: cluster_probs = np.ones(clusters)/clusters labels = [] for i in range(n_cells): c = np.random.choice(range(clusters), p=...
703,442
Generates zero-inflated poisson-distributed data, given a set of means and zero probs for each cluster. Args: M (array): genes x clusters matrix L (array): genes x clusters matrix - zero-inflation parameters n_cells (int): number of output cells cluster_probs (array): prior probabil...
def generate_zip_data(M, L, n_cells, cluster_probs=None): genes, clusters = M.shape output = np.zeros((genes, n_cells)) if cluster_probs is None: cluster_probs = np.ones(clusters)/clusters zip_p = np.random.random((genes, n_cells)) labels = [] for i in range(n_cells): c = np...
703,443
Generates data according to the Poisson Convex Mixture Model. Args: means (array): Cell types- genes x clusters weights (array): Cell cluster assignments- clusters x cells Returns: data matrix - genes x cells
def generate_state_data(means, weights): x_true = np.dot(means, weights) sample = np.random.poisson(x_true) return sample.astype(float)
703,444
Generates data according to the Zero-inflated Poisson Convex Mixture Model. Args: means (array): Cell types- genes x clusters weights (array): Cell cluster assignments- clusters x cells z (float): zero-inflation parameter Returns: data matrix - genes x cells
def generate_zip_state_data(means, weights, z): x_true = np.dot(means, weights) sample = np.random.poisson(x_true) random = np.random.random(x_true.shape) x_true[random < z] = 0 return sample.astype(float)
703,445
Generates data according to the Negative Binomial Convex Mixture Model. Args: means (array): Cell types- genes x clusters weights (array): Cell cluster assignments- clusters x cells R (array): dispersion parameter - 1 x genes Returns: data matrix - genes x cells
def generate_nb_state_data(means, weights, R): cells = weights.shape[1] # x_true = true means x_true = np.dot(means, weights) # convert means into P R_ = np.tile(R, (cells, 1)).T P_true = x_true/(R_ + x_true) sample = np.random.negative_binomial(np.tile(R, (cells, 1)).T, P_true) ret...
703,446
Generates means and weights for the Negative Binomial Mixture Model. Weights are distributed Dirichlet(1,1,...), means are rand(0, 1). Returned values can be passed to generate_state_data(M, W). Args: n_states (int): number of states or clusters n_cells (int): number of cells n_gene...
def generate_nb_states(n_states, n_cells, n_genes): W = np.random.dirichlet([1]*n_states, size=(n_cells,)) W = W.T M = np.random.random((n_genes, n_states))*100 R = np.random.randint(1, 100, n_genes) return M, W, R
703,447
Generates means and weights for the Poisson Convex Mixture Model. Weights are distributed Dirichlet(1,1,...), means are rand(0, 100). Returned values can be passed to generate_state_data(M, W). Args: n_states (int): number of states or clusters n_cells (int): number of cells n_genes...
def generate_poisson_states(n_states, n_cells, n_genes): W = np.random.dirichlet([1]*n_states, size=(n_cells,)) W = W.T M = np.random.random((n_genes, n_states))*100 return M, W
703,448
Generates negative binomial data Args: P (array): genes x clusters R (array): genes x clusters n_cells (int): number of cells assignments (list): cluster assignment of each cell. Default: random uniform Returns: data array with shape genes x cells la...
def generate_nb_data(P, R, n_cells, assignments=None): genes, clusters = P.shape output = np.zeros((genes, n_cells)) if assignments is None: cluster_probs = np.ones(clusters)/clusters labels = [] for i in range(n_cells): if assignments is None: c = np.random.choice(r...
703,450
Generates visualization scatters for all the methods. Args: methods: follows same format as run_experiments. List of tuples. data: genes x cells true_labels: array of integers base_dir: base directory to save all the plots figsize: tuple of ints representing size of figure ...
def generate_visualizations(methods, data, true_labels, base_dir = 'visualizations', figsize=(18,10), **scatter_options): plt.figure(figsize=figsize) for method in methods: preproc= method[0] if isinstance(preproc, Preprocess): preprocessed, ll = preproc.run(data) ...
703,459
Given a vcf, get a profile string for each sample in the vcf based on the profile variants in the database Args: adapter(MongoAdapter): Adapter to mongodb vcf_file(str): Path to vcf file Returns: profiles (dict(str)): The profiles (given as strings) for each sample ...
def get_profiles(adapter, vcf_file): vcf = get_file_handle(vcf_file) individuals = vcf.samples profiles = {individual: [] for individual in individuals} for profile_variant in adapter.profile_variants(): ref = profile_variant['ref'] alt = profile_variant['alt'] pos = pro...
703,516
Given two profiles, determine the ratio of similarity, i.e. the hamming distance between the strings. Args: profile1/2 (str): profile string Returns: similarity_ratio (float): the ratio of similiarity (0-1)
def compare_profiles(profile1, profile2): length = len(profile1) profile1 = np.array(list(profile1)) profile2 = np.array(list(profile2)) similarity_array = profile1 == profile2 matches = np.sum(similarity_array) similarity_ratio = matches/length return similarity_ratio
703,518
For all cases having vcf_path, update the profile string for the samples Args: adapter (MongoAdapter): Adapter to mongodb
def update_profiles(adapter): for case in adapter.cases(): #If the case has a vcf_path, get the profiles and update the #case with new profiled individuals. if case.get('profile_path'): profiles = get_profiles(adapter, case['profile_path']) profiled_individua...
703,519
Calculates the purity score for the given labels. Args: labels (array): 1D array of integers true_labels (array): 1D array of integers - true labels Returns: purity score - a float bewteen 0 and 1. Closer to 1 is better.
def purity(labels, true_labels): purity = 0.0 for i in set(labels): indices = (labels==i) true_clusters = true_labels[indices] if len(true_clusters)==0: continue counts = Counter(true_clusters) lab, count = counts.most_common()[0] purity += count ...
703,521
Calculates the nearest neighbor accuracy (basically leave-one-out cross validation with a 1NN classifier). Args: dim_red (array): dimensions (k, cells) true_labels (array): 1d array of integers Returns: Nearest neighbor accuracy - fraction of points for which the 1NN 1NN cl...
def nne(dim_red, true_labels): # use sklearn's BallTree bt = BallTree(dim_red.T) correct = 0 for i, l in enumerate(true_labels): dist, ind = bt.query([dim_red[:,i]], k=2) closest_cell = ind[0, 1] if true_labels[closest_cell] == l: correct += 1 return float(co...
703,522
Returns the negative binomial log-likelihood of the data. Args: data (array): genes x cells P (array): NB success probability param - genes x clusters R (array): NB stopping param - genes x clusters Returns: cells x clusters array of log-likelihoods
def nb_ll(data, P, R): # TODO: include factorial... #data = data + eps genes, cells = data.shape clusters = P.shape[1] lls = np.zeros((cells, clusters)) for c in range(clusters): P_c = P[:,c].reshape((genes, 1)) R_c = R[:,c].reshape((genes, 1)) # don't need constant ...
703,526
returns the negative LL of a single row. Args: params (array) - [p, r] data_row (array) - 1d array of data Returns: LL of row
def nb_ll_row(params, data_row): p = params[0] r = params[1] n = len(data_row) ll = np.sum(gammaln(data_row + r)) - np.sum(gammaln(data_row + 1)) ll -= n*gammaln(r) ll += np.sum(data_row)*np.log(p) ll += n*r*np.log(1-p) return -ll
703,528
Derivative of log-likelihood wrt r (formula from wikipedia) Args: r (float): the R paramemter in the NB distribution data_row (array): 1d array of length cells
def nb_r_deriv(r, data_row): n = len(data_row) d = sum(digamma(data_row + r)) - n*digamma(r) + n*np.log(r/(r+np.mean(data_row))) return d
703,529
Fits the NB distribution to data using method of moments. Args: data (array): genes x cells P_init (array, optional): NB success prob param - genes x 1 R_init (array, optional): NB stopping param - genes x 1 Returns: P, R - fit to data
def nb_fit(data, P_init=None, R_init=None, epsilon=1e-8, max_iters=100): means = data.mean(1) variances = data.var(1) if (means > variances).any(): raise ValueError("For NB fit, means must be less than variances") genes, cells = data.shape # method of moments P = 1.0 - means/varianc...
703,530
Calculates the zero-inflated Poisson log-likelihood. Args: data (array): genes x cells means (array): genes x k M (array): genes x k - this is the zero-inflation parameter. Returns: cells x k array of log-likelihood for each cell/cluster pair.
def zip_ll(data, means, M): genes, cells = data.shape clusters = means.shape[1] ll = np.zeros((cells, clusters)) d0 = (data==0) d1 = (data>0) for i in range(clusters): means_i = np.tile(means[:,i], (cells, 1)) means_i = means_i.transpose() L_i = np.tile(M[:,i], (cell...
703,533
Returns the negative log-likelihood of a row given ZIP data. Args: params (list): [lambda zero-inf] data_row (array): 1d array Returns: negative log-likelihood
def zip_ll_row(params, data_row): l = params[0] pi = params[1] d0 = (data_row==0) likelihood = d0*pi + (1-pi)*poisson.pmf(data_row, l) return -np.log(likelihood+eps).sum()
703,534
Migrate an old loqusdb instance to 1.0 Args: adapter Returns: nr_updated(int): Number of variants that where updated
def migrate_database(adapter): all_variants = adapter.get_variants() nr_variants = all_variants.count() nr_updated = 0 with progressbar(all_variants, label="Updating variants", length=nr_variants) as bar: for variant in bar: # Do not update if the variants have the correct ...
703,535
Given a data matrix, this returns the per-gene fit error for the Poisson, Normal, and Log-Normal distributions. Args: Dat (array): numpy array with shape (genes, cells) Returns: d (dict): 'poiss', 'norm', 'lognorm' give the fit error for each distribution.
def DistFitDataset(Dat): #Assumes data to be in the form of a numpy matrix (r,c) = Dat.shape Poiss = np.zeros(r) Norm = np.zeros(r) LogNorm = np.zeros(r) for i in range(r): temp = GetDistFitError(Dat[i]) Poiss[i] = temp['poiss'] Norm[i] = temp['norm'] LogNor...
703,555
Delete a case and all of it's variants from the database. Args: adapter: Connection to database case_obj(models.Case) update(bool): If we are in the middle of an update existing_case(models.Case): If something failed during an update we need to revert ...
def delete(adapter, case_obj, update=False, existing_case=False): # This will overwrite the updated case with the previous one if update: adapter.add_case(existing_case) else: adapter.delete_case(case_obj) for file_type in ['vcf_path','vcf_sv_path']: if not case_obj.get(fil...
703,556
Delete variants for a case in the database Args: adapter(loqusdb.plugins.Adapter) vcf_obj(iterable(dict)) ind_positions(dict) case_id(str) Returns: nr_deleted (int): Number of deleted variants
def delete_variants(adapter, vcf_obj, case_obj, case_id=None): case_id = case_id or case_obj['case_id'] nr_deleted = 0 start_deleting = datetime.now() chrom_time = datetime.now() current_chrom = None new_chrom = None for variant in vcf_obj: formated_variant = build_variant(...
703,557
Generates starting points using binarized data. If qualitative data is missing for a given gene, all of its entries should be -1 in the qualitative matrix. Args: data (array): 2d array of genes x cells qualitative (array): 2d array of numerical data - genes x clusters Returns: Array of...
def qualNorm(data, qualitative): genes, cells = data.shape clusters = qualitative.shape[1] output = np.zeros((genes, clusters)) missing_indices = [] qual_indices = [] thresholds = qualitative.min(1) + (qualitative.max(1) - qualitative.min(1))/2.0 for i in range(genes): if qualit...
703,568
Generates starting points using binarized data. If qualitative data is missing for a given gene, all of its entries should be -1 in the qualitative matrix. Args: data (array): 2d array of genes x cells qualitative (array): 2d array of numerical data - genes x clusters Returns: Array of...
def qualNormGaussian(data, qualitative): genes, cells = data.shape clusters = qualitative.shape[1] output = np.zeros((genes, clusters)) missing_indices = [] qual_indices = [] for i in range(genes): if qualitative[i,:].max() == -1 and qualitative[i,:].min() == -1: missing...
703,569
Get current script's directory Args: pyobject (Any): Any Python object in the script follow_symlinks (Optional[bool]): Follow symlinks or not. Defaults to True. Returns: str: Current script's directory
def script_dir(pyobject, follow_symlinks=True): if getattr(sys, 'frozen', False): # py2exe, PyInstaller, cx_Freeze path = abspath(sys.executable) else: path = inspect.getabsfile(pyobject) if follow_symlinks: path = realpath(path) return dirname(path)
703,570
Get current script's directory and then append a filename Args: filename (str): Filename to append to directory path pyobject (Any): Any Python object in the script follow_symlinks (Optional[bool]): Follow symlinks or not. Defaults to True. Returns: str: Current script's direct...
def script_dir_plus_file(filename, pyobject, follow_symlinks=True): return join(script_dir(pyobject, follow_symlinks), filename)
703,571
Annotate a cyvcf variant with observations Args: variant(cyvcf2.variant) var_obj(dict) Returns: variant(cyvcf2.variant): Annotated variant
def annotate_variant(variant, var_obj=None): if var_obj: variant.INFO['Obs'] = var_obj['observations'] if var_obj.get('homozygote'): variant.INFO['Hom'] = var_obj['homozygote'] if var_obj.get('hemizygote'): variant.INFO['Hem'] = var_obj['hemizygote'] ...
703,581
Annotate an SNV/INDEL variant Args: adapter(loqusdb.plugin.adapter) variant(cyvcf2.Variant)
def annotate_snv(adpter, variant): variant_id = get_variant_id(variant) variant_obj = adapter.get_variant(variant={'_id':variant_id}) annotated_variant = annotated_variant(variant, variant_obj) return annotated_variant
703,582
Annotate all SV variants in a VCF Args: adapter(loqusdb.plugin.adapter) vcf_obj(cyvcf2.VCF) Yields: variant(cyvcf2.Variant)
def annotate_svs(adapter, vcf_obj): for nr_variants, variant in enumerate(vcf_obj, 1): variant_info = get_coords(variant) match = adapter.get_structural_variant(variant_info) if match: annotate_variant(variant, match) yield variant
703,583
Annotate all variants in a VCF Args: adapter(loqusdb.plugin.adapter) vcf_obj(cyvcf2.VCF) Yields: variant(cyvcf2.Variant): Annotated variant
def annotate_snvs(adapter, vcf_obj): variants = {} for nr_variants, variant in enumerate(vcf_obj, 1): # Add the variant to current batch variants[get_variant_id(variant)] = variant # If batch len == 1000 we annotate the batch if (nr_variants % 1000) == 0: ...
703,584
Calculates the Poisson log-likelihood. Args: data (array): 2d numpy array of genes x cells means (array): 2d numpy array of genes x k Returns: cells x k array of log-likelihood for each cell/cluster pair
def poisson_ll(data, means): if sparse.issparse(data): return sparse_poisson_ll(data, means) genes, cells = data.shape clusters = means.shape[1] ll = np.zeros((cells, clusters)) for i in range(clusters): means_i = np.tile(means[:,i], (cells, 1)) means_i = means_i.transpo...
703,645
Construct a individual object Args: ind_id (str): The individual id case_id (str): What case it belongs to mother (str): The mother id father (str): The father id sex (str): Sex in ped format phenotype (str): Ph...
def __init__(self, ind_id, case_id=None, mother=None, father=None, sex=None, phenotype=None, ind_index=None, profile=None, similar_samples=None): super(Individual, self).__init__( ind_id=ind_id, name=ind_id, case_id=case_id, ...
703,681
Check if there are any overlapping sv clusters Search the sv variants with chrom start end_chrom end and sv_type Args: variant (dict): A variant dictionary Returns: variant (dict): A variant dictionary
def get_structural_variant(self, variant): # Create a query for the database # This will include more variants than we want # The rest of the calculations will be done in python query = { 'chrom': variant['chrom'], 'end_chrom': variant['end_chrom'...
703,696
Return all structural variants in the database Args: chromosome (str) end_chromosome (str) sv_type (str) pos (int): Left position of SV end (int): Right position of SV Returns: variants (Iterable(Variant))
def get_sv_variants(self, chromosome=None, end_chromosome=None, sv_type=None, pos=None, end=None): query = {} if chromosome: query['chrom'] = chromosome if end_chromosome: query['end_chrom'] = end_chromosome if sv_type: ...
703,697
Search what clusters a variant belongs to Args: variant_id(str): From ID column in vcf Returns: clusters()
def get_clusters(self, variant_id): query = {'variant_id':variant_id} identities = self.db.identity.find(query) return identities
703,698
Given an undirected adjacency list and a pairwise distance matrix between all nodes: calculates distances along graph from start node. Args: start (int): start node edges (list): adjacency list of tuples distances (array): 2d array of distances between nodes Returns: dict o...
def graph_distances(start, edges, distances): # convert adjacency list to adjacency dict adj = {x: [] for x in range(len(distances))} for n1, n2 in edges: adj[n1].append(n2) adj[n2].append(n1) # run dijkstra's algorithm to_visit = [] new_dist = {} for n in adj[start]: ...
703,707
Set up countries data from data in form provided by UNStats and World Bank Args: iso3 (str): ISO3 code for country country (hxl.Row): Country information Returns: None
def _add_countriesdata(cls, iso3, country): # type: (str, hxl.Row) -> None countryname = country.get('#country+name+preferred') cls._countriesdata['countrynames2iso3'][countryname.upper()] = iso3 iso2 = country.get('#country+code+v_iso2') if iso2: cls._countr...
703,711
Set up countries data from data in form provided by UNStats and World Bank Args: countries (str): Countries data in HTML format provided by UNStats Returns: None
def set_countriesdata(cls, countries): # type: (str) -> None cls._countriesdata = dict() cls._countriesdata['countries'] = dict() cls._countriesdata['iso2iso3'] = dict() cls._countriesdata['m49iso3'] = dict() cls._countriesdata['countrynames2iso3'] = dict() ...
703,712
Read countries data from OCHA countries feed (falling back to file) Args: use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. Returns: List[Dict[Dict]]: Countries dictionaries
def countriesdata(cls, use_live=True): # type: (bool) -> List[Dict[Dict]] if cls._countriesdata is None: countries = None if use_live: try: countries = hxl.data(cls._ochaurl) except IOError: logger.e...
703,713
Set World Bank url from which to retrieve countries data Args: url (str): World Bank url from which to retrieve countries data. Defaults to internal value. Returns: None
def set_ocha_url(cls, url=None): # type: (str) -> None if url is None: url = cls._ochaurl_int cls._ochaurl = url
703,714
Get country information from ISO3 code Args: iso3 (str): ISO3 code for which to get country information use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if cou...
def get_country_info_from_iso3(cls, iso3, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[Dict[str]] countriesdata = cls.countriesdata(use_live=use_live) country = countriesdata['countries'].get(iso3.upper()) if country is not None:...
703,715
Get country name from ISO3 code Args: iso3 (str): ISO3 code for which to get country name use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country not found...
def get_country_name_from_iso3(cls, iso3, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[str] countryinfo = cls.get_country_info_from_iso3(iso3, use_live=use_live, exception=exception) if countryinfo is not None: return country...
703,716
Get ISO2 from ISO3 code Args: iso3 (str): ISO3 code for which to get ISO2 code use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country not found. Defaults ...
def get_iso2_from_iso3(cls, iso3, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[str] countriesdata = cls.countriesdata(use_live=use_live) iso2 = countriesdata['iso2iso3'].get(iso3.upper()) if iso2 is not None: return i...
703,717
Get country name from ISO2 code Args: iso2 (str): ISO2 code for which to get country information use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country no...
def get_country_info_from_iso2(cls, iso2, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[Dict[str]] iso3 = cls.get_iso3_from_iso2(iso2, use_live=use_live, exception=exception) if iso3 is not None: return cls.get_country_info_fr...
703,718
Get country name from ISO2 code Args: iso2 (str): ISO2 code for which to get country name use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country not found...
def get_country_name_from_iso2(cls, iso2, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[str] iso3 = cls.get_iso3_from_iso2(iso2, use_live=use_live, exception=exception) if iso3 is not None: return cls.get_country_name_from_iso...
703,719
Get M49 from ISO3 code Args: iso3 (str): ISO3 code for which to get M49 code use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country not found. Defaults to...
def get_m49_from_iso3(cls, iso3, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[int] countriesdata = cls.countriesdata(use_live=use_live) m49 = countriesdata['m49iso3'].get(iso3) if m49 is not None: return m49 ...
703,720
Get country name from M49 code Args: m49 (int): M49 numeric code for which to get country information use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if count...
def get_country_info_from_m49(cls, m49, use_live=True, exception=None): # type: (int, bool, Optional[ExceptionUpperBound]) -> Optional[Dict[str]] iso3 = cls.get_iso3_from_m49(m49, use_live=use_live, exception=exception) if iso3 is not None: return cls.get_country_info_from_i...
703,721
Get country name from M49 code Args: m49 (int): M49 numeric code for which to get country name use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception to raise if country not ...
def get_country_name_from_m49(cls, m49, use_live=True, exception=None): # type: (int, bool, Optional[ExceptionUpperBound]) -> Optional[str] iso3 = cls.get_iso3_from_m49(m49, use_live=use_live, exception=exception) if iso3 is not None: return cls.get_country_name_from_iso3(is...
703,722
Expands abbreviation(s) in country name in various ways (eg. FED -> FEDERATED, FEDERAL etc.) Args: country (str): Country with abbreviation(s)to expand Returns: List[str]: Uppercase country name with abbreviation(s) expanded in various ways
def expand_countryname_abbrevs(cls, country): # type: (str) -> List[str] def replace_ensure_space(word, replace, replacement): return word.replace(replace, '%s ' % replacement).replace(' ', ' ').strip() countryupper = country.upper() for abbreviation in cls.abbrevia...
703,723
Simplifies country name by removing descriptive text eg. DEMOCRATIC, REPUBLIC OF etc. Args: country (str): Country name to simplify Returns: Tuple[str, List[str]]: Uppercase simplified country name and list of removed words
def simplify_countryname(cls, country): # type: (str) -> (str, List[str]) countryupper = country.upper() words = get_words_in_sentence(countryupper) index = countryupper.find(',') if index != -1: countryupper = countryupper[:index] index = countryuppe...
703,724
Get ISO3 code for cls. Only exact matches or None are returned. Args: country (str): Country for which to get ISO3 code use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception...
def get_iso3_country_code(cls, country, use_live=True, exception=None): # type: (str, bool, Optional[ExceptionUpperBound]) -> Optional[str] countriesdata = cls.countriesdata(use_live=use_live) countryupper = country.upper() len_countryupper = len(countryupper) if len_cou...
703,725
Get countries (ISO3 codes) in region Args: region (Union[int,str]): Three digit UNStats M49 region code or region name use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True. exception (Optional[ExceptionUpperBound]): An exception ...
def get_countries_in_region(cls, region, use_live=True, exception=None): # type: (Union[int,str], bool, Optional[ExceptionUpperBound]) -> List[str] countriesdata = cls.countriesdata(use_live=use_live) if isinstance(region, int): regioncode = region else: ...
703,727
Add several variants to the profile_variant collection in the database Args: profile_variants(list(models.ProfileVariant))
def add_profile_variants(self, profile_variants): results = self.db.profile_variant.insert_many(profile_variants) return results
703,769
Construct a identity object Args: cluster_id(str): Ref to a cluster variant_id (str): ID from variant case_id (str): What case it belongs to
def __init__(self, cluster_id, variant_id, case_id): super(Identity, self).__init__( cluster_id=cluster_id, variant_id=variant_id, case_id=case_id, )
703,770
Returns the ZIP parameters that best fit a given data set. Args: data (array): 2d array of genes x cells belonging to a given cluster Returns: L (array): 1d array of means M (array): 1d array of zero-inflation parameter
def zip_fit_params(data): genes, cells = data.shape m = data.mean(1) v = data.var(1) M = (v-m)/(m**2+v-m) #M = v/(v+m**2) #M[np.isnan(M)] = 0.0 M = np.array([min(1.0, max(0.0, x)) for x in M]) L = m + v/m - 1.0 #L = (v + m**2)/m L[np.isnan(L)] = 0.0 L = np.array([max(0.0...
703,771
Dimensionality reduction using MDS, while running diffusion on W. Args: means (array): genes x clusters weights (array): clusters x cells d (int): desired dimensionality Returns: W_reduced (array): array of shape (d, cells)
def diffusion_mds(means, weights, d, diffusion_rounds=10): for i in range(diffusion_rounds): weights = weights*weights weights = weights/weights.sum(0) X = dim_reduce(means, weights, d) if X.shape[0]==2: return X.dot(weights) else: return X.T.dot(weights)
703,795
Dimensionality reduction using MDS. Args: means (array): genes x clusters weights (array): clusters x cells d (int): desired dimensionality Returns: W_reduced (array): array of shape (d, cells)
def mds(means, weights, d): X = dim_reduce(means, weights, d) if X.shape[0]==2: return X.dot(weights) else: return X.T.dot(weights)
703,796
Does a MDS on the data directly, not on the means. Args: data (array): genes x cells d (int): desired dimensionality Returns: X, a cells x d matrix
def dim_reduce_data(data, d): genes, cells = data.shape distances = np.zeros((cells, cells)) for i in range(cells): for j in range(cells): distances[i,j] = poisson_dist(data[:,i], data[:,j]) # do MDS on the distance matrix (procedure from Wikipedia) proximity = distances**2 ...
703,797
Get a case from the database Search the cases with the case id Args: case (dict): A case dictionary Returns: mongo_case (dict): A mongo case dictionary
def case(self, case): LOG.debug("Getting case {0} from database".format(case.get('case_id'))) case_id = case['case_id'] return self.db.case.find_one({'case_id': case_id})
703,798
Return the number of cases in the database Args: snv_cases(bool): If only snv cases should be searched sv_cases(bool): If only snv cases should be searched Returns: cases (Iterable(Case)): A iterable with mongo cases
def nr_cases(self, snv_cases=None, sv_cases=None): query = {} if snv_cases: query = {'vcf_path': {'$exists':True}} if sv_cases: query = {'vcf_sv_path': {'$exists':True}} if snv_cases and sv_cases: query = None return self.db....
703,799
Add a case to the case collection If the case exists and update is False raise error. Args: db (MongoClient): A connection to the mongodb case (dict): A case dictionary update(bool): If existing case should be updated Returns: mongo_case_id(Obje...
def add_case(self, case, update=False): existing_case = self.case(case) if existing_case and not update: raise CaseError("Case {} already exists".format(case['case_id'])) if existing_case: self.db.case.find_one_and_replace( {'case_id': case['case_...
703,800
Delete case from the database Delete a case from the database Args: case (dict): A case dictionary
def delete_case(self, case): mongo_case = self.case(case) if not mongo_case: raise CaseError("Tried to delete case {0} but could not find case".format( case.get('case_id') )) LOG.info("Removing case {0} from database".format( mongo_ca...
703,801
Returns a ProfileVariant object Args: variant (cyvcf2.Variant) Returns: variant (models.ProfileVariant)
def build_profile_variant(variant): chrom = variant.CHROM if chrom.startswith(('chr', 'CHR', 'Chr')): chrom = chrom[3:] pos = int(variant.POS) variant_id = get_variant_id(variant) ref = variant.REF alt = variant.ALT[0] maf = get_maf(variant) profile_variant = ProfileVa...
703,814
Add loqus specific information to a VCF header Args: vcf_obj(cyvcf2.VCF)
def add_headers(vcf_obj, nr_cases=None, sv=False): vcf_obj.add_info_to_header( { 'ID':"Obs", 'Number': '1', 'Type': 'Integer', 'Description': "The number of observations for the variant"} ) if not sv: vcf_obj.add_info_to_header( ...
703,815
Return cyvcf2 VCF object Args: file_path(str) Returns: vcf_obj(cyvcf2.VCF)
def get_file_handle(file_path): LOG.debug("Check if file end is correct") if not os.path.exists(file_path): raise IOError("No such file:{0}".format(file_path)) if not os.path.splitext(file_path)[-1] in VALID_ENDINGS: raise IOError("Not a valid vcf file name: {}".format(file_path)) ...
703,816
Check if there are any problems with the vcf file Args: vcf_path(str) expected_type(str): 'sv' or 'snv' Returns: vcf_info(dict): dict like { 'nr_variants':<INT>, 'variant_type': <STR> in ['snv', 'sv'], 'individuals': <LIST> individual positio...
def check_vcf(vcf_path, expected_type='snv'): LOG.info("Check if vcf is on correct format...") vcf = VCF(vcf_path) individuals = vcf.samples variant_type = None previous_pos = None previous_chrom = None posititon_variants = set() nr_variants = 0 for nr_variants,variant in en...
703,817
Creates an objective function and its derivative for W, given M and X (data) Args: m (array): genes x clusters X (array): genes x cells Z (array): zero-inflation parameters - genes x 1
def _create_w_objective(m, X, Z=None): genes, clusters = m.shape cells = X.shape[1] nonzeros = (X!=0) def objective(w): # convert w into a matrix first... because it's a vector for # optimization purposes w = w.reshape((m.shape[1], X.shape[1])) d = m.dot(w)+eps ...
703,839
Generates kmeans++ initial centers. Args: data (array): A 2d array- genes x cells k (int): Number of clusters centers (array, optional): if provided, these are one or more known cluster centers. 2d array of genes x number of centers (<=k). Returns: centers - a genes x k array o...
def kmeans_pp(data, k, centers=None): # TODO: what if there is missing data for a given gene? # missing data could be if all the entires are -1. genes, cells = data.shape if sparse.issparse(data) and not sparse.isspmatrix_csc(data): data = sparse.csc_matrix(data) num_known_centers = 0 ...
703,841
r"""Diagonal of :math:`\mathrm A\mathrm B^\intercal`. If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in :math:`O(pn)`. Args: A (array_like): Left matrix. B (array_like): Right matrix. out (:class:`numpy.ndarray`, optional): copy result to. Returns: ...
def dotd(A, B, out=None): r A = asarray(A, float) B = asarray(B, float) if A.ndim == 1 and B.ndim == 1: if out is None: return dot(A, B) return dot(A, B, out) if out is None: out = empty((A.shape[0],), float) return einsum("ij,ji->i", A, B, out=out)
703,846
r"""Dot product of a matrix and a diagonal one. Args: L (array_like): Left matrix. R (array_like): Right matrix. out (:class:`numpy.ndarray`, optional): copy result to. Returns: :class:`numpy.ndarray`: Resulting matrix.
def ddot(L, R, left=None, out=None): r L = asarray(L, float) R = asarray(R, float) if left is None: ok = min(L.ndim, R.ndim) == 1 and max(L.ndim, R.ndim) == 2 if not ok: msg = "Wrong array layout. One array should have" msg += " ndim=1 and the other one ndim=2." ...
703,847