-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_gap_lengths.pl
196 lines (162 loc) · 4.9 KB
/
calculate_gap_lengths.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/perl
# Zanfona - calculate_gap_length - contig distance calculator
# v. 1.0
#
# version history
# 1.0 - release candidate
#
# The program takes input from STDIN
# and outputs results on STDOUT.
#
# Copyright 2020 IRIDIAN GENOMES INC
#
# Permission is hereby granted, free of
# charge, to any person obtaining a copy
# of this software and associated documentation
# files (the "Software"), to deal in the
# Software without restriction, including
# without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to
# do so, subject to the following conditions:
#
# The above copyright notice and this permission
# notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
# WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# pragmas
use strict;
##### MAIN BEGIN
my @file_data;
my @query_lines;
my @queries;
my @header_array;
my $header;
my $colsep = ",";
my $results_all_queries;
my @results_all_queries;
# query line columns
use constant {
QUERY_ID => 0,
CONTIG_ID => 1,
MIN_START => 2,
MIN_STOP => 3,
MAX_START => 4,
MAX_STOP => 5,
ORIENTATION => 6
};
# Read all file data into array.
chomp(@file_data = <>);
# Remove header line from data.
@file_data = grep ! /query/, @file_data;
# Get list of queries from file data (first column).
@query_lines = map { (split(/,/, $_))[QUERY_ID] } @file_data;
@queries = uniq(\@query_lines);
# Print output header.
@header_array = ("query","first_contig","first_contig_orientation","second_contig","second_contig_orientation","gap_size");
$header = join $colsep, @header_array;
print $header . "\n";
# Process all queries.
foreach (@queries) {
$results_all_queries = $results_all_queries . doQuery($_, \@file_data) . "\n";
}
@results_all_queries = split(/\n/, $results_all_queries);
# Print results to output.
foreach (@results_all_queries) {
print $_ . "\n";
}
##### MAIN END
# subroutines
# doQuery
sub doQuery {
my $query;
my @contig_lines;
my @contigs;
my @query_lines;
my $first_contig;
my $second_contig;
my $gap_length;
my @result_array;
my $result_string = "";
my $query = $_[0];
# Get data for this query from input data.
@query_lines = grep /^$query,/, @{$_[1]};
# Get list of contigs in this query.
my @contig_lines = map { (split(/,/, $_))[CONTIG_ID] } @query_lines;
my @contigs = uniq(\@contig_lines);
# Only process if there are exactly two contigs.
# (max index of @contigs is 1)
if ($#contigs == 1) {
# Determine order of contigs for calculation of gap length.
if (getContigData(MIN_START, $query, @contigs[0], \@query_lines)
< getContigData(MIN_START, $query, @contigs[1], \@query_lines)) {
$first_contig = 0;
$second_contig = 1;
} else {
$first_contig = 1;
$second_contig = 0;
}
# Calculate gap length.
$gap_length = getContigData(MAX_STOP, $query, @contigs[$first_contig], \@query_lines) -
getContigData(MIN_START, $query, @contigs[$second_contig], \@query_lines);
# Construct result string.
@result_array = (
$query,
getContigData(CONTIG_ID, $query, @contigs[$first_contig], \@query_lines),
getContigData(ORIENTATION, $query, @contigs[$first_contig], \@query_lines),
getContigData(CONTIG_ID, $query, @contigs[$second_contig], \@query_lines),
getContigData(ORIENTATION, $query, @contigs[$second_contig], \@query_lines),
$gap_length
);
$result_string = join $colsep, @result_array;
} else {
# Add message about invalid contig count to STDERR here if desired.
}
return $result_string;
}
# getContigData
sub getContigData {
my $column_number = $_[0];
my $query = $_[1];
my $contig = $_[2];
my @query_lines = @{$_[3]};
my @contig_line;
my @contig_array;
my $column_value = "";
# Get data line for this contig from query data.
@contig_line = grep /^$query,$contig,/, @query_lines;
# Split line into data columns.
@contig_array = split($colsep, @contig_line[0]);
# Get desired column value.
$column_value = @contig_array[$column_number];
# Return data.
return $column_value;
}
# uniq - returns a list of unique array elements
sub uniq {
my @arr = @{$_[0]};
my %count;
my @res;
# Add element to result list exactly once.
foreach my $elem (@arr) {
# Keep count of times this element has appeared.
$count{$elem} = $count{$elem} + 1;
# Add first occurence of element to result array.
if ($count{$elem} == 1) {
push @res, $elem;
}
}
return @res;
}