Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# _____________________________________________________________________________
# -----------------------------------------------------------------------------
# import needed (external) modules
# -----------------------------------------------------------------------------
import sys
import os
import pandas as pd
import numpy as np
import random as rd
from sklearn.decomposition import PCA
from sklearn import preprocessing
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from argparse import ArgumentParser, RawTextHelpFormatter
# _____________________________________________________________________________
# -----------------------------------------------------------------------------
# Main function
# -----------------------------------------------------------------------------
def main():
""" Main function """
__doc__ = "Perform PCA based on a table (rows are genes/transcripts, columns are samples)."
parser = ArgumentParser(
description=__doc__,
formatter_class=RawTextHelpFormatter
)
parser.add_argument(
"--tpm",
dest="tpm",
help="TPM table (tsv)",
required=True,
metavar="FILE"
)
parser.add_argument(
"--out",
dest="out",
help="Output directory",
required=True,
metavar="FILE"
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
default=False,
required=False,
help="Verbose"
)
# _________________________________________________________________________
# -------------------------------------------------------------------------
# get the arguments
# -------------------------------------------------------------------------
try:
options = parser.parse_args()
except(Exception):
parser.print_help()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
if options.verbose:
sys.stdout.write(f"Creating output directory: {options.out} {os.linesep}")
if not os.path.exists(options.out):
os.makedirs(options.out)
if options.verbose:
sys.stdout.write(f"Reading: {options.tpm} {os.linesep}")
df = pd.read_csv(options.tpm, header=0, sep="\t")
df.set_index(["Name"], inplace=True)
if options.verbose:
sys.stdout.write(f"Performing PCA {os.linesep}")
scaled_data = preprocessing.scale(df.T)
pca = PCA()
pca.fit(scaled_data)
pca_data = pca.transform(scaled_data)
per_var = np.round(pca.explained_variance_ratio_* 100, decimals=1)
labels = ['PC' + str(x) for x in range(1, len(per_var)+1)]
plt.figure()
plt.rcParams["figure.figsize"] = (20,20)
plt.bar(x=range(1,len(per_var)+1), height=per_var, tick_label=labels)
plt.ylabel('Percentage of Explained Variance')
plt.xlabel('Principal Component')
plt.title('Scree Plot')
plt.xticks(rotation='vertical')
plt.savefig(os.path.join(options.out, "scree_plot.pdf"))
plt.close()
pca_df = pd.DataFrame(pca_data, index=[*df], columns=labels)
if options.verbose:
sys.stdout.write(f"Generating plots in: {options.out} {os.linesep}")
# -------------------------------------------------------------------------
# PCA 1st and 2nd component
# -------------------------------------------------------------------------
plt.figure()
plt.rcParams["figure.figsize"] = (20,20)
plt.scatter(pca_df.PC1, pca_df.PC2)
plt.xlabel('PC1 - {0}%'.format(per_var[0]), fontsize=22)
plt.ylabel('PC2 - {0}%'.format(per_var[1]), fontsize=22)
plt.title('PCA components 1 & 2', fontsize=22)
for sample in pca_df.index:
plt.annotate(sample, (pca_df.PC1.loc[sample], pca_df.PC2.loc[sample]))
plt.savefig(os.path.join(options.out, "PCA_1_2.pdf"))
plt.close()
# -------------------------------------------------------------------------
# PCA 2nd and 3rd component
# -------------------------------------------------------------------------
plt.figure()
plt.rcParams["figure.figsize"] = (20,20)
plt.scatter(pca_df.PC2, pca_df.PC3)
plt.xlabel('PC2 - {0}%'.format(per_var[1]), fontsize=22)
plt.ylabel('PC3 - {0}%'.format(per_var[2]), fontsize=22)
plt.title('PCA components 2 & 3', fontsize=22)
for sample in pca_df.index:
plt.annotate(sample, (pca_df.PC2.loc[sample], pca_df.PC3.loc[sample]))
plt.savefig(os.path.join(options.out, "PCA_2_3.pdf"))
plt.close()
# -------------------------------------------------------------------------
# PCA 1st and 3rd component
# -------------------------------------------------------------------------
plt.figure()
plt.rcParams["figure.figsize"] = (20,20)
plt.scatter(pca_df.PC1, pca_df.PC3)
plt.xlabel('PC1 - {0}%'.format(per_var[0]), fontsize=22)
plt.ylabel('PC3 - {0}%'.format(per_var[2]), fontsize=22)
plt.title('PCA components 1 & 3', fontsize=22)
for sample in pca_df.index:
plt.annotate(sample, (pca_df.PC1.loc[sample], pca_df.PC3.loc[sample]))
plt.savefig(os.path.join(options.out, "PCA_1_3.pdf"))
plt.close()
# -------------------------------------------------------------------------
# PCA 3D
# -------------------------------------------------------------------------
labels = []
pc1 = []
pc2 = []
pc3 = []
for i, row in pca_df.iterrows():
labels.append(i)
pc1.append(row["PC1"])
pc2.append(row["PC2"])
pc3.append(row["PC3"])
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(pc1, pc2, pc3)
ax.set_xlabel('PC1 - {0}%'.format(per_var[0]))
ax.set_ylabel('PC2 - {0}%'.format(per_var[1]))
ax.set_zlabel('PC3 - {0}%'.format(per_var[2]))
for label, x, y, z in zip(labels, pc1, pc2, pc3):
ax.text(x, y, z, label)
plt.savefig(os.path.join(options.out, "PCA_3D.pdf"))
# -------------------------------------------------------------------------
# Write loading scores
# -------------------------------------------------------------------------
if options.verbose:
sys.stdout.write(f"Writing loading scores in: {options.out} {os.linesep}")
loading_scores = pd.Series(pca.components_[0], index=list(df.index))
sorted_loading_scores = loading_scores.abs().sort_values(ascending=False)
sorted_loading_scores.to_csv(os.path.join(options.out, "loading_scores.tsv"), header=False, sep="\t")
if options.verbose:
sys.stdout.write(f"Done {os.linesep}")
# _____________________________________________________________________________
# -----------------------------------------------------------------------------
# Call the Main function and catch Keyboard interrups
# -----------------------------------------------------------------------------
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.stderr.write("User interrupt!" + os.linesep)
sys.exit(0)