-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_counter_IV.pl
executable file
·73 lines (57 loc) · 2.06 KB
/
read_counter_IV.pl
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
#!/usr/bin/perl
# A script to count the number of overlapping reads for a given window size around a list of given origins
# This script count reads given by single bam file that has been sorted and indexed
# Input:
# 1. file with list of origin locations (ori_data_1.8.txt)
# 2. window size (even integer in bp)
# 3. prefix for sorted bam file (labib_c, note that an indexed bam file must be located in same directory)
# Example: read_counter_IV.pl ori_data_1.8.txt 1000 labib_c
# Prerequisites:
# samtools must be installed and binary in PATH
use strict;
use warnings;
# Arguments
my $origin_locs=$ARGV[0];
my $window_size=$ARGV[1];
my $read_map = $ARGV[2];
# Variables
my $window_start;
my $window_end;
###BEGIN PROGRAM########################################################################
# Assign mapped read files
my $map_file = join("", $read_map, "_sorted.bam");
# Open location file and file to print results
open(ORIGINS, $origin_locs);
open(OUT, ">ori_data_1.9.txt");
# Set variables for origin location input file (need to be adjusted depending on file)
my $chr_col = 1;
my $acs_loc_col = 3;
# Skip the header line of location file
my $header = <ORIGINS>;
$header =~ /.+/;
# Add new read count columns to output file
my $new_header = join("\t", $&, "${read_map}_MCM_counts");
print OUT "$new_header\n";
# Cycle through origin locations outputting read count for each location
while ($_ = <ORIGINS>) {
my @line = split;
# Extract origin location info
my $chr = $line[$chr_col];
my $acs_loc = $line[$acs_loc_col];
# Get read counts overlapping window around acs motif start location
if($acs_loc < ($window_size/2)) {
$window_start = 0;
$window_end = $window_size;
} else {
$window_start = $acs_loc - ($window_size/2);
$window_end = $acs_loc + ($window_size/2);
}
my $mcm_count = `samtools view -c $map_file chr$chr:$window_start-$window_end`;
$mcm_count =~ /.+/;
$mcm_count = $&;
# Print mcm_count to file
push(@line, $mcm_count);
my $new_line = join("\t", @line);
print OUT "$new_line\n";
}
close OUT;