-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbad_sql_results.txt
More file actions
217 lines (184 loc) · 6.72 KB
/
Copy pathbad_sql_results.txt
File metadata and controls
217 lines (184 loc) · 6.72 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
Given these 2 SQL tables
CREATE TABLE labels (
id serial4 NOT NULL,
"path" int4 NOT NULL,
labeling int4 NOT NULL,
"label" int4 NOT NULL,
"position" int4 NOT NULL,
"index" int4 NOT NULL,
CONSTRAINT labels_pkey PRIMARY KEY (id),
CONSTRAINT labels_path_f FOREIGN KEY ("path") REFERENCES public.reaction_paths(id)
);
CREATE TABLE reaction_paths (
id serial4 NOT NULL,
pathways int4 NOT NULL,
start_metabolite varchar NOT NULL,
end_metabolite varchar NOT NULL,
CONSTRAINT reaction_paths_id_key UNIQUE (id),
CONSTRAINT reaction_paths_pkey PRIMARY KEY (id),
);
I have this query:
select count(distinct pos.path), max(positions) as labeled_positons_in_gauge
from (
select max(l."path") as "path", array_agg(l."position" order by l.position asc) as positions from labels l
join reaction_paths rp ON l."path" = rp.id
inner join (
select l."path" , l.labeling, max(l."index") as idx from labels l
join reaction_paths rp on l."path" = rp.id
where l.metabolite = rp.end_metabolite and rp.start_metabolite = 'ALPHA-GLUCOSE' and rp.end_metabolite = 'L-LACTATE'
group by l."path", l.labeling
) as "ti" on l."path" = ti."path" and l.labeling = ti.labeling
where l."index" = ti.idx
group by l."path", l.labeling
having count(l."label") = 2
)as pos
group by pos.positions
Can you translate this into accurate and correct Python code using the SQLAlchemy 2.0 library?
From ChatGPT:
=================================================
from sqlalchemy import create_engine, select, func, and_, text, Integer, String, ForeignKey, Column, Table, MetaData, ARRAY
from sqlalchemy.orm import declarative_base, aliased
# Define database connection
engine = create_engine("your_database_url")
# Define metadata and models
Base = declarative_base()
class Labels(Base):
__tablename__ = "labels"
id = Column(Integer, primary_key=True)
path = Column(Integer, ForeignKey("reaction_paths.id"), nullable=False)
labeling = Column(Integer, nullable=False)
label = Column(Integer, nullable=False)
position = Column(Integer, nullable=False)
index = Column(Integer, nullable=False)
class ReactionPaths(Base):
__tablename__ = "reaction_paths"
id = Column(Integer, primary_key=True, unique=True)
pathways = Column(Integer, nullable=False)
start_metabolite = Column(String, nullable=False)
end_metabolite = Column(String, nullable=False)
# Create aliased tables for subqueries
LabelsAlias = aliased(Labels)
# Subquery "ti"
ti_subquery = (
select(
Labels.path,
Labels.labeling,
func.max(Labels.index).label("idx")
)
.join(ReactionPaths, Labels.path == ReactionPaths.id)
.where(
and_(
Labels.label == ReactionPaths.end_metabolite,
ReactionPaths.start_metabolite == "ALPHA-GLUCOSE",
ReactionPaths.end_metabolite == "L-LACTATE"
)
)
.group_by(Labels.path, Labels.labeling)
.subquery(name="ti")
)
# Subquery "positions"
positions_subquery = (
select(
func.max(Labels.path).label("path"),
func.array_agg(Labels.position).order_by(Labels.position).label("positions")
)
.join(ReactionPaths, Labels.path == ReactionPaths.id)
.join(ti_subquery, and_(
Labels.path == ti_subquery.c.path,
Labels.labeling == ti_subquery.c.labeling
))
.where(Labels.index == ti_subquery.c.idx)
.group_by(Labels.path, Labels.labeling)
.having(func.count(Labels.label) == 2)
.subquery(name="positions")
)
# Main query
main_query = (
select(
func.count(func.distinct(positions_subquery.c.path)).label("distinct_paths_count"),
func.max(positions_subquery.c.positions).label("labeled_positions_in_gauge")
)
.select_from(positions_subquery)
.group_by(positions_subquery.c.positions)
)
# Execute the query
with engine.connect() as connection:
result = connection.execute(main_query).fetchall()
print(result)
=====================================================
Notes
From Codium:
=====================================================
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
Base = declarative_base()
class Labels(Base):
__tablename__ = 'labels'
id = Column(Integer, primary_key=True)
path = Column(Integer, ForeignKey('reaction_paths.id'), nullable=False)
labeling = Column(Integer, nullable=False)
label = Column(Integer, nullable=False)
position = Column(Integer, nullable=False)
index = Column(Integer, nullable=False)
class ReactionPaths(Base):
__tablename__ = 'reaction_paths'
id = Column(Integer, primary_key=True)
pathways = Column(Integer, nullable=False)
start_metabolite = Column(String, nullable=False)
end_metabolite = Column(String, nullable=False)
labels = relationship("Labels")
# Assuming you have already created the engine
engine = create_engine('your_database_connection_string')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
from sqlalchemy import func
subquery = session.query(Labels.path, func.max(Labels.position).label('positions')).\
group_by(Labels.path, Labels.labeling).\
having(func.count(Labels.label) == 2).subquery()
query = session.query(func.count(subquery.c.path), func.max(subquery.c.positions).label('labeled_positions_in_gauge')).\
group_by(subquery.c.positions)
results = query.all()
for result in results:
print(result)
=====================================================
Correct code:
=====================================================
gauge_labels = (
select(Label.path, Label.labeling, func.max(Label.index).label("idx"))
.join(ReactionPath)
.where(
and_(
Label.metabolite == ReactionPath.end_metabolite,
ReactionPath.start_metabolite == tracer,
ReactionPath.end_metabolite == gauge,
)
)
.group_by(Label.path, Label.labeling)
).subquery()
possible_positions = (
select(
func.max(Label.path).label("path"),
func.array_agg(
aggregate_order_by(Label.position, Label.position.asc())
).label("positions"),
)
.join(ReactionPath)
.join(
gauge_labels,
and_(
Label.path == gauge_labels.c.path,
Label.labeling == gauge_labels.c.labeling,
),
isouter=False,
)
.where(and_(Label.index == gauge_labels.c.idx, Label.label.in_(labeling)))
.group_by(Label.path, Label.labeling)
.having(func.count(Label.label) == label_count)
).subquery()
query = select(
func.count(distinct(possible_positions.c.path)),
func.max(possible_positions.c.positions),
).group_by(possible_positions.c.positions)
return session.execute(query).all()