Skip to content

Instantly share code, notes, and snippets.

View fomightez's full-sized avatar

Wayne's Bioinformatics Code Portal fomightez

View GitHub Profile
@fomightez
fomightez / my_python_functions.py
Last active April 21, 2020 02:51
examples of my frequently used Python functions and slight variations
# generate output file name
def generate_output_file_name(id_of_seq_element,id_of_seq_scanned):
'''
Takes a file name as an argument and returns string for the name of the
output file. The generated name is based on a prefix that can be adjusted
under the section ' USER ADJUSTABLE VALUES ', plus the provided text in
@fomightez
fomightez / useful_BLAST_handling.py
Last active June 6, 2018 20:14
snippets for dealing with BLAST
# see https://gist.github.com/fomightez/ef57387b5d23106fabd4e02dab6819b4 for
# the main ones dealing with BLAST and dataframes, search `blast` to find
# see two Jupyter notebooks in https://github.com/fomightez/blast-binder
# SEE MOE FUNCTIONAL VERSION OF THIS FUNCTION AT https://github.com/fomightez/sequencework/tree/master/blast-utilities
def BLAST_to_df(results_file):
'''
BLAST results to Pandas dataframe
@fomightez
fomightez / useful_FASTA_handling.py
Last active July 3, 2024 21:19
snippets for dealing with FASTA
# This is not presently all encompassing as it was started well after my sequence work repo
# at https://github.com/fomightez/sequencework , where much of this related code is.
# For making FASTA files/entriees out of dataframes, see 'specific dataframe contents saved as formatted text file example'
# in my useful pandas snippets gist https://gist.github.com/fomightez/ef57387b5d23106fabd4e02dab6819b4
@fomightez
fomightez / run_every_eight_mins.py
Created March 28, 2018 17:33
do something every eight minutes with Python
import time
def executeSomething():
#code here
print ('.')
time.sleep(480) #60 seconds times 8 minutes
while True:
executeSomething()
@fomightez
fomightez / switch_statement_example.py
Last active June 13, 2019 00:01
Python to emulate switch/case statements
# I like the example at https://inventwithpython.com/blog/2019/06/05/pythonic-ways-to-use-dictionaries/ better than this one I had found earlier>
##
##
def dispatch_dict(operator, x, y):
'''
Python to emulate switch/case statements
based on https://books.google.com/books?id=C0VKDwAAQBAJ&pg=PT226&lpg=PT226&dq=%22dispatch_dict+(operator%22&source=bl&ots=Ja0bhOYv9A&sig=j7OZGsWpps97z8jHByquiYIuRBY&hl=en&sa=X&ved=2ahUKEwjW4siOl8fcAhWKTN8KHSa3ApYQ6AEwAnoECAMQAQ#v=onepage&q=%22dispatch_dict%20(operator%22&f=false
https://twitter.com/w_cazzola/status/1023917802627973126
https://gist.github.com/carlessanagustin/6d7584b963338426a3691d355fe28f55
@fomightez
fomightez / useful_notebook_snippets
Last active November 4, 2025 19:06
Useful snippets for Jupyter notebooks
# Use `%%capture` to hush 'noisy' stdout and stderr streams, but still combine with getting `%%time` after
%%capture out_stream
%%time
---rest of a cell that does something with LOTS of output--
#In cell after, put following to get time of completion from that:
#time it took to run cell above
for x in out_stream.stdout.split("\n")[-3:]:
print(x)
@fomightez
fomightez / Stv1p_Vph1p_muscle_alignment.clw
Last active August 2, 2019 20:17
Stv1p vs Vph1p MUSCLE alignment
CLUSTAL multiple sequence alignment by MUSCLE (3.8)
STV1 -MNQEEAIFRSADMTYVQLYIPLEVIREVTFLLGKMSVFMVMDLNKDLTAFQRGYVNQLR
VPH1 MAEKEEAIFRSAEMALVQFYIPQEISRDSAYTLGQLGLVQFRDLNSKVRAFQRTFVNEIR
::********:*: **:*** *: *: :: **::.:. . ***..: **** :**::*
STV1 RFDEVERMVGFLNEVVEKHAAETW-----KYILHIDDEGNDIAQPDMADLINTMEPLSLE
VPH1 RLDNVERQYRYFYSLLKKHDIKLYEGDTDKYL----DGSGELYVPPSGSVI---------
*:*:*** :: .:::** : : **: * ..:: * ..:*
@fomightez
fomightez / argparse_snippets.py
Last active October 30, 2022 07:11
useful argparse snippets
# from `get_seq_following_seq_from_FASTA.py`
if __name__ == "__main__" and '__file__' in globals():
""" This is executed when run from the command line """
# Code with just `if __name__ == "__main__":` alone will be run if pasted
# into a notebook. The addition of ` and '__file__' in globals()` is based
# on https://stackoverflow.com/a/22923872/8508004
# See also https://stackoverflow.com/a/22424821/8508004 for an option to
# provide arguments when prototyping a full script in the notebook.
###-----------------for parsing command line arguments-------------------###
import argparse
@fomightez
fomightez / diff_strings.py
Last active September 12, 2019 20:55 — forked from ines/diff_strings.py
Print colored visual diff in Python - from Ines Montani https://twitter.com/_inesmontani/status/1156904128666316800
import difflib
from wasabi import color
def diff_strings(a, b):
output = []
matcher = difflib.SequenceMatcher(None, a, b)
for opcode, a0, a1, b0, b1 in matcher.get_opcodes():
if opcode == "equal":
output.append(a[a0:a1])
elif opcode == "insert":
@fomightez
fomightez / How to remove a substring from each line using Python 3.ipynb
Created October 9, 2019 18:58
How to remove a substring from each line using python 3
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.