annotate PanExplorer_workflow/Perl/ProjectPAVinCircos.pl @ 1:032f6b3806a3 draft

Uploaded
author dereeper
date Thu, 30 May 2024 11:16:08 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
2
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
3 use strict;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
4
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
5 my $pav = $ARGV[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
6 my $ref = $ARGV[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
7 my $strain_info = $ARGV[2];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
8 my $gff = $ARGV[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
9 my $ordering = $ARGV[4];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
10 my $outfile = $ARGV[5];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
11
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
12 my $circos_exe = "circos/bin/circos";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
13 #my $circos_exe = "circos";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
14
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
15 my %metadata;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
16 open(M,$strain_info);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
17 while(my $line = <M>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
18 chomp($line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
19 my ($id,$name) = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
20 $metadata{$id} = $name;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
21 $metadata{$name} = $id;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
22 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
23 close(M);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
24
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
25
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
26 my $pangenome_size = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
27 my $nb_core_genes = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
28 my $nb_dispensable_genes = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
29 my $nb_specific_genes = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
30 my %cluster_of_gene;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
31 my %genes_of_cluster;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
32 my %tagging;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
33 open(P,$pav);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
34 my $firstline = <P>;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
35 chomp($firstline);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
36 my @infos = split(/\t/,$firstline);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
37 my %samples;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
38 my %hash_presence;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
39 my $col;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
40 for (my $j = 1; $j <= $#infos; $j++){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
41 my $val = $infos[$j];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
42 if ($val eq $ref){$col = $j;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
43 $samples{$j} = $val;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
44 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
45 while(my $line = <P>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
46 chomp($line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
47 my @infos = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
48 my $cluster_num = $infos[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
49 my $clnb = $infos[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
50 my $pos = $clnb;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
51 my $pos_before = $pos - 1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
52 $pangenome_size++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
53 if (length($clnb) == 1){$clnb = "000".$clnb;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
54 elsif (length($clnb) == 2){$clnb = "00".$clnb;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
55 elsif (length($clnb) == 3){$clnb = "0".$clnb;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
56 my $name = "CLUSTER".$clnb;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
57
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
58 # exclude lines where several genes for one strains
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
59 if ($line =~/,/){next;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
60
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
61 my $gene_ref = $infos[$col];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
62 for (my $j = 1; $j <= $#infos; $j++){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
63 my $gene = $infos[$j];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
64 my $sample = $samples{$j};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
65 if ($gene =~/\w+/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
66 $hash_presence{$gene_ref}{$sample} = 1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
67 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
68 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
69 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
70 close(P);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
71
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
72 my $track_thin = 0.5 / scalar keys(%samples);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
73 my $r0 = 0.4;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
74 my $r1 = $r0+$track_thin;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
75 my $highlight_block = "";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
76 my %max_position;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
77 open(O,$ordering);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
78 <O>;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
79 while(my $sample_name = <O>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
80 #foreach my $sample(keys(%samples)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
81 chomp($sample_name);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
82 #my $sample_name = $samples{$sample};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
83 my $sample_id = $metadata{$sample_name};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
84 print "$sample_name $sample_id\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
85 my $ref_id = $metadata{$ref};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
86 open(GFF,$gff);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
87 open(CIRCOS_TRACK,">$outfile.$sample_id.circos.heatmap.txt");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
88 my $current_start;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
89 my $current_stop;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
90 my $current_chrom;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
91 $r0+=$track_thin;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
92 $r1+=$track_thin;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
93 #my $r1_final = $r1-($track_thin/3);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
94 my $r1_final = $r1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
95 $r0.="r";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
96 $r1_final.="r";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
97 $highlight_block .= qq~
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
98 <highlight>
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
99 file = $outfile.$sample_id.circos.heatmap.txt
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
100 r0 = $r0
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
101 r1 = $r1_final
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
102 </highlight>
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
103 ~;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
104 while(my $line = <GFF>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
105 chomp($line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
106 my @infos = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
107 if ($infos[2] eq "gene" && $line =~/ID=([^;]+);*/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
108 my $chr = $infos[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
109 my $start = $infos[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
110 my $end = $infos[4];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
111 $current_start = $start;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
112 $current_stop = $end;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
113 $current_chrom = $chr;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
114 if ($end > $max_position{$current_chrom}){$max_position{$current_chrom} = $end;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
115 my $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
116 my $genelength = $end-$start;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
117 my $gene_complete = $sample_name.":".$gene;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
118 if ($hash_presence{$gene}{$sample_name}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
119 print CIRCOS_TRACK "$current_chrom $current_start $current_stop fill_color=red\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
120 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
121 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
122 if ($infos[2] eq "CDS" && $line =~/Parent=([^;]+);*/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
123 my $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
124 my $gene_complete = $sample_name.":".$gene;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
125 if ($line =~/protein_id=([^;]+);/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
126 $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
127 if ($hash_presence{$gene}{$sample_name}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
128 print CIRCOS_TRACK "$current_chrom $current_start $current_stop fill_color=red\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
129 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
130 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
131 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
132 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
133 close(GFF);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
134 close(CIRCOS_TRACK);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
135 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
136 open(KARYOTYPE,">$outfile.karyotype.txt");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
137 foreach my $chr(keys(%max_position)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
138 my $max = $max_position{$chr};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
139 print KARYOTYPE "chr - $chr 1 0 $max black";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
140 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
141 close(KARYOTYPE);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
142
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
143 open(F,"circos_templates/circos1.conf");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
144 open(CIRCOS_CONF,">$outfile.circos1.conf");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
145 while(<F>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
146 if (/HIGHLIGHT_BLOCK/){print CIRCOS_CONF $highlight_block;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
147 elsif (/KARYOTYPE_PATH/){print CIRCOS_CONF "karyotype = $outfile.karyotype.txt\n";}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
148 elsif (/OUTPUT_PNG/){print CIRCOS_CONF "file = $outfile\n";}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
149 else{print CIRCOS_CONF $_;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
150 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
151 close(F);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
152 close(CIRCOS_CONF);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
153
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
154 system("$circos_exe -conf $outfile.circos1.conf");