forked from tallulandrews/scRNASeqPipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0_Anno_Extract_Transcriptome.pl
183 lines (171 loc) · 5.21 KB
/
0_Anno_Extract_Transcriptome.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
use strict;
use warnings;
if (@ARGV < 2) {die "0_My_Extract_Transcriptome.pl .gtf .fa\n";}
my %Ensg2Seq = ();
my %Ensg2Gtf = ();
my @Ensgs = ();
my $flank = 10;
my $nascent = 1;
open (my $fa, $ARGV[1]) or die $!;
open (my $fa_out, ">","Transcripts.fa") or die $!;
open (my $gtf_out, ">","Transcripts.gtf") or die $!;
my $chr = "None";
my $chr_seq = "";
my $COUNT = 0;
while (<$fa>) {
if($_ =~ /^#/) {next;} # skip headers
if ($_ =~ /^\>/) {
# New Chr
my @line = split(/\s+/);
my $newchr = $line[0]; $newchr =~ s/>//g;
if ($chr eq "None") {
$chr = $newchr;
next;
}
# Output gene sequences for this chromosome
open (my $gtf, $ARGV[0]) or die $!;
my $gtf_line = "";
while ($gtf_line = <$gtf>) {
# Extract sequence for each gene on this Chr.
if ($gtf_line =~ /^#/) {next;} # ignore headers
my $geneid = "";
if ($gtf_line =~ /gene_id "(.+?)";/) {
$geneid = $1;
} else {
next;
} # get gene id
# Get coordinates
my @record = split(/\t/, $gtf_line);
my $seq_chr = $record[0];
if ($seq_chr ne $chr) {next;}
my $seq_st = $record[3];
my $seq_end = $record[4];
if ($seq_chr ne $chr) {die "Something has gone terribly wrong $seq_chr $chr\n";}
# Get sequence
if ($record[2] eq "gene") {
# Add null flanks as necessary
if ($seq_st-$flank < 0) {
$chr_seq = ('N' x $flank) . $chr_seq;
$seq_st = $seq_st+$flank;
$seq_end = $seq_end+$flank;
}
if ($seq_end+$flank > length($chr_seq)) {
$chr_seq = $chr_seq . ('N' x $flank);
}
$Ensg2Seq{$geneid} = substr($chr_seq, $seq_st-$flank, ($seq_end-$seq_st+$flank));
push(@Ensgs, $geneid);
}
# Store Annotations
if ($record[2] eq "exon" || $record[2] eq "UTR" || $record[2] eq "gene") {
push(@{$Ensg2Gtf{$geneid}}, $gtf_line);
}
}
close($gtf);
# Write output for all genes on this Chr
foreach my $ensg (@Ensgs) {
print $fa_out ">$ensg\n";
print $fa_out $Ensg2Seq{$ensg}."\n";
my $seq_length = length($Ensg2Seq{$ensg});
my $shift = -1;
foreach my $old_gtf (@{$Ensg2Gtf{$ensg}}) {
$old_gtf =~ s/transcript_id "(.+?)"/transcript_id "$ensg"/s;
$old_gtf =~ s/gene_id "(.+?)"/gene_id "$ensg"/s;
$old_gtf =~ s/gene_name "(.+?)"/gene_name "$ensg"/s;
my @record = split(/\t/, $old_gtf);
if($shift == -1 && $record[2] ne "gene") {die "ERROR: Requires first entry for each ensg to be \"gene\".\n";}
if ($shift == -1) {
$shift = $record[3]-$flank;
}
if (scalar(@record) < 5) {die "$old_gtf not enough entries\n";}
$record[0] = $ensg;
$record[3] = $record[3]-$shift;
$record[4] = $record[4]-$shift;
if ($record[4] > $seq_length) {
print STDERR "$chr $ensg $record[2] $record[3] $record[4], seq = $seq_length\n";
die "ERROR: annotation exceeds sequence length\n";
}
print $gtf_out join("\t",@record);
}
}
print "$chr $newchr\n";
$chr = $newchr;
$chr_seq="";
$COUNT=0;
@Ensgs=();
} else {
# Read in chr sequence
chomp;
$chr_seq = $chr_seq.$_;
}
}
# Output last chromosome
# Output gene sequences
{
# Output gene sequences for this chromosome
open (my $gtf, $ARGV[0]) or die $!;
my $gtf_line = "";
while ($gtf_line = <$gtf>) {
# Extract sequence for each gene on this Chr.
if ($gtf_line =~ /^#/) {next;} # ignore headers
my $geneid = "";
if ($gtf_line =~ /gene_id "(.+?)";/) {
$geneid = $1;
} else {
next;
} # get gene id
# Get coordinates
my @record = split(/\t/, $gtf_line);
my $seq_chr = $record[0];
if ($seq_chr ne $chr) {next;}
my $seq_st = $record[3];
my $seq_end = $record[4];
if ($seq_chr ne $chr) {die "Something has gone terribly wrong $seq_chr $chr\n";}
# Get sequence
if ($record[2] eq "gene") {
# Add null flanks as necessary
if ($seq_st-$flank < 0) {
$chr_seq = ('N' x $flank) . $chr_seq;
$seq_st = $seq_st+$flank;
$seq_end = $seq_end+$flank;
}
if ($seq_end+$flank > length($chr_seq)) {
$chr_seq = $chr_seq . ('N' x $flank);
}
$Ensg2Seq{$geneid} = substr($chr_seq, $seq_st-$flank, ($seq_end-$seq_st+$flank));
push(@Ensgs, $geneid);
}
# Store Annotations
if ($record[2] eq "exon" || $record[2] eq "UTR" || $record[2] eq "gene") {
push(@{$Ensg2Gtf{$geneid}}, $gtf_line);
}
}
close($gtf);
# Write output for all genes on this Chr
foreach my $ensg (@Ensgs) {
print $fa_out ">$ensg\n";
print $fa_out $Ensg2Seq{$ensg}."\n";
my $seq_length = length($Ensg2Seq{$ensg});
my $shift = -1;
foreach my $old_gtf (@{$Ensg2Gtf{$ensg}}) {
$old_gtf =~ s/transcript_id "(.+?)"/transcript_id "$ensg"/s;
$old_gtf =~ s/gene_id "(.+?)"/gene_id "$ensg"/s;
$old_gtf =~ s/gene_name "(.+?)"/gene_name "$ensg"/s;
my @record = split(/\t/, $old_gtf);
if($shift == -1 && $record[2] ne "gene") {die "ERROR: Requires first entry for each ensg to be \"gene\".\n";}
if ($shift == -1) {
$shift = $record[3]-$flank;
}
$record[0] = $ensg;
$record[3] = $record[3]-$shift;
$record[4] = $record[4]-$shift;
if ($record[4] > $seq_length) {die "ERROR: annotation exceeds sequence length\n";}
print $gtf_out join("\t",@record);
}
}
exit();
$chr_seq="";
$COUNT=0;
@Ensgs=();
}
close($gtf_out);
close($fa);