-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_sequences_with_gaps_or_ambiguous_bases.pl
90 lines (74 loc) · 1.97 KB
/
remove_sequences_with_gaps_or_ambiguous_bases.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
#!/usr/bin/env perl
# Removes sequences with -s or bases that are not A, T, C, or G.
# Usage:
# perl remove_sequences_with_gaps_or_ambiguous_bases.pl [fasta file path]
# Prints to console. To print to file, use
# perl remove_sequences_with_gaps_or_ambiguous_bases.pl [fasta file path]
# > [output fasta file path]
use strict;
use warnings;
my $fasta_file = $ARGV[0]; # fasta file
# verifies that fasta file exists and is non-empty
if(!$fasta_file)
{
print STDERR "Error: no input fasta file provided. Exiting.\n";
die;
}
if(!-e $fasta_file)
{
print STDERR "Error: input fasta file does not exist:\n\t".$fasta_file."\nExiting.\n";
die;
}
if(-z $fasta_file)
{
print STDERR "Error: input fasta file is empty:\n\t".$fasta_file."\nExiting.\n";
die;
}
# reads in fasta file
my $current_sequence = "";
my $current_sequence_name = "";
open FASTA_FILE, "<$fasta_file" || die "Could not open $fasta_file to read; terminating =(\n";
while(<FASTA_FILE>) # for each line in the file
{
chomp;
if($_ =~ /^>(.*)/) # header line
{
# process previous sequence if it has been read in
if($current_sequence)
{
if(!sequence_contains_gaps_or_ambiguous_bases($current_sequence))
{
print ">".$current_sequence_name."\n";
print $current_sequence."\n";
}
}
# save new sequence name and prepare to read in new sequence
$current_sequence_name = $1;
$current_sequence = "";
}
else # not header line
{
$current_sequence .= uc($_);
}
}
if($current_sequence)
{
if(!sequence_contains_gaps_or_ambiguous_bases($current_sequence))
{
print ">".$current_sequence_name."\n";
print $current_sequence."\n";
}
}
close FASTA_FILE;
# returns 1 if sequence contains a - or any bases other than A, T, C, and G
sub sequence_contains_gaps_or_ambiguous_bases
{
my $sequence = $_[0];
$sequence = uc($sequence);
if($sequence =~ /[^ATCG]/)
{
return 1; # sequence contains a letter other than A, T, C, or G
}
return 0; # sequence is only composed of As, Ts, Cs, or Gs
}
# October 23, 2024