Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,54 +7,65 @@ from io import BytesIO
|
|
7 |
|
8 |
st.set_page_config(page_title="Protein Repeat Comparator", layout="centered")
|
9 |
st.title("🧬 Protein Repeat Comparator")
|
10 |
-
st.write("Upload two Excel files
|
11 |
|
12 |
-
# File upload
|
13 |
uploaded_file1 = st.file_uploader("Upload First Excel File", type=["xlsx"])
|
14 |
uploaded_file2 = st.file_uploader("Upload Second Excel File", type=["xlsx"])
|
15 |
|
16 |
if uploaded_file1 and uploaded_file2:
|
17 |
try:
|
18 |
-
# Read both Excel files
|
19 |
-
df1 = pd.read_excel(uploaded_file1)
|
20 |
-
df2 = pd.read_excel(uploaded_file2)
|
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 |
st.download_button(
|
52 |
-
label="📥 Download
|
53 |
-
data=
|
54 |
-
file_name="
|
55 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
56 |
)
|
57 |
|
58 |
except Exception as e:
|
59 |
st.error(f"⚠️ Error: {e}")
|
60 |
-
|
|
|
7 |
|
8 |
st.set_page_config(page_title="Protein Repeat Comparator", layout="centered")
|
9 |
st.title("🧬 Protein Repeat Comparator")
|
10 |
+
st.write("Upload two Excel files. Only changed repeat frequencies will be shown in the result.")
|
11 |
|
|
|
12 |
uploaded_file1 = st.file_uploader("Upload First Excel File", type=["xlsx"])
|
13 |
uploaded_file2 = st.file_uploader("Upload Second Excel File", type=["xlsx"])
|
14 |
|
15 |
if uploaded_file1 and uploaded_file2:
|
16 |
try:
|
17 |
+
# Read both Excel files, assuming header is in second row
|
18 |
+
df1 = pd.read_excel(uploaded_file1, header=1)
|
19 |
+
df2 = pd.read_excel(uploaded_file2, header=1)
|
20 |
+
|
21 |
+
# Column names
|
22 |
+
id_col = "Entry ID"
|
23 |
+
name_col = "Protein Name"
|
24 |
+
repeat_cols = [col for col in df1.columns if col not in [id_col, name_col]]
|
25 |
+
|
26 |
+
records = []
|
27 |
+
|
28 |
+
for _, row1 in df1.iterrows():
|
29 |
+
entry_id = row1[id_col]
|
30 |
+
protein_name = row1[name_col]
|
31 |
+
|
32 |
+
# Match protein in second file
|
33 |
+
match = df2[(df2[id_col] == entry_id) & (df2[name_col] == protein_name)]
|
34 |
+
if match.empty:
|
35 |
+
continue
|
36 |
+
row2 = match.iloc[0]
|
37 |
+
|
38 |
+
for repeat in repeat_cols:
|
39 |
+
freq1 = row1[repeat]
|
40 |
+
freq2 = row2[repeat]
|
41 |
+
if freq1 != freq2:
|
42 |
+
diff = abs(freq1 - freq2)
|
43 |
+
records.append({
|
44 |
+
"Entry ID": entry_id,
|
45 |
+
"Protein Name": protein_name,
|
46 |
+
"Repeat": repeat,
|
47 |
+
"Frequency File 1": freq1,
|
48 |
+
"Frequency File 2": freq2,
|
49 |
+
"Difference": diff
|
50 |
+
})
|
51 |
+
|
52 |
+
result_df = pd.DataFrame(records)
|
53 |
+
result_df = result_df.sort_values(by="Difference", ascending=False)
|
54 |
+
|
55 |
+
# In-memory Excel
|
56 |
+
output = BytesIO()
|
57 |
+
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
58 |
+
result_df.to_excel(writer, index=False)
|
59 |
+
output.seek(0)
|
60 |
+
|
61 |
+
st.success("✅ Comparison complete! Showing only changed repeats.")
|
62 |
st.download_button(
|
63 |
+
label="📥 Download Changed Repeats Excel",
|
64 |
+
data=output,
|
65 |
+
file_name="changed_protein_repeats.xlsx",
|
66 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
67 |
)
|
68 |
|
69 |
except Exception as e:
|
70 |
st.error(f"⚠️ Error: {e}")
|
71 |
+
|