-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_to_fasta_headers_containing_query.pl
65 lines (49 loc) · 1.52 KB
/
add_to_fasta_headers_containing_query.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
#!/usr/bin/env perl
# Adds text to sequence names containing query.
# Usage:
# perl add_to_fasta_headers_containing_query.pl "[query 1]"
# "[text to add to sequence names containing query 1]" "[query 2]"
# "[text to add to sequence names containing query 2]" [etc.]
# Prints to console. To print to file, use
# perl add_to_fasta_headers_containing_query.pl "[query 1]"
# "[text to add to sequence names containing query 1]" "[query 2]"
# "[text to add to sequence names containing query 2]" [etc.] > [output fasta file path]
use strict;
use warnings;
my $fasta_file = $ARGV[0];
my @queries = @ARGV[grep {$_ % 2 == 1} 1..$#ARGV];
my @text_to_add = @ARGV[grep {$_ % 2 == 0} 2..$#ARGV];
my $NEWLINE = "\n";
# verifies that fasta file exists and is non-empty
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 "Warning: input fasta file is empty:\n\t".$fasta_file."\n";
}
# reads in fasta file; adds text to each sequence name with query text in it
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
{
my $sequence_name = $1;
for my $index(0..$#queries)
{
my $query = $queries[$index];
my $this_text_to_add = $text_to_add[$index];
if($sequence_name =~ /$query/)
{
$_ .= $this_text_to_add;
}
}
}
print $_;
print $NEWLINE;
}
close FASTA_FILE;
# February 14, 2025