from itertools import compress
arr = np.array([x=='TRUE' for x in xList]) # Turn a string list to boolean
list(compress(yList, arr))
Source:
https://www.geeksforgeeks.org/python-itertools-compress/
from itertools import compress
arr = np.array([x=='TRUE' for x in xList]) # Turn a string list to boolean
list(compress(yList, arr))
Source:
https://www.geeksforgeeks.org/python-itertools-compress/
When the values are numeric and apply operators:
df.groupby(level=0).sum()
df.groupby(df.index).sum()
df[specifiedColumn].groupby(level=0).sum()
When the values are numeric and apply functions:
# Common usage, merging values
df[1].groupby(level=0).apply(lambda xList: ';'.join(list([x for x in xList])))
Source:
https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.core.groupby.GroupBy.apply.html
https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.GroupBy.sum.html
Merge multiple row or column values into one row or column:
df.loc[colSelected,].apply(lambda x: ';'.join(x.astype(str)), axis=0)
df.loc[,rowSelected].apply(lambda x: ';'.join(x.astype(str)), axis=1)
Fonts output into pdf as text, not shape, to be recognized in Illustrator:
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
Change fonts style for all, change the default:
import matplotlib as mpl
matplotlib.rcParams['font.family'] = 'Arial'
Change fonts style for labels, tick marks, and titles: (for each plot, not changing the default)
plt.xlabel('XXXX', fontsize=12, fontname='Arial')
plt.ylabel('XXXXX', fontsize=12, fontname='Arial')
plt.xticks(fontname='Arial')
plt.yticks(fontname='Arial')
plt.title('XXXXX', fontsize=14, fontname='Arial')
Change legend font style: (for each plot, not changing the default)
plt.legend(prop=matplotlib.font_manager.FontProperties(family='Arial', size=12, weight='bold', style='normal'))
Source:
https://jonathansoma.com/lede/data-studio/matplotlib/exporting-from-matplotlib-to-open-in-adobe-illustrator/
https://stackoverflow.com/questions/20753782/default-fonts-in-seaborn-statistical-data-visualization-in-ipython
https://stackoverflow.com/questions/47112522/matplotlib-how-to-set-legends-font-type
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html
yourVar = 'data or your variable'
%store yourVar
del yourVar # only deletes the variable in this notebook but not in store
In the second notebook:
%store -r yourVar # if you have a variable with the same name, it will rewrite it.
yourVar
To list and delete the stored variable:
%store #list variable in store
%store -d yourVar #delete the variable in store
Source:
https://stackoverflow.com/questions/35935670/share-variables-between-different-jupyter-notebooks
https://ipython.org/ipython-doc/rel-0.12/config/extensions/storemagic.html
If there are NaN in the output from computeMatrix, the generated heatmap is not sorted and a warning message stating Mean of empty slice
will show up.
To overcome this, those null values need to be replaced using 0 in the computeMatrix
step by --missingDataAsZero
tag.
computeMatrix scale-regions -S xxx.bw -R xxx.bed --missingDataAsZero -m xxx -b xxx -a xxx --numberOfProcessors xx -o xxx.gz
plotHeatmap -m xxx.gz -out xxx.png
Source:
https://groups.google.com/g/deeptools/c/B55e0kT_0Ec?pli=1
https://github.com/deeptools/deepTools/issues/490
list.remove('element')
Source:
https://www.programiz.com/python-programming/methods/list/remove
import os.path
# Check if a file exist, return True/False
os.path.exists("test.txt")
# Check if a directory exist, return True/False, for >= python3.4
import pathlib
pathlib.Path("test").exists()
Source:
https://www.guru99.com/python-check-if-file-exists.html
When we want to use Unix command in python we can directly use os.system()
to realize it. However, if we only want to return the output as a string, for example, return ls
file names into a string, we need to use subprocess.check_out
instead.
To note that, the output from subprocess.check_out()
is a bytes object instead of a string, thus we need to further decode
to transform into a string.
fileList = subprocess.check_output('ls someFolder/*', shell=True).decode('utf-8').strip().split('\n')
Source:
https://stackoverflow.com/questions/606191/convert-bytes-to-a-string
Check running servers:
jupyter notebook list
Stop running servers (both works):
jupyter notebook stop
kill $(pgrep jupyter)
Source:
https://github.com/jupyter/notebook/issues/1950
https://stackoverflow.com/questions/10162707/how-to-close-ipython-notebook-properly/32745046#32745046