-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_explore.py
184 lines (159 loc) · 6.33 KB
/
feature_explore.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
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
import os
import time
import pickle
import argparse
import requests
import pandas as pd
from tqdm import tqdm
from playsound import playsound
import concurrent.futures as cf
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Protein_Family:
def __init__(self, ID):
self.ID = ID
self.in_DNA_diff = False
self.in_RNA_diff = False
self.in_DNA_filter = False
self.in_RNA_filter = False
self.in_all = False
self.UniRef_URL = None
self.UniRef_result = None
def __repr__(self):
rep = (
f'{self.ID}\nDNA Δ: {self.in_DNA_diff} DNA %: {self.in_DNA_filter} '
f'\nRNA Δ: {self.in_RNA_diff} RNA %: {self.in_RNA_filter} \nIn all sets: '
f'{self.in_all}\nURL: {self.UniRef_URL}\n'
f'{self.UniRef_result}\n'
)
return rep
def __eq__(self, other):
if self.ID == other.ID: return True
return False
# end Protein Family class -------------------------------------------------------------------------------------------------------------------------------------------------
#
def build_sets(data_path, destination):
# find all dataframes in directory
feature_sets = os.listdir(data_path)
data = {}
combined_features = set()
# create dictionary of filename: feature list
for s in feature_sets:
# import data file
with open(data_path + s, 'rb') as f:
data[s] = pickle.load(f)
# form set of all protein families
for d in data:
for f in data[d]:
combined_features.add(f)
combined_features.remove('diagnosis')
# create objects holding
prot_families = []
for family in combined_features:
fam_obj = Protein_Family(family)
if fam_obj.ID in data['DNA_diff_feats.pkl']: fam_obj.in_DNA_diff = True
if fam_obj.ID in data['DNA_filter_feats.pkl']: fam_obj.in_DNA_filter = True
if fam_obj.ID in data['RNA_diff_feats.pkl']: fam_obj.in_RNA_diff = True
if fam_obj.ID in data['RNA_filter_feats.pkl']: fam_obj.in_RNA_filter = True
if fam_obj.in_DNA_diff and fam_obj.in_DNA_filter and fam_obj.in_RNA_diff and fam_obj.in_RNA_diff and fam_obj.in_RNA_filter:
fam_obj.in_all = True
prot_families.append(fam_obj)
pickle.dump(data, open(destination + 'features.pkl', 'wb'))
pickle.dump(prot_families, open(destination + 'protein_families.pkl', 'wb'))
# end build_sets() --------------------------------------------------------------------------------------------------------------------------------------
#
def get_urls(fam):#, destination):
cluster = fam.ID.split('_')[1] # extract cluster name
if cluster[:3] == 'UPI':
url = f'https://www.uniprot.org/uniparc/{cluster}.tab'
else:
url = f'https://www.uniprot.org/uniprot/?query=accession:{cluster}&format=tab'
if fam.UniRef_URL is None:
try:
res = requests.head(url)
# save URL if it works
if res.status_code == 200:
fam.UniRef_URL = url
except:
print('Something went wrong')
# end get_urls() ------------------------------------------------------------------------------------------------------------------------------------------
#
def download_data(fam):
if fam.UniRef_result is None:
# try to download if not done yet
try:
res = requests.get(fam.UniRef_URL)
rows = res.text.split('\n')
tab = [row.split('\t') for row in rows]
df = pd.DataFrame(tab[1:],columns=tab[0])
# add to family object
fam.UniRef_result = df
except:
print('Something went wrong')
# end download_data() -------------------------------------------------------------------------------------------------------------------------------------
# script args
parser = argparse.ArgumentParser(description='Test')
parser.add_argument('-o', '--output', type=str, required=True, help='Output Destination')
parser.add_argument('-d', '--data', type=str, required=True, help='Import dataset path')
parser.add_argument('-save', '--save', type=str, required=False, help='pickle output file')
parser.add_argument('-c', '--columns', type=str, required=False, help='Extract dataset column names')
parser.add_argument('-set', '--sets', type=str, required=False, help='Organize protein falmilies')
parser.add_argument('-url', '--url', type=str, required=False, help='find and store UniRef urls for each family')
parser.add_argument('-dl', '--download', type=str, required=False, help='download UniProt data based on found url')
args = parser.parse_args()
data_path = args.data
destination = args.output
data = None
# import data file
with open(data_path, 'rb') as f:
data = pickle.load(f)
# data = data[:5]
# extract features from a dataframe
if args.columns:
features = data.columns
pickle.dump(features, open(destination, 'wb'))
if args.sets:
build_sets(data_path, destination)
if args.url:
with cf.ThreadPoolExecutor() as executor:
list(tqdm(executor.map(get_urls, data), total = len(data)))
if args.download:
with cf.ThreadPoolExecutor() as executor:
list(tqdm(executor.map(download_data, data), total = len(data)))
# yes, no = 0,0
shapes = set()
ct = 0
s27 = 0
for fam in data:
cluster = fam.ID.split('_')[1] # extract cluster name
if cluster[:3] == 'UPI':
s = fam.UniRef_result.shape
if s == (2,7):
s27 += 1
shapes.add(s)
ct += 1
print(ct,s27)
print(shapes)
# shapes.add(fam.UniRef_result.shape)
# # print(yes,no)
# for shape in shapes:
# for fam in data:
# if fam.UniRef_result.shape == shape:
# print(shape)
# # print(fam)
# break
# tests = ['UPI0001A25914', 'UPI0003ED083A','UPI0001631492']
# bad = 0
# for fam in data:
# cluster = fam.ID.split('_')[1] # extract cluster name
# if cluster in tests:
# print(fam.ID, True)
# if fam.UniRef_result.shape == (0,1):
# bad += 1
# # print(fam.ID, fam.UniRef_URL)
# fam.UniRef_result = None
# fam.UniRef_URL = None
# print(fam.ID)
# print(bad)
if args.save:
pickle.dump(data, open(destination + 'fam_urls.pkl', 'wb'))
playsound('ping.wav')