forked from liulab-dfci/TRUST4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildDatabaseFa.pl
144 lines (123 loc) · 2.85 KB
/
BuildDatabaseFa.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
#!/usr/bin/env perl
use warnings ;
use strict ;
die "usage: perl BuildDatabaseFa.pl reference.fa annotation.gtf interested_gene_name_list > output.fa\n" if ( @ARGV != 3 ) ;
my %genome ;
my %interestedGeneName ;
# Read in the reference genome.
open FP1, $ARGV[0] ;
my $chrom = "" ;
my $seq = "" ;
my $hasChrPrefix = 0 ;
while ( <FP1> )
{
if ( /^>/ )
{
$genome{ $chrom } = $seq if ( $chrom ne "" ) ;
$seq = "" ;
$chrom = substr( ( split )[0], 1 ) ;
if ( $chrom =~ /^c/ )
{
$hasChrPrefix = 1 ;
}
}
else
{
chomp ;
$seq .= $_ ;
}
}
$genome{ $chrom } = $seq if ( $chrom ne "" ) ;
close FP1 ;
# Read in the gene name we interested in.
open FP1, $ARGV[2] ;
while ( <FP1> )
{
chomp ;
$interestedGeneName{ $_ } = 1 ;
}
close FP1 ;
# Read in the gtf file and output interested sequences.
open FP1, $ARGV[1] ;
$seq = 0 ;
#chr14 HAVANA UTR 21712321 21712330 . + . gene_id "ENSG00000211776.2"; transcript_id "ENST00000390424.2"; gene_type "TR_V_gene"; gene_name "TRAV2"; transcript_type "TR_V_gene"; transcript_name "TRAV2-201"; exon_number 1; exon_id "ENSE00001508005.2"; level 2; protein_id "ENSP00000438195.1"; transcript_support_level "NA"; tag "mRNA_end_NF"; tag "cds_end_NF"; tag "basic"; tag "appris_principal_1"; havana_gene "OTTHUMG00000168980.2"; havana_transcript "OTTHUMT00000401875.2";
my $prevTname = "" ;
my $gname = "" ;
my $strand = "." ;
my @range ;
sub OutputGene
{
my $i ;
$chrom = $range[0] ;
my $start = $range[1] ;
my $end = $range[-1] ;
my $output = "" ;
if ($strand eq "-" )
{
$start = $range[-2] ;
$end = $range[2] ;
}
die "Unknown chrom id $chrom " if ( !defined $genome{ $chrom } ) ;
print ">$gname $chrom $start $end $strand\n" ;
for ( $i = 0 ; $i < scalar( @range ) ; $i += 3 )
{
my $tmp = uc( substr( $genome{ $range[$i] }, $range[$i + 1] - 1, $range[$i + 2] - $range[$i + 1] + 1 ) ) ;
if ( $strand eq "-" )
{
$tmp = reverse( $tmp ) ;
$tmp =~ tr/ACGT/TGCA/ ;
}
$output .= $tmp ;
}
print( "$output\n" ) ;
}
while ( <FP1> )
{
next if ( /^#/ ) ;
chomp ;
my @cols = split /\t/ ;
next if ( $cols[2] ne "exon" ) ;
my $tname ;
if ( $cols[8] =~ /transcript_name \"(.*?)\"/ )
{
#print $1, "\n" ;
$tname = $1 ;
}
else
{
die "No transcript_name", $_, "\n" ;
}
if ( $tname ne $prevTname )
{
if ( (defined $interestedGeneName{ $gname } ) && @range > 0 )
{
OutputGene() ;
}
$prevTname = $tname ;
if ( $cols[8] =~ /gene_name \"(.*?)\"/ )
{
#print $1, "\n" ;
$gname = uc($1) ;
}
else
{
die "No gene_name: ", $_, "\n" ;
}
$strand = $cols[6] ;
undef @range ;
}
if ( $hasChrPrefix == 1 && !( $cols[0] =~ /^c/) )
{
$cols[0] = "chr".$cols[0] ;
}
elsif ( $hasChrPrefix == 0 && $cols[0] =~ /^c/ )
{
$cols[0] = substr( $cols[0], 3 ) ;
}
push @range, $cols[0], $cols[3], $cols[4] ;
}
if ( (defined $interestedGeneName{ $gname } ) && @range > 0 )
{
OutputGene() ;
}
close FP1 ;