File: segment_GFF_partitions.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 (166 lines) | stat: -rwxr-xr-x 2,969 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
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env perl

use strict;
use warnings;


my $usage = "usage: $0 partitions.gff clip_pts.wig\n\n";

my $partitions_gff = $ARGV[0] or die $usage;
my $clip_pts_wig = $ARGV[1] or die $usage;

my $MIN_PARTITION_SIZE = 50;

main: {

	my %scaff_to_gff = &parse_gff_entries($partitions_gff);
	my %scaff_to_clip = &parse_wig_entries($clip_pts_wig);

	
	foreach my $scaff (sort keys %scaff_to_gff) {
		
		my @gffs = @{$scaff_to_gff{$scaff}};
		@gffs = sort {$a->{lend}<=>$b->{lend}} @gffs;

		my @clips;
		if (exists $scaff_to_clip{$scaff}) {
			@clips = @{$scaff_to_clip{$scaff}};
			@clips = sort {$a<=>$b} @clips;
		}
		
		foreach my $gff (@gffs) {
			
			my ($lend, $rend) = ($gff->{lend}, $gff->{rend});
			
			my @overlapping_clips;

			if (@clips) {
				while (@clips && $clips[0] < $rend) {
					my $clip = shift @clips;
					if ($clip > $lend) {
						push (@overlapping_clips, $clip);
					}
				}
			}
			
			if (@overlapping_clips) {
				my @x = @{$gff->{line}};
				
				my $gff_line = join("\t", @x);
				
				#print "$gff_line\nHas overlapping clips: @overlapping_clips\n";
				

				my $prev_lend = $lend;
				
				foreach my $overlapping_clip (@overlapping_clips) {
					
					my $rend = $overlapping_clip - 1;
					
					my @y = @x;
					$y[3] = $prev_lend;
					$y[4] = $rend;

					my $part_length = $rend - $prev_lend + 1;
					
					if ($part_length >= $MIN_PARTITION_SIZE) {

						print join("\t", @y) . "\n";

					}
					
					$prev_lend = $rend + 1;
					
					
					#print "$gff_line\nWith clips: " . join("\t", @overlapping_clips) . "\n\n";
				}
				## report last partition of clip:
				
				$x[3] = $prev_lend;
				print join("\t", @x) . "\n";
				

				#print "\n\n";
			}

			else {
				
				## no clipping.  Report original partition.
				
				print join("\t", @{$gff->{line}}) . "\n";
			}
			

		}

	}

	exit(0);

}
			


####
sub parse_gff_entries {
	my ($gff_file) = @_;

	my %scaffold_to_gff;
	
	open (my $fh, $gff_file) or die "Error, cannot open file $gff_file";
	while (<$fh>) {
		chomp;
		
		my @x = split(/\t/);
		
		my $scaff = $x[0];
		my $lend = $x[3];
		my $rend = $x[4];
		
		my $struct = { line => [@x],
					   scaff => $scaff,
					   lend => $lend,
					   rend => $rend,
				   };

		push (@{$scaffold_to_gff{$scaff}}, $struct);
	}
	close $fh;

	return(%scaffold_to_gff);
}


#### 
sub parse_wig_entries {
	my ($jaccard_file) = @_;

	my %scaffold_to_entries;

	my $curr_scaff;
        
	open (my $fh, $jaccard_file) or die "Error, cannot open file $jaccard_file";
	while (<$fh>) {
		unless (/\w/) { next; }
		chomp;
		if (/^variableStep chrom=(\S+)/) {
			$curr_scaff = $1;
			next;
		}
                        
		my ($coord, $val) = split(/\t/);
                
		unless (defined($coord) && defined($val)) {
                        #die "Error, line $_ lacks expected format";
			next;
		}
                
		push (@{$scaffold_to_entries{$curr_scaff}}, $coord);
	}

	close $fh;


	return(%scaffold_to_entries);
}