File: component_to_graph_dot.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 (153 lines) | stat: -rwxr-xr-x 4,104 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
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env perl

use strict;
use warnings;

my $usage = "\n\nusage: $0 chrysalis_dir/ component_no (WELDS|SCAFF|BOTH)\n\n";

my $chrysalis_dir = $ARGV[0] or die $usage;
my $component_no = $ARGV[1];
my $draw_type = $ARGV[2] or die $usage;

unless (defined $component_no) {
    die $usage;
}

unless ($draw_type =~ /^(WELDS|SCAFF|BOTH)$/) {
    die $usage;
}

main: {
    
    my $graph_from_iworm_file = "$chrysalis_dir/GraphFromIwormFasta.out";
    
    my %iworm_accs = &get_iworm_list_from_component($graph_from_iworm_file, $component_no);
    
    ## get the weld links:
    my %welds = &parse_welds($graph_from_iworm_file, \%iworm_accs);
    
    ## get the scaffolding links:
    my $scaffolding_file = "$chrysalis_dir/../iworm_scaffolds.txt";
    my %scaffolds = &parse_scaffolded_pairs($scaffolding_file, \%iworm_accs);
    
    ## write dot file
    print "digraph {\n";
    
    
    my %iworm_to_id;
    ## write node labels
    my $counter = 0;
    foreach my $iworm_acc (keys %iworm_accs) {
        $counter++;
        my $iworm_id = $iworm_to_id{$iworm_acc} = $counter;
        print "\t$iworm_id [label=\"$iworm_acc\"];\n";
    }
 
    if ($draw_type =~ /WELDS|BOTH/) {
        ## add weld links
        foreach my $iworm_acc (keys %welds) {
            my $iworm_id = $iworm_to_id{$iworm_acc};
            foreach my $welded_iworm_acc (keys %{$welds{$iworm_acc}}) {
                my $welded_id = $iworm_to_id{$welded_iworm_acc};
                print "\t$iworm_id" . "->" . "$welded_id [color=\"blue\"];\n";
            }
        }
    }
    
    if ($draw_type =~ /SCAFF|BOTH/) {
        ## add scaffolding links
        foreach my $iworm_acc (keys %scaffolds) {
            my $iworm_id = $iworm_to_id{$iworm_acc};
            unless (defined $iworm_id) { next; }
            
            foreach my $scaffolded_iworm_acc (keys %{$scaffolds{$iworm_acc}}) {
                my $scaffolded_id = $iworm_to_id{$scaffolded_iworm_acc};
                unless (defined $scaffolded_id) { next; }  # not all scaffolded entries were chosen for clustering if read support was insufficient
                
                print "\t$iworm_id" . "->" . "$scaffolded_id [color=\"green\"];\n";
            }
        }
    }
    
    print "}\n";

    exit(0);
    

}


####
sub parse_welds {
    my ($graph_file, $iworm_accs_href) = @_;
    
    my %welds;

    open (my $fh, $graph_file) or die "Error, cannot oepn file $graph_file";
    while (<$fh>) {
        chomp;
        if (/^\#Welding: >(a\d+;\d+)_\S+ to >(a\d+;\d+)_/) {
            my $iworm_acc_A = $1;
            my $iworm_acc_B = $2;

            ($iworm_acc_A, $iworm_acc_B) = sort ($iworm_acc_A, $iworm_acc_B);

            if ($iworm_accs_href->{$iworm_acc_A} || $iworm_accs_href->{$iworm_acc_B}) { # really should only have situations where they're both included
                
                $welds{$iworm_acc_A}->{$iworm_acc_B} = 1;
            }
        }
    }

    close $fh;

    return(%welds);
}


####
sub get_iworm_list_from_component {
    my ($graph_file, $component_no) = @_;

    my %iworm_accs;
    
    open (my $fh, $graph_file) or die "Error, cannot open file $graph_file";
    while (<$fh>) {
        if (/^>Component_$component_no /) {
            /\[iworm>(a\d+;\d+)_/ or die "Error, cannot parse iworm acc from $_ ";
            my $iworm_acc = $1;
            
            $iworm_accs{$iworm_acc} = 1;
        }
    }
    close $fh;


    return(%iworm_accs);
}


####
sub parse_scaffolded_pairs {
    my ($scaffolding_file, $iworm_accs_href) = @_;

    my %scaffolded_pairs;

    open (my $fh, $scaffolding_file) or die "Error, cannot open file $scaffolding_file";
    while (<$fh>) {
        chomp;
        my @x = split(/\t/);
        my ($iworm_acc_A, $iworm_acc_B) = ($x[0], $x[2]);
        
        ($iworm_acc_A, $iworm_acc_B) = sort ($iworm_acc_A, $iworm_acc_B);

        if ($iworm_accs_href->{$iworm_acc_A} || $iworm_accs_href->{$iworm_acc_B}) {
            
            $scaffolded_pairs{$iworm_acc_A}->{$iworm_acc_B} = 1;
        }
    }
    close $fh;

    return(%scaffolded_pairs);
}