Mercurial > repos > richard-burhans > segalign
annotate diagonal_partition.py @ 10:ec709ce3d91b draft
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 701d5b6d0b25dbd9d2a8c08234f3aca7ef502131
author | richard-burhans |
---|---|
date | Thu, 11 Jul 2024 15:50:48 +0000 |
parents | 08e987868f0f |
children | 25fa179d9d0a |
rev | line source |
---|---|
9
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
1 #!/usr/bin/env python |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
2 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
3 """ |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
4 Diagonal partitioning for segment files output by SegAlign. |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
5 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
6 Usage: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
7 diagonal_partition.py <max-segments> <lastz-command> |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
8 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
9 set <max-segments> = 0 to skip partitioning, -1 to infer best parameter |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
10 """ |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
11 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
12 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
13 import os |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
14 import sys |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
15 import typing |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
16 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
17 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
18 def chunks(lst: tuple[str, ...], n: int) -> typing.Iterator[tuple[str, ...]]: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
19 """Yield successive n-sized chunks from list.""" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
20 for i in range(0, len(lst), n): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
21 yield lst[i:i + n] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
22 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
23 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
24 if __name__ == "__main__": |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
25 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
26 DELETE_AFTER_CHUNKING = True |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
27 MIN_CHUNK_SIZE = 5000 # don't partition segment files with line count below this value |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
28 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
29 # input_params = "10000 sad sadsa sad --segments=tmp21.block0.r0.minus.segments dsa sa --strand=plus --output=out.maf sadads 2> logging.err" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
30 # sys.argv = [sys.argv[0]] + input_params.split(' ') |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
31 chunk_size = int(sys.argv[1]) # first parameter contains chunk size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
32 params = sys.argv[2:] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
33 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
34 # don't do anything if 0 chunk size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
35 if chunk_size == 0: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
36 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
37 exit(0) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
38 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
39 # Parsing command output from SegAlign |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
40 segment_key = "--segments=" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
41 segment_index = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
42 input_file = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
43 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
44 for index, value in enumerate(params): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
45 if value[:len(segment_key)] == segment_key: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
46 segment_index = index |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
47 input_file = value[len(segment_key):] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
48 break |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
49 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
50 if segment_index is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
51 sys.exit(f"Error: could not get segment key {segment_key} from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
52 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
53 if input_file is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
54 sys.exit(f"Error: could not get segment file from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
55 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
56 if not os.path.isfile(input_file): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
57 sys.exit(f"Error: File {input_file} does not exist") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
58 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
59 line_size = None # each char in 1 byte |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
60 file_size = os.path.getsize(input_file) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
61 with open(input_file, "r") as f: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
62 line_size = len(f.readline()) # add 1 for newline |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
63 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
64 estimated_lines = file_size // line_size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
65 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
66 # check if chunk size should be estimated |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
67 if chunk_size < 0: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
68 # optimization, do not need to get each file size in this case |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
69 if estimated_lines < MIN_CHUNK_SIZE: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
70 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
71 exit(0) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
72 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
73 from collections import defaultdict |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
74 from statistics import quantiles |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
75 # get size of each segment assuming DELETE_AFTER_CHUNKING == True |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
76 # takes into account already split segments |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
77 files = [i for i in os.listdir(".") if i.endswith(".segments")] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
78 # find . -maxdepth 1 -name "*.segments" -print0 | du -ba --files0-from=- |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
79 # if not enough segment files for estimation, continue |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
80 if len(files) <= 2: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
81 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
82 exit(0) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
83 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
84 fdict: typing.DefaultDict[str, int] = defaultdict(int) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
85 for filename in files: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
86 size = os.path.getsize(filename) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
87 f_ = filename.split(".split", 1)[0] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
88 fdict[f_] += size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
89 chunk_size = int(quantiles(fdict.values())[-1] // line_size) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
90 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
91 if file_size // line_size <= chunk_size: # no need to sort if number of lines <= chunk_size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
92 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
93 exit(0) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
94 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
95 # Find rest of relevant parameters |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
96 output_key = "--output=" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
97 output_index = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
98 output_alignment_file = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
99 output_alignment_file_base = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
100 output_format = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
101 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
102 strand_key = "--strand=" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
103 strand_index = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
104 for index, value in enumerate(params): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
105 if value[:len(output_key)] == output_key: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
106 output_index = index |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
107 output_alignment_file = value[len(output_key):] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
108 output_alignment_file_base, output_format = output_alignment_file.rsplit(".", 1) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
109 if value[:len(strand_key)] == strand_key: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
110 strand_index = index |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
111 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
112 if output_index is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
113 sys.exit(f"Error: could not get output key {output_key} from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
114 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
115 if output_alignment_file_base is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
116 sys.exit(f"Error: could not get output alignment file base from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
117 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
118 if output_format is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
119 sys.exit(f"Error: could not get output format from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
120 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
121 if strand_index is None: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
122 sys.exit(f"Error: could not get strand key {strand_key} from parameters {params}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
123 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
124 err_index = -1 # error file is at very end |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
125 err_name_base = params[-1].split(".err", 1)[0] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
126 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
127 data: dict[tuple[str, str], list[tuple[int, int, str]]] = {} # dict of list of tuple (x, y, str) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
128 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
129 direction = None |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
130 if "plus" in params[strand_index]: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
131 direction = "f" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
132 elif "minus" in params[strand_index]: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
133 direction = "r" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
134 else: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
135 sys.exit(f"Error: could not figure out direction from strand value {params[strand_index]}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
136 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
137 for line in open(input_file, "r"): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
138 if line == "": |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
139 continue |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
140 seq1_name, seq1_start, seq1_end, seq2_name, seq2_start, seq2_end, _dir, score = line.split() |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
141 # data.append((int(seq1_start), int(seq2_start), line)) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
142 half_dist = int((int(seq1_end) - int(seq1_start)) // 2) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
143 assert int(seq1_end) > int(seq1_start) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
144 assert int(seq2_end) > int(seq2_start) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
145 seq1_mid = int(seq1_start) + half_dist |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
146 seq2_mid = int(seq2_start) + half_dist |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
147 data.setdefault((seq1_name, seq2_name), []).append((seq1_mid, seq2_mid, line)) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
148 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
149 # If there are chromosome pairs with segment count <= chunk_size |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
150 # then no need to sort and split these pairs into separate files. |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
151 # It is better to keep these pairs in a single segment file. |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
152 skip_pairs = [] # pairs that have count <= chunk_size. these will not be sorted |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
153 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
154 # save query key order |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
155 # for lastz segment files: 'Query sequence names must appear in the same |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
156 # order as they do in the query file' |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
157 query_key_order = list(dict.fromkeys([i[1] for i in data.keys()])) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
158 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
159 # NOTE: assuming data.keys() preserves order of keys. Requires Python 3.7+ |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
160 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
161 if len(data.keys()) > 1: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
162 for pair in data.keys(): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
163 if len(data[pair]) <= chunk_size: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
164 skip_pairs.append(pair) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
165 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
166 # sorting for forward segments |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
167 if direction == "r": |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
168 for pair in data.keys(): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
169 if pair not in skip_pairs: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
170 data[pair] = sorted(data[pair], key=lambda coord: (coord[1] - coord[0], coord[0])) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
171 # sorting for reverse segments |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
172 elif direction == "f": |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
173 for pair in data.keys(): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
174 if pair not in skip_pairs: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
175 data[pair] = sorted(data[pair], key=lambda coord: (coord[1] + coord[0], coord[0])) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
176 else: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
177 sys.exit(f"INVALID DIRECTION VALUE: {direction}") |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
178 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
179 # Writing file in chunks |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
180 ctr = 0 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
181 for pair in data.keys() - skip_pairs: # [i for i in data_keys if i not in set(skip_pairs)]: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
182 for chunk in chunks(list(zip(*data[pair]))[2], chunk_size): |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
183 ctr += 1 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
184 name_addition = f".split{ctr}" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
185 fname = input_file.split(".segments", 1)[0] + name_addition + ".segments" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
186 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
187 assert len(chunk) != 0 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
188 with open(fname, "w") as f: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
189 f.writelines(chunk) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
190 # update segment file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
191 params[segment_index] = segment_key + fname |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
192 # update output file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
193 params[output_index] = output_key + output_alignment_file_base + name_addition + "." + output_format |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
194 # update error file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
195 params[-1] = err_name_base + name_addition + ".err" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
196 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
197 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
198 # writing unsorted skipped pairs |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
199 if len(skip_pairs) > 0: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
200 skip_pairs_with_len = sorted([(len(data[p]), p) for p in skip_pairs]) # list of tuples of (pair length, pair) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
201 # NOTE: This can violate lastz query key order requirement |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
202 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
203 query_key_order_table = {item: idx for idx, item in enumerate(query_key_order)} # used for sorting |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
204 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
205 aggregated_skip_pairs: list[list[tuple[str, str]]] = [] # list of list of pair names |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
206 current_count = 0 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
207 aggregated_skip_pairs.append([]) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
208 for count, pair in skip_pairs_with_len: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
209 if current_count + count <= chunk_size: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
210 current_count += count |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
211 aggregated_skip_pairs[-1].append(pair) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
212 else: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
213 aggregated_skip_pairs.append([]) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
214 current_count = count |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
215 aggregated_skip_pairs[-1].append(pair) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
216 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
217 for aggregate in aggregated_skip_pairs: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
218 ctr += 1 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
219 name_addition = f".split{ctr}" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
220 fname = input_file.split(".segments", 1)[0] + name_addition + ".segments" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
221 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
222 with open(fname, "w") as f: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
223 # fix possible lastz query key order violations |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
224 for pair in sorted(aggregate, key=lambda p: query_key_order_table[p[1]]): # p[1] is query key |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
225 chunk = list(zip(*data[pair]))[2] |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
226 f.writelines(chunk) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
227 # update segment file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
228 params[segment_index] = segment_key + fname |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
229 # update output file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
230 params[output_index] = output_key + output_alignment_file_base + name_addition + "." + output_format |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
231 # update error file in command |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
232 params[-1] = err_name_base + name_addition + ".err" |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
233 print(" ".join(params), flush=True) |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
234 |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
235 if DELETE_AFTER_CHUNKING: |
08e987868f0f
planemo upload for repository https://github.com/richard-burhans/galaxytools/tree/main/tools/segalign commit 062a761a340e095ea7ef7ed7cd1d3d55b1fdc5c4
richard-burhans
parents:
diff
changeset
|
236 os.remove(input_file) |