code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
if random_seed is not None: random.seed(random_seed) result = [] for i, record in enumerate(records): if len(result) < k: result.append(record) else: r = random.randint(0, i) if r < k: result[r] = record return result
def sample(records, k, random_seed=None)
Choose a length-``k`` subset of ``records``, retaining the input order. If k > len(records), all are returned. If an integer ``random_seed`` is provided, sets ``random.seed()``
1.782074
1.902716
0.936595
logging.info('Applying _head generator: ' 'limiting results to top ' + head + ' records.') if head == '-0': for record in records: yield record elif '-' in head: with _record_buffer(records) as r: record_count = sum(1 for record in r()) ...
def head(records, head)
Limit results to the top N records. With the leading `-', print all but the last N records.
4.752141
4.244349
1.119639
logging.info('Applying _tail generator: ' 'limiting results to top ' + tail + ' records.') if tail == '+0': for record in records: yield record elif '+' in tail: tail = int(tail) - 1 for record in itertools.islice(records, tail, None): y...
def tail(records, tail)
Limit results to the bottom N records. Use +N to output records starting with the Nth.
4.37566
3.899229
1.122186
aln_len = None gaps = [] for i, sequence in enumerate(sequences): if aln_len is None: aln_len = len(sequence) gaps = [0] * aln_len else: if not len(sequence) == aln_len: raise ValueError(("Unexpected sequence length {0}. Is this " ...
def gap_proportion(sequences, gap_chars='-')
Generates a list with the proportion of gaps by index in a set of sequences.
3.036359
3.181043
0.954517
with _record_buffer(records) as r: gap_proportions = gap_proportion(r()) keep_columns = [g < gap_threshold for g in gap_proportions] for record in r(): sequence = str(record.seq) # Trim squeezed = itertools.compress(sequence, keep_columns) ...
def squeeze(records, gap_threshold=1.0)
Remove any gaps that are present in the same position across all sequences in an alignment. Takes a second sequence iterator for determining gap positions.
5.263414
4.854981
1.084127
logging.info('Applying _strip_range generator: ' 'removing /<start>-<stop> ranges from IDs') # Split up and be greedy. cut_regex = re.compile(r"(?P<id>.*)\/(?P<start>\d+)\-(?P<stop>\d+)") for record in records: name = record.id match = cut_regex.match(str(record.id)...
def strip_range(records)
Cut off trailing /<start>-<stop> ranges from IDs. Ranges must be 1-indexed and the stop integer must not be less than the start integer.
4.287136
3.541526
1.210533
logging.info('Applying _transcribe generator: ' 'operation to perform is ' + transcribe + '.') for record in records: sequence = str(record.seq) description = record.description name = record.id if transcribe == 'dna2rna': dna = Seq(sequence, IUP...
def transcribe(records, transcribe)
Perform transcription or back-transcription. transcribe must be one of the following: dna2rna rna2dna
2.774122
2.532181
1.095546
logging.info('Applying translation generator: ' 'operation to perform is ' + translate + '.') to_stop = translate.endswith('stop') source_type = translate[:3] alphabet = {'dna': IUPAC.ambiguous_dna, 'rna': IUPAC.ambiguous_rna}[source_type] # Get a translation table table...
def translate(records, translate)
Perform translation from generic DNA/RNA to proteins. Bio.Seq does not perform back-translation because the codons would more-or-less be arbitrary. Option to translate only up until reaching a stop codon. translate must be one of the following: dna2protein dna2proteinstop rna2prot...
4.783607
4.401067
1.08692
logging.info('Applying _max_length_discard generator: ' 'discarding records longer than ' '.') for record in records: if len(record) > max_length: # Discard logging.debug('Discarding long sequence: %s, length=%d', record.id, ...
def max_length_discard(records, max_length)
Discard any records that are longer than max_length.
4.976514
5.064428
0.982641
logging.info('Applying _min_length_discard generator: ' 'discarding records shorter than %d.', min_length) for record in records: if len(record) < min_length: logging.debug('Discarding short sequence: %s, length=%d', record.id, len(record)) else:...
def min_length_discard(records, min_length)
Discard any records that are shorter than min_length.
3.636918
3.704447
0.981771
for record in records: if len(ungap_all(record)) >= min_length: yield record
def min_ungap_length_discard(records, min_length)
Discard any records that are shorter than min_length after removing gaps.
4.384673
5.256048
0.834215
direction_text = 'ascending' if direction == 1 else 'descending' logging.info('Indexing sequences by length: %s', direction_text) # Adapted from the Biopython tutorial example. # Get the lengths and ids, and sort on length len_and_ids = sorted((len(rec), rec.id) for ...
def sort_length(source_file, source_file_type, direction=1)
Sort sequences by length. 1 is ascending (default) and 0 is descending.
2.976954
2.835616
1.049844
i = iter(iterable) while True: r = list(itertools.islice(i, chunk_size)) if not r: break yield r
def batch(iterable, chunk_size)
Return items from iterable in chunk_size bits. If len(iterable) % chunk_size > 0, the last item returned will be shorter.
2.188579
2.46357
0.888377
# Ignore SIGPIPE, for head support common.exit_on_sigpipe() logging.basicConfig() prot_sequences = SeqIO.parse(arguments.protein_align, fileformat.from_handle(arguments.protein_align)) nucl_sequences = SeqIO.parse(arguments.nucl_align, ...
def action(arguments)
Run
5.390335
5.49285
0.981337
codons = [''.join(i) for i in batch(str(aligned_nucl), 3)] for codon, aa in zip(codons, str(aligned_prot)): # Check gaps if codon == '---' and aa == '-': continue try: trans = self.translation_table.forward_table[codon] ...
def _validate_translation(self, aligned_prot, aligned_nucl)
Given a seq for protein and nucleotide, ensure that the translation holds
3.831625
3.723269
1.029102
if prot_seq.id != nucl_seq.id: logging.warning( 'ID mismatch: %s != %s. Are the sequences in the same order?', prot_seq.id, nucl_seq.id) # Ungap nucleotides codons = batch(str(nucl_seq.seq.ungap('-')), 3) codons = [''.join(i) for i in...
def map_alignment(self, prot_seq, nucl_seq)
Use aligned prot_seq to align nucl_seq
3.31528
3.301343
1.004222
zipped = itertools.zip_longest(prot_alignment, nucl_sequences) for p, n in zipped: if p is None: raise ValueError("Exhausted protein sequences") elif n is None: raise ValueError("Exhausted nucleotide sequences") yield self.map_...
def map_all(self, prot_alignment, nucl_sequences)
Convert protein sequences to nucleotide alignment
2.915451
2.798032
1.041965
if not extension.startswith('.'): raise ValueError("Extensions must begin with a period.") try: return EXTENSION_TO_TYPE[extension.lower()] except KeyError: raise UnknownExtensionError( "seqmagick does not know how to handle " + "files with extensions lik...
def from_extension(extension)
Look up the BioPython file type corresponding with input extension. Look up is case insensitive.
4.836839
4.729134
1.022775
base, extension = os.path.splitext(file_name) if extension in COMPRESS_EXT: # Compressed file extension = os.path.splitext(base)[1] return from_extension(extension)
def from_filename(file_name)
Look up the BioPython file type corresponding to an input file name.
4.25771
4.222269
1.008394
if fh in (sys.stdin, sys.stdout, sys.stderr): return stream_default return from_filename(fh.name)
def from_handle(fh, stream_default='fasta')
Look up the BioPython file type corresponding to a file-like object. For stdin, stdout, and stderr, ``stream_default`` is used.
3.538938
3.683815
0.960672
parser = argparse.ArgumentParser(description='seqmagick - Manipulate ' + \ ' sequence files.', prog='seqmagick') parser.add_argument('-V', '--version', action='version', version='seqmagick v' + version, help="Print the version number and exit") parser.add_argument('-v', ...
def parse_arguments(argv)
Extract command-line arguments for different actions.
3.094478
3.068791
1.00837
counter = itertools.count(0).__next__ ungap_indexes = [ counter() if c not in gap_chars else None for c in iter(sequence) ] return dict( (ungapped, gapped) for ungapped, gapped in zip(ungap_indexes, range(len(sequence))) if ungapped is not None)
def ungap_index_map(sequence, gap_chars='-')
Returns a dict mapping from an index in the ungapped sequence to an index in the gapped sequence. >>> ungap_index_map('AC-TG-') {0: 0, 1: 1, 2: 3, 3: 4}
4.031763
4.443757
0.907287
return dict( (v, k) for k, v in list(ungap_index_map(sequence, gap_chars).items()))
def gap_index_map(sequence, gap_chars='-')
Opposite of ungap_index_map: returns mapping from gapped index to ungapped index. >>> gap_index_map('AC-TG-') {0: 0, 1: 1, 3: 2, 4: 3}
5.898921
6.86094
0.859783
iupac_translation = { 'A': 'A', 'C': 'C', 'G': 'G', 'T': 'T', 'U': 'U', 'R': 'AG', 'Y': 'CT', 'S': 'GC', 'W': 'AT', 'K': 'GT', 'M': 'AC', 'B': 'CGT', 'D': 'AGT', 'H': 'ACT', 'V': 'ACG', ...
def _iupac_ambiguous_equal(ambig_base, unambig_base)
Tests two bases for equality, accounting for IUPAC ambiguous DNA ambiguous base may be IUPAC ambiguous, unambiguous must be one of ACGT
1.792578
1.770711
1.012349
if not len(s1) == len(s2): raise ValueError("String lengths are not equal") # Number of non-matching characters: return sum(not equality_function(c1, c2) for c1, c2 in zip(s1, s2))
def hamming_distance(s1, s2, equality_function=operator.eq)
Returns the hamming distance between two strings.
2.67173
2.535858
1.05358
forward_loc = None reverse_loc = None seq_length = None # Reverse complement the reverse primer, if appropriate if reverse_complement: reverse_primer = reverse_primer.reverse_complement() forward_aligner = PrimerAligner(forward_primer) reverse_aligner = PrimerAligner(reverse_p...
def locate_primers(sequences, forward_primer, reverse_primer, reverse_complement, max_hamming_distance)
Find forward and reverse primers in a set of sequences, return two tuples: (forward_start, forward_end), (reverse_start, reverse_end)
2.461906
2.43716
1.010154
logging.info("Trimming from %d to %d", start, end) return (sequence[start:end] for sequence in sequences)
def trim(sequences, start, end)
Slice the input sequences from start to end
3.604483
3.825739
0.942166
# Determine file format for input and output source_format = (arguments.source_format or fileformat.from_handle(arguments.source_file)) output_format = (arguments.output_format or fileformat.from_handle(arguments.output_file)) # Load the alignment with...
def action(arguments)
Trim the alignment as specified
2.94055
2.921592
1.006489
seq_aln, primer_aln, score, start, end = pairwise2.align.globalms( str(sequence).upper(), str(self.primer).upper(), self.match, self.difference, self.gap_open, self.gap_extend, one_alignment_only=True, penalize_end_gaps=self.penalize_end_gaps)[0] ...
def align(self, sequence)
Aligns the primer to the given query sequence, returning a tuple of: hamming_distance, start, end Where hamming distance is the distance between the primer and aligned sequence, and start and end give the start and end index of the primer relative to the input sequence.
3.918909
3.855601
1.01642
for record in records: seq_hash = hashlib.sha1(str(record.seq)).hexdigest() if seq_hash[0].isdigit(): yield record
def hash_starts_numeric(records)
Very useful function that only accepts records with a numeric start to their sha-1 hash.
3.546213
3.137775
1.130168
if permissions is None: permissions = apply_umask() # Handle stdout: if path == '-': yield sys.stdout else: base_dir = os.path.dirname(path) kwargs['suffix'] = os.path.basename(path) tf = tempfile.NamedTemporaryFile( dir=base_dir, mode=mode, delet...
def atomic_write(path, mode='wt', permissions=None, file_factory=None, **kwargs)
Open a file for atomic writing. Generates a temp file, renames to value of ``path``. Arguments: ``permissions``: Permissions to set (default: umask) ``file_factory``: If given, the handle yielded will be the result of calling file_factory(path) Additional arguments are passed to tempfile....
3.179289
3.265848
0.973496
value_range = string.split(':') if len(value_range) == 1: start = int(value_range[0]) stop = start elif len(value_range) == 2: start, stop = tuple(int(i) if i else None for i in value_range) else: msg = "{0} is not a valid, 1-indexed range.".format(string) ra...
def cut_range(string)
A custom argparse 'type' to deal with sequences ranges such as 5:500. Returns a 0-based slice corresponding to the selection defined by the slice
2.430252
2.368044
1.02627
@functools.wraps(type_func) def inner(string): result = type_func(string) if not result >= minimum and result <= maximum: raise argparse.ArgumentTypeError( "Please provide a value between {0} and {1}".format( minimum, maximum)) ...
def typed_range(type_func, minimum, maximum)
Require variables to be of the specified type, between minimum and maximum
2.526406
2.554895
0.988849
if isinstance(argument_keys, str): argument_keys = [argument_keys] argument_keys = argument_keys or [] class PartialAppendAction(argparse.Action): def __init__(self, option_strings, dest, const=None, de...
def partial_append_action(fn, argument_keys=None)
Creates a new class extending argparse.Action, which appends a partially-applied function to dest. The optional argument_keys argument should either be None (no additional arguments to fn) or an iterable of function keys to partially apply.
1.998098
2.026181
0.98614
def inner(string): value = target_type(string) if not value >= 0: raise argparse.ArgumentTypeError("Invalid positive number: " + string) return value return inner
def positive_value(target_type)
Wraps target_type in a function that requires the parsed argument be >= 0
5.721087
5.145907
1.111774
add_options(parser) parser.add_argument('source_file', type=common.FileType('rt'), help="Input sequence file") parser.add_argument('dest_file', help="Output file") return parser
def build_parser(parser)
Add shared arguments to the convert or mogrify parser.
4.564107
4.133883
1.104073
parts = string.split(':', 2) if len(parts) < 2: raise ValueError( "Illegal specification. Should be module:function[:parameter]") module_path, function_name = parts[:2] # Import the module module_vars = {} exec(compile(open(module_path).read(), module_path, 'exec'), mod...
def module_function(string)
Load a function from a python module using a file name, function name specification of format: /path/to/x.py:function_name[:parameter]
2.874858
2.700205
1.064681
out = defaultdict(list) for header, seq in parse_fasta(cgmlst_fasta): if not '|' in header: raise Exception('Unexpected format for cgMLST fasta file header. No "|" (pipe) delimiter present! Header="{}"'.format(header)) marker_name, allele_name = header.split('|') out[mar...
def parse_cgmlst_alleles(cgmlst_fasta)
Parse cgMLST alleles from fasta file cgMLST FASTA file must have a header format of ">{marker name}|{allele name}" Args: cgmlst_fasta (str): cgMLST fasta file path Returns: dict of list: Marker name to list of allele sequences
4.274293
3.646484
1.172169
''' Parse a fasta file returning a generator yielding tuples of fasta headers to sequences. Note: This function should give equivalent results to SeqIO from BioPython .. code-block:: python from Bio import SeqIO # biopython to dict of header-seq hseqs_b...
def parse_fasta(filepath)
Parse a fasta file returning a generator yielding tuples of fasta headers to sequences. Note: This function should give equivalent results to SeqIO from BioPython .. code-block:: python from Bio import SeqIO # biopython to dict of header-seq hseqs_bio = {r.desc...
3.043956
1.338936
2.273415
header_count = 0 line_count = 1 nt_count = 0 with open(fasta_path) as f: for l in f: l = l.strip() if l == '': continue if l[0] == '>': header_count += 1 continue if header_count == 0 and l[0] !...
def fasta_format_check(fasta_path, logger)
Check that a file is valid FASTA format. - First non-blank line needs to begin with a '>' header character. - Sequence can only contain valid IUPAC nucleotide characters Args: fasta_str (str): FASTA file contents string Raises: Exception: If invalid FASTA format
2.283862
2.189877
1.042918
return np.array([[NT_TO_INT[c] for c in x.upper()] for x in seqs])
def seq_int_arr(seqs)
Convert list of ACGT strings to matix of 1-4 ints Args: seqs (list of str): nucleotide sequences with only 'ACGT' characters Returns: numpy.array of int: matrix of integers from 1 to 4 inclusive representing A, C, G, and T str: nucleotide sequence string
6.460312
9.066488
0.712548
starts = arr[:,0:bp] ends = arr[:,-bp:] starts_ends_idxs = defaultdict(list) l, seq_len = arr.shape for i in range(l): start_i = starts[i] end_i = ends[i] start_i_str = ''.join([str(x) for x in start_i]) end_i_str = ''.join([str(x) for x in end_i]) starts...
def group_alleles_by_start_end_Xbp(arr, bp=28)
Group alleles by matching ends Args: arr (numpy.array): 2D int matrix of alleles bp (int): length of ends to group by Returns: dict of lists: key of start + end strings to list of indices of alleles with matching ends
2.270187
2.348312
0.966731
clusters = fcluster(linkage(dists), 0.025, criterion='distance') cluster_idx = defaultdict(list) for idx, cl in enumerate(clusters): cluster_idx[cl].append(idx) return cluster_idx
def allele_clusters(dists, t=0.025)
Flat clusters from distance matrix Args: dists (numpy.array): pdist distance matrix t (float): fcluster (tree cutting) distance threshold Returns: dict of lists: cluster number to list of indices of distances in cluster
2.611364
3.383235
0.771854
row_sums = np.apply_along_axis(arr=dists, axis=0, func1d=np.sum) return row_sums.argmin()
def min_row_dist_sum_idx(dists)
Find the index of the row with the minimum row distance sum This should return the index of the row index with the least distance overall to all other rows. Args: dists (np.array): must be square distance matrix Returns: int: index of row with min dist row sum
2.862901
3.9465
0.725428
centroid_alleles = set() len_allele = group_alleles_by_size(alleles) for length, seqs in len_allele.items(): # if only one alelle of a particular size, add as centroid, move onto next size group if len(seqs) == 1: centroid_alleles.add(seqs[0]) continue # ...
def find_centroid_alleles(alleles, bp=28, t=0.025)
Reduce list of alleles to set of centroid alleles based on size grouping, ends matching and hierarchical clustering Workflow for finding centroid alleles: - grouping by size (e.g. 100bp, 101bp, 103bp, etc) - then grouped by `bp` nucleotides at ends matching - size and ends grouped alleles hierarchical...
4.141847
3.90484
1.060696
args = [MASH_BIN, 'dist', MASH_SKETCH_FILE, fasta_path] p = Popen(args, stderr=PIPE, stdout=PIPE) (stdout, stderr) = p.communicate() retcode = p.returncode if retcode != 0: raise Exception('Could not run Mash dist {}'.format(stderr)) return stdou...
def mash_dist_trusted(fasta_path)
Compute Mash distances of sketch file of genome fasta to RefSeq sketch DB. Args: mash_bin (str): Mash binary path Returns: (str): Mash STDOUT string
3.119581
3.305265
0.943822
gs_collapse = [] genome_idx_dict = {} indices = [] patt_dict = {} for i, g in enumerate(genomes): p = arr[i, :].tostring() if p in patt_dict: parent = patt_dict[p] idx = genome_idx_dict[parent] gs_collapse[idx].append(g) else: ...
def nr_profiles(arr, genomes)
Get a condensed cgMLST pairwise distance matrix for specified Genomes_ where condensed means redundant cgMLST profiles are only represented once in the distance matrix. Args: user_name (list): List of Genome_ names to retrieve condensed distance matrix for Returns: (numpy.array, list): tup...
2.824436
3.048773
0.926417
assert isinstance(df, pd.DataFrame) markers = [] alleles = [] for x in df['qseqid']: marker, allele = x.split('|') markers.append(marker) alleles.append(int(allele)) df.loc[:, 'marker'] = markers df.loc[:, 'allele'] = alleles df.loc[:, 'is_match'] = (df['coverag...
def process_cgmlst_results(df)
Append informative fields to cgMLST330 BLAST results DataFrame The `qseqid` column must contain cgMLST330 query IDs with `{marker name}|{allele number}` format. The `qseqid` parsed allele numbers and marker names are appended as new fields. `is_perfect` column contains boolean values for whether an allele...
2.720761
2.604042
1.044822
contig_blastn_records = defaultdict(list) markers = df.marker.unique() for m in markers: dfsub = df[df.marker == m] for i, r in dfsub.iterrows(): if r.coverage < 1.0: contig_blastn_records[r.stitle].append(r) break return contig_blastn_records
def alleles_to_retrieve(df)
Alleles to retrieve from genome fasta Get a dict of the genome fasta contig title to a list of blastn results of the allele sequences that must be retrieved from the genome contig. Args: df (pandas.DataFrame): blastn results dataframe Returns: {str:[pandas.Series]}: dict of contig tit...
4.06161
3.568126
1.138303
assert isinstance(df, pd.DataFrame) from collections import defaultdict d = defaultdict(list) for idx, row in df.iterrows(): marker = row['marker'] d[marker].append(row) marker_results = {} for k,v in d.items(): if len(v) > 1: logging.debug('Multiple pot...
def matches_to_marker_results(df)
Perfect BLAST matches to marker results dict Parse perfect BLAST matches to marker results dict. Args: df (pandas.DataFrame): DataFrame of perfect BLAST matches Returns: dict: cgMLST330 marker names to matching allele numbers
2.579875
2.562669
1.006714
closest_distance = df_relatives['distance'].min() if closest_distance > CGMLST_SUBSPECIATION_DISTANCE_THRESHOLD: logging.warning('Min cgMLST distance (%s) above subspeciation distance threshold (%s)', closest_distance, CGMLST_SUBSPECIATION_DISTANCE_THRESHOLD) retur...
def cgmlst_subspecies_call(df_relatives)
Call Salmonella subspecies based on cgMLST results This method attempts to find the majority subspecies type within curated public genomes above a cgMLST allelic profile distance threshold. Note: ``CGMLST_SUBSPECIATION_DISTANCE_THRESHOLD`` is the cgMLST distance threshold used to determine...
2.867805
2.621683
1.093879
filename = os.path.basename(fasta_path) return re.sub(r'(\.fa$)|(\.fas$)|(\.fasta$)|(\.fna$)|(\.\w{1,}$)', '', filename)
def genome_name_from_fasta_path(fasta_path)
Extract genome name from fasta filename Get the filename without directory and remove the file extension. Example: With fasta file path ``/path/to/genome_1.fasta``:: fasta_path = '/path/to/genome_1.fasta' genome_name = genome_name_from_fasta_path(fasta_path) print(...
3.154746
3.942099
0.800271
needs_revcomp = df.sstart > df.send add_to_end = df.qlen - df.qend add_to_start = df.qstart - 1 ssum2 = (df.send + df.sstart) / 2.0 sabs2 = np.abs(df.send - df.sstart) / 2.0 end_idx = ssum2 + sabs2 - 1 start_idx = ssum2 - sabs2 - 1 start_idx[needs_revcomp] -= add_to_end start_id...
def extend_subj_match_vec(df)
Get the extended clipped (clamped) start and end subject sequence indices Also get whether each match needs to be reverse complemented and whether each extended match would be truncated by the end of the subject sequence. Args: df (pandas.DataFrame): blastn results dataframe Returns: ...
2.39604
2.166208
1.106099
return [attr for attr in dir(x) if not attr.startswith("__") and not callable(getattr(x, attr))]
def listattrs(x)
Get all instance and class attributes for an object Get all instance and class attributes for an object except those that start with "__" (double underscore). __dict__ of an object only reports the instance attributes while dir() reports all of the attributes of an object including private on...
2.358208
3.389309
0.695778
if x is None or isinstance(x, (str, int, float, bool)): return x if isinstance(x, np.int_): return int(x) if isinstance(x, np.int64): return int(x) if isinstance(x, np.float_): return float(x) if isinstance(x, np.float64): return float(x) if isinstanc...
def to_dict(x, depth, exclude_keys=set(), depth_threshold=8)
Transform a nested object/dict/list into a regular dict json.dump(s) and pickle don't like to un/serialize regular Python objects so this function should handle arbitrarily nested objects to be serialized to regular string, float, int, bool, None values. This is a recursive function so by defa...
1.61829
1.66375
0.972677
if x is None or isinstance(x, (str, int, float, bool)): out[key] = x return out if isinstance(x, list): for i, v in enumerate(x): new_key = '{}{}{}'.format(key, sep, i) out = _recur_flatten(new_key, v, out, sep) if isinstance(x, dict): for k, v in...
def _recur_flatten(key, x, out, sep='.')
Helper function to flatten_dict Recursively flatten all nested values within a dict Args: key (str): parent key x (object): object to flatten or add to out dict out (dict): 1D output dict sep (str): flattened key separator string Returns: dict: flattene...
1.586797
1.550283
1.023553
out = {} for k, v in x.items(): out = _recur_flatten(k, v, out) return out
def flatten_dict(x)
Flatten a dict Flatten an arbitrarily nested dict as output by to_dict .. note:: Keys in the flattened dict may get very long. Args: x (dict): Arbitrarily nested dict (maybe resembling a tree) with literal/scalar leaf values Returns: dict: flattened...
3.374915
4.662932
0.723775
if df is not None: return [dict(r) for i, r in df.head(1).iterrows()][0]
def df_first_row_to_dict(df)
First DataFrame row to list of dict Args: df (pandas.DataFrame): A DataFrame with at least one row Returns: A list of dict that looks like: [{'C1': 'x'}, {'C2': 'y'}, {'C3': 'z'}] from a DataFrame that looks like: C1 C2 C3 ...
4.125495
5.812114
0.70981
q_match_len = abs(qstart - qend) + 1 s_max = max(sstart, send) s_min = min(sstart, send) return (q_match_len < qlen) and (s_max >= slen or s_min <= 1)
def is_blast_result_trunc(qstart, qend, sstart, send, qlen, slen)
Check if a query sequence is truncated by the end of a subject sequence Args: qstart (int): Query sequence start index qend (int): Query sequence end index sstart (int): Subject sequence start index send (int): Subject sequence end index qlen (int): Q...
2.862886
3.345784
0.85567
ssum2 = (send + sstart) / 2.0 sabs2 = np.abs(send - sstart) / 2.0 smax = ssum2 + sabs2 smin = ssum2 - sabs2 q_match_len = np.abs(qstart - qend) + 1 return (q_match_len < qlen) & ((smax >= slen) | (smin <= 1))
def trunc(qstart, qend, sstart, send, qlen, slen)
Check if a query sequence is truncated by the end of a subject sequence Args: qstart (int pandas.Series): Query sequence start index qend (int pandas.Series): Query sequence end index sstart (int pandas.Series): Subject sequence start index send (int pandas.Serie...
3.865307
4.119784
0.938231
if self.is_missing: return None df_perfect_matches = self.df[(self.df['coverage'] == 1.0) & (self.df['pident'] == 100.0)] if df_perfect_matches.shape[0] == 0: return None return df_perfect_matches
def perfect_matches(self)
Return pandas DataFrame with perfect BLAST matches (100% identity and coverage) Returns: pandas.DataFrame or None: DataFrame of perfect BLAST matches or None if no perfect matches exist
3.074895
2.652994
1.159028
if self.is_missing: return None df_perfect_matches = self.df[(self.df['coverage'] == 1.0) & (self.df['pident'] == 100.0)] if df_perfect_matches.shape[0]: self.is_perfect_match = True return BlastReader.df_first_row_to_dict(df_perfect_matches) ...
def top_result(self)
Return top `blastn` result Try to find a 100% identity and coverage result (perfect match). If one does not exist, then retrieve the result with the highest bitscore. Returns: Ordered dict of BLASTN results or None if no BLASTN results generated
3.409648
3.030763
1.125013
genome_name = genome_name_from_fasta_path(fasta_path) outpath = os.path.join(outdir, genome_name) args = ['mash', 'sketch', '-o', outpath, fasta_path] logging.info('Running Mash sketch with command: %s', ' '.join(args)) p = Popen(args) p.wait() sketch_path = outpath + '.msh' assert ...
def sketch_fasta(fasta_path, outdir)
Create a Mash sketch from an input fasta file Args: fasta_path (str): input fasta file path. Genome name in fasta filename outdir (str): output directory path to write Mash sketch file to Returns: str: output Mash sketch file path
2.351736
2.385705
0.985761
merge_sketch_path = os.path.join(outdir, 'sistr.msh') args = ['mash', 'paste', merge_sketch_path] for x in sketch_paths: args.append(x) args.append(MASH_SKETCH_FILE) logging.info('Running Mash paste with command: %s', ' '.join(args)) p = Popen(args) p.wait() assert os.path.e...
def merge_sketches(outdir, sketch_paths)
Merge new Mash sketches with current Mash sketches Args: outdir (str): output directory to write merged Mash sketch file sketch_paths (list of str): Mash sketch file paths for input fasta files Returns: str: output path for Mash sketch file with new and old sketches
3.182477
3.079458
1.033454
self._dstore.delete(key) self.cache.delete(key)
def delete(self, key)
Implementation of :meth:`~simplekv.KeyValueStore.delete`. If an exception occurs in either the cache or backing store, all are passing on.
9.017484
10.008787
0.900957
try: return self.cache.get(key) except KeyError: # cache miss or error, retrieve from backend data = self._dstore.get(key) # store in cache and return self.cache.put(key, data) return data except IOError: ...
def get(self, key)
Implementation of :meth:`~simplekv.KeyValueStore.get`. If a cache miss occurs, the value is retrieved, stored in the cache and returned. If the cache raises an :exc:`~exceptions.IOError`, the cache is ignored, and the backing store is consulted directly. It is possible for a c...
4.523324
4.071337
1.111017
try: return self.cache.get_file(key, file) except KeyError: # cache miss, load into cache fp = self._dstore.open(key) self.cache.put_file(key, fp) # return from cache return self.cache.get_file(key, file)
def get_file(self, key, file)
Implementation of :meth:`~simplekv.KeyValueStore.get_file`. If a cache miss occurs, the value is retrieved, stored in the cache and returned. If the cache raises an :exc:`~exceptions.IOError`, the retrieval cannot proceed: If ``file`` was an open file, data maybe been written to it ...
3.760046
4.273208
0.879912
try: return self.cache.open(key) except KeyError: # cache miss, load into cache fp = self._dstore.open(key) self.cache.put_file(key, fp) return self.cache.open(key) except IOError: # cache error, ignore completely ...
def open(self, key)
Implementation of :meth:`~simplekv.KeyValueStore.open`. If a cache miss occurs, the value is retrieved, stored in the cache, then then another open is issued on the cache. If the cache raises an :exc:`~exceptions.IOError`, the cache is ignored, and the backing store is consulted direct...
5.072225
5.248951
0.966331
try: k = self._dstore.copy(source, dest) finally: self.cache.delete(dest) return k
def copy(self, source, dest)
Implementation of :meth:`~simplekv.CopyMixin.copy`. Copies the data in the backing store and removes the destination key from the cache, in case it was already populated. Does not work when the backing store does not implement copy.
7.235035
6.015285
1.202775
try: return self._dstore.put(key, data) finally: self.cache.delete(key)
def put(self, key, data)
Implementation of :meth:`~simplekv.KeyValueStore.put`. Will store the value in the backing store. After a successful or unsuccessful store, the cache will be invalidated by deleting the key from it.
5.478619
6.711468
0.816307
try: return self._dstore.put_file(key, file) finally: self.cache.delete(key)
def put_file(self, key, file)
Implementation of :meth:`~simplekv.KeyValueStore.put_file`. Will store the value in the backing store. After a successful or unsuccessful store, the cache will be invalidated by deleting the key from it.
4.922434
6.029267
0.816423
self._check_valid_key(key) if isinstance(file, str): return self._get_filename(key, file) else: return self._get_file(key, file)
def get_file(self, key, file)
Write contents of key to file Like :meth:`.KeyValueStore.put_file`, this method allows backends to implement a specialized function if data needs to be written to disk or streamed. If *file* is a string, contents of *key* are written to a newly created file with the filename *f...
3.000023
3.492803
0.858916
# FIXME: shouldn't we call self._check_valid_key here? if isinstance(file, str): return self._put_filename(key, file) else: return self._put_file(key, file)
def put_file(self, key, file)
Store into key from file on disk Stores data from a source into key. *file* can either be a string, which will be interpretet as a filename, or an object with a *read()* method. If the passed object has a *fileno()* method, it may be used to speed up the operation. The...
4.658128
5.147916
0.904857
if not isinstance(key, key_type): raise ValueError('%r is not a valid key type' % key) if not VALID_KEY_RE.match(key): raise ValueError('%r contains illegal characters' % key)
def _check_valid_key(self, key)
Checks if a key is valid and raises a ValueError if its not. When in need of checking a key for validity, always use this method if possible. :param key: The key to be checked
3.379622
3.72654
0.906906
buf = BytesIO() self._get_file(key, buf) return buf.getvalue()
def _get(self, key)
Implementation for :meth:`~simplekv.KeyValueStore.get`. The default implementation will create a :class:`io.BytesIO`-buffer and then call :meth:`~simplekv.KeyValueStore._get_file`. :param key: Key of value to be retrieved
7.554614
6.883566
1.097485
bufsize = 1024 * 1024 # note: we do not use a context manager here or close the source. # the source goes out of scope shortly after, taking care of the issue # this allows us to support file-like objects without close as well, # such as BytesIO. source = self.o...
def _get_file(self, key, file)
Write key to file-like object file. Either this method or :meth:`~simplekv.KeyValueStore._get_filename` will be called by :meth:`~simplekv.KeyValueStore.get_file`. Note that this method does not accept strings. :param key: Key to be retrieved :param file: File-like object to wri...
5.623796
6.357171
0.884638
with open(filename, 'wb') as dest: return self._get_file(key, dest)
def _get_filename(self, key, filename)
Write key to file. Either this method or :meth:`~simplekv.KeyValueStore._get_file` will be called by :meth:`~simplekv.KeyValueStore.get_file`. This method only accepts filenames and will open the file with a mode of ``wb``, then call :meth:`~simplekv.KeyValueStore._get_file`. :p...
5.38139
5.087996
1.057664
self._check_valid_key(key) if not isinstance(data, bytes): raise IOError("Provided data is not of type bytes") return self._put(key, data, self._valid_ttl(ttl_secs))
def put(self, key, data, ttl_secs=None)
Like :meth:`~simplekv.KeyValueStore.put`, but with an additional parameter: :param ttl_secs: Number of seconds until the key expires. See above for valid values. :raises exceptions.ValueError: If ``ttl_secs`` is invalid. :raises exceptions.IOError...
3.969429
5.225909
0.759567
if ttl_secs is None: ttl_secs = self.default_ttl_secs self._check_valid_key(key) if isinstance(file, str): return self._put_filename(key, file, self._valid_ttl(ttl_secs)) else: return self._put_file(key, file, self._valid_ttl(ttl_secs))
def put_file(self, key, file, ttl_secs=None)
Like :meth:`~simplekv.KeyValueStore.put_file`, but with an additional parameter: :param ttl_secs: Number of seconds until the key expires. See above for valid values. :raises exceptions.ValueError: If ``ttl_secs`` is invalid.
2.564226
2.802206
0.915074
self._check_valid_key(source) self._check_valid_key(dest) return self._copy(source, dest)
def copy(self, source, dest)
Copies a key. The destination is overwritten if does exist. :param source: The source key to copy :param dest: The destination for the copy :returns: The destination key :raises: exceptions.ValueError: If the source or target key are not valid :raises: exceptions.KeyError: If ...
3.491227
3.892233
0.896973
try: if self.transport in ('http', 'https'): connection = pyeapi.client.connect( transport=self.transport, host=self.hostname, username=self.username, password=self.password, ...
def open(self)
Implementation of NAPALM method open.
5.165677
4.910399
1.051987
if self.config_session is None: return '' else: commands = ['show session-config named %s diffs' % self.config_session] result = self.device.run_commands(commands, encoding='text')[0]['output'] result = '\n'.join(result.splitlines()[2:]) ...
def compare_config(self)
Implementation of NAPALM method compare_config.
6.264561
5.408361
1.15831
commands = [] commands.append('copy startup-config flash:rollback-0') commands.append('configure session {}'.format(self.config_session)) commands.append('commit') commands.append('write memory') self.device.run_commands(commands) self.config_session = N...
def commit_config(self)
Implementation of NAPALM method commit_config.
6.103019
4.744704
1.28628
if self.config_session is not None: commands = [] commands.append('configure session {}'.format(self.config_session)) commands.append('abort') self.device.run_commands(commands) self.config_session = None
def discard_config(self)
Implementation of NAPALM method discard_config.
4.093068
3.387625
1.208241
commands = [] commands.append('configure replace flash:rollback-0') commands.append('write memory') self.device.run_commands(commands)
def rollback(self)
Implementation of NAPALM method rollback.
9.962878
7.473568
1.333082
commands = [] commands.append('show version') commands.append('show hostname') commands.append('show interfaces') result = self.device.run_commands(commands) version = result[0] hostname = result[1] interfaces_dict = result[2]['interfaces'] ...
def get_facts(self)
Implementation of NAPALM method get_facts.
3.399842
3.316766
1.025048
# Default values snmp_dict = { 'chassis_id': '', 'location': '', 'contact': '', 'community': {} } commands = [ 'show snmp chassis', 'show snmp location', 'show snmp contact' ] s...
def get_snmp_information(self)
get_snmp_information() for EOS. Re-written to not use TextFSM
3.08269
3.009965
1.024162
get_startup = retrieve == "all" or retrieve == "startup" get_running = retrieve == "all" or retrieve == "running" get_candidate = (retrieve == "all" or retrieve == "candidate") and self.config_session if retrieve == "all": commands = ['show startup-config', ...
def get_config(self, retrieve="all")
get_config implementation for EOS.
1.958822
1.930104
1.014879
output = self._show_vrf() vrfs = {} all_vrf_interfaces = {} for vrf in output: if (vrf.get('route_distinguisher', '') == "<not set>" or vrf.get('route_distinguisher', '') == 'None'): vrf['route_distinguisher'] = u'' el...
def get_network_instances(self, name='')
get_network_instances implementation for EOS.
2.285931
2.232895
1.023752
from boto.exception import BotoClientError, BotoServerError, \ StorageResponseError try: yield except StorageResponseError as e: if e.code == 'NoSuchKey': raise KeyError(key) raise IOError(str(e)) except (BotoClientError, BotoServerError) as e: if...
def map_boto_exceptions(key=None, exc_pass=())
Map boto-specific exceptions to the simplekv-API.
2.839473
2.73574
1.037918
if not isinstance(key, key_type) and key is not None: raise ValueError('%r is not a valid key type' % key) if not VALID_KEY_RE_EXTENDED.match(key) or key == u'/': raise ValueError('%r contains illegal characters' % key)
def _check_valid_key(self, key)
Checks if a key is valid and raises a ValueError if its not. When in need of checking a key for validity, always use this method if possible. :param key: The key to be checked
4.876864
5.497001
0.887186
md5 = hashlib.md5() chunk_size = 128 * md5.block_size for chunk in iter(lambda: file_.read(chunk_size), b''): md5.update(chunk) file_.seek(0) byte_digest = md5.digest() return base64.b64encode(byte_digest).decode()
def _file_md5(file_)
Compute the md5 digest of a file in base64 encoding.
1.867354
1.762675
1.059387
md5 = hashlib.md5(buffer_) byte_digest = md5.digest() return base64.b64encode(byte_digest).decode()
def _byte_buffer_md5(buffer_)
Computes the md5 digest of a byte buffer in base64 encoding.
2.571183
2.44601
1.051174
from azure.common import AzureMissingResourceHttpError, AzureHttpError,\ AzureException try: yield except AzureMissingResourceHttpError as ex: if ex.__class__.__name__ not in exc_pass: s = str(ex) if s.startswith(u"The specified container does not exist."...
def map_azure_exceptions(key=None, exc_pass=())
Map Azure-specific exceptions to the simplekv-API.
2.381289
2.31908
1.026825
if self.closed: raise ValueError("I/O operation on closed file") with map_azure_exceptions(key=self.key): if size < 0: size = self.size - self.pos end = min(self.pos + size - 1, self.size - 1) if self.pos > end: re...
def read(self, size=-1)
Returns 'size' amount of bytes or less if there is no more data. If no size is given all data is returned. size can be >= 0.
2.701162
2.696676
1.001664
if self.closed: raise ValueError("I/O operation on closed file") if whence == 0: if offset < 0: raise IOError('seek would move position outside the file') self.pos = offset elif whence == 1: if self.pos + offset < 0: ...
def seek(self, offset, whence=0)
Move to a new offset either relative or absolute. whence=0 is absolute, whence=1 is relative, whence=2 is relative to the end. Any relative or absolute seek operation which would result in a negative position is undefined and that case can be ignored in the implementation. Any ...
1.75579
1.794564
0.978393
# pattern-matching: if len(components) == 1: if isinstance(obj, Blob): mode = 0o100644 elif isinstance(obj, Tree): mode = 0o040000 elif obj is None: mode = None else: raise TypeError('Can only mount Blobs or Trees') na...
def _on_tree(repo, tree, components, obj)
Mounts an object on a tree, using the given path components. :param tree: Tree object to mount on. :param components: A list of strings of subpaths (i.e. ['foo', 'bar'] is equivalent to '/foo/bar') :param obj: Object to mount. If None, removes the object found at path ...
2.884693
2.685792
1.074057
allen_namespaces = { 'JAX': 'http://jaxmice.jax.org/strain/', 'MMRRC': 'http://www.mmrrc.org/catalog/getSDS.jsp?mmrrc_id=', 'AIBS': 'http://api.brain-map.org/api/v2/data/TransgenicLine/', } for prefix, iri in allen_namespaces.items(): self...
def build_transgenic_lines(self)
init class | "transgenic_line_source_name":"stock_number" a Class add superClass | rdfs:subClassOf ilxtr:transgenicLine add *order* | ilxtr:useObjectProperty ilxtr:<order> add name | rdfs:label "name" add def | definition: "description" add transtype | ...
3.757436
3.270839
1.148768
with open('myfile', 'rt') as f: rows = [r for r in csv.reader(f)] dothing = lambda _: [i for i, v in enumerate(_)] rows = [dothing(_) for _ in rows] raise NotImplementedError('You need to implement this yourlself!') return rows
def datagetter(cls)
example datagetter function, make any local modifications here
8.04245
6.821722
1.178947
for thing in data: graph.add_trip(*thing) raise NotImplementedError('You need to implement this yourlself!')
def dataproc(cls, graph, data)
example datagetter function, make any local modifications here
23.050945
18.174095
1.268341