-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_sequences_by_position_in_fasta_file.pl
101 lines (81 loc) · 2.61 KB
/
remove_sequences_by_position_in_fasta_file.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
#!/usr/bin/env perl
# Removes query sequences by position from fasta file.
# Usage:
# perl remove_sequences_by_position_in_fasta_file.pl [fasta file path]
# [position of sequence to remove (1-indexed)] [position of another sequence to remove]
# [etc.]
# Prints to console. To print to file, use
# perl remove_sequences_by_position_in_fasta_file.pl [fasta file path]
# [position of sequence to remove (1-indexed)] [position of another sequence to remove]
# [etc.] > [output fasta file path]
use strict;
use warnings;
my $fasta_file = $ARGV[0];
my @positions = @ARGV[1..$#ARGV];
# verifies that query sequence positions have been provided
if(!scalar @positions)
{
print STDERR "Error: no query sequence positions provided. Exiting.\n";
die;
}
# 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;
}
# builds hash of query sequence names for fast checking
my %sequence_position_excluded = (); # key: sequence position in fasta file -> value: 1 if sequence is to be excluded in output
foreach my $position(@positions)
{
$sequence_position_excluded{$position} = 1;
}
# reads in fasta file and retrieves sequences matching query sequence names
open FASTA_FILE, "<$fasta_file" || die "Could not open $fasta_file to read; terminating =(\n";
my $printing_this_sequence = 1; # 1 if we are printing the sequence we are currently reading
my %position_found = (); # key: sequence position in fasta file -> value: 1 if sequence has been found and printed
my $sequence_position = 0;
while(<FASTA_FILE>) # for each line in the file
{
chomp;
if($_ =~ /^>(.*)$/) # header line
{
# checks if this sequence position is one of our query sequences
$sequence_position++;
$printing_this_sequence = 1;
if($sequence_position_excluded{$sequence_position})
{
# records that we are printing lines belonging to this sequence
$printing_this_sequence = 0;
# marks this sequence as found
$position_found{$sequence_position} = 1;
}
}
# prints this line (header or sequence) if we are printing this sequence
if($printing_this_sequence)
{
print $_."\n";
}
}
close FASTA_FILE;
# verifies that all query sequences have been found and printed
foreach my $position(@positions)
{
if(!$position_found{$position})
{
print STDERR "Error: sequence position ".$position." not found\n";
}
}
# July 12, 2021
# March 24, 2022
# March 30, 2022