-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatc.track.maker.pl
200 lines (162 loc) · 3.93 KB
/
gatc.track.maker.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
#!/usr/bin/perl -w
use strict;
$|++;
my @in_files;
my %vars = (
'name' => '',
'scaffolds' => 0,
'mito' => 0,
);
my %vars_details = (
'name' => 'Name of organism (for output file)',
'scaffolds' => 'Process scaffold assemblies (not recommended)',
'mito' => 'Process mitochondrial chromosome (not recommended)',
);
process_cli();
# Globals
my $motif = "GATC";
my $motif_len = 4;
my $file = shift(@in_files);
my ($fhead) = $file =~ m/(.*?)./;
$vars{'name'} ||= $fhead;
generate_track($file);
print STDOUT "All done. \n\n";
sub generate_track {
my $fn = shift;
my $chr;
# open output files
my $track_name = $vars{'name'}.".GATC.gff";
print STDOUT "Writing data to $track_name ...\n\n";
open (TRACK, ">$track_name") || die "Cannot open $track_name for writing: $!\n";
# load sequence data
print STDOUT "Opening $fn ...\n";
my $in="";
if ($fn =~ m/\.gz$/) {
# gzipped fasta file
open (FN, "gunzip -c $fn |") || die "Error: cannot open $fn: $!\n\n";
} else {
open (FN, "<$fn") || die "Error: cannot open $fn: $!\n\n";
}
while (<FN>) {
if (m/^\>/) {
# New chromosome header
if (m/scaffold/i) {
unless ($vars{'scaffolds'}) {
process($chr, $in);
$in = "";
$chr = "";
next;
}
}
if (m/mito/i) {
unless ($vars{'mito'}) {
process($chr, $in);
$in = "";
$chr = "";
next;
}
}
process($chr, $in);
# get new chromosome name
($chr) = m/^>(.*?)\s/;
# Reset $in, and don't add the header line!
$in = "";
next;
}
chomp;
s/\s//g;
$in .= $_;
}
# process the last chromosome
process($chr, $in);
close FN;
close TRACK;
}
sub process {
my ($chr, $in) = @_;
return unless $chr && $in;
# process what we've got ...
print STDERR "Processing $chr ... \n";
motif_hash($in, $chr);
}
sub motif_hash {
my ($in, $chr) = @_;
my $l = length($in);
for my $base (0 .. ($l-($motif_len+1))) {
if ($base%100000==0) {
my $pc = sprintf("%0.0f",($base*100)/$l);
print STDERR " $pc% done ...\r";
}
my $w = substr($in,$base,$motif_len);
if ($w =~ m/$motif/gi) {
print TRACK join("\t",$chr, ".", ".", $base, ($base+$motif_len), 1, '+', '.', '.'), "\n";
}
}
}
sub process_cli {
# CLI processing
foreach (@ARGV) {
if (/--(.*)=(.*)/) {
unless (defined($vars{$1})) {
print STDERR "Did not understand $_ ...\n";
help();
}
my ($v, $opt) = ($1,$2);
$vars{$v} = $opt;
next;
} elsif (/--h[elp]*/) {
help();
} elsif (/--(.*)/) {
# if no parameter is specified we assume it's a switch ...
# (could be a bit nicer and check this is ok with a hash representing data type ...)
if (defined($vars{$1})) {
$vars{$1} = 1;
} else {
print STDERR "Did not understand $_ ...\n";
help();
}
next;
}
push @in_files, $_;
}
help() unless @in_files;
}
sub help {
print STDERR <<EOT;
gatc.track.maker.pl -- generates a GFF file containing the locations of all GATC sites in the genome sequence
(Use on a single genome FASTA file)
Options:
EOT
my $opt_len = 0;
foreach (keys %vars) {
my $l = length($_);
#print "--> $_: $l\n";
$opt_len = $l if $l > $opt_len;
}
$opt_len+=2;
my $cols= `tput cols` || 80;
my ($v, $val, $def, $def_format);
my $help_format = "format STDOUT =\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-4) . "\n"
.'$v, $def_format'."\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-6) . "~~\n"
.'$v, $def_format'."\n"
.".\n";
eval $help_format;
die $@ if $@;
foreach my $k (sort (keys %vars)) {
($v, $val, $def) = ($k, $vars{$k}, $vars_details{$k});
$def||="";
$def_format = $val ? "$def\n\r[Current value: $val]" : $def;
$v = "--$v";
# format =
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#$v, $def_format
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
#$v, $def_format
#.
write();
}
print STDOUT "\n";
exit 1;
}