Skip to content
Snippets Groups Projects
Commit ee445b70 authored by Bartosz Bartmanski's avatar Bartosz Bartmanski
Browse files

Improved __repr__ for Insert class. Changed Insert.get behaviour.

parent 7971cab6
No related branches found
Tags 0.3.4
No related merge requests found
......@@ -64,9 +64,12 @@ class Insert(object):
return (
"Insert("
f"seq_id={self.seq_id}, "
f"idx={self.idx}, "
f"start={self.start}, "
f"end={self.end}, "
f"hit_id={self.hit_id})"
f"hit_id={self.hit_id}, "
f"coverage={self.coverage:.2f}, "
f"matched={self.matched})"
)
def __len__(self):
......
......@@ -287,11 +287,12 @@ class InsertsDict(object):
def get(
self,
seq_id_or_idx=None,
insert_types="both",
insert_type="both",
filter_threshold=None,
):
assert insert_types in ("matched", "unmatched", "both")
seq_id_or_idx = seq_id_or_idx or self.seq_ids
assert insert_type in ("matched", "unmatched", "both")
if seq_id_or_idx is None:
seq_id_or_idx = self.seq_ids
inserts = self.__getitem__(seq_id_or_idx)
# Apply the coverage filter
......@@ -301,14 +302,14 @@ class InsertsDict(object):
inserts = [x for x in inserts if x.coverage > filter_threshold]
if insert_types == "matched":
if insert_type == "matched":
inserts = [x for x in inserts if x.matched]
elif insert_types == "unmatched":
elif insert_type == "unmatched":
inserts = [x for x in inserts if not x.matched]
return inserts
def to_dataframe(self, insert_types="both", filter_threshold=None):
def to_dataframe(self, insert_type="both", filter_threshold=None):
return pd.DataFrame(
[
(
......@@ -322,7 +323,7 @@ class InsertsDict(object):
x.coverage,
)
for x in self.get(
insert_types=insert_types, filter_threshold=filter_threshold
insert_type=insert_type, filter_threshold=filter_threshold
)
],
columns=(
......@@ -340,11 +341,11 @@ class InsertsDict(object):
def genes_to_dataframe(
self,
seq_id_or_idx=None,
insert_types="both",
insert_type="both",
filter_threshold=None,
buffer=4000,
):
inserts = self.get(seq_id_or_idx, insert_types, filter_threshold)
inserts = self.get(seq_id_or_idx, insert_type, filter_threshold)
if len(inserts):
df_genes = pd.concat(
......@@ -415,7 +416,9 @@ class InsertsDict(object):
def plot(
self,
inserts=None,
seq_id_or_idxs=None,
insert_type="both",
filter_threshold=None,
show_labels=True,
col1="#8DDEF7",
col2="#CFFCCC",
......@@ -426,7 +429,7 @@ class InsertsDict(object):
if "figsize" not in kwargs:
kwargs["figsize"] = (10, 8)
inserts = inserts or []
inserts = self.get(seq_id_or_idxs, insert_type, filter_threshold)
rec = self._get_graphic_records_genome(inserts, show_labels, col1, col2)
if backend == "matplotlib":
......
......@@ -5,7 +5,6 @@ from pathlib import Path
from tempfile import TemporaryDirectory
import matplotlib.pyplot as plt
import pandas as pd
import streamlit as st
import plotting_on_genome as pog
......@@ -15,8 +14,8 @@ import plotting_on_genome as pog
if "stage" not in st.session_state:
st.session_state.stage = 0
if "insert_types" not in st.session_state:
st.session_state.insert_types = "both"
if "insert_type" not in st.session_state:
st.session_state.insert_type = "both"
if "workdir" not in st.session_state:
st.session_state.workdir = None
......@@ -113,7 +112,7 @@ def get_main_inputs():
fwd_suf = st.text_input("Forward suffix:", "_F", key="fwd_suf")
rev_suf = st.text_input("Reverse suffix:", "_R", key="rev_suf")
st.session_state.insert_types = st.selectbox(
st.session_state.insert_type = st.selectbox(
"insert types:", ["both", "matched", "unmatched"]
)
......@@ -143,7 +142,7 @@ def convert_df(df, **kwargs):
def show_results():
inserts_all = st.session_state.pipeline
insert_types = st.session_state.insert_types
insert_type = st.session_state.insert_type
filter_threshold = st.slider("Filter threshold:", 0.0, 1.0, 0.7)
......@@ -154,7 +153,7 @@ def show_results():
value=4000,
help="Number of bases either side of the insert",
)
params = dict(insert_types=insert_types, filter_threshold=filter_threshold)
params = dict(insert_type=insert_type, filter_threshold=filter_threshold)
df_inserts = inserts_all.to_dataframe(**params)
df_genes = inserts_all.genes_to_dataframe(**params, buffer=buffer)
......@@ -217,15 +216,14 @@ def show_results():
st.write(f"No inserts found for {seq_id}!")
elif option == "plot genome":
labels = st.toggle("Labels")
inserts = []
if st.toggle("Plot inserts", True):
inserts = inserts_all.get(**params)
st.write(df_inserts)
labels = st.toggle("Labels")
insert_idxs = None if st.toggle("Plot inserts", True) else []
fig, ax = plt.subplots(figsize=(10, 10 * (1 + 2 * labels)))
ax = inserts_all.plot(inserts, show_labels=labels, ax=ax)
ax = inserts_all.plot(insert_idxs, show_labels=labels, ax=ax, **params)
st.pyplot(fig, use_container_width=True)
plt.close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment