-
Notifications
You must be signed in to change notification settings - Fork 7
/
gffbytype.pl
executable file
·70 lines (58 loc) · 1.69 KB
/
gffbytype.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
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use File::Basename;
my $suffix = ".gff";
my %type; # hash to hold gff lines of each type
# Process the command line.
my $outDir = ".";
GetOptions("o=s" => \$outDir);
if(@ARGV == 0)
{
help();
die();
}
# Read lines from file(s) specified on command line. Store in $_.
while (<>)
{
# \S matches non-whitespace. If not found in $_, skip to next line.
next unless /\S/;
# \s matches whitespace followed by a "#". If a comment is found in $_ line, skip.
next if /^\s*\#/;
# Split the $_ line into 9 tab separated fields.
my @f = split /\t/, $_, 9;
# Save the line in the "type" hash with key of gff type (third field).
$type{$f[2]} .= $_;
}
# Create output directory if it doesn't exist.
if (! -e $outDir)
{
mkdir($outDir) or die("Unable to create output directory $outDir");
}
local *FILE;
# Iterate through the hash.
while (my ($type, $file) = each %type)
{
my $filename = $outDir . "/" . $type . $suffix;
open FILE, ">$filename" or die "Couldn't write '$filename'";
print FILE $file;
close FILE or die "Couldn't write '$filename'";
# Sort the output file.
my $sortCmd = "gffsort.pl $filename";
my $sortedFile = `$sortCmd` or die "Couldn't sort $filename:$?";
open FILE, ">$filename" or die "Couldn't write '$filename'";
print FILE $sortedFile;
close FILE or die "Couldn't write '$filename'";
}
sub help
{
my $prog = basename($0);
print STDERR <<EOF;
$prog:
Split input gff files into subfiles by gff type.
gffsort.pl is called on each subfile.
Usage: $prog [options] <gffFile1> <gffFile2> ...
Options
-o <directory>: specify output directory (default is current directory)
EOF
}