-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPreprocessing.py
More file actions
217 lines (177 loc) · 7.09 KB
/
DataPreprocessing.py
File metadata and controls
217 lines (177 loc) · 7.09 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
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
211
212
213
214
215
216
217
import numpy as np
from Neo4jDriver import Neo4jDriver
from neo4j import GraphDatabase
import csv
import psycopg2
# Sample possible values in the fixed order
TAGS = {
"derived_biosample_lc11mtype": [
"INTACT nuclei isolation",
"TAPIN nuclei isolation",
"biotic treatment",
"cell cycle synchronized cells",
"cell isolation",
"chemical cell dissociation",
"chemical treatment",
"culture supernatant",
"enzymatic cell dissociation",
"gene perturbation",
"light-dark cycle",
"multi-individual sample",
"physical treatment",
"stably transfected cell line",
"starvation",
"targeted cell labeling",
"tissue dissection",
"transiently transfected cell line"
],
"derived_biosample_lc2btype": [
"immortalized cell line",
"isolated cells",
"isolated nuclei",
"tissue",
"whole organism"
],
"derived_stage": [
"adult stage",
"blastoderm stage",
"dorsal closure stage",
"early extended germ band stage",
"gastrula stage",
"larval stage",
"late embryonic stage",
"late extended germ band stage",
"oogenesis",
"pharate adult stage",
"pre-blastoderm stage",
"prepupal stage",
"pupal stage"
],
"lc2btype": [
"CAGE-Seq", "ChIP-Seq", "ChIP-chip", "DNase-Seq", "DamID-chip", "FAIRE-Seq",
"RAMPAGE-Seq", "RIP-Seq", "RNA expression microarray", "RNA tiling array",
"RNA-Seq", "RNA-protein interaction", "RNA-seq profile", "RNAi construct collection",
"TSS identification", "aberration stock collection",
"affinity purification and mass spectrometry", "allele collection", "analysis",
"binding site identification", "cDNA library", "cell clustering analysis",
"cell type-specific gene expression profile", "comparative genomic hybridization by array",
"construct collection", "dsRNA amplicon collection", "editing site identification",
"exon junction identification", "expression cluster", "expression clustering",
"fly strain collection", "gene expression profile", "genome", "genome binding",
"genome variation", "genomic clone collection", "immortalized cell line",
"interactome", "isolated cells", "isolated nuclei", "microarray library",
"poly(A) site identification", "protein mass spectrometry", "protein-protein interaction",
"proteome", "reagent", "short RNA-Seq", "single-cell RNA-Seq",
"single-nucleus RNA-Seq", "tissue", "transcriptional cell cluster",
"transcriptome", "transgenic insertion collection", "umbrella project", "whole organism"
]
}
TOTAL_VALUES = 91
def calculate_pearson_correlation(primary_gene_expression, other_gene_expression):
return np.corrcoef(primary_gene_expression, other_gene_expression)[0, 1]
# def construct_coexpression_network(primary_gene_array, random_sample_array):
# coexpression_network = {}
#
# for gene_value in :
# correlation = calculate_pearson_correlation(primary_gene_value, gene_value)
# coexpression_network[gene_value] = correlation
#
# return coexpression_network
def get_metadata_from_postgresql(dataset):
# Set up the connection
conn = psycopg2.connect(
dbname="FB2023_05",
user="20724926",
password=input(),
host="lazebnik",
port="5433"
)
# Create a cursor to execute SQL commands
cur = conn.cursor()
# Formulate the SQL query using the dataset
query = f"""
SELECT type.name AS type, c.name AS value
FROM library_cvterm AS lc
JOIN cvterm AS c ON lc.cvterm_id = c.cvterm_id
JOIN cv ON cv.cv_id = c.cv_id
JOIN library_cvtermprop AS lcp ON lc.library_cvterm_id = lcp.library_cvterm_id
JOIN cvterm AS type ON lcp.type_id = type.cvterm_id
WHERE type.name IN ('derived_stage', 'derived_biosample_lc2btype', 'lc2btype', 'derived_biosample_lc11mtype')
AND library_id = (
SELECT library_id
FROM library_dbxref
WHERE dbxref_id = (
SELECT dbxref_id
FROM dbxref
WHERE accession IN ('{dataset}')
)
);
"""
cur.execute(query)
results = cur.fetchall()
# Close the connection
conn.close()
return results
def create_gene_dictionary_from_csv(csv_filename, primary_experiment_name, random_experiment_name):
# Read the genes from the CSV
with open(csv_filename, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip the header
master_genes = {row[0]: {primary_experiment_name: None, random_experiment_name: None} for row in reader}
# Query genes and values
neo4j_driver_temp = Neo4jDriver()
primary_experiment_data = neo4j_driver_temp.fetch_all_genes_with_property(primary_experiment_name)
random_experiment_data = neo4j_driver_temp.fetch_all_genes_with_property(random_experiment_name)
neo4j_driver_temp.close()
# Create dictionaries for faster lookups
primary_experiment_dict = dict(zip(primary_experiment_data[0], primary_experiment_data[1]))
random_experiment_dict = dict(zip(random_experiment_data[0], random_experiment_data[1]))
# Fill in the master_genes dictionary
for gene in master_genes.keys():
master_genes[gene][primary_experiment_name] = primary_experiment_dict.get(gene, None)
master_genes[gene][random_experiment_name] = random_experiment_dict.get(gene, None)
return master_genes
def one_hot_encode(tags_output):
# Initialize encoding with zeros
encoding = []
for tag_name, possible_values in TAGS.items():
tag_vector = [0] * len(possible_values)
value = tags_output.get(tag_name)
# Check if value is not in the list of possible values
if value and value not in possible_values:
raise ValueError(f"Unexpected value '{value}' for tag '{tag_name}'.")
if value:
index = possible_values.index(value)
tag_vector[index] = 1
encoding.extend(tag_vector)
if len(encoding) != TOTAL_VALUES:
raise ValueError(f"Incorrect encoding length. Expected {TOTAL_VALUES}, got {len(encoding)}.")
return encoding
# # Test
tags_output = {
"lc2btype": "RNA-seq profile",
"derived_biosample_lc2btype": "whole organism",
"derived_stage": "larval stage"
}
# print(one_hot_encode(tags_output))
neo4j_driver = Neo4jDriver()
neo4j_driver.write_fb_ids_to_csv('fb_ids.csv')
# Usage:
gene = neo4j_driver.get_random_fb_id()
print(gene)
sample1 = neo4j_driver.get_fblc_property_and_expression(gene, 'FBlc0000102')
sample2 = neo4j_driver.get_fblc_property_and_expression(gene, 'FBlc0003725')
print(sample1)
print(sample2)
metadata = get_metadata_from_postgresql('FBlc0000102')
print(metadata)
tags = {key: value for key, value in metadata}
# print(tags)
encoding = one_hot_encode(tags)
print(encoding)
sample1 = sample1[0]
sample2 = sample2[0]
dictionary = create_gene_dictionary_from_csv('fb_ids.csv', sample1, sample2)
# print(dictionary)
# print(all_genes_with_prop, all_genes_with_prop_exp)
neo4j_driver.close()