the-way 0.13.0

A code snippets manager for your terminal
Documentation
1
{"index":1,"description":"Append to an existing key in sled","language":"rust","code":"\n/// If key exists add value to existing values delimited with a semicolon\n/// use as `db.set_merge_operator(merge_index)`\nfn merge_index(_key: &[u8], old_indices: Option<&[u8]>, new_index: &[u8]) -> Option<Vec<u8>> {\n    let mut ret = old_indices.map_or_else(Vec::new, |old| old.to_vec());\n    if !ret.is_empty() {\n        ret.extend_from_slice(&[utils::SEMICOLON]);\n    }\n    ret.extend_from_slice(new_index);\n    Some(ret)\n}","extension":".rs","tags":["database","sled"],"date":"2020-05-14T00:00:00Z","updated":"2020-07-23T19:23:41.397241Z"}{"index":10,"description":"Linking to a remote jupyter notebook","language":"bash","code":"ssh -N -L localhost:8913:eddy.bioinformatics.nl:8913 durai001@sftp.ab.wur.nl","extension":".sh","tags":["one-line"],"date":"2020-05-15T11:27:16.663806Z","updated":"2020-07-12T17:39:28.830779Z"}{"index":11,"description":"Fetch a sequence from UniProt","language":"python","code":"import requests\ndef get_uniprot_sequence(uniprot_id: str) -> str:\n    mapping_url = f\"http://www.uniprot.org/uniprot/{uniprot_id}.fasta\"\n    response = requests.get(mapping_url).text\n    if \"html\" not in response.strip():\n        return ''.join(response.split('\\n')[1:])\n    else:\n        mapping_url = f\"http://www.uniprot.org/uniparc/{uniprot_id}.fasta\"\n        response = requests.get(mapping_url).text\n        if \"html\" not in response.strip():\n            return ''.join(response.split('\\n')[1:])\n        else:\n            return None","extension":".py","tags":["uniprot"],"date":"2020-06-18T10:20:21.461597Z","updated":"2020-07-12T17:39:28.830794Z"}{"index":12,"description":"Customize fuzzy search","language":"rust","code":"use chrono::{Date, Utc};\nuse chrono_english::{parse_date_string, Dialect};\n/// Makes a date from a string, can be colloquial like \"next Friday\"\npub fn parse_date(date_string: &str) -> Result<Date<Utc>, Error> {\n    if date_string.to_ascii_lowercase() == \"today\" {\n        Ok(Utc::now().date())\n    } else {\n        Ok(parse_date_string(date_string, Utc::now(), Dialect::Uk)?.date())\n    }\n}","extension":".rs","tags":["cli","skim"],"date":"2020-05-14T07:38:53.725323Z","updated":"2020-07-12T17:39:28.830826Z"}{"index":13,"description":"Heatmap with values annotated","language":"python","code":"import seaborn as sns\nsns.heatmap(matrix, vmin=-1, vmax=1, \n            center=0, annot=True, square=True)","extension":".py","tags":["plotting"],"date":"2020-08-06T13:20:17.029053Z","updated":"2020-08-06T13:20:37.143501Z"}{"index":14,"description":"Parse fasta file","language":"python","code":"import typing\nfrom pathlib import Path\n\ndef get_sequences_from_fasta_yield(fasta_file: typing.Union[str, Path]) -> tuple:\n    \"\"\"\n    Returns (accession, sequence) iterator\n    Parameters\n    ----------\n    fasta_file\n    Returns\n    -------\n    (accession, sequence)\n    \"\"\"\n    with open(fasta_file) as f:\n        current_sequence = \"\"\n        current_key = None\n        for line in f:\n            if not len(line.strip()):\n                continue\n            if \"==\" in line:\n                continue\n            if \">\" in line:\n                if current_key is None:\n                    current_key = line.split(\">\")[1].strip()\n                else:\n                    yield current_key, current_sequence\n                    current_sequence = \"\"\n                    current_key = line.split(\">\")[1].strip()\n            else:\n                current_sequence += line.strip()\n        yield current_key, current_sequence\n\n\ndef get_sequences_from_fasta(fasta_file: typing.Union[str, Path]) -> dict:\n    \"\"\"\n    Returns dict of accession to sequence from fasta file\n    Parameters\n    ----------\n    fasta_file\n    Returns\n    -------\n    {accession:sequence}\n    \"\"\"\n    return {key: sequence for (key, sequence) in get_sequences_from_fasta_yield(fasta_file)}","extension":".py","tags":["bioinformatics"],"date":"2020-08-27T10:49:41.923270Z","updated":"2020-08-27T10:50:00.972672Z"}{"index":2,"description":"Take user input from terminal","language":"rust","code":"use clipboard::{ClipboardContext, ClipboardProvider};\n/// Set clipboard contents to text\npub fn copy_to_clipboard(text: String) -> Result<(), Error> {\n    let mut ctx: ClipboardContext =\n        ClipboardProvider::new().map_err(|_| LostTheWay::ClipboardError)?;\n    ctx.set_contents(text)\n        .map_err(|_| LostTheWay::ClipboardError)?;\n    Ok(())\n}","extension":".rs","tags":["cli","dialoguer"],"date":"2020-05-14T07:38:53.725404Z","updated":"2020-07-12T17:39:28.830877Z"}{"index":3,"description":"Take user input from external editor","language":"rust","code":"use dialoguer::Editor;\n/// Gets input from external editor, optionally displays default text in editor\npub fn external_editor_input(default: Option<&str>, extension: &str) -> Result<String, Error> {\n    match Editor::new()\n        .extension(extension)\n        .edit(default.unwrap_or(\"\"))?\n    {\n        Some(input) => Ok(input),\n        None => Err(LostTheWay::EditorError.into()),\n    }\n}","extension":".rs","tags":["cli","dialoguer"],"date":"2020-05-14T07:38:53.725421Z","updated":"2020-07-12T17:39:28.830890Z"}{"index":4,"description":"Parse dates from English date expressions","language":"rust","code":"use chrono::{Date, Utc};\nuse chrono_english::{parse_date_string, Dialect};\n/// Makes a date from a string, can be colloquial like \"next Friday\"\npub fn parse_date(date_string: &str) -> Result<Date<Utc>, Error> {\n    if date_string.to_ascii_lowercase() == \"today\" {\n        Ok(Utc::now().date())\n    } else {\n        Ok(parse_date_string(date_string, Utc::now(), Dialect::Uk)?.date())\n    }\n}","extension":".rs","tags":["date","chrono-english"],"date":"2020-05-14T07:38:53.725438Z","updated":"2020-07-12T17:39:28.830907Z"}{"index":5,"description":"Plot an ROC curve","language":"python","code":"from matplotlib import cm\ncmap_colors = cm.tab20.colors\n\nplt.figure(figsize=(20, 10))\nlabels = [\"l1\", \"l2\", \"l3\"]\nmapping = dict(zip(labels, range(len(labels))))\ngroups = [mapping[x] for x in data_labels]\n\nfor i in range(len(labels)):\n    indices = np.where(np.array(groups) == i)[0]\n    plt.scatter(data[indices, 0], data[indices, 1], label=labels[i], color=cmap_colors[i])\nplt.legend()","extension":".py","tags":["plotting","matplotlib"],"date":"2020-07-14T00:00:00Z","updated":"2020-07-14T21:03:21.662302Z"}{"index":6,"description":"Hello world","language":"kotlin","code":"fun main(args: Array<String>) {\n    println(\"Hello World!\")\n}","extension":".kt","tags":["test"],"date":"2020-07-15T08:32:50.780421Z","updated":"2020-07-15T08:33:07.372478Z"}{"index":7,"description":"Copy some text to clipboard","language":"rust","code":"use clipboard::{ClipboardContext, ClipboardProvider};\n/// Set clipboard contents to text\npub fn copy_to_clipboard(text: String) -> Result<(), Error> {\n    let mut ctx: ClipboardContext =\n        ClipboardProvider::new().map_err(|_| LostTheWay::ClipboardError)?;\n    ctx.set_contents(text)\n        .map_err(|_| LostTheWay::ClipboardError)?;\n    Ok(())\n}","extension":".rs","tags":["cli","clipboard"],"date":"2020-05-14T07:38:53.725452Z","updated":"2020-07-12T17:39:28.830917Z"}{"index":8,"description":"Groups identifiers according to lineage","language":"python","code":"import requests\ndef get_uniprot_sequence(uniprot_id: str) -> str:\n    mapping_url = f\"http://www.uniprot.org/uniprot/{uniprot_id}.fasta\"\n    response = requests.get(mapping_url).text\n    if \"html\" not in response.strip():\n        return ''.join(response.split('\\n')[1:])\n    else:\n        mapping_url = f\"http://www.uniprot.org/uniparc/{uniprot_id}.fasta\"\n        response = requests.get(mapping_url).text\n        if \"html\" not in response.strip():\n            return ''.join(response.split('\\n')[1:])\n        else:\n            return None","extension":".py","tags":["bioinformatics"],"date":"2020-05-14T07:38:53.726501Z","updated":"2020-07-12T17:39:28.830965Z"}{"index":9,"description":"Scatter plot with grouped and labelled data","language":"python","code":"from matplotlib import cm\ncmap_colors = cm.tab20.colors\n\nplt.figure(figsize=(20, 10))\nlabels = [\"l1\", \"l2\", \"l3\"]\nmapping = dict(zip(labels, range(len(labels))))\ngroups = [mapping[x] for x in data_labels]\n\nfor i in range(len(labels)):\n    indices = np.where(np.array(groups) == i)[0]\n    plt.scatter(data[indices, 0], data[indices, 1], label=labels[i], color=cmap_colors[i])\nplt.legend()","extension":".py","tags":["plotting","matplotlib"],"date":"2020-05-14T07:38:53.726522Z","updated":"2020-07-12T17:39:28.830772Z"}