-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpyrad2fasta.pl
More file actions
executable file
·97 lines (69 loc) · 1.97 KB
/
pyrad2fasta.pl
File metadata and controls
executable file
·97 lines (69 loc) · 1.97 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Path;
# Declare variables
my $line;
my @loci;
my $workdir="";
my @fasta;
my $i = 1;
my $input;
my $batch;
parseArgs();
my $output="loci";
$batch and $output.=$batch;
# open file and read it in
open( LOCI, $input ) || die "Can't open $input: $!\n";
# Make the loci directory to put fasta files from pyrad2fasta subroutine
chdir $workdir;
rmtree $output;
mkdir $output;
chdir $output;
while ( $line = <LOCI> ){
if( $line =~ /^\/\// ){
if( $line =~ /\*|\-/ ){
pyrad2fasta( @loci, $i );
undef( @loci );
$i++;
}else{
undef( @loci );
}
}else{
push @loci, $line;
}
}
close LOCI;
exit;
###########################SUBROUTINES###################################
sub parseArgs{
#Message to print if mandatory variables not declared
my $usage ="\npyrad2fasta.pl takes the custom .loci output from pyRAD and creates a new FASTA file for each locus containing at least 1 SNP.
Usage: $0 --i /path/to/*.loci --w /path/to/workdir
Mandatory
-i, --input - path to the input file (*.loci from pyRAD)
-w, --workdir - path to working directoy (new fasta files will be placed within /workdir/loci
Optional
-b, --batch - Provide a batch number
\n";
my $options = GetOptions
(
'input|i=s' => \$input,
'workdir|w=s' => \$workdir,
'batch|b=i' => \$batch,
);
$input or die "\n\nError: Input not specified!\n\n$usage\n";
if ( $workdir eq ""){die "\nDerp: Working directory not specified!\n\n"};
}
#########################################################################
sub pyrad2fasta{
# split at whitespace
for my $element ( @loci ){
open( OUT, '>>', "$i.fasta" ) || die "Error. Can't write to $i.fasta: $!\n\n";
@fasta = split( /\s+/, $element );
print OUT $fasta[0], "\n";
print OUT $fasta[1], "\n";
}
# Print the loci in FASTA format
}