forked from wang-q/fig_table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta2csv.pl
208 lines (174 loc) · 5.03 KB
/
fasta2csv.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use YAML qw(Dump Load DumpFile LoadFile);
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use List::MoreUtils qw(any all);
#----------------------------------------------------------#
# GetOpt Section
#----------------------------------------------------------#
my $infile = "../116_112.fasta";
my $wrap = 60;
my $spacing = 0;
my $outfile = "";
my $has_reference = 1;
my $filter = "ingroup"; # outgroup or ingroup
my $man = 0;
my $help = 0;
GetOptions(
'help|?' => \$help,
'man' => \$man,
'infile=s' => \$infile,
'wrap=i' => \$wrap,
'spacing=i' => \$spacing,
'outfile=s' => \$outfile,
'reference=s' => \$has_reference,
'filter=s' => \$filter,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
unless ($outfile) {
$outfile = $infile;
if ( $outfile =~ /\./ ) {
$outfile =~ s/\.\w*?$/\.csv/;
}
else {
$outfile .= ".csv";
}
}
#----------------------------------------------------------#
# START, read in seqs
#----------------------------------------------------------#
# Read out file
open my $fasta_fh, '<', $infile
or die "Cannot open Pearson file $infile";
my @file_contents = <$fasta_fh>;
close $fasta_fh;
my @seq_names;
my %seqs;
foreach my $current_line (@file_contents) {
if ( $current_line =~ /^\>([\w-])+/ ) {
$current_line =~ s/\>//;
chomp $current_line;
push @seq_names, $current_line;
$seqs{$current_line} = '';
}
elsif ( $current_line =~ /^[\w-]+/ ) {
$current_line =~ s/[^\w-]//g;
chomp $current_line;
my $current_seq_name = $seq_names[-1];
$seqs{$current_seq_name} .= $current_line;
}
else { # Blank line, do nothing
}
}
# seq names
my $ref_name;
my @total_names;
if ($has_reference) {
@total_names = @seq_names;
$ref_name = shift @seq_names;
}
else {
@total_names = @seq_names;
}
# seq number
my $seq_number = scalar @seq_names;
my $total_number = scalar @total_names;
# seq length
my @total_lengths;
foreach (@total_names) {
push @total_lengths, length $seqs{$_};
}
unless ( all { $_ == $total_lengths[0] } @total_lengths ) {
die "Seq length error.\n";
}
else {
print "Length: $total_lengths[0] bp\n";
}
#----------------------------------------------------------#
# Collect SNP sites (indel and Ns are treated as SNPs)
#----------------------------------------------------------#
my $result_matrix = [ [] ]; # csv matrix
my $section = 0;
my $snp_count = 0;
# add seq names to lines
foreach ( 0 .. $total_number ) {
push @{ $result_matrix->[$_] }, ( " ", @total_names )[$_];
}
for ( my $i = 1; $i <= $total_lengths[0]; $i++ ) {
my @cur_column;
my @cur_in_column;
foreach (@total_names) {
my $base = substr( $seqs{$_}, $i - 1, 1 );
push @cur_column, $base;
}
# if ingroup, reference is not count as SNP sites
if ( $filter eq "ingroup" ) {
@cur_in_column = @cur_column;
shift @cur_in_column;
}
else {
@cur_in_column = @cur_column;
}
if ( minstr(@cur_in_column) ne maxstr(@cur_in_column) ) {
unshift @cur_column, $i;
$snp_count++;
foreach ( 0 .. $total_number ) {
push @{ $result_matrix->[ $_
+ $section * ( $total_number + 1 + $spacing ) ] },
$cur_column[$_];
}
# add seq names to wrapped lines
if ( ( $snp_count % $wrap ) == 0 ) {
$section++;
print "Section: $section\n";
foreach ( 0 .. $total_number ) {
push @{ $result_matrix->[ $_
+ $section * ( $total_number + 1 + $spacing ) ] },
( " ", @total_names )[$_];
}
}
}
}
#----------------------------------------------------------#
# Output CSV
#----------------------------------------------------------#
open my $csv_fh, '>', $outfile
or die "Cannot write CSV file $outfile";
foreach (@$result_matrix) {
if ( ref($_) eq 'ARRAY' ) {
print {$csv_fh} join( ",", (@$_) ), "\n";
}
else {
print {$csv_fh} "\n";
}
}
close $csv_fh;
__END__
=head1 NAME
fasta2csv.pl - Extract polymorphic sites from fasta alignment file
=head1 SYNOPSIS
fasta2csv.pl [options]
Options:
--help brief help message
--man full documentation
--infile input file name (full path)
--wrap wrap number
--spacing wrapped line spacing
--outfile output file name
--reference has reference?
--filter ingroup or outgroup sites
=head1 OPTIONS
=over 8
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do someting
useful with the contents thereof.
=cut