-
Notifications
You must be signed in to change notification settings - Fork 13
/
insertTEsintoFasta.pl
212 lines (180 loc) · 6.28 KB
/
insertTEsintoFasta.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
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/perl
use strict;
use warnings;
use Bio::SeqIO ;
use Getopt::Long;
use File::Basename ;
use Data::Dumper ;
my $VERSION = "1.0" ;
my $lastmodif = "2017-03-02" ;
my $prog = basename($0) ;
my $outformat= "FASTA";
my $bedFile ;
my $fastadb ;
my $help ;
&GetOptions(
"bed=s" => \$bedFile,
"db=s" => \$fastadb,
"h" => \$help
);
$help and &help;
&main( \@ARGV );
sub main {
my $self = {};
bless $self;
my $datestring = localtime();
print STDERR "INFO $prog: Running script $datestring\n" ;
$self->tsdSize();
$self->setOptions();
$self->{inputFiles} = shift;
foreach ( @{ $self->{inputFiles} } ) {
$self->{_currentFile} = $_;
unless ( -e $self->{_currentFile} ) {
print STDERR "INFO $prog: Could not find file: " . $self->{_currentFile} . " -> next file\n";
next;
}
my $finalSeq ;
my $idFile ;
my $description ;
my $seqio = Bio::SeqIO->new( -format => "FASTA" , -file => $self->{_currentFile});
while (my $seq = $seqio->next_seq) {
$idFile = $seq->display_id ;
$description = $seq->desc ;
my $kseq = 0;
foreach my $chr (sort keys(%{$self->{bedFile}})){
foreach my $bedkey (sort {$a <=> $b} keys(%{$self->{bedFile}->{$chr}})){
my $tsd = $self->{tsd}->{$self->{bedFile}->{$chr}->{$bedkey}->{sF}} ;
if($bedkey == 1){
$self->{subSeq}->{++$kseq} = $seq->subseq(1,$self->{bedFile}->{$chr}->{$bedkey}->{start}) ;
$self->{subSeq}->{++$kseq} = $self->{fastadb}->{$self->{bedFile}->{$chr}->{$bedkey}->{id}}->seq ;
}
else{
my $prevSt = $self->{bedFile}->{$chr}->{$bedkey-1}->{start}-$tsd+1 ;
if($prevSt > $self->{bedFile}->{$chr}->{$bedkey}->{start}){
print STDERR "DIE $prog: Bed file should be sorted\n";
exit ;
}
$self->{subSeq}->{++$kseq} = $seq->subseq($prevSt,$self->{bedFile}->{$chr}->{$bedkey}->{start}) ;
$self->{subSeq}->{++$kseq} = $self->{fastadb}->{$self->{bedFile}->{$chr}->{$bedkey}->{id}}->seq ;
}
if(not defined $self->{bedFile}->{$chr}->{$bedkey+1}){ # last feat, add the end of the seq
$self->{subSeq}->{++$kseq} = $seq->subseq($self->{bedFile}->{$chr}->{$bedkey}->{start}-$tsd+1,$seq->length) ;
}
}
}
}
foreach my $key (sort {$a <=> $b} keys %{$self->{subSeq}}){
$finalSeq .= $self->{subSeq}->{$key} ;
}
my $finalSeqObj = Bio::Seq ->new(
'-seq' => $finalSeq,
'-id' => $idFile,
'-desc' => $description) ;
my $out_seqio_obj = Bio::SeqIO ->new('-fh' => \*STDOUT, '-format' => "FASTA") ;
$out_seqio_obj ->write_seq ($finalSeqObj) ;
} # END OF foreach inputFiles
}
sub setOptions {
my $self = shift;
if(defined $bedFile){
$self->readBedFile() ;
}
else{
print STDERR "ERROR $prog: Need to specify either option -bed.\n" and &help ;
}
if(defined $fastadb){
$self->readFastadb() ;
}
else{
print STDERR "ERROR $prog: Need to specify either option -db.\n" and &help ;
}
}
sub readFastadb{
my $self = shift;
unless (-e $fastadb) {print STDERR "ERROR $prog: Could not find file: $fastadb \n" ; &help ; }
my $seqio = Bio::SeqIO->new( -format => "FASTA" , -file => $fastadb);
while (my $seq = $seqio->next_seq) {
$self->{fastadb}->{$seq->display_id} = $seq ;
}
}
sub readBedFile {
my $self = shift;
my $count = 0;
unless ( -e $bedFile ) {print STDERR "ERROR $prog: Could not find file: " . $bedFile . "\n" ; &help ; }
open( "BED", $bedFile )
or print STDERR "DIE $prog: Could not open file: " . $bedFile . "\n" and die;
while (<BED>) {
next if /^#/;
chomp;
my @line = split("\t");
my ($chr, $start, $stop, $featid);
if ( scalar(@line) < 3 ) {
print STDERR "ERROR $prog: Bed file must have 3 column in line: $_ \n";
die;
}
elsif(scalar(@line) == 3 ){
($chr, $start, $stop) = @line;
$featid = $chr.":".$start."-".$stop ;
}
else{
($chr, $start, $stop, $featid) = @line;
}
if ( $start !~ /^\d+$/ or $stop !~ /^\d+$/ ) {
print STDERR "ERROR $prog: Warning in bed file: start or stop should be a number in line: $_ \n";
next;
}
( $start, $stop ) = ( $stop, $start ) if ( $start > $stop );
$start += 1; # put start in base 1
$count += 1 ;
$self->{bedFile}->{$chr}->{$count} = { start => $start, stop => $stop, id => $featid, fam => $line[4], sF => $line[5]};
}
print STDERR "Bed file loaded, $count lines\n";
if ( defined( $bedFile ) ) {
print STDERR "INFO $prog: read tab File $bedFile done !\n";
}
}
sub tsdSize {
my $self = shift;
$self->{tsd}->{"LTR/Copia"} = 5 ;
$self->{tsd}->{"LTR/Gypsy"} = 5 ;
$self->{tsd}->{"RC/Helitron"} = 0 ;
$self->{tsd}->{"DNA/En-Spm"} = 3 ;
$self->{tsd}->{"DNA/MuDR"} = 9 ;
$self->{tsd}->{"Unassigned"} = 5 ;
$self->{tsd}->{"DNA/HAT"} = 5 ;
$self->{tsd}->{"LINE/L1"} = 9 ;
$self->{tsd}->{"RLC"} = 5 ;
$self->{tsd}->{"RLG"} = 5 ;
$self->{tsd}->{"DHH"} = 0 ;
$self->{tsd}->{"RST"} = 9 ;
$self->{tsd}->{"DTC"} = 3 ;
$self->{tsd}->{"DTM"} = 9 ;
$self->{tsd}->{"DTA"} = 5 ;
$self->{tsd}->{"DTH"} = 5 ;
}
#=============================================================================
# HELP
#=============================================================================
sub help {
my $prog = basename($0);
print STDERR <<EOF ;
### $prog ###
#
# CREATED: 2017-01-04
# LAST MODIF: $lastmodif
# AUTHOR: Josquin DARON (Ohio State University, Slotkin Lab)
# VERSION: $VERSION
# PURPOSE: Insert sequences (TEs) and it corresponding TSD within single FASTA sequence.
USAGE: $prog -bed <bed> -db <FASTA> <FASTA>
<FASTA> Single FASTA file such as one chr sequence.
### OPTIONS ###
-bed <FILE> Bed file with customized format
Column 4: TE id of TE that will be inserted (and present in -db FASTA file)
Column 6: superfamily id of TE that will be inserted (will decided TSD size).
Wicker classification accronymes is accepted (Wicker et al., 2007).
-db <FILE> Multi-FASTA file of TE sequences that will be inserted.
-h|--help: print this help
EOF
exit(1) ;
}
# END OF SCRIPT