-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertSNP2Image.py
63 lines (47 loc) · 1.64 KB
/
ConvertSNP2Image.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
##Generate SNP images for each chromosome.
from pyDeepInsight import ImageTransformer
from pyDeepInsight.utils import Norm2Scaler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.manifold import TSNE
import pandas as pd
import numpy as np
import pyreadr
def SNP_img_generator(chrr="1", resolution=277):
# Get the list from the Rdata file
result = pyreadr.read_r('../../Geno/SNPchrs/SNPchr'+ chrr+ '.RData')
geno_chr = result["geno_chr"]
expr = geno_chr
y = expr.index
X = expr.values
X_train = X
# Converting into scaler style.
ln = Norm2Scaler()
X_train_norm = ln.fit_transform(X_train)
# X_test_norm = ln.transform(X_test)
# Create t-SNE object
distance_metric = 'cosine'
reducer = TSNE(
n_components=2,
metric=distance_metric,
init='random',
learning_rate='auto',
n_jobs=-1,
random_state=42
)
# Initialize image transformer.
pixel_size = (resolution,resolution)
it = ImageTransformer(
feature_extractor=reducer,
pixels=pixel_size)
# Train image transformer on training data and transform training
# and testing sets. Values should be between 0 and 1.
it.fit(X_train, plot=False)
X_train_img = it.transform(X_train_norm, empty_value=1)
# X_test_img = it.transform(X_test_norm, empty_value=1)
np.save(f'../../SNPimg/Chr{Chr}_tsne_{resolution}.npy', X_train_img)
print(f'Done with Chr {Chr}, Res {resolution}')
for Chr in range(1, 13):
Chr = str(Chr)
res = 277
SNP_img_generator(chrr=Chr, resolution=res)