data = [False, False, False]
# Return any True in the list
any(data)
# Return any False in the list
not any(data)
# Same goes for all values True in the list
all(data)
Author / bioinfocore
Python dictionary use value to get key
With value to find the matching key:
# function to return key for any value
def get_key(my_dict, val):
for key, value in my_dict.items():
if val in value: # when your value is a list
# if val == value: # when your value is a string
return key
return "key doesn't exist"
Source:
https://www.geeksforgeeks.org/python-get-key-from-value-in-dictionary/
Pandas rearrange the order of only two columns
When you have a table with many columns but only want to rearrange or swap two or a few columns.
cols = list(df.columns)
a, b = cols.index('XXXX'), cols.index('YYYY')
cols[b], cols[a] = cols[a], cols[b]
df = df[cols]
Source:
https://stackoverflow.com/questions/53141240/pandas-how-to-swap-or-reorder-columns
Python pickle save/load dictionary into a file
Sometimes you want to save a dictionary into a reusable file, and reuse it later. This is specifically useful when you need to save a color scheme for a large category and want to keep it consistent across your whole project.
Output it as a file:
import pickle
colorDicOut = open('colorOut_dic.pkl', 'wb')
pickle.dump(colorDic, colorDicOut)
colorDicOut.close()
with open('colorOut_dic.pkl', 'wb') as colorDicOut:
pickle.dump(colorDic, colorDicOut)
Load it into a variable:
colorDicIn = open("colorOut_dic.pkl","rb")
colorDic = pickle.load(colorDicIn)
If
UnicodeDecodeError
comes up, then it is likely an issue of incompatibility between python2 and python3, please refer to this post for solutions.
Source:
https://stackoverflow.com/questions/11218477/how-can-i-use-pickle-to-save-a-dict
Pandas replace a value with another value or replace values not equal to a given value
Replace a specific value with another value in a dataframe:
df.replace(originalValue, newValue, inplace=True)
df = df.replace(originalValue, newValue)
Replace values not equal to a given value with another value:
df[df != originalValue] = newValue
Interrupt python process without exit python in cmd
In Mac: control+c
Liftover bigwig files
There are two ways to lift-over bigwig files from one genome build to another. One is using CrossMap
or step by step as below. (CrossMap
is almost the same to the break down steps)
CrossMap method: (taking from hg19 to hg38 as example)
pip install CrossMap
CrossMap.py bigwig hg19ToHg38.over.chain input.bw output.bw
genome liftover chain files can be downloaded here: http://hgdownload.cse.ucsc.edu/goldenpath/hg19/liftOver/ (change according to your needs)
Step by step method: (bw –> bedGraph –> liftover –> bw)
bigWigToBedGraph input.bw input.bedGraph
liftOver input.bedGraph hg19ToHg38.over.chain input_hg38.bedgraph unMapped
fetchChromSizes hg38 > hg38.chrom.sizes
LC_COLLATE=C sort -k1,1 -k2,2n input_hg38.bedgraph > input_hg38.sorted.bedgraph
bedGraphToBigWig input_hg38.sorted.bedgraph hg38.chrom.sizes output.bw
bigWigToBedGraph
,liftOver
,fetchChromSizes
,bedGraphToBigWig
are all UCSC utilities which can be installed from here: http://hgdownload.soe.ucsc.edu/downloads.html#utilities_downloads
Source:
http://hgdownload.soe.ucsc.edu/downloads.html#utilities_downloads
http://crossmap.sourceforge.net/#
Run Unix commands in python
To run Unix command in python:
import os
os.system('your unix code')
os.system('ls')
Source:
https://code.tutsplus.com/articles/how-to-run-unix-commands-in-your-python-program–cms-25926
Python Pandas remove rows with the same index
Remove rows with the same index, only keep one first or last of them.
df = df.loc[~df.index.duplicated(keep='first')]
df = df.loc[~df.index.duplicated(keep='last')]
Unix insert a line to the beginning of a file
Insert a line to the beginning of a file using sed:
sed '1 s/^/This is my first line\n/' inFile.txt > outFile.txt
Source:
https://linuxconfig.org/how-to-insert-line-to-the-beginning-of-file-on-linux