This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
l19test.py
130 lines (123 loc) · 3.54 KB
/
l19test.py
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
""" example of pcoa based biplot.
here I'm considering an abundance matrix of species by sample,
analagous to samples by OTUs.
Outline:
1. get (sample x sample) dist matrix from abundance matrix
2. do pcoa on that distance matrix to get sample coordinates
3. use those sample coords to generate species coordinates
sample_coords and sp_coords format:
row = sample or species, column 0 = x coord, col 1 = y coord ...
use sample_coords[:,0:2] to get an (n by 2) matrix
"""
from __future__ import division
from species_coords import species_coords
from numpy import array
#import matplotlib.pyplot as plt
import cogent.cluster.metric_scaling as pcoa
from cogent.maths.distance_transform import binary_dist_chisq,\
binary_dist_chord,\
binary_dist_euclidean,\
binary_dist_hamming,\
binary_dist_jaccard,\
binary_dist_lennon,\
binary_dist_ochiai,\
binary_dist_pearson,\
binary_dist_sorensen_dice,\
dist_bray_curtis,\
dist_canberra,\
dist_chisq,\
dist_chord,\
dist_euclidean,\
dist_gower,\
dist_hellinger,\
dist_kulczynski,\
dist_manhattan,\
dist_morisita_horn,\
dist_pearson,\
dist_soergel,\
dist_spearman_approx,\
dist_specprof
import sys
print("1")
o = open('data.txt', 'r');
l = o.read()
o.close()
ptmtx = array(eval(l),'float')
ptmtx = ptmtx.transpose()
print("1")
#get distance metric
dist_metric = sys.argv[1]
dist_functions = {
"Bray-Curtis": dist_bray_curtis,
"Canberra": dist_canberra,
"Chi-squared": dist_chisq,
"Chord": dist_chord,
"Euclidean": dist_euclidean,
"Gower": dist_gower,
"Hellinger": dist_hellinger,
"Kulczynski": dist_kulczynski,
"Manhattan": dist_manhattan,
"Morisita-Horn": dist_morisita_horn,
"Pearson": dist_pearson,
"Soergel": dist_soergel,
"Spearman-Approx": dist_spearman_approx,
"Species-Profile": dist_specprof,
"Binary-Chi-Squared": binary_dist_chisq,
"Binary-Chord": binary_dist_chord,
"Binary-Euclidean": binary_dist_euclidean,
"Binary-Hamming": binary_dist_hamming,
"Binary-Jaccard": binary_dist_jaccard,
"Binary-Lennon": binary_dist_lennon,
"Binary-Ochiai": binary_dist_ochiai,
"Binary-Pearson": binary_dist_pearson,
"Binary-Sorensen-Dice": binary_dist_sorensen_dice
}
print("1")
if dist_metric == "Custom...":
#get custom distance matrix
filename = sys.argv[2]
o = open(filename, 'r');
distance_matrix = o.read().split('\n')
#remove comment lines
for i in range(len(distance_matrix)-1,-1,-1):
if distance_matrix[i][0] == '#':
del distance_matrix[i]
#split each line by tabs
distance_matrix = [i.split('\t') for i in distance_matrix]
#convert each element to a number
distance_matrix = array([[float(i) for i in j] for j in distance_matrix])
print distance_matrix
else:
#create distance matrix
distance_matrix = dist_functions[dist_metric](ptmtx)
o = open("distmtx.txt", 'w')
o.write(distance_matrix)
o.close();
print("1")
aa = pcoa.principal_coordinates_analysis(distance_matrix)
sample_coords = aa[0].transpose()
sp_coords = species_coords(aa[0], ptmtx, dims=len(sample_coords[0])) * 3
print("1")
evals = aa[1]/sum(aa[1])
#scale axes by eigenvalues
sp_coords = sp_coords*array([list(evals)]*len(sp_coords));
sample_coords = sample_coords*array([list(evals)]*len(sample_coords))
o = open('sample_coords.txt', 'w')
for i in sample_coords:
for j in i:
o.write(str(j) + '\t')
o.write('\n')
o.close()
print("1")
o = open('sp_coords.txt', 'w')
for i in sp_coords:
for j in i:
o.write(str(j) + '\t')
o.write('\n')
o.close()
#output eigenvalues
o = open('evals.txt', 'w')
for i in aa[1]:
o.write(str(i/sum(aa[1])) + '\t')
o.close()
print("1")