-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_input_tdr
More file actions
executable file
·152 lines (138 loc) · 3.46 KB
/
check_input_tdr
File metadata and controls
executable file
·152 lines (138 loc) · 3.46 KB
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
#!/usr/bin/perl -w
# check_input_tdr
# Check input tDR sequences for validity
use strict;
use Getopt::Long;
our ($opt_fasta, $opt_min, $opt_max, $opt_filter, $opt_help);
&set_options();
&parse_fasta();
exit;
sub set_options
{
$opt_fasta = '';
$opt_min = 15;
$opt_max = 70;
$opt_filter = "";
Getopt::Long::GetOptions("fasta=s", "min=i", "max=i", "filter=s", "help|h");
if ($opt_help || $opt_fasta eq "" || $opt_min <= 0 || $opt_max <= 0 || $opt_max < $opt_min)
{
die "Usage: check_input_tdr [options] > <log file>\n",
"Options\n",
"--fasta=<file name> Fasta file of fragments\n",
"--min=<value> Minimum length of sequence (default = 15)\n",
"--max=<value> Maximum length of sequence (default = 70)\n",
"--filter=<file name> Output fasta file with invalid sequences filtered (optional)\n",
"--help -h Print this help\n\n";
}
}
sub parse_fasta
{
my $seq = "";
my $seq_name = "";
my $line = "";
my $ct = 0;
my @invalid_counts = (0,0,0,0,0);
my $valid = 1;
my $output = 0;
my $filtered_count = 0;
if ($opt_filter ne "")
{
open(FILE_OUT, ">$opt_filter") or die "Fail to open $opt_filter\n";
$output = 1;
}
open(FILE_IN, "$opt_fasta") or die "Fail to open $opt_fasta\n";
while ($line = <FILE_IN>)
{
chomp($line);
if ($line =~ /^#/ or $line eq "")
{}
elsif ($line =~ /^>(\S+)/)
{
if ($seq ne "")
{
$ct++;
$valid = &check_seq($ct, $seq_name, $seq, \@invalid_counts);
if ($valid and $output)
{
print FILE_OUT ">".$seq_name."\n";
print FILE_OUT $seq."\n";
$filtered_count++;
}
}
$seq_name = $1;
$seq = "";
}
else
{
$seq .= $line;
}
}
if ($seq ne "")
{
$ct++;
$valid = &check_seq($ct, $seq_name, $seq, \@invalid_counts);
if ($valid and $output)
{
print FILE_OUT ">".$seq_name."\n";
print FILE_OUT $seq."\n";
$filtered_count++;
}
}
close(FILE_IN);
if ($output)
{
close(FILE_OUT);
}
print "Total number of sequences: ".$ct."\n";
print "Missing sequence name: ".$invalid_counts[0]."\n";
print "Sequence name with special characters: ".$invalid_counts[1]."\n";
print "Below minimum sequence length (".$opt_min." bp): ".$invalid_counts[2]."\n";
print "Above maximum sequence length (".$opt_max." bp): ".$invalid_counts[3]."\n";
print "Invalid sequence: ".$invalid_counts[4]."\n";
if ($output)
{
print "Total number of sequences after filtering: ".$filtered_count."\n";
}
}
sub check_seq
{
my ($ct, $seq_name, $seq, $invalid_counts) = @_;
my $valid = 1;
if (&trim($seq_name) eq "")
{
print $ct.": Sequence name for ".$seq." is missing\n";
$invalid_counts->[0] += 1;
$valid = 0;
}
elsif (&trim($seq_name) !~ /^[\w\-\_\:\|]+$/)
{
print $ct.": Sequence name for ".$seq." contains unsupported special character(s).\n";
$invalid_counts->[1] += 1;
}
my $s = &trim($seq);
if (length($s) < $opt_min)
{
print "Entry ".$ct.": Sequence ".$seq_name." is shorter than minimum (".$opt_min." bp) - length = ".length($s)."\n";
$invalid_counts->[2] += 1;
$valid = 0;
}
elsif (length($s) > $opt_max)
{
print "Entry ".$ct.": Sequence ".$seq_name." is longer than maximum (".$opt_max." bp) - length = ".length($s)."\n";
$invalid_counts->[3] += 1;
$valid = 0;
}
elsif ($s !~ /[ACGTUacgtu]+/)
{
print "Entry ".$ct.": Sequence ".$seq_name." is invalid. Only A, C, G, T, or U can be included.\n";
$invalid_counts->[4] += 1;
$valid = 0;
}
return $valid;
}
sub trim
{
my ($value) = @_;
$value =~ s/\ //g;
return $value;
}