-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsort_paired_end_fasta.pl
More file actions
106 lines (106 loc) · 3.24 KB
/
Copy pathsort_paired_end_fasta.pl
File metadata and controls
106 lines (106 loc) · 3.24 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
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl -w
#############################
### Jennifer Meneghin ###
### June 25, 2012 ###
#############################
#---------------------------------------------------------------------------------------------------------------------------
#Deal with passed parameters
#---------------------------------------------------------------------------------------------------------------------------
if ($#ARGV == -1) {
&usage;
}
$in_file = "";
$out_file = "SortedSequences.fa";
$split_text = "";
%my_args = @ARGV;
for $i (sort keys %my_args) {
if ($i eq "-i") {
$in_file = $my_args{$i};
}
elsif ($i eq "-o") {
$out_file = $my_args{$i};
}
elsif ($i eq "-s") {
$split_text = $my_args{$i};
}
else {
print "\nUnrecognized argument: $i\n\n";
&usage;
}
}
unless ( open(IN, "$in_file") ) {
print "\nGot a bad sequence ID list file: $in_file\n\n";
&usage;
}
unless ( open(OUT, ">$out_file") ) {
print "\nGot a bad output file: $out_file\n\n";
&usage;
}
print "Parameters:\nfasta file = $in_file\noutput file = $out_file\n\n";
#---------------------------------------------------------------------------------------------------------------------------
#The main event
#---------------------------------------------------------------------------------------------------------------------------
$seq = "";
$header = "";
while(<IN>) {
chomp;
if (/^>/) {
if (length($header) > 0) {
@parts = split(/$split_text/, $header);
@parts2 = split(/\//, $parts[1]);
unless ($parts2[0] =~ /^\d+$/) {
print "This must be an integer for this sort: " . $parts[0] . "\n";
&usage;
exit(1);
}
$my_num = $parts2[0] . $parts2[1];
$seqs{$my_num} = $seq;
$headers{$my_num} = $header;
$header = "";
$seq = "";
}
$header = $_;
}
else {
$seq = $seq . $_;
}
}
if (length($header) > 0) {
@parts = split(/Rec=/, $header);
@parts2 = split(/\//, $parts[1]);
unless ($parts2[0] =~ /^\d+$/) {
print "This must be an integer for this sort: " . $parts[0] . "\n";
&usage;
exit(1);
}
$my_num = $parts2[0] . $parts2[1];
$seqs{$my_num} = $seq;
$headers{$my_num} = $header;
}
for $key (sort sort_by_num keys %headers) {
print OUT "$headers{$key}\n";
print OUT "$seqs{$key}\n";
}
close(IN);
close(OUT);
#-----------------------------------------------------------------------
sub sort_by_num {
$a <=> $b;
}
#-----------------------------------------------------------------------
sub usage {
print "\nUsage: sort_fasta.pl\n\n";
print "Parameters:\n";
print "-i input file\tA fasta file\n";
print "-o output file\tA fasta file\n";
print "-s text\t\tText to split on\n\n";
print "This script sorts a paired end fasta file based on the header.\n";
print "It will sort NUMERICALLY based on the header information that comes AFTER the text provided (-s).\n\n";
print "Because it assumes paired ends, it assumes that the header ends with /1 or /2.\n";
print "So, for this to work your headers should look something like:\n";
print ">header text here:text to split on here:1234567890/1\n";
print "\nJennifer Meneghin\n";
print "July 2, 2012\n\n";
exit;
}
#-----------------------------------------------------------------------