forked from Ming-Lian/Bioinfo_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitFasta.pl
77 lines (63 loc) · 1.92 KB
/
splitFasta.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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use POSIX;
# 帮助文档
=head1 Description
This script is used to split fasta file, which is too large with thosands of sequence
=head1 Usage
$0 -i <input> -o <output_dir> [-n <seq_num_per_file>] [-m <output_file_num>]
=head1 Parameters
-i [str] Input raw fasta file
-o [str] Output file to which directory
-n [int] Sequence number per file, alternate chose paramerter "-n" or "-m", if set "-n" and "-m" at the same time, only take "-n" parameter
-m [int] Output file number (default:100)
=cut
my ($input,$output_dir,$seq_num,$file_num);
GetOptions(
"i:s"=>\$input,
"o:s"=>\$output_dir,
"n:i"=>\$seq_num,
"m:i"=>\$file_num
);
die `pod2text $0` if ((!$input) or (!$output_dir));
# 设置每个文件的序列条数
if(!defined($seq_num)){
if(!defined($file_num)){
$file_num=100;
my $total_seq_num=`awk 'BEGIN{n=0} /^>/{n++} END{print n}' $input`;
chomp $total_seq_num;
$seq_num=ceil($total_seq_num/$file_num);
}else{
my $total_seq_num=`awk 'BEGIN{n=0} /^>/{n++} END{print n}' $input`;
chomp $total_seq_num;
$seq_num=ceil($total_seq_num/$file_num);
}
}
open IN,"<$input" or die "Cann't open $input\n";
my $n_seq=0; # 该变量用于记录当前扫描到的序列数
my $n_file=1; # 该变量用于记录当前真正写入的文件的计数
my $input_base=`basename $input`;
chomp $input_base;
open OUT,">$output_dir/${input_base}_${n_file}" or die "Cann't create $output_dir/${input_base}_${n_file}\n";
while(<IN>){
next if (/^\s+$/); # 跳过空行
chomp;
if (/^>/){
$n_seq++;
# 判断目前已经扫描到的序列数,若大于设定的split的序列数,则创建新文件
if ($n_seq>$seq_num){
$n_seq=1;
$n_file++;
close OUT;
open OUT,">$output_dir/${input_base}_${n_file}" or die "Cann't create $output_dir/${input_base}_${n_file}\n";
print OUT "$_\n";
}else{
print OUT "$_\n";
}
}else{
print OUT "$_\n";
}
}
close IN;