From b9ee273f554e773d640ef961ee65619fa62a8250 Mon Sep 17 00:00:00 2001
From: Remi Planel <rplanel@pasteur.fr>
Date: Fri, 31 Jan 2020 16:07:13 +0100
Subject: [PATCH] Fix bug no off-target feature. Fix #6

---
 CHANGELOG             |  4 ++++
 crisprbact/predict.py | 55 ++++++++++++++++++++++++-------------------
 2 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 6328333..9e5c7de 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+# 0.3.2
+- Fix bugg when no off-target feature
+- Handle genome in fasta file
+
 # 0.3.1
 - Fix bug when no feature
 - Add off-target strand to output
diff --git a/crisprbact/predict.py b/crisprbact/predict.py
index 14a3eac..20e97dd 100644
--- a/crisprbact/predict.py
+++ b/crisprbact/predict.py
@@ -72,31 +72,38 @@ def on_target_predict(seq, genome=None, seed_size=7):
                     target["guide"], seed_size, records, genome_features
                 )
                 off_targets_list = []
-                off_targets = off_target_df.loc[
-                    0:, ["start", "end", "pampos", "strand", "features"]
-                ]
-                for index, off_t in enumerate(off_targets.values.tolist()):
-                    off_target_dict = {
-                        "off_target_start": off_t[0],
-                        "off_target_end": off_t[1],
-                        "pampos": off_t[2],
-                        "strand": off_t[3],
-                    }
-                    if len(off_t[4]) > 0:
-                        for feat in off_t[4]:
-                            feature_dict = {
-                                "feat_strand": feat.location.strand,
-                                "feat_start": feat.location.start,
-                                "feat_end": feat.location.end,
-                                "feat_type": feat.type,
-                            }
-                            for k, feat in feat.qualifiers.items():
-                                if k != "translation":
-                                    feature_dict[k] = " :: ".join(feat)
+                if not off_target_df.empty:
+                    off_targets = off_target_df.loc[
+                        0:, ["start", "end", "pampos", "strand", "recid", "features"]
+                    ]
+                    for index, off_t in enumerate(off_targets.values.tolist()):
+                        off_target_dict = {
+                            "off_target_start": off_t[0],
+                            "off_target_end": off_t[1],
+                            "pampos": off_t[2],
+                            "strand": off_t[3],
+                            "recid": off_t[4],
+                        }
+                        if len(off_t[5]) > 0:
+                            # Loop for each feature
+                            for feat in off_t[5]:
+                                feature_dict = {
+                                    "feat_strand": feat.location.strand,
+                                    "feat_start": feat.location.start,
+                                    "feat_end": feat.location.end,
+                                    "feat_type": feat.type,
+                                }
+                                for k, feat in feat.qualifiers.items():
+                                    if k != "translation":
+                                        feature_dict[k] = "::".join(feat)
+                                off_targets_list.append(
+                                    {**feature_dict, **off_target_dict}
+                                )
+                        else:
                             off_targets_list.append(off_target_dict)
-                    else:
-                        off_targets_list.append({**feature_dict, **off_target_dict})
-                target.update({"off_targets": off_targets_list})
+                    target.update({"off_targets": off_targets_list})
+                else:
+                    target.update({"off_targets": off_targets_list})
             else:
                 target.update({"off_targets": []})
         return alltargets
-- 
GitLab