-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_dinucleotide_freqs
executable file
·137 lines (113 loc) · 2.31 KB
/
count_dinucleotide_freqs
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
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $usage = <<USAGE;
This script outputs nucleotide and di-nucleotide frequencies
of sequences in a fasta file
reads from stdin and writes to stdout a tab-delimited matrix of frequencies
unexpected bases in sequences (anything but A,C,G, or T) will be ignored
usage: $0 fasta_file
Mark Stenglein 10.9.2015
USAGE
# if not stdin output usage and exit
if (-t STDIN && @ARGV == 0) { print ($usage) and exit; }
my $print_usage = 0;
GetOptions ("h" => \$print_usage);
if ($print_usage) { print ($usage) and exit; }
my $header = undef;
my $seq = undef;
my @expected_nts = qw (A C G T);
my %expected_nts_hash = ();
# output header row
# single nts
foreach my $nt (@expected_nts)
{
print ("\t$nt");
$expected_nts_hash{$nt} = 1;
}
# di nts
foreach my $nt1 (@expected_nts)
{
foreach my $nt2 (@expected_nts)
{
my $di_nt = $nt1.$nt2;
print ("\t$di_nt");
}
}
print "\n";
# read through fasta input
while (<>)
{
chomp;
if (/^>(.+)/)
{
if ($seq)
{
process_seq($header, $seq);
$seq = "";
}
$header = $1;
}
else
{
# in case an RNA seq...
$_ =~ tr/Uu/Tt/;
$seq .= $_;
}
}
if ($seq)
{
process_seq($header, $seq);
}
# process one sequence
sub process_seq
{
my ($header, $seq) = @_;
my %nts = ();
my %di_nts = ();
# go through seq, count nts
# store in a hash
for (my $i = 0; $i < length $seq; $i++)
{
my $nt = substr($seq, $i, 1);
if (!$expected_nts_hash{$nt})
{
warn "warning: unexpected nt $nt in sequence $header\n";
}
$nts{$nt} += 1;
}
# go through seq, count di nts
# store in a hash
for (my $i = 0; $i < ((length $seq) - 1); $i++)
{
my $di_nt = substr($seq, $i, 2);
$di_nts{$di_nt} += 1;
}
my $seq_length = length $seq;
if ($seq_length <= 0)
{
die ("error: seq length <= 0 for sequence $header\n");
}
# output seq name (fasta header)
print "$header";
# output frequencies
# single nts
foreach my $nt (@expected_nts)
{
my $nt_count = $nts{$nt};
my $nt_freq = $nt_count / $seq_length;
printf ("\t%0.3f", $nt_freq);
}
# di nts
foreach my $nt1 (@expected_nts)
{
foreach my $nt2 (@expected_nts)
{
my $di_nt = $nt1.$nt2;
my $nt_count = $di_nts{$di_nt};
my $nt_freq = $nt_count / $seq_length;
printf ("\t%0.3f", $nt_freq);
}
}
print "\n";
}