annotate PanExplorer_workflow/Perl/GeneratePAVfromBed.pl @ 2:97e4e3e818b6 draft

Uploaded
author dereeper
date Thu, 30 May 2024 11:48:09 +0000
parents 032f6b3806a3
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 use Graph::Undirected;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
6
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
7 my $strain_info = $ARGV[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
8 my $bed_directory = $ARGV[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
9 my $PAV = $ARGV[2];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
10 my $threshold_coverage = $ARGV[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
11
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
12 my %strains;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
13 open(S,$strain_info);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
14 while (my $line = <S>) {
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
15 chomp $line;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
16 my ($id,$name) = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
17 $strains{$id} = $name;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
18 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
19 close(S);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
20
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
21 my %gene_lengths;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
22 foreach my $id(keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
23
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
24 my $gff = `ls $bed_directory/$id*.gff`;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
25 open(GFF,$gff);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
26 my $current_gene_length;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
27 while(my $line = <GFF>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
28 chomp($line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
29 my @infos = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
30 if ($infos[2] eq "gene" && $line =~/ID=([^;]+);*/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
31 my $chr = $infos[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
32 my $start = $infos[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
33 my $end = $infos[4];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
34 my $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
35 my $genelength = $end-$start;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
36 $current_gene_length = $genelength;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
37 $gene_lengths{$id}{$gene} = $genelength;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
38 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
39 if ($infos[2] eq "CDS"){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
40 my $gene;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
41 if ($line =~/Parent=([^;]+);*/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
42 $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
43 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
44 elsif ($line =~/ID=([^;]+);*/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
45 $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
46 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
47 my $start = $infos[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
48 my $end = $infos[4];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
49 my $genelength = $end-$start;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
50 $gene_lengths{$id}{$gene} += $genelength;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
51 if ($line =~/protein_id=([^;]+);/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
52 $gene = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
53 $gene_lengths{$id}{$gene} += $genelength;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
54 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
55 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
56 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
57 close(GFF);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
58 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
59
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
60
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
61
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
62
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
63 my %genes;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
64 my $graph = Graph::Undirected->new;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
65 my $num = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
66 foreach my $id(keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
67
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
68 my $file = `ls $bed_directory/$id.*.bed`;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
69 chomp($file);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
70 open(B,$file);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
71 while(my $line = <B>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
72 chomp($line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
73 my @infos = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
74 my $gene1 = $infos[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
75 $genes{"$id:$gene1"} = 1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
76 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
77 close(B);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
78
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
79 foreach my $id2(keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
80 my $file2 = `ls $bed_directory/$id2.*.bed`;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
81 chomp($file2);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
82 if (-e $file && -e $file2 && $file ne $file2){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
83 $num++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
84 system("bedtools intersect -a $file -b $file2 -wo >$PAV.$num.intersect.out");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
85 my %cumul_match;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
86 my %cumul_match2;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
87 open(INTER,"$PAV.$num.intersect.out");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
88 while(<INTER>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
89 my $line = $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
90 $line =~s/\n//g;$line =~s/\r//g;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
91 my ($segment,$start_gene1,$end_gene1,$gene1,$segment,$start_gene2,$end_gene2,$gene2,$size_overlap) = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
92 $cumul_match{"$gene1;$gene2"}+=$size_overlap;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
93 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
94 close(INTER);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
95
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
96 #unlink("$PAV.$num.intersect.out");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
97
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
98 foreach my $pair(keys(%cumul_match)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
99 my $size1 = $cumul_match{$pair};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
100 my ($gene1,$gene2) = split(/;/,$pair);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
101 my $gene1_length = $gene_lengths{$id}{$gene1};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
102 my $gene2_length = $gene_lengths{$id2}{$gene2};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
103 my $size_match_gene = $cumul_match{"$gene1;$gene2"};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
104 my $percentage_overlap_gene1 = ($size_match_gene/$gene1_length)*100;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
105 my $percentage_overlap_gene2 = ($size_match_gene/$gene2_length)*100;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
106 #print "$pair $size_match_gene $percentage_overlap_gene1 $percentage_overlap_gene2\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
107 if ($percentage_overlap_gene1 > $threshold_coverage && $percentage_overlap_gene2 > $threshold_coverage){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
108 $graph->add_edge("$id:$gene1","$id2:$gene2");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
109 delete($genes{"$id:$gene1"});
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
110 delete($genes{"$id2:$gene2"});
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
111 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
112 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
113 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
114 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
115 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
116
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
117 open(OUT,">$PAV");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
118 print OUT "ClutserID";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
119 foreach my $id(sort keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
120 my $name = $strains{$id};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
121 print OUT "\t".$name;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
122 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
123 print OUT "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
124 my $clnum = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
125 my @cc = $graph->connected_components();
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
126 foreach my $component (@cc){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
127 $clnum++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
128 print OUT $clnum;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
129 my @genes = @$component;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
130 my %h;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
131 foreach my $gene(@genes){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
132 my ($id,$genename) = split(/:/,$gene);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
133 $h{$id}.="$genename,";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
134 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
135 foreach my $id(sort keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
136 my $ids = "-";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
137 if ($h{$id}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
138 $ids = $h{$id};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
139 chop($ids);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
140 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
141 print OUT "\t".$ids;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
142 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
143 print OUT "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
144 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
145 #######################
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
146 # add singletons
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
147 #######################
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
148 foreach my $gene(keys(%genes)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
149 $clnum++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
150 print OUT $clnum;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
151 my ($id,$genename) = split(/:/,$gene);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
152 my %h;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
153 $h{$id}.="$genename,";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
154 foreach my $id(sort keys(%strains)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
155 my $ids = "-";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
156 if ($h{$id}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
157 $ids = $h{$id};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
158 chop($ids);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
159 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
160 print OUT "\t".$ids;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
161 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
162 print OUT "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
163 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
164 close(OUT);