annotate Python/Heatmap.py @ 15:dbde253606c5 draft default tip

Uploaded
author dereeper
date Wed, 11 Dec 2024 08:25:06 +0000
parents e42d30da7a74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
1 import numpy as np
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
2 import pandas as pd
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
3 import seaborn as sns
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
4 import plotly.express as px
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
5 import xarray as xr
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
6
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
7 import sys, getopt
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
8
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
9 np.bool = np.bool_
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
10
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
11
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
12 def main(argv):
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
13 inputfile = ''
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
14 outputfile = ''
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
15 try:
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
16 opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
17 except getopt.GetoptError:
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
18 print ('Heatmap.py -i <inputfile> -o <outputfile>')
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
19 sys.exit(2)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
20 for opt, arg in opts:
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
21 if opt == '-h':
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
22 print ('Heatmap.py -i <inputfile> -o <outputfile>')
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
23 sys.exit()
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
24 elif opt in ("-i", "--ifile"):
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
25 inputfile = arg
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
26 elif opt in ("-o", "--ofile"):
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
27 outputfile = arg
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
28 print ('Input file is "', inputfile)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
29 print ('Output file is "', outputfile)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
30
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
31 data = pd.read_csv(inputfile, sep='\t')
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
32
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
33 #data.drop(columns=data.columns[0], axis=1, inplace=True)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
34
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
35 data2 = pd.read_csv(inputfile, sep='\t', header=None)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
36 data2.drop(index=data2.index[0], axis=0, inplace=True)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
37 data3 = data2[data2.columns[1:]]
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
38 data3_transposed = data3.T
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
39
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
40 myList = list(data.columns)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
41 del myList[0]
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
42
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
43 myClusters = list(data['ClutserID'])
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
44
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
45 fig = px.imshow(data3_transposed,
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
46 x=myClusters,
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
47 y=myList,
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
48 labels=dict(y="Strains", x="Clusters"),
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
49 height = 900,width = 900,
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
50 color_continuous_scale=["lightgrey", "red","green"]
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
51 )
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
52
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
53 fig.update_traces(hovertemplate="<br>".join(["Cluster: %{x}","Strain: %{y}"]))
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
54 fig.update_coloraxes(showscale=False)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
55 fig.update_layout(xaxis_scaleanchor="x")
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
56 fig.write_html(outputfile)
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
57
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
58 if __name__ == "__main__":
e42d30da7a74 Uploaded
dereeper
parents:
diff changeset
59 main(sys.argv[1:])