-
Notifications
You must be signed in to change notification settings - Fork 10
/
map_records.pl
295 lines (232 loc) · 8.17 KB
/
map_records.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/perl -w
#
# map_records.pl -- mapping records from one file to another file
#
#
# Author: Nowind
# Created: 2012-05-31
# Updated: 2023-10-30
# Version: 1.2.0
#
# Change logs:
# Version 1.0.0 12/09/18: The initial version.
# Version 1.1.0 12/11/19: Add option "--rows2" to specify rows in subject file.
# Version 1.1.1 12/12/07: Query and subject rows now can have only a single value.
# Version 1.1.2 12/12/29: Change the way to import functions from MyPerl::FileIO.
# Version 1.1.3 13/01/23: Add option "--interval" and "--flanking" to map records in intervals.
# Version 1.1.4 13/03/14: Change output header.
# Version 1.1.5 13/03/15: Bug fixed while output file header.
# Version 1.1.6 13/04/11: Bug fixed while reading query file from STDIN.
# Version 1.1.7 16/05/31: Updated: Add option "--no-secondary"; remove mapped subject rows to reduce redundancy.
# Version 1.1.8 16/06/13: Bug fixed while no remain fields present in subject file.
# Version 1.1.9 16/02/16: Deprecated non-functional "-q" and "-s" options.
# Version 1.2.0 23/10/30: Bug fixed: --no-dups prints empty results; add support for setting delimiters separately for query and subject files.
use strict;
use Data::Dumper;
use Getopt::Long;
use File::Find::Rule;
use File::Basename;
use MyPerl::FileIO qw(:all);
################################# Main ###############################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.2.0';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $qdelimiter = '\s+';
my $sdelimiter = '\s+';
my $flank_len = 0;
my ($query_file, $subject_file, $output, $interval,
@query_rows, @subject_rows, $match_str, @sub_set, $out_first_only, $merge_sub_dups);
GetOptions(
"query=s" => \$query_file,
"subject=s" => \$subject_file,
"output=s" => \$output,
"qdelimiter=s" => \$qdelimiter,
"sdelimiter=s" => \$sdelimiter,
"interval" => \$interval,
"flanking=i" => \$flank_len,
"match=s" => \$match_str,
"Q|rows1=i{,}" => \@query_rows,
"S|rows2=i{,}" => \@subject_rows,
"no-dups" => \$out_first_only,
"merge-dups" => \$merge_sub_dups,
);
unless( $query_file && $subject_file ) {
print <<EOF;
$0 -- query fasta sequences
Version: $VERSION
Usage: perl $0 [options]
Options:
--query <filename>
query file, required
--subject <filename>
subject file, required
-o, --output <filename>
output file, default to STDOUT
-Q, --rows1 <numbers>
specify rows for comparing in query file [default: 0 1]
-S, --rows2 <numbers>
specify rows for comparing in subject file [default: 0 1]
-i, --interval
use this option to map all records in the interval between
query start and end, while this option is set, the -Q option
should be specified with following rows in the order:
start position, end position, [other keys...]
while the -S option should be specified as:
position, [other keys...]
-f, --flanking <interger>
flanking region length to extend the interval, only valid while
-i option is specified
--qdelimiter <string>
--sdelimiter <string>
specify a delimiter while reading input files, such as ",",
"\\t", multiple delimiters can be set such as ",|\\t"
[default: "\\s+" for both query and subject]
--match <string>
only considering lines matching a pattern, support perl
regular expression
-n, --no-dups
if query record have multiple hits in subject file, only the first
one would be written to output
--merge-dups
if query record have multiple hits in subject file, all hits would
be written in a single record and seperate by semicolon
EOF
exit(1);
}
$|++;
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
if ($output) {
open (STDOUT, "> $output") || die $!;
}
unless(@query_rows >= 1){ @query_rows = (0, 1) };
unless(@subject_rows >= 1){ @subject_rows = (0, 1) };
##
## read and parsing query file
##
print STDERR ">> Read in $query_file ... ";
my %Query = ();
my @Query_Records = ();
my $query_header = '';
my $fh1 = getInputFilehandle($query_file);
while (<$fh1>)
{
push @Query_Records, $_;
if (/^\#\w/) {
chomp($query_header = $_);
}
next if (/\#/ || /^\s+$/);
next if ($match_str && !/$match_str/);
if ($interval) {
my ($start, $end, @cmp_rows) = (split /$qdelimiter/, $_)[@query_rows];
for my $pos (($start-$flank_len)..($end+$flank_len))
{
my $cmp_rows = join "\t", ($pos, @cmp_rows);
$Query{$cmp_rows}->{query} = 1;
}
}
else {
my $cmp_rows = join "\t", ((split /$qdelimiter/, $_)[@query_rows]);
$Query{$cmp_rows}->{query} = 1;
}
}
print STDERR "done!\n";
##
## retrieving query records in subject file
##
print STDERR ">> Search records in $subject_file ... ";
my $subject_header = '';
my $fh2 = getInputFilehandle($subject_file);
while (<$fh2>)
{
if (/\#\#/ || /^\s+$/) {
next;
}
chomp;
my @all_rows = (split /$sdelimiter/, $_);
my $cmp_rows = join "\t", @all_rows[@subject_rows];
@all_rows[@subject_rows] = ();
my @remain_rows = grep defined, @all_rows;
my $remain_rows = (@remain_rows > 0) ? join "\t", @remain_rows : "Found";
if (/^\#\w/) {
$subject_header = $remain_rows;
next;
}
next unless( $Query{$cmp_rows}->{query} );
push @{$Query{$cmp_rows}->{cmp}}, $remain_rows;
}
print STDERR "done!\n";
##
## generate results
##
print STDERR ">> Start generate results ... ";
print STDOUT "$HEADER##" . (scalar localtime()) . "\n";
foreach (@Query_Records)
{
if (/\#\#/ || /^\s+$/) {
print STDOUT; next;
}
elsif (/^\#\w/) {
print STDOUT "$query_header\t$subject_header\n";
}
next if (/\#/ || /^\s+$/);
next if ($match_str && !/$match_str/);
chomp;
if ($interval) {
my ($start, $end, @cmp_rows) = (split /$qdelimiter/, $_)[@query_rows];
for my $pos (($start-$flank_len)..($end+$flank_len))
{
my $cmp_rows = join "\t", ($pos, @cmp_rows);
unless( $Query{$cmp_rows}->{cmp} ) {
print STDOUT "$_\tN/A\n";
next;
}
my @out_records = ();
for my $record (@{$Query{$cmp_rows}->{cmp}})
{
if ($out_first_only) {
print STDOUT "$_\t$record\n";
last;
}
elsif ($merge_sub_dups) {
chomp($record);
push @out_records, $record;
}
else {
print STDOUT "$_\t$record\n";
}
}
if ($merge_sub_dups) {
my $merged_record = join ";", @out_records;
print STDOUT "$_\t$merged_record\n";
}
}
}
else {
my $cmp_rows = join "\t", ((split /$qdelimiter/, $_)[@query_rows]);
unless( $Query{$cmp_rows}->{cmp} ) {
print STDOUT "$_\tN/A\n";
next;
}
my @out_records = ();
for my $record (@{$Query{$cmp_rows}->{cmp}})
{
if ($out_first_only) {
print STDOUT "$_\t$record\n";
last;
}
elsif ($merge_sub_dups) {
chomp($record);
push @out_records, $record;
}
else {
print STDOUT "$_\t$record\n";
}
}
if ($merge_sub_dups) {
my $merged_record = join ";", @out_records;
print STDOUT "$_\t$merged_record\n";
}
}
}
print STDERR "done!\n";
print STDERR "# " . (scalar localtime()) . "\n";