File: fusion_comparisons_via_maps_files.pl

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (140 lines) | stat: -rwxr-xr-x 3,461 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl

use strict;
use warnings;

my $usage = "usage: $0 fileA.maps fileB.maps gene_annots.gff3\n\n";

my $file_A_maps = $ARGV[0] or die $usage;
my $file_B_maps = $ARGV[1] or die $usage;
my $gene_annots_gff3 = $ARGV[2] or die $usage;

main: {

    my ($A_fusion_to_genes_href, $A_FL_genes) = &parse_map_file($file_A_maps);
    
    my ($B_fusion_to_genes_href, $B_FL_genes) = &parse_map_file($file_B_maps);

    my %gene_structs = &get_gene_positions($gene_annots_gff3);
        
    my %all_fusions = map { $_ => 1 } (keys %$A_fusion_to_genes_href, keys %$B_fusion_to_genes_href);

    foreach my $fusion (keys %all_fusions) {

        my $neighboring = &are_neighboring($fusion, \%gene_structs);

        print "$fusion\t$neighboring\t";
        
        if ($A_fusion_to_genes_href->{$fusion} && $B_fusion_to_genes_href->{$fusion}) {
            print "BOTH\n";
        }
        elsif ($A_fusion_to_genes_href->{$fusion}) {
            print "A_only\n";
        }
        elsif ($B_fusion_to_genes_href->{$fusion}) {
            print "B_only\n";
        }
        
    }
    
    exit(0);
    
}

####
sub parse_map_file {
    my ($map_file) = @_;

    my %fusion_to_genes;
    my %FL_genes;

    open (my $fh, $map_file) or die $!;
    while (<$fh>) {
        chomp;
        my ($fl_entries, $rest) = split(/\t/);
        
        my @genes = split(/,/, $fl_entries);
        
        foreach my $gene (@genes) {

            $FL_genes{$gene} = $fl_entries;
        }

        if (scalar @genes > 1) {
            $fusion_to_genes{$fl_entries} = \@genes;
        }
    }
    close $fh;

    return(\%fusion_to_genes, \%FL_genes);
}


####
sub get_gene_positions {
    my ($annot_file) = @_;

    my %gene_structs;
    my %scaff_to_structs;

    open (my $fh, $annot_file) or die $!;
    while (<$fh>) {
        chomp;
        unless (/\w/) { next; }
        my @x = split(/\t/);
        if ($x[2] eq 'gene') {
            $x[8] =~ /ID=([^;]+)/ or die "Error, cannot parse ID from $x[8]";
            my $gene_id = $1;
            my $struct = { scaffold => $x[0],
                           midpt =>  ($x[3]+$x[4])/2,
                           order => undef,
                           gene_id => $gene_id,
                       };
        
            $gene_structs{$gene_id} = $struct;
            push (@{$scaff_to_structs{$x[0]}}, $struct);
            
        }
    }
    close $fh;


    ## order the genes.
    
    my $pos = 0;
    foreach my $scaff (keys %scaff_to_structs) {
        my @structs = @{$scaff_to_structs{$scaff}};
        @structs = sort {$a->{midpt}<=>$b->{midpt}} @structs;

        foreach my $struct (@structs) {
            $pos++;
            $struct->{order} = $pos;
        }
        $pos++; # gap between scaffs
    }

    return(%gene_structs);
}

####
sub are_neighboring {
    my ($fusion, $gene_structs_href) = @_;

    my @gene_structs;
    foreach my $gene (split(/,/, $fusion)) {
        my ($trans_id, $gene_id) = split(/;/, $gene);
        
        my $gene_struct = $gene_structs_href->{$gene_id} or die "Error, no gene struct for $gene_id";
        
        push (@gene_structs, $gene_struct);
    }
    
    @gene_structs = sort {$a->{order}<=>$b->{order}} @gene_structs;
    
    if ($gene_structs[1]->{order} - $gene_structs[0]->{order} <= 4 ) {
        return("Neighbors");
    }
    else {
        return("NOT_neighbors ($gene_structs[0]->{order} vs. $gene_structs[1]->{order})");
    }
}