-
Notifications
You must be signed in to change notification settings - Fork 26
/
make_nr_pangenome_matrix.pl
executable file
·752 lines (631 loc) · 24.4 KB
/
make_nr_pangenome_matrix.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#!/usr/bin/env perl
# 2017-22 Bruno Contreras-Moreira (1) and Pablo Vinuesa (2):
# 1: http://www.eead.csic.es/compbio (Laboratory of Computational Biology, EEAD/CSIC/Fundacion ARAID, Spain)
# 2: http://www.ccg.unam.mx/~vinuesa (Center for Genomic Sciences, UNAM, Mexico)
# This script computes a non-redundant pangenome matrix by comparing clusters with BLAST
# Takes as input the name of a pangenome matrix generated by compare_clusters.pl, other parameters
# are documented when running the script without args or with -h option
$|=1;
use strict;
use warnings;
use Getopt::Std;
use File::Basename;
use File::Spec;
use FindBin '$Bin';
use lib "$Bin/lib";
use lib "$Bin/lib/bioperl-1.5.2_102/";
use phyTools; #imports constants SEQ,NAME used as array subindices
use marfil_homology;
## list of features/binaries required by this program (do not edit)
my @FEATURES2CHECK = ('EXE_BLASTP','EXE_BLASTN','EXE_FORMATDB','EXE_SPLITBLAST');
## settings for local batch blast jobs
my $BATCHSIZE = 100;
my $ONLYBESTREFHIT = 0; # take only best hit among reference hits
my ($INP_matrix,$INP_taxa,$INP_length,$INP_nucleotides,%opts) = ('',1,0,1);
my ($INP_identity,$INP_cover,$INP_reference_cover,$INP_reference_identity) = (90,75,50,50);
my ($INP_reference_cluster_dir,$INP_reference_file,$INP_use_short_sequence) = ('','',0);
my ($n_of_cpus) = ($BLAST_NOCPU);
getopts('hPes:c:f:r:n:t:m:l:C:S:', \%opts);
if(($opts{'h'})||(scalar(keys(%opts))==0))
{
print "\n[options]: \n";
print "-h \t this message\n";
print "-m \t input pangenome matrix .tab (required, made by compare_clusters.pl)\n";
print "-t \t consider only clusters with at least t taxa (optional, default:$INP_taxa)\n";
print "-l \t min mean sequence length per cluster (optional, default:$INP_length)\n";
print "-S \t \%sequence identity cutoff (optional, default:$INP_identity)\n";
print "-C \t \%alignment coverage cutoff (optional, default:$INP_cover,\n";
print " \t calculates coverage over longest sequence)\n";
print "-P \t sequences are peptides (optional, by default expects nucleotides)\n";
print "-n \t number of threads for BLAST jobs (optional, default:$n_of_cpus)\n";
print "-e \t sequences are (probably) incomplete ESTs (optional, calculates coverage over shortest)\n";
print "\nParameters used to match input clusters to a reference:\n";
print "-r \t reference clusters contained in directory (optional, example: -r cross_species_clusters)\n";
print "-f \t parse sequences in this file (optional, ignores -r; example: -f swissprot.faa)\n";
print "-s \t \%sequence identity cutoff for matching reference seqs (optional, default:$INP_reference_identity)\n";
print "-c \t \%alignment coverage cutoff for matching reference seqs (optional, default:$INP_reference_cover,\n";
print " \t calculates coverage over longest sequence)\n";
exit(-1);
}
print check_installed_features(@FEATURES2CHECK);
if(defined($opts{'m'})){ $INP_matrix = $opts{'m'} }
else{ die "\n# $0 : need -m parameter, exit\n"; }
if(defined($opts{'t'}) && $opts{'t'} > 0)
{
$INP_taxa = $opts{'t'};
}
if(defined($opts{'l'}) && $opts{'l'} > 0)
{
$INP_length = $opts{'l'};
}
if(defined($opts{'C'}) && $opts{'C'} > 0 && $opts{'C'} <= 100)
{
$INP_cover = $opts{'C'};
}
if(defined($opts{'S'}) && $opts{'S'} > 0 && $opts{'S'} <= 100)
{
$INP_identity = $opts{'S'};
}
if(defined($opts{'P'}))
{
$INP_nucleotides = 0;
}
if(defined($opts{'n'}) && $opts{'n'} > 0)
{
$n_of_cpus = int($opts{'n'});
$BLAST_NOCPU = $n_of_cpus;
}
if(defined($opts{'f'}) || defined($opts{'r'}))
{
if($opts{'f'} && -s $opts{'f'}){ $INP_reference_file = $opts{'f'} }
elsif(defined($opts{'r'}) && -d $opts{'r'}){ $INP_reference_cluster_dir = $opts{'r'} }
else
{
print "\n# ignoring reference...\n\n";
}
if(defined($opts{'c'}) && $opts{'c'} > 0 && $opts{'c'} <= 100)
{
$INP_reference_cover = $opts{'c'};
}
if(defined($opts{'s'}) && $opts{'s'} > 0 && $opts{'s'} <= 100)
{
$INP_reference_identity = $opts{'s'};
}
}
if(defined($opts{'e'}))
{
$INP_use_short_sequence = 1;
}
print "\n# ONLYBESTREFHIT=$ONLYBESTREFHIT\n";
printf("\n# %s -m %s -t %d -l %d -C %d -S %d -P %s -f %s -r %s -c %d -s %d -e %d -n %d\n\n",
$0,$INP_matrix,$INP_taxa,$INP_length,$INP_cover,$INP_identity,!$INP_nucleotides,
$INP_reference_file,$INP_reference_cluster_dir,
$INP_reference_cover,$INP_reference_identity,
$INP_use_short_sequence,$n_of_cpus);
#################################### MAIN PROGRAM ################################################
my (@cluster_names,@header,@taxa,@length,@filtered,@median_seq,@id2name,@trash);
my (%pangemat,%redundant,%nr,%clust2col,%median_length,%matched_clusters,%ref_match);
my ($n_of_clusters,$filtered_clusters,$merged_clusters,$taxon,$cluster_name,$command) = (0,0,0);
my ($col2,$col3,$seq,$size,$median,$l,$col,$cluster_dir,$red_name,$pQcov,$pScov);
my ($pQid,$querylen,$pSid,$subjectlen,$pEvalue,$ppercID,$simspan,$pbits,$cover);
my $outfile_root = $INP_matrix; $outfile_root =~ s/\.tab//g;
$outfile_root .= "_nr_t$INP_taxa\_l$INP_length\_e$INP_use_short_sequence\_C$INP_cover\_S$INP_identity";
my $nr_pangenome_fasta_file = $outfile_root;
my $nr_pangenome_blast_file = $outfile_root . '.blast';
my $nr_pangenome_bpo_file = $outfile_root . '.bpo';
my $nr_pangenome_file = $outfile_root.'.tab';
my $nr_pangenome_log_file = $outfile_root.'.log';
if($INP_reference_file || $INP_reference_cluster_dir)
{
$nr_pangenome_file = $outfile_root."_ref_c$INP_reference_cover\_s$INP_reference_identity.tab";
}
my ($nr_pangenome_reference_fasta_file,$nr_pangenome_reference_blast_file);
my ($nr_pangenome_reference_bpo_file,$nr_pangenome_tmp_fasta_file);
if($INP_nucleotides)
{
$nr_pangenome_fasta_file .= '.fna';
push(@trash,$nr_pangenome_fasta_file.'.nhr',
$nr_pangenome_fasta_file.'.nin',
$nr_pangenome_fasta_file.'.nsq');
}
else
{
$nr_pangenome_fasta_file .= '.faa';
push(@trash,$nr_pangenome_fasta_file.'.phr',
$nr_pangenome_fasta_file.'.pin',
$nr_pangenome_fasta_file.'.psq');
}
unlink($nr_pangenome_fasta_file,$nr_pangenome_file);
## 1) parse pangenome matrix
open(MAT,$INP_matrix) || die "# EXIT : cannot read $INP_matrix\n";
while(<MAT>)
{
next if(/^#/ || /^$/);
chomp;
my @data = split(/\t/,$_);
if($data[0] =~ /^source:(\S+)/) #source:path/to/clusters/t101964_thrB.fna/t101965_thrC.fna/t...
{
$cluster_dir = $1;
foreach $col (1 .. $#data){ $cluster_names[$col] = $data[$col] }
$n_of_clusters = $#data;
}
else #_Escherichia_coli_ETEC_H10407_uid42749.gbk 1 1 ...
{
if(!grep(/^$data[0]$/,@taxa)){ push(@taxa,$data[0]) }
foreach $col (1 .. $#data)
{
$pangemat{$data[0]}[$col] = $data[$col];
}
}
}
close(MAT);
print "# input matrix contains $n_of_clusters clusters and ".scalar(keys(%pangemat))." taxa\n";
## 2) filter clusters by median sequence length
print "\n# filtering clusters ...\n";
foreach $col (1 .. $n_of_clusters)
{
my ($present_taxa) = (0);
foreach $taxon (@taxa)
{
if($pangemat{$taxon}[$col]){ $present_taxa++ }
}
next if($present_taxa < $INP_taxa);
# calculate median length of cluster sequences
$size = 0;
$cluster_name = $cluster_names[$col];
my $fasta_ref = read_FASTA_file_array( $cluster_dir.'/'.$cluster_name );
if(!@$fasta_ref){
die "# ERROR: could not read $cluster_dir/$cluster_name, ".
"please check your matrix and the path to clusters therein\n"
}
my @cluster_l;
foreach $seq ( 0 .. $#{$fasta_ref} )
{
if($INP_nucleotides && $fasta_ref->[$seq][SEQ] =~ /^[^ACGTWSRYKMXN\-\s]+$/i)
{
print "# ERROR: cluster $cluster_dir/$cluster_name contains protein sequences, exit\n";
exit;
}
push(@cluster_l,length($fasta_ref->[$seq][SEQ]));
$size++;
}
@cluster_l = sort {$a<=>$b} @cluster_l;
$median = $cluster_l[int($size/2)];
foreach $seq ( 0 .. $#{$fasta_ref} )
{
$l = length($fasta_ref->[$seq][SEQ]);
if($l == $median)
{
$median_seq[$col] = $fasta_ref->[$seq][SEQ];
$header[$col] = $fasta_ref->[$seq][NAME];
$length[$col] = $l;
print "# cluster $col\r";
last;
}
}
next if($length[$col] < $INP_length);
$filtered_clusters++;
push(@filtered,$col);
}
print "# $filtered_clusters clusters with taxa >= $INP_taxa and sequence length >= $INP_length\n";
## 3) sort clusters by length, number them and get representative sequence from each
print "\n# sorting clusters and extracting median sequence ...\n";
$filtered_clusters = 0;
open(FASTA,">$nr_pangenome_fasta_file") || die "# ERROR : cannot create $nr_pangenome_fasta_file\n";
foreach $col (sort {$length[$b] <=> $length[$a]} @filtered)
{
$filtered_clusters++;
$clust2col{$filtered_clusters} = $col;
print FASTA ">$filtered_clusters $col $cluster_names[$col]\n$median_seq[$col]\n";
$median_length{$filtered_clusters} = $length[$col];
}
close(FASTA);
## 4) format cluster sequences and run BLAST
if(!-s $nr_pangenome_blast_file)
{
if($INP_nucleotides)
{
executeFORMATDB($nr_pangenome_fasta_file,1);
if(!-s $nr_pangenome_fasta_file . '.nsq')
{
die "# EXIT: cannot format BLAST nucleotide sequence base $nr_pangenome_fasta_file\n";
}
$command = format_BLASTN_command($nr_pangenome_fasta_file,$nr_pangenome_blast_file,
$nr_pangenome_fasta_file,$BLAST_PVALUE_CUTOFF_DEFAULT,2,1,'megablast');
}
else
{
executeFORMATDB($nr_pangenome_fasta_file);
if(!-s $nr_pangenome_fasta_file . '.psq')
{
die "# EXIT: cannot format BLAST protein sequence base $nr_pangenome_fasta_file\n";
}
$command = format_BLASTP_command($nr_pangenome_fasta_file,$nr_pangenome_blast_file,
$nr_pangenome_fasta_file,$BLAST_PVALUE_CUTOFF_DEFAULT,2,1);
}
$command = format_SPLITBLAST_command()."$BATCHSIZE $command > /dev/null";
system("$command");
if($? != 0)
{
die "# EXIT: failed while running BLAST search ($command)\n";
}
}
else
{
print "# re-using previous BLAST output $nr_pangenome_blast_file\n";
}
## 5) merge clusters based on BLAST similarities
blast_parse($nr_pangenome_blast_file,$nr_pangenome_bpo_file,\%median_length,$BLAST_PVALUE_CUTOFF_DEFAULT,1);
unlink($nr_pangenome_fasta_file);
open(LOG,">",$nr_pangenome_log_file) || die "# ERROR : cannot create $nr_pangenome_log_file, cannot proceed\n";
open(BPO,$nr_pangenome_bpo_file) || die "# ERROR : cannot find $nr_pangenome_bpo_file, cannot proceed\n";
while(<BPO>)
{
#$similarityid;$pQid;$querylen;$pSid;$subjectlen;$pEvalue;$ppercID;$simspan;$pbits
#16;2;274;37937;274;1e-103;66;1:5-272:6-274.;375
#313 313 100.00 291 0 0 1 291 1 291 0.0 590
#313 93 26.69 236 166 4 40 273 279 509 6e-21 95.5
($pQid,$pSid,$pEvalue,$ppercID,$pQcov,$pScov,$querylen,$subjectlen,$simspan,$pbits) = split(/\t/,$_);
# redundant clusters are $pQid > $pSid, as seqs are length-sorted
next if($pQid <= $pSid);
next if($ppercID < $INP_identity);
if($INP_use_short_sequence)
{
if($querylen < $subjectlen){ $cover = $pQcov }
else{ $cover = $pScov }
}
else
{
if($querylen > $subjectlen){ $cover = $pQcov }
else{ $cover = $pScov }
}
if($cover >= $INP_cover)
{
# record parent cluster and merge
# Note: cluster ids are converted to columns
$redundant{$clust2col{$pQid}}++;
push(@{$nr{$clust2col{$pSid}}},$clust2col{$pQid});
# log redundant clusters
print LOG "$pQid ($cluster_names[$clust2col{$pQid}]) is redundant with $pSid ".
"($cluster_names[$clust2col{$pSid}]) : $ppercID $cover\n";
}
}
close(BPO);
close(LOG);
open(NR,">$nr_pangenome_fasta_file") ||
die "# ERROR : cannot create $nr_pangenome_fasta_file\n";
foreach $col (@filtered)
{
next if($redundant{$col});
print NR ">$cluster_names[$col] $header[$col]\n$median_seq[$col]\n";
$merged_clusters++;
}
close(NR);
print "\n# $merged_clusters non-redundant clusters\n";
print "# created: $nr_pangenome_fasta_file\n";
print "# see log: $nr_pangenome_log_file\n";
## 6) if required match nr clusters to external reference sequences
if($INP_reference_file || $INP_reference_cluster_dir)
{
my ($ref_cluster_id,$id,$n_of_ref_sequences);
my (@ref_clusterfiles,%ref_length,%ref_seen,@ref_cluster_size);
$nr_pangenome_reference_fasta_file = $outfile_root;
$nr_pangenome_reference_blast_file = $outfile_root.'_ref.blast';
$nr_pangenome_reference_bpo_file = $outfile_root.'_ref.bpo';
$nr_pangenome_tmp_fasta_file = $outfile_root.'_nr';
if($INP_reference_file) # reference sequences in a single FASTA file
{
if($INP_nucleotides)
{
$nr_pangenome_reference_fasta_file .= '_ref.fna';
$nr_pangenome_tmp_fasta_file .= '.fna';
push(@trash,$nr_pangenome_reference_fasta_file.'.nhr',
$nr_pangenome_reference_fasta_file.'.nin',
$nr_pangenome_reference_fasta_file.'.nsq');
}
else
{
$nr_pangenome_reference_fasta_file .= '_ref.faa';
$nr_pangenome_tmp_fasta_file .= '.faa';
push(@trash,$nr_pangenome_reference_fasta_file.'.phr',
$nr_pangenome_reference_fasta_file.'.pin',
$nr_pangenome_reference_fasta_file.'.psq');
}
# save reference sequences in a single FASTA file to be scanned by BLAST
open(REF,">$nr_pangenome_reference_fasta_file") || die "# ERROR : cannot create $nr_pangenome_reference_fasta_file\n";
$n_of_ref_sequences = 0;
my $fasta_ref = read_FASTA_file_array($INP_reference_file);
foreach $seq ( 0 .. $#{$fasta_ref} )
{
$id = $n_of_ref_sequences.'_'.$n_of_ref_sequences;
print REF ">$id $fasta_ref->[$seq][NAME]\n$fasta_ref->[$seq][SEQ]\n";
# check nr sequences and reference sequences are compatible
if( ($INP_nucleotides && $fasta_ref->[$seq][SEQ] =~ m/([PEQILF])/) ||
(!$INP_nucleotides && $fasta_ref->[$seq][SEQ] !~ m/[PEQILF]/) )
{
print "# both input clusters and references must be either nucleotides or peptides\n";
print "# offending sequence ($1): \n".substr($fasta_ref->[$seq][SEQ],0,10)."...\n";
exit;
}
$ref_length{$id} = length($fasta_ref->[$seq][SEQ]);
$ref_cluster_size[$n_of_ref_sequences] = 1;
$id2name[$n_of_ref_sequences] = $fasta_ref->[$seq][NAME];
#print "$ref_cluster_id $fasta_ref->[$seq][NAME]\n"; 66321 ChrSy.fgenesh.mRNA.73 cDNA|expressed protein
$n_of_ref_sequences++;
}
close(REF);
if(!$n_of_ref_sequences++)
{
die "# EXIT : cannot parse sequences in $INP_reference_file\n";
}
else
{
printf("\n# %d reference sequences parsed in %s\n",$n_of_ref_sequences,$INP_reference_file);
}
# create a temporary FASTA file with the previosuly computed nr cluster sequences
open(NR,">$nr_pangenome_tmp_fasta_file") || die "# ERROR : cannot create $nr_pangenome_tmp_fasta_file\n";
foreach $col (@filtered)
{
next if($redundant{$col});
print NR ">$col $cluster_names[$col]\n$median_seq[$col]\n";
}
close(NR);
}
elsif($INP_reference_cluster_dir) # pre-built reference clusters, each in one FASTA file
{
opendir(CLDIR,$INP_reference_cluster_dir) || die "# EXIT : cannot list $INP_reference_cluster_dir\n";
if($INP_nucleotides)
{
@ref_clusterfiles = sort grep {/\.fna$/i} readdir(CLDIR);
$nr_pangenome_reference_fasta_file .= '_ref.fna';
$nr_pangenome_tmp_fasta_file .= '.fna';
push(@trash,$nr_pangenome_reference_fasta_file.'.nhr',
$nr_pangenome_reference_fasta_file.'.nin',
$nr_pangenome_reference_fasta_file.'.nsq');
}
else
{
@ref_clusterfiles = sort grep {/\.faa$/i} readdir(CLDIR);
$nr_pangenome_reference_fasta_file .= '_ref.faa';
$nr_pangenome_tmp_fasta_file .= '.faa';
push(@trash,$nr_pangenome_reference_fasta_file.'.phr',
$nr_pangenome_reference_fasta_file.'.pin',
$nr_pangenome_reference_fasta_file.'.psq');
}
closedir(CLDIR);
if(!@ref_clusterfiles)
{
die "# EXIT : cannot find .fna/.faa files in $INP_reference_cluster_dir\n";
}
else
{
printf("\n# %d reference clusters found in %s\n",scalar(@ref_clusterfiles),$INP_reference_cluster_dir);
}
# save all reference cluster sequences in a single FASTA file to be scanned by BLAST
open(REF,">$nr_pangenome_reference_fasta_file") || die "# ERROR : cannot create $nr_pangenome_reference_fasta_file\n";
$ref_cluster_id = $n_of_ref_sequences = 0;
foreach my $clusterfile (@ref_clusterfiles)
{
my $fasta_ref = read_FASTA_file_array($INP_reference_cluster_dir.'/'.$clusterfile);
foreach $seq ( 0 .. $#{$fasta_ref} )
{
$id = $ref_cluster_id.'_'.$n_of_ref_sequences;
#print REF ">$id $clusterfile $fasta_ref->[$seq][NAME]\n$fasta_ref->[$seq][SEQ]\n"; # might cause trouble with BLAST
print REF ">$id $clusterfile\n$fasta_ref->[$seq][SEQ]\n";
# check nr sequences and reference sequences are compatible
if( ($INP_nucleotides && $fasta_ref->[$seq][SEQ] =~ m/[PEQILF]/) ||
(!$INP_nucleotides && $fasta_ref->[$seq][SEQ] !~ m/[PEQILF]/) )
{
print "# both input clusters and references must be either nucleotides or peptides\n";
print "# offending sequence: \n$fasta_ref->[$seq][SEQ]\n";
exit;
}
$ref_length{$id} = length($fasta_ref->[$seq][SEQ]);
$n_of_ref_sequences++;
$ref_cluster_size[$ref_cluster_id]++;
}
$id2name[$ref_cluster_id] = $clusterfile;
$ref_cluster_id++;
}
close(REF);
# create a temporary FASTA file with the previously computed nr cluster sequences
open(NR,">$nr_pangenome_tmp_fasta_file") || die "# ERROR : cannot create $nr_pangenome_tmp_fasta_file\n";
foreach $col (@filtered)
{
next if($redundant{$col});
print NR ">$col $cluster_names[$col]\n$median_seq[$col]\n";
}
close(NR);
}
# run BLAST
if(!-s $nr_pangenome_reference_blast_file)
{
if($INP_nucleotides) # nucl to nucl
{
executeFORMATDB($nr_pangenome_reference_fasta_file,1);
if(!-s $nr_pangenome_reference_fasta_file . '.nsq')
{
die "# EXIT: cannot format BLAST nucleotide sequence base $nr_pangenome_reference_fasta_file\n";
}
$command = format_BLASTN_command($nr_pangenome_tmp_fasta_file,$nr_pangenome_reference_blast_file,
$nr_pangenome_reference_fasta_file,$BLAST_PVALUE_CUTOFF_DEFAULT,500,1,'megablast');
}
else # prot to prot
{
executeFORMATDB($nr_pangenome_reference_fasta_file);
if(!-s $nr_pangenome_reference_fasta_file . '.psq')
{
die "# EXIT: cannot format BLAST peptide sequence base $nr_pangenome_reference_fasta_file\n";
}
$command = format_BLASTP_command($nr_pangenome_tmp_fasta_file,$nr_pangenome_reference_blast_file,
$nr_pangenome_reference_fasta_file,$BLAST_PVALUE_CUTOFF_DEFAULT,500,1);
}
$command = format_SPLITBLAST_command()."$BATCHSIZE $command > /dev/null"; #die $command;
system("$command");
if($? != 0)
{
die "# EXIT: failed while running BLAST search ($command)\n";
}
}
else
{
print "# re-using previous BLAST output $nr_pangenome_reference_blast_file\n";
print "\n# WARNING: remove this file if trying different reference sequences\n";
}
blast_parse($nr_pangenome_reference_blast_file,$nr_pangenome_reference_bpo_file,
\%ref_length,$BLAST_PVALUE_CUTOFF_DEFAULT,1);
print "\n# matching nr clusters to reference (\%alignment coverage cutoff=$INP_reference_cover) ...\n";
open(BPO,$nr_pangenome_reference_bpo_file) ||
die "# ERROR : cannot find $nr_pangenome_reference_bpo_file, cannot proceed\n";
while(<BPO>)
{
# col -> nr sequence, actually a column of the pangenome matrix
# pSid -> reference sequence
# 1,49439_49439
($col,$pSid,$pEvalue,$ppercID,$pQcov,$pScov,$querylen,$subjectlen,$simspan,$pbits) = split(/\t/,$_);
#print if($col eq '890'); # debugging
next if($ref_match{$col} && $ONLYBESTREFHIT);
if($INP_use_short_sequence)
{
if($querylen < $subjectlen){ $cover = $pQcov }
else{ $cover = $pScov }
}
else
{
if($querylen > $subjectlen){ $cover = $pQcov }
else{ $cover = $pScov }
}
if($cover >= $INP_reference_cover && $ppercID >= $INP_reference_identity)
{
$ref_cluster_id = (split(/_/,$pSid))[0]; #print "$pQid $pSid $ref_cluster_id $cover\n";
next if($ref_seen{$ref_cluster_id}{$col});
$ref_match{$col} .= "$ref_cluster_id,";
$ref_seen{$ref_cluster_id}{$col}=1;
# add this cluster sequences' to the right pangenome matrix column
$matched_clusters{$col} += $ref_cluster_size[$ref_cluster_id];
}
}
close(BPO);
printf("# %d nr clusters matched by %d reference sequences/clusters\n\n",
scalar(keys(%matched_clusters)),scalar(keys(%ref_seen)));
}
## 7) print nr pangenome matrix
print "\n# printing nr pangenome matrix ...\n";
my ($num_red, @red_cols, %recursive_red_cols);
open(NRMAT,">$nr_pangenome_file");
print NRMAT "non-redundant";
foreach $col (@filtered)
{
next if($redundant{$col});
$cluster_name = $cluster_names[$col];
@red_cols = ();
if($nr{$col})
{
# recursively compile list of redundant clusters
@red_cols = ($col);
$num_red = get_redundant_cols(1, \@red_cols, \%nr);
while($num_red > 0) {
$num_red = get_redundant_cols($num_red, \@red_cols, \%nr);
}
push(@{ $recursive_red_cols{$col} }, @red_cols);
# concat number of redundant clusters
$cluster_name .= sprintf("+%d",scalar(@red_cols)-1);
}
print NRMAT "\t$cluster_name";
} print NRMAT "\t\n";
foreach $taxon (@taxa)
{
print NRMAT "$taxon";
foreach $col (@filtered)
{
next if($redundant{$col});
$size = $pangemat{$taxon}[$col];
if($nr{$col})
{
foreach $col2 (@{$nr{$col}}){ $size += $pangemat{$taxon}[$col2] }
}
print NRMAT "\t$size";
} print NRMAT "\t\n";
}
# add reference clusters row if required
if($INP_reference_cluster_dir)
{
print NRMAT "reference:$INP_reference_cluster_dir";
foreach $col (@filtered)
{
next if($redundant{$col});
$size = $matched_clusters{$col} || 0;
print NRMAT "\t$size";
} print NRMAT "\t\n";
}
print NRMAT "redundant";
foreach $col (@filtered)
{
next if($redundant{$col});
if($nr{$col})
{
# concatenate names of redundant clusters
$red_name = '';
foreach $col2 (@{$recursive_red_cols{$col}} ){
$red_name .= "$cluster_names[$col2],";
}
}
else{ $red_name = 'NA' }
print NRMAT "\t$red_name";
} print NRMAT "\t\n";
# add reference clusters row of additional data if required
if($INP_reference_file || $INP_reference_cluster_dir)
{
print NRMAT "reference";
foreach $col (@filtered)
{
next if($redundant{$col});
$cluster_name = '';
if($matched_clusters{$col})
{
foreach my $cl (split(/,/,$ref_match{$col}))
{
$cluster_name .= "$id2name[$cl],";
}
}
else{ $cluster_name = 'NA' }
print NRMAT "\t$cluster_name";
} print NRMAT "\t\n";
}
close(NRMAT);
print "# created: $nr_pangenome_file\n";
print "\n# NOTE: matrix can be transposed for convenience with:\n\n";
print <<'TRANS';
perl -F'\t' -ane '$F[$#F]=~s/\n//g;$r++;for(1 .. @F){$m[$r][$_]=$F[$_-1]};$mx=@F;END{for(1 .. $mx){for $t(1 .. $r){print"$m[$t][$_]\t"}print"\n"}}' \
TRANS
print " $nr_pangenome_file\n";
## 8) clean tmp blast files (comment while debugging)
unlink($nr_pangenome_bpo_file,@trash);
if($INP_reference_cluster_dir)
{
unlink($nr_pangenome_reference_fasta_file,$nr_pangenome_reference_bpo_file,$nr_pangenome_tmp_fasta_file);
}
###########################################
# Takes 3 args:
# i) number of elements to take from the end of ii)
# ii) ref to list with elements, new redundant ones pushed to end
# iii) ref to hash of lists of redundant elements
sub get_redundant_cols
{
my ($lastn, $ref_list, $ref_hash) = @_;
my ($new_redundants, $index, $elem, $elem2) = (0);
my $first = scalar(@$ref_list)-($lastn+1);
if($first < 0){ $first = 0 }
my $last = scalar(@$ref_list)-1;
foreach $index ($first .. $last)
{
$elem = $ref_list->[$index];
foreach $elem2 (@{ $ref_hash->{$elem} })
{
next if(grep(/^$elem2$/,@$ref_list));
push(@$ref_list, $elem2);
$new_redundants++;
}
}
return $new_redundants;
}