-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrapid_join.pl
executable file
·301 lines (254 loc) · 8.13 KB
/
rapid_join.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/software/bin/perl -w
# kj2 08.10.2020
# jmdw added in hap flag and changed output suffix for files
use strict;
use Bio::SeqIO;
use Getopt::Long;
no warnings 'uninitialized'; # turn that off for debugging
my $tpf;
my $fa;
my $out;
my $csvin;
my $hap;
my $help;
GetOptions (
"fa:s" => \$fa,
"tpf:s" => \$tpf,
"out:s" => \$out,
"csv:s" => \$csvin,
"hap" => \$hap,
"h" => \$help,
"help" => \$help,
);
if (($help) || (!$fa) || (!$tpf)) {
print "This script takes an original assembly fasta file, a one-line per chromosome pre-csv file and a TPF file generated with split.pl and creates the assembly from the TPF file\n";
print "Usage:\n";
print "perl join.pl -fa <fasta>\n";
print " -tpf <tpf>\n";
print " -csv <pre-csv>\n";
print " -out <outfile_fasta_prefix> \# unless specified the output will be written to <fasta>.curated.fasta\n";
print " -hap \# optional use only if generating haplotigs fasta\n";
print " -h/help # this message\n";
exit(0);
}
my $newout;
if ($out) {
$newout = $out;
}
else {
($newout) = ($fa =~ /(\S+)\.fa/);
$out = ${newout}.".intermediate.fa" unless ($out);
}
my $seqout;
if ($hap) {
$seqout = Bio::SeqIO->new(-format => 'fasta',
-file => "> ${newout}.additional_haplotigs.unscrubbed.fa");
}
else {
$seqout = Bio::SeqIO->new(-format => 'fasta',
-file => "> ${newout}.inter.fa");
}
# storing sequences
my %seqhash;
my $seqin = Bio::SeqIO->new('-format' => 'fasta',
'-file' => $fa);
while (my $seqobj = $seqin->next_seq()) {
die("ERROR: you are trying to run rapid_join.pl on a full curation file\n") if $seqobj->display_id =~/_ctg1/;
$seqhash{$seqobj->display_id} = $seqobj;
}
open(TPF,$tpf);
my $seqobj;
my $lastscaff;
my $seqstring;
my %seen;
my $no;
my $lastgap;
my %created;
my %assembly;
while (<TPF>) {
$no++;
my $line = $_;
next if $line =~/^\n/;
chomp;
# gaps
if (/^GAP/) {
# no gaps at start of component
die("Sequence can't begin with gap in line $no\n") unless ($lastscaff);
my $length;
if (/^GAP\s+\S+\s+(\d+)/) {
$length = $1;
}
else {
$length = 200;
}
$seqstring .= "N"x$length;
$lastgap++;
}
# sequence
else {
my ($undef,$ctg,$scaff,$ori) = split;
my ($ctgname,$start,$end) = ($ctg =~ /(\S+)\:(\d+)-(\d+)/);
# check whether scaffold name was used previously
die("ERROR: attempt to reuse scaffold name $scaff, line $no\n") if (exists $created{$scaff});
# check whether sequence is being reused
push @{$seen{$ctgname}}, [$start,$end];
# continuation of scaffold
if ($lastscaff &&($scaff eq $lastscaff)) {
undef $lastgap;
my $oldobj = $seqhash{$ctgname};
my $subseq;
eval {
$subseq = $oldobj->subseq($start,$end);
};
die("ERROR: $scaff:$start-$end does not exist\n") if ($@);
# revcom
if ($ori eq "MINUS") {
$subseq = reverse($subseq);
$subseq =~ tr/atcgATCG/tagcTAGC/;
}
$seqstring .= $subseq;
}
# new scaff
else {
# check whether there's a gap at start of last scaffold
die("ERROR: Previous component ended in gap, line ",$no-1,"\n") if ($lastgap);
# out with last
if ($lastscaff) {
$seqobj->seq($seqstring);
$seqobj->display_id($lastscaff);
$seqout->write_seq($seqobj);
$assembly{$seqobj->display_id($lastscaff)} = $seqobj;
$created{$lastscaff}= length($seqstring);
#print "Created $lastscaff with ", length($seqstring),"\n";
undef $seqobj;
undef $seqstring;
}
$seqobj = Bio::Seq->new();
my $oldobj = $seqhash{$ctgname};
$seqobj->display_id($scaff);
my $subseq = $oldobj->subseq($start,$end);
# revcom
if ($ori eq "MINUS") {
$subseq = reverse($subseq);
$subseq =~ tr/atcgATCG/tagcTAGC/;
}
$seqstring .= $subseq;
$lastscaff = $scaff;
undef $lastgap;
}
}
}
# out with last
$seqobj->seq($seqstring);
$seqobj->display_id($lastscaff);
$seqout->write_seq($seqobj);
$assembly{$seqobj->display_id($lastscaff)} = $seqobj;
$created{$lastscaff}= length($seqstring);
#print "Created $lastscaff with ", length($seqstring),"\n";
# check for overlaps
foreach my $ctg (keys %seen) {
my @sorted = sort {$a->[0] <=> $b->[0]} @{$seen{$ctg}};
my $max;
foreach my $aref (@sorted) {
my $start = $aref->[0];
my $end = $aref->[1];
my $tick = 0;
if ($max) {
if ($start < $max) {
$tick++;
}
elsif (($start < $max) && ($end > $max)) {
$tick++;
}
elsif ($end < $max) {
$tick++;
}
}
if ($tick > 0) {
die("ERROR: Sequence from ctg $ctg was used more than once, e.g. in region $start-$end\n");
}
$max = $end unless ($max && ($max > $end));
}
}
##############################
exit(0) unless ($csvin);
# creating csv file and renamed fasta file
my %chrom;
my %ind;
my %done;
open(CSV,$csvin);
open(CSVOUT,">${newout}.inter.csv");
open(CSVOUU,">${newout}.chromosome.list.csv");
my $secondout = Bio::SeqIO->new(-format => 'fasta',
-file => "> ${newout}.curated_primary.no_mt.unscrubbed.fa");
my $loc;
#open(TSVC,">${out}.tsv");
#open(TSVU,">${out}.unloc.tsv");
# gathering intel
while (<CSV>) {
chomp;
$loc++;
my $entryname = $loc;
my @a = split ",";
my $total;
my %part;
foreach my $a (@a) {
#print STDERR "searching for $a with $created{$a}\n";
$total += $created{$a};
$part{$a} = $created{$a};
$entryname = "Z" if ($a =~ /Z/);
$entryname = "W" if ($a =~ /W/);
$entryname = "X" if ($a =~ /X/);
$entryname = "Y" if ($a =~ /Y/);
}
$chrom{$entryname}->{total} = $total;
@{$chrom{$entryname}->{parts}} = (reverse sort {$part{$a} <=> $part{$b}} keys %part);
}
# sorting and naming by size
my $number;
my $seenx;
foreach my $c (reverse sort {$chrom{$a}->{total} <=> $chrom{$b}->{total}} keys %chrom) {
my $chrname;
#print STDERR "checking $c\n";
if ($c =~ /Z|W|Y|X/) {
$chrname = $c;
#print STDERR "found $c\n";
# check that X/Z are bigger than Y/W
if ($c =~ /Y|W/) {
warn("Error: Sex chromosomes might be the wrong way around as found Y/W not smaller than X/Z\n") unless ($seenx);
}
$seenx++ if ($c =~ /Z|X/);
}
else {
$number++;
$chrname = $number;
}
my $first = 1;
my $no = 1;
foreach my $sub (@{$chrom{$c}->{parts}}) {
die "ERROR: $sub (from $csvin) is not in the TPF ($tpf)\n " unless $assembly{$sub};
if ($first) {
print CSVOUT "$sub,$chrname,yes\n";
# print TSVC "$sub\t$chrname\tChromosome\n";
print CSVOUU "SUPER_${chrname},$chrname,yes\n";
my $newobj = $assembly{$sub};
$newobj->display_id("SUPER_${chrname}");
$secondout->write_seq($newobj);
$done{$sub}++;
}
else {
print CSVOUT "$sub,$chrname,no\n";
# print TSVU "$sub\t$chrname\n";
print CSVOUU "SUPER_${chrname}_unloc_$no,$chrname,no\n";
my $newobj = $assembly{$sub};
$newobj->display_id("SUPER_${chrname}_unloc_$no");
$secondout->write_seq($newobj);
$done{$sub}++;
$no++;
}
undef $first;
}
}
foreach my $key (keys %assembly) {
$secondout->write_seq($assembly{$key}) unless (exists $done{$key});
}