-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusterize.py
More file actions
147 lines (120 loc) · 4.57 KB
/
Copy pathclusterize.py
File metadata and controls
147 lines (120 loc) · 4.57 KB
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
from __future__ import print_function
import argparse, sys
from rdkit import DataStructs
from rdkit.DataStructs.cDataStructs import SparseBitVect
from Bio.Phylo.TreeConstruction import _DistanceMatrix
from Bio.Phylo.TreeConstruction import DistanceTreeConstructor
from Bio import Phylo
from Bio.Phylo import draw
parser = argparse.ArgumentParser(prog='Clusterize.py', usage='%(prog)s [options]', description='description',
epilog="\xa9 Avktex 2016")
parser.add_argument('input', type=str, help='Input peptides.')
parser.add_argument('-o', '--output', default='out_tree.newick', type=str, help='Output newick tree.')
parser.add_argument('-t', '--output_ascii', default='out_tree_ascii.txt', type=str, help='Output newick tree.')
parser.add_argument('-a', '--annotation', type=str, help='Annotation')
args = parser.parse_args()
def get_similarity_from_bit_vectors(bit_vectors):
simil = []
count = 0
n = 1
for mol1 in range(len(bit_vectors)):
simil.append([1-x for x in DataStructs.BulkTanimotoSimilarity(bit_vectors[mol1], bit_vectors[:mol1+1])])
if count >= 100:
#print(n * 100)
n += 1
count = 0
count += 1
return simil
def construct_distance_matrix(names, bit_vectors):
return _DistanceMatrix(names, get_similarity_from_bit_vectors(bit_vectors))
def distance_matrix_to_tree(distance_matrix, method = "upgma"):
constructor = DistanceTreeConstructor()
if method == "upgma":
return constructor.upgma(distance_matrix)
else:
return constructor.nj(distance_matrix)
def output_asci_tree(file_name, tree):
with open(file_name, 'w') as handle:
Phylo.draw_ascii(tree, file=handle)
def output_tree(file_name, tree):
Phylo.write(tree, file_name, 'newick')
def read_patients_input(input_file):
handle = open(input_file)
header = handle.next().strip().split('\t')[1:]
if 'Amount' in header:
header.remove('Amount')
#print(header)
result = dict()
for patient in header:
result[patient] = dict()
for line in handle:
items = line.strip().split()
peptide = items[0]
for index, patient in enumerate(header):
result[patient][peptide] = int(items[index + 1])
handle.close()
return result
def patient_params_to_vectors(patients_info_dict):
result = dict()
for patient in patients_info_dict:
vector = SparseBitVect(len(patients_info_dict[patient]))
index = 0
for peptide_name in patients_info_dict[patient]:
vector[index] = patients_info_dict[patient][peptide_name] > 0
index += 1
result[patient] = vector
return result
class AnnotationBadFormat(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return 'AnnotationBadFormat ' + self.message
class DoubleAnotation(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return 'DoubleAnotation ' + self.message
def get_annotation(file_name):
try:
with open(file_name) as handle:
header = handle.next().strip().split('\t')
if 'ID' not in header:
raise AnnotationBadFormat('ID not found')
annotation = dict()
for line in handle:
entry = dict(zip(header, [x.strip() for x in line.split('\t')]))
if len(entry['ID'].strip()) > 0:
if entry['ID'] in annotation:
#print(entry['ID'], annotation[entry['ID']])
raise DoubleAnotation(entry['ID'])
annotation[entry['ID']] = entry
return annotation
except Exception as e:
print('There was error with parsing annotation ' + str(e))
return None
def compose_annotation_id(sample):
return sample['Diagnosis'] + ':' + sample['Diagnosis_add'] + ':' + sample['Folder'] + ':' + sample['FIO'] + ':' + sample['Gender'] + ':' + sample['Age'] + ':[' + sample['ID'] + ']'
patients_info = read_patients_input(args.input)
samples_peptides_vectors_dict = patient_params_to_vectors(patients_info)
# for patient in samples_peptides_vectors_dict:
# print(samples_peptides_vectors_dict[patient].ToBitString())
annotation = get_annotation(args.annotation)
# for item in annotation:
# print(compose_annotation_id(annotation[item]))
sample_names = []
sample_peptide_vectors = []
for sample in samples_peptides_vectors_dict:
if annotation:
if sample in annotation:
sample_names.append(compose_annotation_id(annotation[sample]))
else:
print('There is no sample id ' + sample + ' in annotation')
sample_names.append(sample)
else:
sample_names.append(sample)
sample_peptide_vectors.append(samples_peptides_vectors_dict[sample])
# print(len(sample_names), len(sample_peptide_vectors))
distance_matrix = construct_distance_matrix(sample_names, sample_peptide_vectors)
tree = distance_matrix_to_tree(distance_matrix)
output_tree(args.output, tree)
output_asci_tree(args.output_ascii, tree)