Previous changeset 3:10000414e916 (2024-08-12) Next changeset 5:00aab5199327 (2024-08-12) |
Commit message:
planemo upload commit c3db49ef44729e27fa7ca5ade2ea27d7074072ca |
modified:
intersect_and_adjacent.py |
b |
diff -r 10000414e916 -r 3e3b5ba626b9 intersect_and_adjacent.py --- a/intersect_and_adjacent.py Mon Aug 12 04:23:34 2024 +0000 +++ b/intersect_and_adjacent.py Mon Aug 12 04:33:20 2024 +0000 |
[ |
b'@@ -84,73 +84,40 @@\n b_pos = []\n tree_a = []\n tree_b = []\n- if stranding == True:\n- for feat in rec_a_i.features:\n- if feat.type == "remark" or feat.type == "annotation":\n- continue\n+\n+ for feat in rec_a_i.features:\n+ if feat.type == "remark" or feat.type == "annotation":\n+ continue\n+ interval = Interval(\n+ int(feat.location.start) - int(window),\n+ int(feat.location.end) + int(window),\n+ feat.id,\n+ )\n+ if stranding:\n if feat.strand > 0:\n- a_pos.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n- else:\n- a_neg.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n-\n- for feat in rec_b_i.features:\n- if feat.type == "remark" or feat.type == "annotation":\n- continue\n- if feat.strand > 0:\n- b_pos.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n+ a_pos.append(interval)\n else:\n- b_neg.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n+ a_neg.append(interval)\n+ else:\n+ tree_a.append(interval)\n \n- else:\n- for feat in rec_a_i.features:\n- if feat.type == "remark" or feat.type == "annotation":\n- continue\n- tree_a.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n- for feat in rec_b_i.features:\n- if feat.type == "remark" or feat.type == "annotation":\n- continue\n- tree_b.append(\n- Interval(\n- int(feat.location.start) - int(window),\n- int(feat.location.end) + int(window),\n- feat.id,\n- )\n- )\n+ for feat in rec_b_i.features:\n+ if feat.type == "remark" or feat.type == "annotation":\n+ continue\n+ interval = Interval(\n+ int(feat.location.start) - int(window),\n+ int(feat.location.end) + int(window),\n+ feat.id,\n+ )\n+ if stranding:\n+ if feat.strand > 0:\n+ b_pos.append(interval)\n+ else:\n+ b_neg.append(interval)\n+ else:\n+ tree_b.append(interval)\n+\n if stranding:\n- '..b'a_neg = IntervalTree(a_neg)\n tree_b_pos = IntervalTree(b_pos)\n@@ -167,61 +134,48 @@\n rec_b_hits_in_a = []\n \n for feature in rec_a_i.features:\n- # Save each feature in rec_a that overlaps a feature in rec_b\n- # hits = tree_b.find_range((int(feature.location.start), int(feature.location.end)))\n-\n if feature.type == "remark" or feature.type == "annotation":\n continue\n \n- if stranding == False:\n+ if not stranding:\n hits = tree_b[\n int(feature.location.start) : int(feature.location.end)\n ]\n-\n- # feature id is saved in interval result.data, use map to get full feature\n for hit in hits:\n rec_a_hits_in_b.append(rec_b_map[hit.data])\n-\n else:\n if feature.strand > 0:\n- hits_pos = tree_b_pos[\n+ hits = tree_b_pos[\n int(feature.location.start) : int(feature.location.end)\n ]\n- for hit in hits_pos:\n- rec_a_hits_in_b.append(rec_b_map[hit.data])\n else:\n- hits_neg = tree_b_neg[\n+ hits = tree_b_neg[\n int(feature.location.start) : int(feature.location.end)\n ]\n- for hit in hits_neg:\n- rec_a_hits_in_b.append(rec_b_map[hit.data])\n+ for hit in hits:\n+ rec_a_hits_in_b.append(rec_b_map[hit.data])\n \n for feature in rec_b_i.features:\n if feature.type == "remark" or feature.type == "annotation":\n continue\n \n- if stranding == False:\n+ if not stranding:\n hits = tree_a[\n int(feature.location.start) : int(feature.location.end)\n ]\n-\n- # feature id is saved in interval result.data, use map to get full feature\n for hit in hits:\n rec_b_hits_in_a.append(rec_a_map[hit.data])\n-\n else:\n if feature.strand > 0:\n- hits_pos = tree_a_pos[\n+ hits = tree_a_pos[\n int(feature.location.start) : int(feature.location.end)\n ]\n- for hit in hits_pos:\n- rec_b_hits_in_a.append(rec_a_map[hit.data])\n else:\n- hits_neg = tree_a_neg[\n+ hits = tree_a_neg[\n int(feature.location.start) : int(feature.location.end)\n ]\n- for hit in hits_neg:\n- rec_b_hits_in_a.append(rec_a_map[hit.data])\n+ for hit in hits:\n+ rec_b_hits_in_a.append(rec_a_map[hit.data])\n \n # Remove duplicate features using sets\n rec_a_out.append(\n@@ -268,16 +222,14 @@\n help="Allows features this far away to still be considered \'adjacent\'",\n )\n parser.add_argument(\n- "stranding",\n- nargs="?",\n- default="",\n- help="Only allow adjacency for same-strand features if set to \'-stranding\'",\n+ "-stranding",\n+ action="store_true",\n+ help="Only allow adjacency for same-strand features",\n )\n parser.add_argument("--oa", type=str, default="a_hits_near_b.gff")\n parser.add_argument("--ob", type=str, default="b_hits_near_a.gff")\n args = parser.parse_args()\n \n- stranding = args.stranding == "-stranding"\n b, a = intersect(args.a, args.b, args.window, args.stranding)\n \n with open(args.oa, "w") as handle:\n' |