-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRandom.pl
173 lines (155 loc) · 4.25 KB
/
getRandom.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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Switch;
my $chr1 = 3951982;
my $chr2 = 3683506;
my $chr3 = 3351453;
my $chr4 = 2873318;
my $chr5 = 2822964;
my $chr6 = 2483831;
my $chr7 = 2434682;
my $chr8 = 2299506;
my $chr9 = 2122865;
my $chr10 = 2105496;
my $chr11 = 2052242;
my $chr12 = 1878461;
my $chr13 = 1845946;
my $chr14 = 1815632;
my $chr15 = 1765292;
my $chr16 = 1419421;
my $bed = "";
my $fasta = "";
GetOptions('bed=s' => \$bed, 'fasta=s' => \$fasta);
my $usage = <<USAGE;
$0 --bed <bed file> --fasta <fasta file>
USAGE
if (($bed eq "") or ($fasta eq "")){
die ("\nIncorrect input\n$usage\n");
}
my $prefix = $bed;
$prefix =~ s/\.\S+//g;
open my $fh, "<", $bed or die ("\nFile not readable\n$usage\n");
while (my $line = <$fh>){
chomp $line;
my @fields = split /\s+/, $line;
my $seqSize = $fields[2] - $fields[1];
my $strand = $fields[5];
my $start;
switch ($fields[0]){
case "Chr_1" {$start = int(rand($chr1))}
case "Chr_2" {$start = int(rand($chr2))}
case "Chr_3" {$start = int(rand($chr3))}
case "Chr_4" {$start = int(rand($chr4))}
case "Chr_5" {$start = int(rand($chr5))}
case "Chr_6" {$start = int(rand($chr6))}
case "Chr_7" {$start = int(rand($chr7))}
case "Chr_8" {$start = int(rand($chr8))}
case "Chr_9" {$start = int(rand($chr9))}
case "Chr_10" {$start = int(rand($chr10))}
case "Chr_11" {$start = int(rand($chr11))}
case "Chr_12" {$start = int(rand($chr12))}
case "Chr_13" {$start = int(rand($chr13))}
case "Chr_14" {$start = int(rand($chr14))}
case "Chr_15" {$start = int(rand($chr15))}
case "Chr_16" {$start = int(rand($chr16))}
}
my $end = $start + $seqSize;
open my $fh1, ">>", "$prefix.randomised.bed";
print $fh1 "$fields[0]\t$start\t$end\t.\t.\t$strand\n";
close $fh1;
}
close $fh;
system("bedtools getfasta -fi $fasta -bed $prefix.bed -fo $prefix.fa");
system("bedtools getfasta -fi $fasta -bed $prefix.randomised.bed -fo $prefix.randomised.fa");
system("sizeseq -descending -sequences $prefix.fa -outseq $prefix.sorted.fa");
system("sizeseq -descending -sequences $prefix.randomised.fa -outseq $prefix.randomised.sorted.fa");
my $count = 0;
open $fh, "<", "$prefix.sorted.fa";
while (my $line = <$fh>){
if ($count < 51){
open my $fh1, ">>", "$prefix.top50.fa";
print $fh1 $line;
close $fh1;
}
if ($line =~ />/){
$count++;
}
}
close $fh;
$count = 0;
open $fh, "<", "$prefix.randomised.sorted.fa";
while (my $line = <$fh>){
if ($count < 51){
open my $fh1, ">>", "$prefix.randomised.top50.fa";
print $fh1 $line;
close $fh1;
}
if ($line =~ />/){
$count++;
}
}
close $fh;
my @seqArray;
my $head;
open $fh, "<", "$prefix.randomised.top50.fa" or die;
while (my $line = <$fh>){
chomp $line;
if ($line =~ />/){
if ($. > 1){
my $ta = () = join("", @seqArray) =~ /TA/g;
my $at = () = join("", @seqArray) =~ /AT/g;
my $ratio = $ta/$at;
open my $fh1, ">>", "$prefix.top50.index";
print $fh1 "random_$prefix\t$ratio\n";
close $fh1;
}
$head = $line;
$head =~ s/>//g;
@seqArray = ();
}
else {
my @seqs = split "", $line;
push @seqArray, @seqs;
if (eof($fh)){
my $ta = () = join("", @seqArray) =~ /TA/g;
my $at = () = join("", @seqArray) =~ /AT/g;
my $ratio = $ta/$at;
open my $fh1, ">>", "$prefix.top50.index";
print $fh1 "random_$prefix\t$ratio\n";
close $fh1;
}
}
}
close $fh;
open $fh, "<", "$prefix.top50.fa" or die ("\nFasta $prefix.fa not readable \n$usage\n");
while (my $line = <$fh>){
chomp $line;
if ($line =~ />/){
if ($. > 1){
my $ta = () = join("", @seqArray) =~ /TA/g;
my $at = () = join("", @seqArray) =~ /AT/g;
my $ratio = $ta/$at;
open my $fh1, ">>", "$prefix.top50.index";
print $fh1 "$prefix\t$ratio\n";
close $fh1;
}
$head = $line;
$head =~ s/>//g;
@seqArray = ();
}
else {
my @seqs = split "", $line;
push @seqArray, @seqs;
if (eof($fh)){
my $ta = () = join("", @seqArray) =~ /TA/g;
my $at = () = join("", @seqArray) =~ /AT/g;
my $ratio = $ta/$at;
open my $fh1, ">>", "$prefix.top50.index";
print $fh1 "$prefix\t$ratio\n";
close $fh1;
}
}
}
close $fh;