annotate RaGOO/ragoo.py @ 13:b9a3aeb162ab draft default tip

Uploaded
author dereeper
date Mon, 26 Jul 2021 18:22:37 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/env python
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
2 from collections import defaultdict
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
3 from collections import OrderedDict
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
4 import copy
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
5
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
6 from intervaltree import IntervalTree
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
7
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
8 from ragoo_utilities.PAFReader import PAFReader
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
9 from ragoo_utilities.SeqReader import SeqReader
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
10 from ragoo_utilities.ReadCoverage import ReadCoverage
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
11 from ragoo_utilities.ContigAlignment import ContigAlignment
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
12 from ragoo_utilities.ContigAlignment import UniqueContigAlignment
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
13 from ragoo_utilities.ContigAlignment import LongestContigAlignment
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
14 from ragoo_utilities.GFFReader import GFFReader
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
15 from ragoo_utilities.utilities import run, log, reverse_complement, read_contigs, read_gz_contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
16 from ragoo_utilities.break_chimera import get_ref_parts, cluster_contig_alns, avoid_gff_intervals, update_gff, break_contig, get_intra_contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
17
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
18
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
19 def update_misasm_features(features, breaks, contig, ctg_len):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
20
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
21 # Get ctg len from ReadCoverage object
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
22 break_list = [0] + sorted(breaks) + [ctg_len]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
23 borders = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
24 for i in range(len(break_list) - 1):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
25 borders.append((break_list[i], break_list[i+1]))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
26
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
27 # Pop the features to be updated
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
28 contig_feats = features.pop(contig)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
29
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
30 # Initialize lists for new broken contig headers
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
31 for i in range(len(borders)):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
32 features[contig + '_misasm_break:' + str(borders[i][0]) + '-' + str(borders[i][1])] = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
33
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
34 t = IntervalTree()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
35 for i in borders:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
36 t[i[0]:i[1]] = i
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
37
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
38 for i in contig_feats:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
39 query = t[i.start]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
40 assert len(query) == 1
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
41 break_start = list(query)[0].begin
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
42 break_end = list(query)[0].end
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
43 query_border = (break_start, break_end)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
44 break_number = borders.index(query_border)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
45 i.seqname = contig + '_misasm_break:' + str(borders[break_number][0]) + '-' + str(borders[break_number][1])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
46 i.start = i.start - break_start
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
47 i.end = i.end - break_start
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
48 features[
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
49 contig + '_misasm_break:' + str(borders[break_number][0]) + '-' + str(borders[break_number][1])].append(i)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
50
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
51 return features
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
52
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
53
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
54 def remove_gff_breaks(gff_ins, breaks):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
55 """
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
56 Given a list of candidate breakpoints proposed by misassembly correction, remove any such break points that
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
57 fall within the interval of a gff feature. This should be called once per contig.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
58 :param gff_ins: List of GFFLines
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
59 :param breaks: candidate break points
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
60 :return:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
61 """
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
62 # Make an interval tree from the intervals of the gff lines
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
63 t = IntervalTree()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
64 for line in gff_ins:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
65 # If the interval is one bp long, skip
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
66 if line.start == line.end:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
67 continue
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
68 t[line.start:line.end] = (line.start, line.end)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
69
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
70 return [i for i in breaks if not t[i]]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
71
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
72
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
73 def write_misasm_broken_ctgs(contigs_file, breaks, out_prefix, in_gff=None, in_gff_name=None):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
74 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
75 os.chdir('ctg_alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
76
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
77 if in_gff and in_gff_name:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
78 with open(in_gff_name, 'w') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
79 for i in in_gff.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
80 for j in in_gff[i]:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
81 f.write(str(j) + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
82
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
83 x = SeqReader("../../" + contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
84 f = open(out_prefix + ".misasm.break.fa", 'w')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
85 for header, seq in x.parse_fasta():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
86 header = header[1:]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
87 if header not in breaks:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
88 f.write(">" + header + "\n")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
89 f.write(seq + "\n")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
90 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
91 # Break the contig
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
92 ctg_len = len(seq)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
93 break_list = [0] + sorted(breaks[header]) + [ctg_len]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
94 for i in range(len(break_list) - 1):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
95 f.write(">" + header + "_misasm_break:" + str(break_list[i]) + "-" + str(break_list[i+1]) + "\n")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
96 f.write(seq[break_list[i]:break_list[i+1]] + "\n")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
97 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
98
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
99
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
100 def align_misasm_broken(out_prefix):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
101 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
102 os.chdir('ctg_alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
103
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
104 ctgs_file = out_prefix + ".misasm.break.fa"
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
105 cmd = '{} -k19 -w19 -t{} ../../{} {} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
106 '> contigs_brk_against_ref.paf 2> contigs_brk_against_ref.paf.log'.format(minimap_path, t, reference_file,
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
107 ctgs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
108 if not os.path.isfile('contigs_brk_against_ref.paf'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
109 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
110 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
111
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
112
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
113 def write_contig_clusters(unique_dict, thresh, skip_list):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
114 # Get a list of all chromosomes
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
115 all_chroms = set([unique_dict[i].ref_chrom for i in unique_dict.keys()])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
116 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
117 output_path = current_path + '/groupings'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
118 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
119 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
120
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
121 os.chdir('groupings')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
122 for i in all_chroms:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
123 open(i + '_contigs.txt', 'w').close()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
124
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
125 for i in unique_dict.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
126 this_chr = unique_dict[i].ref_chrom
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
127 this_confidence = unique_dict[i].confidence
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
128 if this_confidence > thresh:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
129 if not i in skip_list:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
130 file_name = str(this_chr) + '_contigs.txt'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
131 with open(file_name, 'a') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
132 f.write(i + '\t' + str(this_confidence) + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
133 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
134
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
135
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
136 def clean_alignments(in_alns, l=10000, in_exclude_file='', uniq_anchor_filter=False, merge=False):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
137 # Exclude alignments to undesired reference headers and filter alignment lengths.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
138 exclude_list = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
139 if in_exclude_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
140 with open('../' + in_exclude_file) as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
141 for line in f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
142 exclude_list.append(line.rstrip().replace('>', '').split()[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
143
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
144 empty_headers = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
145 for header in in_alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
146 in_alns[header].exclude_ref_chroms(exclude_list)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
147 in_alns[header].filter_lengths(l)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
148 if uniq_anchor_filter:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
149 in_alns[header].unique_anchor_filter()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
150
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
151 if merge:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
152 in_alns[header].merge_alns()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
153
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
154 # Check if our filtering has removed all alignments for a contig
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
155 if len(in_alns[header].ref_headers) == 0:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
156 empty_headers.append(header)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
157
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
158 for header in empty_headers:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
159 in_alns.pop(header)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
160 return in_alns
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
161
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
162
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
163 def read_paf_alignments(in_paf):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
164 # Read in PAF alignments
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
165 # Initialize a dictionary where key is contig header, and value is ContigAlignment.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
166 alns = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
167 x = PAFReader(in_paf)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
168 for paf_line in x.parse_paf():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
169 if paf_line.contig in alns:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
170 alns[paf_line.contig].add_alignment(paf_line)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
171 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
172 alns[paf_line.contig] = ContigAlignment(paf_line.contig)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
173 alns[paf_line.contig].add_alignment(paf_line)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
174 return alns
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
175
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
176
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
177 def get_contigs_from_groupings(in_file):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
178 contigs = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
179 with open(in_file) as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
180 for line in f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
181 contigs.append(line.split('\t')[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
182 return contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
183
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
184
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
185 def get_location_confidence(in_ctg_alns):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
186 # Use interval tree to get all alignments with the reference span
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
187 # Go through each of them and if any start is less than the min_pos or any end is greater than
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
188 # the max_pos, change the borders to those values. Then use the algorithm that Mike gave me.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
189 min_pos = min(in_ctg_alns.ref_starts)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
190 max_pos = max(in_ctg_alns.ref_ends)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
191 t = IntervalTree()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
192
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
193 # Put the reference start and end position for every alignment into the tree
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
194 for i in range(len(in_ctg_alns.ref_headers)):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
195 t[in_ctg_alns.ref_starts[i]:in_ctg_alns.ref_ends[i]] = (in_ctg_alns.ref_starts[i], in_ctg_alns.ref_ends[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
196
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
197 overlaps = t[min_pos:max_pos]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
198 if not overlaps:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
199 return 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
200
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
201 # If any intervals fall beyond the boundaries, replace the start/end with the boundary it exceeds
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
202 ovlp_list = [i.data for i in overlaps]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
203 bounded_list = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
204 for i in ovlp_list:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
205 if i[0] < min_pos:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
206 i[0] = min_pos
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
207 if i[1] > max_pos:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
208 i[1] = max_pos
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
209 bounded_list.append(i)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
210
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
211 # Now can just calculate the total range covered by the intervals
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
212 ovlp_range = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
213 sorted_intervals = sorted(bounded_list, key=lambda tup: tup[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
214 max_end = -1
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
215 for j in sorted_intervals:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
216 start_new_terr = max(j[0], max_end)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
217 ovlp_range += max(0, j[1] - start_new_terr)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
218 max_end = max(max_end, j[1])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
219
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
220 return ovlp_range / (max_pos - min_pos)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
221
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
222
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
223 def order_orient_contigs(in_unique_contigs, in_alns):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
224 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
225 output_path = current_path + '/orderings'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
226 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
227 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
228
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
229 # Get longest alignments
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
230 longest_contigs = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
231 for i in in_alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
232 # Only consider alignments to the assigned chromosome
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
233 uniq_aln = UniqueContigAlignment(in_alns[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
234 best_header = uniq_aln.ref_chrom
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
235 ctg_alns = copy.deepcopy(in_alns[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
236 ctg_alns.filter_ref_chroms([best_header])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
237 longest_contigs[i] = LongestContigAlignment(ctg_alns)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
238
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
239 # Save the orientations
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
240 final_orientations = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
241 for i in longest_contigs.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
242 final_orientations[i] = longest_contigs[i].strand
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
243
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
244 # Get the location and orientation confidence scores
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
245 orientation_confidence = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
246 location_confidence = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
247 forward_bp = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
248 reverse_bp = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
249 for i in in_alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
250 uniq_aln = UniqueContigAlignment(in_alns[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
251 best_header = uniq_aln.ref_chrom
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
252 ctg_alns = copy.deepcopy(in_alns[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
253 ctg_alns.filter_ref_chroms([best_header])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
254
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
255 # Orientation confidence scores
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
256 # Every base pair votes for the orientation of the alignment in which it belongs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
257 # Score is # votes for the assigned orientation over all votes
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
258 for j in range(len(ctg_alns.ref_headers)):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
259 if ctg_alns.strands[j] == '+':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
260 forward_bp += ctg_alns.aln_lens[j]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
261 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
262 reverse_bp += ctg_alns.aln_lens[j]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
263
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
264 if final_orientations[i] == '+':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
265 orientation_confidence[i] = forward_bp / (forward_bp + reverse_bp)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
266 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
267 orientation_confidence[i] = reverse_bp / (forward_bp + reverse_bp)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
268
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
269 forward_bp = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
270 reverse_bp = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
271
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
272 # Location confidence
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
273 location_confidence[i] = get_location_confidence(ctg_alns)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
274
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
275 all_chroms = set([in_unique_contigs[i].ref_chrom for i in in_unique_contigs.keys()])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
276
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
277 for this_chrom in all_chroms:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
278
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
279 # Intialize the list of start and end positions w.r.t the query
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
280 ref_pos = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
281
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
282 groupings_file = 'groupings/' + this_chrom + '_contigs.txt'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
283 contigs_list = get_contigs_from_groupings(groupings_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
284
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
285 for i in range(len(contigs_list)):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
286 # There is a scope issue here. Pass this (longest_contigs) to the method explicitly.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
287 ref_pos.append((longest_contigs[contigs_list[i]].ref_start, longest_contigs[contigs_list[i]].ref_end, i))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
288
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
289 final_order = [contigs_list[i[2]] for i in sorted(ref_pos)]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
290
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
291 # Get ordering confidence
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
292 # To do this, get the max and min alignments to this reference chromosome
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
293 # Then within that region, what percent of bp are covered
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
294
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
295 with open('orderings/' + this_chrom + '_orderings.txt', 'w') as out_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
296 for i in final_order:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
297 # Also have a scope issue here.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
298 out_file.write(i + '\t' + final_orientations[i] + '\t' + str(location_confidence[i]) + '\t' + str(orientation_confidence[i]) + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
299
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
300
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
301 def get_orderings(in_orderings_file):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
302 all_orderings = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
303 with open(in_orderings_file) as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
304 for line in f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
305 L1 = line.split('\t')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
306 all_orderings.append((L1[0], L1[1].rstrip()))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
307 return all_orderings
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
308
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
309
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
310 def create_pseudomolecules(in_contigs_file, in_unique_contigs, gap_size, chr0=True):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
311 """
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
312 Need to make a translation table for easy lift-over.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
313 :param in_contigs_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
314 :param in_unique_contigs:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
315 :param gap_size:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
316 :return:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
317 """
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
318 # First, read all of the contigs into memory
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
319 remaining_contig_headers = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
320 all_seqs = OrderedDict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
321 x = SeqReader('../' + in_contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
322 if in_contigs_file.endswith(".gz"):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
323 for header, seq in x.parse_gzip_fasta():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
324 remaining_contig_headers.append(header.split(' ')[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
325 all_seqs[header.split(' ')[0]] = seq
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
326 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
327 for header, seq in x.parse_fasta():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
328 remaining_contig_headers.append(header.split(' ')[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
329 all_seqs[header.split(' ')[0]] = seq
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
330
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
331 # Get all reference chromosomes
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
332 all_chroms = sorted(list(set([in_unique_contigs[i].ref_chrom for i in in_unique_contigs.keys()])))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
333
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
334 # Iterate through each orderings file and store sequence in a dictionary
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
335 all_pms = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
336 pad = ''.join('N' for i in range(gap_size))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
337 for this_chrom in all_chroms:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
338 orderings_file = 'orderings/' + this_chrom + '_orderings.txt'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
339 orderings = get_orderings(orderings_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
340 if orderings:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
341 seq_list = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
342 for line in orderings:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
343 # Mark that we have seen this contig
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
344 remaining_contig_headers.pop(remaining_contig_headers.index('>' + line[0]))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
345 if line[1] == '+':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
346 seq_list.append(all_seqs['>' + line[0]])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
347 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
348 assert line[1] == '-'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
349 seq_list.append(reverse_complement(all_seqs['>' + line[0]]))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
350 all_pms[this_chrom] = pad.join(seq_list)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
351 all_pms[this_chrom] += '\n'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
352
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
353 # Get unincorporated sequences and place them in Chr0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
354 if remaining_contig_headers:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
355 if chr0:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
356 chr0_headers = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
357 chr0_seq_list = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
358 for header in remaining_contig_headers:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
359 chr0_headers.append(header)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
360 chr0_seq_list.append(all_seqs[header])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
361 all_pms['Chr0'] = pad.join(chr0_seq_list)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
362 all_pms['Chr0'] += '\n'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
363
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
364 # Write out the list of chr0 headers
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
365 f_chr0_g = open('groupings/Chr0_contigs.txt', 'w')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
366 f_chr0_o = open('orderings/Chr0_orderings.txt', 'w')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
367 for i in chr0_headers:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
368 f_chr0_g.write(i[1:] + "\t" + "0" + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
369 f_chr0_o.write(i[1:] + '\t' + "+" + '\t' + "0" + '\t' + "0" + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
370 f_chr0_g.close()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
371 f_chr0_o.close()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
372 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
373 # Instead of making a chromosome 0, add the unplaced sequences as is.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
374 for header in remaining_contig_headers:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
375 all_pms[header[1:]] = all_seqs[header] + "\n"
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
376 f_chr0_g = open('groupings/' + header[1:] + '_contigs.txt', 'w')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
377 f_chr0_o = open('orderings/' + header[1:] + '_orderings.txt', 'w')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
378 f_chr0_g.write(header[1:] + "\t" + "0" + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
379 f_chr0_o.write(header[1:] + '\t' + "+" + '\t' + "0" + '\t' + "0" + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
380 f_chr0_g.close()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
381 f_chr0_o.close()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
382
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
383 # Write the final sequences out to a file
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
384 with open('ragoo.fasta', 'w') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
385 for out_header in all_pms:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
386 f.write(">" + out_header + "_RaGOO\n")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
387 f.write(all_pms[out_header])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
388
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
389
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
390 def write_broken_files(in_contigs, in_contigs_name, in_gff=None, in_gff_name=None):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
391 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
392 output_path = current_path + '/chimera_break'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
393 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
394 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
395
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
396 os.chdir('chimera_break')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
397 if in_gff and in_gff_name:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
398 with open(in_gff_name, 'w') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
399 for i in in_gff.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
400 for j in in_gff[i]:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
401 f.write(str(j) + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
402
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
403 with open(in_contigs_name, 'w') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
404 for i in in_contigs.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
405 f.write('>' + i + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
406 f.write(in_contigs[i] + '\n')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
407
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
408 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
409
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
410
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
411 def align_breaks(break_type, m_path, in_reference_file, in_contigs_file, in_num_threads):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
412 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
413 os.chdir('chimera_break')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
414 if break_type == 'inter':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
415 cmd = '{} -k19 -w19 -t{} ../../{} {} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
416 '> inter_contigs_against_ref.paf 2> inter_contigs_against_ref.paf.log'.format(m_path, in_num_threads, in_reference_file, in_contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
417 if not os.path.isfile('inter_contigs_against_ref.paf'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
418 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
419 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
420 cmd = '{} -k19 -w19 -t{} ../../{} {} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
421 '> intra_contigs_against_ref.paf 2> intra_contigs_against_ref.paf.log'.format(m_path, in_num_threads, in_reference_file, in_contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
422 if not os.path.isfile('intra_contigs_against_ref.paf'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
423 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
424
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
425 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
426
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
427
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
428 def align_pms(m_path, num_threads, in_reference_file):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
429 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
430 output_path = current_path + '/pm_alignments'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
431 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
432 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
433 os.chdir('pm_alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
434
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
435 cmd = '{} -ax asm5 --cs -t{} ../../{} {} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
436 '> pm_against_ref.sam 2> pm_contigs_against_ref.sam.log'.format(m_path, num_threads,
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
437 in_reference_file, '../ragoo.fasta')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
438 if not os.path.isfile('pm_against_ref.sam'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
439 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
440
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
441 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
442
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
443
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
444 def get_SVs(sv_min, sv_max, in_ref_file):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
445 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
446 os.chdir('pm_alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
447 # Change this when setup.py is ready. Just call script directly
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
448 cmd = 'sam2delta.py pm_against_ref.sam'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
449 if not os.path.isfile('pm_against_ref.sam.delta'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
450 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
451
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
452 cmd_2 = 'Assemblytics_uniq_anchor.py --delta pm_against_ref.sam.delta --unique-length 10000 --out assemblytics_out --keep-small-uniques'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
453 if not os.path.isfile('assemblytics_out.Assemblytics.unique_length_filtered_l10000.delta'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
454 run(cmd_2)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
455
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
456 cmd_3 = 'Assemblytics_between_alignments.pl assemblytics_out.coords.tab %r %r all-chromosomes exclude-longrange bed > assemblytics_out.variants_between_alignments.bed' %(sv_min, sv_max)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
457 if not os.path.isfile('assemblytics_out.variants_between_alignments.bed'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
458 run(cmd_3)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
459
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
460 cmd_4 = 'Assemblytics_within_alignment.py --delta assemblytics_out.Assemblytics.unique_length_filtered_l10000.delta --min %r > assemblytics_out.variants_within_alignments.bed' %(sv_min)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
461 if not os.path.isfile('assemblytics_out.variants_within_alignments.bed'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
462 run(cmd_4)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
463
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
464 header = "reference\tref_start\tref_stop\tID\tsize\tstrand\ttype\tref_gap_size\tquery_gap_size\tquery_coordinates\tmethod\n"
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
465
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
466 with open('assemblytics_out.variants_between_alignments.bed', 'r')as f1:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
467 b1 = f1.read()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
468
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
469 with open('assemblytics_out.variants_within_alignments.bed', 'r') as f2:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
470 b2 = f2.read()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
471
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
472 with open('assemblytics_out.Assemblytics_structural_variants.bed', 'w') as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
473 f.write(header)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
474 # Might need to add newlines here
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
475 f.write(b1)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
476 f.write(b2)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
477
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
478 # Filter out SVs caused by gaps
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
479 cmd_5 = 'filter_gap_SVs.py ../../%s' %(in_ref_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
480 run(cmd_5)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
481
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
482 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
483
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
484
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
485 def align_reads(m_path, num_threads, in_ctg_file, reads, tech='ont'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
486 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
487 output_path = current_path + '/ctg_alignments'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
488 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
489 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
490 os.chdir('ctg_alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
491
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
492 if tech == 'sr':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
493 cmd = '{} -x sr -t{} ../../{} ../../{} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
494 '> reads_against_ctg.paf 2> reads_against_ctg.paf.log'.format(m_path, num_threads, in_ctg_file, reads)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
495 elif tech == 'corr':
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
496 cmd = '{} -x asm10 -t{} ../../{} ../../{} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
497 '> reads_against_ctg.paf 2> reads_against_ctg.paf.log'.format(m_path, num_threads, in_ctg_file, reads)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
498 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
499 raise ValueError("Only 'sr' or 'corr' are accepted for read type.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
500
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
501 if not os.path.isfile('reads_against_ctg.paf'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
502 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
503
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
504 os.chdir(current_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
505
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
506
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
507 if __name__ == "__main__":
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
508 import os
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
509 import argparse
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
510
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
511 parser = argparse.ArgumentParser(description='order and orient contigs according to minimap2 alignments to a reference (v1.1)')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
512 parser.add_argument("contigs", metavar="<contigs.fasta>", type=str, help="fasta file with contigs to be ordered and oriented (gzipped allowed)")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
513 parser.add_argument("reference", metavar="<reference.fasta>", type=str, help="reference fasta file (gzipped allowed)")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
514 #parser.add_argument("-o", metavar="PATH", type=str, default="ragoo_output", help="output directory name")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
515 parser.add_argument("-e", metavar="<exclude.txt>", type=str, default="", help="single column text file of reference headers to ignore")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
516 parser.add_argument("-gff", metavar="<annotations.gff>", type=str, default='', help="lift-over gff features to chimera-broken contigs")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
517 parser.add_argument("-m", metavar="PATH", type=str, default="minimap2", help='path to minimap2 executable')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
518 parser.add_argument("-b", action='store_true', default=False, help="Break chimeric contigs")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
519 parser.add_argument("-R", metavar="<reads.fasta>", type=str, default="", help="Turns on misassembly correction. Align provided reads to the contigs to aid misassembly correction. fastq or fasta allowed. Gzipped files allowed. Turns off '-b'.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
520 parser.add_argument("-T", metavar="sr", type=str, default="", help="Type of reads provided by '-R'. 'sr' and 'corr' accepted for short reads and error corrected long reads respectively.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
521 parser.add_argument("-p", metavar="5", type=int, default=5, help=argparse.SUPPRESS)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
522 parser.add_argument("-l", metavar="10000", type=int, default=10000, help=argparse.SUPPRESS)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
523 parser.add_argument("-r", metavar="100000", type=int, default=100000, help="(with -b) this many bp of >1 reference sequence must be covered for a contig to be considered an interchromosomal chimera.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
524 parser.add_argument("-c", metavar="1000000", type=int, default=1000000, help="(with -b) distance threshold between consecutive alignments with respect to the contig.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
525 parser.add_argument("-d", metavar="2000000", type=int, default=2000000, help="(with -b) distance threshold between consecutive alignments with respect to the reference.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
526 parser.add_argument("-t", metavar="3", type=int, default=3, help="Number of threads when running minimap.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
527 parser.add_argument("-g", metavar="100", type=int, default=100, help="Gap size for padding in pseudomolecules.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
528 parser.add_argument("-s", action='store_true', default=False, help="Call structural variants")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
529 parser.add_argument("-a", metavar="50", type=int, default=50, help=argparse.SUPPRESS)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
530 parser.add_argument("-f", metavar="10000", type=int, default=10000, help=argparse.SUPPRESS)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
531 parser.add_argument("-i", metavar="0.2", type=float, default=0.2, help="Minimum grouping confidence score needed to be localized.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
532 parser.add_argument("-j", metavar="<skip.txt>", type=str, default="", help="List of contigs to automatically put in chr0.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
533 parser.add_argument("-C", action='store_true', default=False, help="Write unplaced contigs individually instead of making a chr0")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
534
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
535 # Get the command line arguments
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
536 args = parser.parse_args()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
537 contigs_file = args.contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
538 reference_file = args.reference
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
539 #output_path = args.o
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
540 exclude_file = args.e
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
541 minimap_path = args.m
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
542 break_chimeras = args.b
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
543 gff_file = args.gff
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
544 min_break_pct = args.p
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
545 min_len = args.l
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
546 min_range = args.r
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
547 intra_wrt_ref_min = args.d
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
548 intra_wrt_ctg_min = args.c
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
549 t = args.t
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
550 g = args.g
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
551 call_svs = args.s
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
552 min_assemblytics = args.a
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
553 max_assemblytics = args.f
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
554 group_score_thresh = args.i
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
555 skip_file = args.j
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
556 corr_reads = args.R
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
557 corr_reads_tech = args.T
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
558 make_chr0 = not args.C
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
559
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
560 if corr_reads:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
561 log("Misassembly correction has been turned on. This automatically inactivates chimeric contig correction.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
562 break_chimeras = False
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
563
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
564 # Make sure that if -R, -T has been specified
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
565 if corr_reads and not corr_reads_tech:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
566 raise ValueError("'-T' must be provided when using -R.")
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
567
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
568 skip_ctg = []
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
569 if skip_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
570 with open(skip_file) as f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
571 for line in f:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
572 skip_ctg.append(line.rstrip())
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
573
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
574 current_path = os.getcwd()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
575 output_path = current_path + '/ragoo_output'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
576 if not os.path.exists(output_path):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
577 os.makedirs(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
578 os.chdir(output_path)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
579
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
580 # Run minimap2
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
581 cmd = '{} -k19 -w19 -t{} ../{} ../{} ' \
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
582 '> contigs_against_ref.paf 2> contigs_against_ref.paf.log'.format(minimap_path, t, reference_file, contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
583
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
584 if not os.path.isfile('contigs_against_ref.paf'):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
585 run(cmd)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
586
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
587 # Read in the minimap2 alignments just generated
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
588 log('Reading alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
589 alns = read_paf_alignments('contigs_against_ref.paf')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
590 alns = clean_alignments(alns, l=1000, in_exclude_file=exclude_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
591
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
592 # Process the gff file
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
593 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
594 log('Getting gff features')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
595 features = defaultdict(list)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
596 z = GFFReader('../' + gff_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
597 for i in z.parse_gff():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
598 features[i.seqname].append(i)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
599
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
600 # Break chimeras if desired
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
601 if break_chimeras:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
602 # Record how many contigs are broken
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
603 total_inter_broken = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
604 total_intra_broken = 0
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
605
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
606 alns = clean_alignments(alns, l=10000, in_exclude_file=exclude_file, uniq_anchor_filter=True)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
607 # Process contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
608 log('Getting contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
609 if contigs_file.endswith(".gz"):
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
610 contigs_dict = read_gz_contigs('../' + contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
611 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
612 contigs_dict = read_contigs('../' + contigs_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
613
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
614 log('Finding interchromosomally chimeric contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
615 all_chimeras = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
616 for i in alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
617 ref_parts = get_ref_parts(alns[i], min_len, min_break_pct, min_range)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
618 if len(ref_parts) > 1:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
619 all_chimeras[i] = ref_parts
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
620
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
621 log('Finding break points and breaking interchromosomally chimeric contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
622 break_intervals = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
623 for i in all_chimeras.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
624 break_intervals[i] = cluster_contig_alns(i, alns, all_chimeras[i], min_len)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
625
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
626 # If its just going to break it into the same thing, skip it.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
627 if len(break_intervals[i]) <= 1:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
628 continue
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
629
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
630 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
631 # If desired, ensure that breakpoints don't disrupt any gff intervals
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
632 break_intervals[i] = avoid_gff_intervals(break_intervals[i], features[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
633 features = update_gff(features, break_intervals[i], i)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
634
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
635 # Break contigs according to the final break points
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
636 contigs_dict = break_contig(contigs_dict, i, break_intervals[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
637 total_inter_broken += 1
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
638
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
639 # Next, need to re-align before finding intrachromosomal chimeras
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
640 # First, write out the interchromosomal chimera broken fasta
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
641 out_inter_fasta = contigs_file[:contigs_file.rfind('.')] + '.inter.chimera.broken.fa'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
642 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
643 out_gff = gff_file[:gff_file.rfind('.')] + '.inter.chimera_broken.gff'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
644 write_broken_files(contigs_dict, out_inter_fasta, features, out_gff)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
645 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
646 write_broken_files(contigs_dict, out_inter_fasta)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
647
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
648 # Next, realign the chimera broken contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
649 align_breaks('inter', minimap_path, reference_file, out_inter_fasta, t)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
650
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
651 # Now, use those new alignments for intrachromosomal chimeras
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
652 log('Reading interchromosomal chimera broken alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
653 inter_alns = read_paf_alignments('chimera_break/inter_contigs_against_ref.paf')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
654 inter_alns = clean_alignments(inter_alns, l=1000, in_exclude_file=exclude_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
655
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
656 log('Finding intrachromosomally chimeric contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
657 # Find intrachromosomally chimeric contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
658 for i in inter_alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
659 intra = get_intra_contigs(inter_alns[i], 15000, intra_wrt_ref_min, intra_wrt_ctg_min)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
660 if intra:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
661 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
662 intra_break_intervals = avoid_gff_intervals(intra[1], features[intra[0]])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
663 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
664 intra_break_intervals = intra[1]
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
665 # Check if the avoidance of gff intervals pushed the break point to the end of the contig.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
666 if intra_break_intervals[-1][0] == intra_break_intervals[-1][1]:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
667 continue
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
668
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
669 # break the contigs and update features if desired
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
670 contigs_dict = break_contig(contigs_dict, intra[0], intra_break_intervals)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
671 total_intra_broken += 1
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
672
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
673 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
674 features = update_gff(features, intra_break_intervals, intra[0])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
675
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
676 # Write out the intrachromosomal information
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
677 out_intra_fasta = contigs_file[:contigs_file.rfind('.')] + '.intra.chimera.broken.fa'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
678 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
679 out_intra_gff = gff_file[:gff_file.rfind('.')] + '.intra.chimera_broken.gff'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
680 write_broken_files(contigs_dict, out_intra_fasta, features, out_intra_gff)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
681 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
682 write_broken_files(contigs_dict, out_intra_fasta)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
683
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
684 # Re align the contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
685 # Next, realign the chimera broken contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
686 align_breaks('intra', minimap_path, reference_file, out_intra_fasta, t)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
687
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
688 # Read in alignments of intrachromosomal chimeras and proceed with ordering and orientation
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
689 log('Reading intrachromosomal chimera broken alignments')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
690 alns = read_paf_alignments('chimera_break/intra_contigs_against_ref.paf')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
691 alns = clean_alignments(alns, l=1000, in_exclude_file=exclude_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
692 contigs_file = '/ragoo_output/chimera_break/' + out_intra_fasta
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
693 log('The total number of interchromasomally chimeric contigs broken is %r' % total_inter_broken)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
694 log('The total number of intrachromasomally chimeric contigs broken is %r' % total_intra_broken)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
695
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
696 # Check if misassembly correction is turned on. This is mutually exclusive with chimeric contig correction
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
697 if corr_reads:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
698 # Align the raw reads to the assembly.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
699 log('Aligning raw reads to contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
700 align_reads(minimap_path, t, contigs_file, corr_reads, corr_reads_tech)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
701 log('Computing contig coverage')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
702 cov_map = ReadCoverage('ctg_alignments/reads_against_ctg.paf')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
703 alns = clean_alignments(alns, l=10000, in_exclude_file=exclude_file, uniq_anchor_filter=True, merge=True)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
704
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
705 # Get the initial candidate break points.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
706 candidate_breaks = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
707 for i in alns:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
708 candidates = alns[i].get_break_candidates()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
709 if candidates:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
710 candidate_breaks[i] = candidates
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
711
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
712 # Validate each breakpoint by checking for excessively high or low coverage
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
713 # Also, if a gff is provided, check to ensure that we don't break within a gff feature interval
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
714 val_candidate_breaks = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
715 for i in candidate_breaks:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
716 candidates = cov_map.check_break_cov(i, candidate_breaks[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
717 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
718 candidates = remove_gff_breaks(features[i], candidates)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
719 if candidates:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
720 val_candidate_breaks[i] = list(set(candidates))
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
721 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
722 features = update_misasm_features(features, val_candidate_breaks[i], i, cov_map.ctg_lens[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
723
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
724 # Break the contigs
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
725 if gff_file:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
726 out_misasm_gff = gff_file[:gff_file.rfind('.')] + '.misasm.broken.gff'
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
727 write_misasm_broken_ctgs(contigs_file, val_candidate_breaks, contigs_file[:contigs_file.rfind('.')], in_gff=features, in_gff_name=out_misasm_gff)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
728 else:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
729 write_misasm_broken_ctgs(contigs_file, val_candidate_breaks, contigs_file[:contigs_file.rfind('.')])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
730
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
731 # Align the broken contigs back to the reference
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
732 align_misasm_broken(contigs_file[:contigs_file.rfind('.')])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
733 alns = read_paf_alignments('ctg_alignments/contigs_brk_against_ref.paf')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
734 alns = clean_alignments(alns, l=1000, in_exclude_file=exclude_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
735 contigs_file = '/ragoo_output/ctg_alignments/' + contigs_file[:contigs_file.rfind('.')] + ".misasm.break.fa"
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
736
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
737 # Assign each contig to a corresponding reference chromosome.
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
738 log('Assigning contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
739 all_unique_contigs = dict()
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
740 for i in alns.keys():
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
741 all_unique_contigs[i] = UniqueContigAlignment(alns[i])
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
742
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
743 # Add to this the list of headers that did not make it
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
744 write_contig_clusters(all_unique_contigs, group_score_thresh, skip_ctg)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
745
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
746 log('Ordering and orienting contigs')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
747 order_orient_contigs(all_unique_contigs, alns)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
748
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
749 log('Creating pseudomolecules')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
750 create_pseudomolecules(contigs_file, all_unique_contigs, g, make_chr0)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
751
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
752 if call_svs:
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
753 log('Aligning pseudomolecules to reference')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
754 align_pms(minimap_path, t, reference_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
755
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
756 log('Getting structural variants')
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
757 get_SVs(min_assemblytics, max_assemblytics, reference_file)
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
758
b9a3aeb162ab Uploaded
dereeper
parents:
diff changeset
759 log('goodbye')