forked from vikas0633/perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnv_ltrseq2gff.pl
734 lines (570 loc) · 20.9 KB
/
cnv_ltrseq2gff.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#!/usr/bin/perl -w
#-----------------------------------------------------------+
# |
# cnv_ltrseq2gff.pl - Convert LTR_seq output to gff format. |
# |
#-----------------------------------------------------------+
# |
# AUTHOR: James C. Estill |
# CONTACT: JamesEstill_@_gmail.com |
# STARTED: 09/03/2007 |
# UPDATED: 03/02/2010 |
# |
# DESCRIPTION: |
# Converts LTR_seq output to gff file format. |
# |
# LICENSE: |
# GNU General Public License, Version 3 |
# http://www.gnu.org/licenses/gpl.html |
# |
#-----------------------------------------------------------+
# TODO: Extract only the unique LTR Predictions
package DAWGPAWS;
#-----------------------------+
# INCLUDES |
#-----------------------------+
use strict;
use Getopt::Long;
# The following needed for printing help
use Pod::Select; # Print subsections of POD documentation
use Pod::Text; # Print POD doc as formatted text file
use IO::Scalar; # For print_help subfunction
use IO::Pipe; # Pipe for STDIN, STDOUT for POD docs
use File::Spec; # Convert a relative path to an abosolute path
#-----------------------------+
# PROGRAM VARIABLES |
#-----------------------------+
my ($VERSION) = q$Rev: 948 $ =~ /(\d+)/;
# Get GFF version from environment, GFF2 is DEFAULT
my $gff_ver = uc($ENV{DP_GFF}) || "GFF2";
#-----------------------------+
# VARIABLE SCOPE |
#-----------------------------+
my $infile; # Path of the LTR_seq file to convert
my $outfile; # Path to the gff format file created
my $inseqname; # Name of the sequence analyzed
my $parameter; # The parameter set used
my $program="LTR_seq"; # The program name
# Optional
my $infasta; # Path to the FASTA file analyzed
# Booleans
my $quiet = 0;
my $verbose = 0;
my $show_help = 0;
my $show_usage = 0;
my $show_man = 0;
my $show_version = 0;
my $do_gff_append = 0;
#-----------------------------+
# COMMAND LINE OPTIONS |
#-----------------------------+
my $ok = GetOptions(# Required options
"i|infile=s" => \$infile,
"o|outfile=s" => \$outfile,
"s|seqname=s" => \$inseqname,
# Additional Optons
"gff-ver=s" => \$gff_ver,
"program=s" => \$program,
"param=s" => \$parameter,
#"f|fastafile=s" => \$infasta,
"append" => \$do_gff_append,
"q|quiet" => \$quiet,
"verbose" => \$verbose,
# Additional information
"usage" => \$show_usage,
"version" => \$show_version,
"man" => \$show_man,
"h|help" => \$show_help,);
#-----------------------------+
# SHOW REQUESTED HELP |
#-----------------------------+
if ( ($show_usage) ) {
# print_help ("usage", File::Spec->rel2abs($0) );
print_help ("usage", $0 );
}
if ( ($show_help) || (!$ok) ) {
# print_help ("help", File::Spec->rel2abs($0) );
print_help ("help", $0 );
}
if ($show_version) {
print "\ncnv_ltrseq2gff.pl:\nVersion: $VERSION\n\n";
exit;
}
if ($show_man) {
# User perldoc to generate the man documentation.
system("perldoc $0");
exit($ok ? 0 : 2);
}
#-----------------------------+
# CHECK REQUIRED ARGS |
#-----------------------------+
if ( (!$inseqname) ) {
print "\a";
print STDERR "ERROR: A sequence name was not specified at the".
" command line\n" if (!$inseqname);
print_help ("usage", $0 );
}
#-----------------------------+
# STANDARDIZE GFF VERSION |
#-----------------------------+
unless ($gff_ver =~ "GFF3" ||
$gff_ver =~ "GFF2") {
# Attempt to standardize GFF format names
if ($gff_ver =~ "3") {
$gff_ver = "GFF3";
}
elsif ($gff_ver =~ "2") {
$gff_ver = "GFF2";
}
else {
print "\a";
die "The gff-version \'$gff_ver\' is not recognized\n".
"The options GFF2 or GFF3 are supported\n";
}
}
#-----------------------------------------------------------+
# MAIN PROGRAM BODY |
#-----------------------------------------------------------+
if ($do_gff_append) {
ltrseq2gff ( $infile, $outfile, 1, $inseqname, $program, $parameter);
}
else {
ltrseq2gff ( $infile, $outfile, 0, $inseqname, $program, $parameter);
}
exit;
#-----------------------------------------------------------+
# SUBFUNCTIONS |
#-----------------------------------------------------------+
sub print_help {
my ($help_msg, $podfile) = @_;
# help_msg is the type of help msg to use (ie. help vs. usage)
print "\n";
#-----------------------------+
# PIPE WITHIN PERL |
#-----------------------------+
# This code made possible by:
# http://www.perlmonks.org/index.pl?node_id=76409
# Tie info developed on:
# http://www.perlmonks.org/index.pl?node=perltie
#
#my $podfile = $0;
my $scalar = '';
tie *STDOUT, 'IO::Scalar', \$scalar;
if ($help_msg =~ "usage") {
podselect({-sections => ["SYNOPSIS|MORE"]}, $0);
}
else {
podselect({-sections => ["SYNOPSIS|ARGUMENTS|OPTIONS|MORE"]}, $0);
}
untie *STDOUT;
# now $scalar contains the pod from $podfile you can see this below
#print $scalar;
my $pipe = IO::Pipe->new()
or die "failed to create pipe: $!";
my ($pid,$fd);
if ( $pid = fork() ) { #parent
open(TMPSTDIN, "<&STDIN")
or die "failed to dup stdin to tmp: $!";
$pipe->reader();
$fd = $pipe->fileno;
open(STDIN, "<&=$fd")
or die "failed to dup \$fd to STDIN: $!";
my $pod_txt = Pod::Text->new (sentence => 0, width => 78);
$pod_txt->parse_from_filehandle;
# END AT WORK HERE
open(STDIN, "<&TMPSTDIN")
or die "failed to restore dup'ed stdin: $!";
}
else { #child
$pipe->writer();
$pipe->print($scalar);
$pipe->close();
exit 0;
}
$pipe->close();
close TMPSTDIN;
print "\n";
exit 0;
}
sub ltrseq2gff {
#-----------------------------+
# SUBFUNCTION VARS |
#-----------------------------+
# $source is the program
my ($ltrseq_in, $gff_out, $append_gff, $seqname,
$source, $param) = @_;
#-----------------------------+
# LTRSEQ VARS |
#-----------------------------+
my $ltrseq_infile; # Input file that was analyzed by LTR_seq
my $ltrseq_numseqs; # Number of seqs analyzed by LTR_seq
my $ltrseq_msrt; # Max Score Ration Threshold
my $ltrseq_ltrmin; # LTRmin
my $ltrseq_ltr_minexact; # LTR Min Exact Match
my $ltrseq_dmin; # Dmin
my $ltrseq_dmax; # Dmax
# Postion of the LTR Retrotransposon features
my $ltrseq_name; # Unique name assigned to the prediction
my $ltrspan_start; # Start of the entire LTR Retrotransposon
my $ltrspan_end; # End of the entire LTR Retrotransposon
my $ltrspan_len; # Length of the LTR span
my $ltr5_start; # Start of the 5' LTR
my $ltr5_end; # End of the 5' LTR
my $ltr3_start; # Start of the 3' LTR
my $ltr3_end; # End of the 3' LTR
my $ltr_len; # Length of the LTRs
my $mid_start; # Start of the LTR Mid region
my $mid_end; # End of the LTR Mid region
my $ltr_diff; # Percent Difference in the LTRs
my $ltr_conf; # Confidence score for the prediction
my $ltr_tsr; # Target site rep
my @in_split = (); # Split of the infile line
my $num_in; # Number of split vars in the infile
# Initialize Counters
my $ltrseq_num = 0; # ID Number of putatitve LTR retro (Incremented Number)
#-----------------------------+
# OPEN FILES |
#-----------------------------+
if ($ltrseq_in) {
open (INFILE, "<$ltrseq_in") ||
die "ERROR: Can not open input file:\n$ltrseq_in\n";
}
else {
print STDERR "Expecting input from STDIN\n";
open (INFILE, "<&STDIN") ||
die "Can not accept input from standard input.\n";
}
if ($gff_out) {
if ($append_gff) {
open (GFFOUT, ">>$gff_out") ||
die "ERROR: Can not open output file for appending\n$gff_out\n";
}
else {
open (GFFOUT, ">$gff_out") ||
die "ERROR: Can not open output file for output\n$gff_out\n";
if ($gff_ver =~ "GFF3") {
print GFFOUT "##gff-version 3\n";
}
} # End of if append_gff
}
else {
open (GFFOUT, ">&STDOUT") ||
die "Can not print to STDOUT\n";
if ($gff_ver =~ "GFF3") {
print GFFOUT "##gff-version 3\n";
}
}
if ($param) {
$source = $source.":".$param
}
#-----------------------------+
# PROCESS INFILE |
#-----------------------------+
while (<INFILE>) {
chomp;
# Print the INFILE
#print "$_\n";
if (m/^Report:/) {
# If this is a Report line, accepted or rejected can be
# determined by counting the line number
# 15 is a rejected line
# 19 is an accepted line
my @in_split = split;
my $num_in = @in_split;
#print "Report Line: $num_in parts\n";
#print "\t$_\n";
if ($num_in == 15) {
#print "\tREJECTED\n";
}
#-----------------------------+
# ACCEPTED LTR RETRO Parts |
#-----------------------------+
elsif ($num_in == 19) {
#print "\tACCEPTED\n";
print STDERR "\n".$_."\n" if $verbose;
$ltrseq_num++; # Increment the count
$ltr5_start = $in_split[4] || "0"; # Replaced with zero
$ltr5_end = $in_split[5] || "NULL";
$ltr3_start = $in_split[7] || "NULL";
$ltr3_end = $in_split[8] || "NULL";
$mid_start = $ltr5_end + 1;
$mid_end = $ltr3_start - 1;
$ltrspan_start = $ltr5_start;
$ltrspan_end = $ltr3_end;
$ltrspan_len = $in_split[12];
$ltr_len = $in_split[10];
$ltr_diff = $in_split[14];
$ltr_conf = $in_split[16];
$ltr_tsr = $in_split[18];
$ltrseq_name = "ltrseq_".$ltrseq_num;
if ($ltr_tsr =~ m/\#(.*)\#/) {
$ltr_tsr = $1;
}
# SHOW INFO IF VERBOSE
if ($verbose) {
print STDERR "$ltrseq_name\n";
print STDERR "\tSTART: $ltrspan_start\n";
print STDERR "\tEND: $ltrspan_end\n";
print STDERR "\tLTRLEN: $ltr_len\n";
print STDERR "\tSPAN_LEN: $ltrspan_len\n";
print STDERR "\tLTSR: $ltr_tsr\n";
print STDERR "\tLTRCON: $ltr_conf\n";
print STDERR "\tLTRDIF: $ltr_diff\n";
} # End of if verbose
#-----------------------------+
# PRINT TO GFF OUTPUT FILE |
#-----------------------------+
my $attribute = $ltrseq_name;
my $parent_id = $ltrseq_name;
#-----------------------------+
# LTR RETRO PREDICTION SPAN
#-----------------------------+
if ($gff_ver =~ "GFF3") {
$attribute = "ID=".$parent_id;
}
print GFFOUT "$seqname\t". # Name of sequence
"$source\t". # Source
"LTR_retrotransposon\t". # Feature, exon for Apollo
"$ltrspan_start\t". # Start of the ltr span
"$ltrspan_end\t". # End of the ltr span
"$ltr_conf\t". # Score, LTR Confidence Score
".\t". # Strand
".\t". # Frame
"$attribute\n"; # Features (Name)
#-----------------------------+
# 5' LTR |
#-----------------------------+
if ($gff_ver =~ "GFF3") {
$attribute = "ID=".$parent_id."_five_prime_LTR".
";Name=Five Prime LTR".
";Parent=".$parent_id;
}
print GFFOUT "$seqname\t". # Name of sequence
"$source\t". # Source
"five_prime_LTR\t". # Feature, exon for Apollo
"$ltr5_start\t". # Start of the 5'ltr
"$ltr5_end\t". # End of the 5' ltr span
"$ltr_conf\t". # Score, LTR Confidence Score
".\t". # Strand
".\t". # Frame
"$attribute\n"; # Features (Name)
#-----------------------------+
# 3' LTR |
#-----------------------------+
if ($gff_ver =~ "GFF3") {
$attribute = "ID=".$parent_id."_three_prime_LTR".
";Name=Three Prime LTR".
";Parent=".$parent_id;
}
print GFFOUT "$seqname\t". # Name of sequence
"$source\t". # Source
"three_prime_LTR\t". # Feature, exon for Apollo
"$ltr3_start\t". # Start of the 3'ltr
"$ltr3_end\t". # End of the 3' ltr span
"$ltr_conf\t". # Score, LTR Confidence Score
".\t". # Strand
".\t". # Frame
"$attribute\n"; # Features (Name)
}
# 15 Parts is Rejected
}
# HEADER INFORMATION
elsif (m/^Info:/) {
}
# The input string that was passed
elsif (m/^Input:/) {
}
} # End of while INFILE
}
sub seqid_encode {
# Following conventions for GFF3 v given at http://gmod.org/wiki/GFF3
# Modified from code for urlencode in the perl cookbook
# Ids must not contain unescaped white space, so spaces are not allowed
my ($value) = @_;
$value =~ s/([^[a-zA-Z0-9.:^*$@!+_?-|])/"%" . uc(sprintf "%lx" , unpack("C", $1))/eg;
return ($value);
}
sub gff3_encode {
# spaces are allowed in attribute, but tabs must be escaped
my ($value) = @_;
$value =~ s/([^[a-zA-Z0-9.:^*$@!+_?-| ])/"%" . uc(sprintf "%lx" , unpack("C", $1))/eg;
return ($value);
}
1;
__END__
# Deprecated print_help
sub print_help {
# Print requested help or exit.
# Options are to just print the full
my ($opt) = @_;
my $usage = "USAGE:\n".
"MyProg.pl -i InFile -o OutFile";
my $args = "REQUIRED ARGUMENTS:\n".
" --infile # Path to the input file\n".
" --outfile # Path to the output file\n".
"\n".
"OPTIONS::\n".
" --version # Show the program version\n".
" --usage # Show program usage\n".
" --help # Show this help message\n".
" --man # Open full program manual\n".
" --quiet # Run program with minimal output\n";
if ($opt =~ "full") {
print "\n$usage\n\n";
print "$args\n\n";
}
else {
print "\n$usage\n\n";
}
exit;
}
=head1 NAME
cnv_ltrseq2gff.pl - Convert LTR_seq output to GFF format.
=head1 VERSION
This documentation refers to program version $Rev: 948 $
=head1 SYNOPSIS
=head2 Usage
cnv_ltrseq2gff.pl -i InFile -o OutFile -s SeqName
=head2 Required Arguments
-i,--infile # Directory of fasta files to process
-o,--outfile # Path to the output file
-s,--seqname # ID of the sequence record analyzed
=head1 DESCRIPTION
Converts program output from LTR_seq LTR prediction program to GFF format.
=head1 REQUIRED ARGUMENTS
=over 2
=item -i,--infile
Path of the input file which is the prediction results from LTR_seq.
If an input file is not specified, the program will expect intput from
STDIN.
=item -o,--outfile
Path of the output file which will be the GFF output from cnv_ltrseq2gff.pl.
If an input file is not specified, the program will print output
to STDOUT.
=item -s,--seqname
The sequence ID of the query sequence that was analyzed. This will be
used to write to the first column of the GFF output file.
=back
=head1 OPTIONS
=over 2
=item --gff-ver
The GFF version for the output. This will accept either gff2 or gff3 as the
options. By default the GFF version will be GFF2 unless specified otherwise.
The default GFF version for output can also be set in the user environment
with the DP_GFF option. The command line option will always override the option
defined in the user environment.
=item --append
Append the GFF results to an existing GFF file. The path to the existing
GFF file is indicated by the --outfile option.
=item --usage
Short overview of how to use program from command line.
=item --help
Show program usage with summary of options.
=item --version
Show program version.
=item --man
Show the full program manual. This uses the perldoc command to print the
POD documentation for the program.
=item -v,--verbose
Run the program in verbose mode.
=item -q,--quiet
Run the program with minimal output.
=back
=head1 DIAGNOSTICS
Error messages generated by this program and possible solutions are listed
below.
=over 2
=item ERROR: Can not open input file
The path to the input file you provided by the --infile option
may be incorrect, or you do not have permission to read the file
that exists at that location.
=item ERROR: Can not open output file
The directory that you specified at the --outfile option may not
exist, or you may not have permission to write files in that
directory.
=back
=head1 CONFIGURATION AND ENVIRONMENT
The cnv_ltrseq2gff.pl program does not required an external configuration
file or make use of variables defined in the user's environment.
=head1 DEPENDENCIES
=head2 Required Software
=over
=item * LTR_seq Program
This program requires output from LTR_seq. LTR_seq is available upon email
request from the author, Ananth Kalyanaraman:
http://www.eecs.wsu.edu/~ananth/contact.htm.
Also see the original publication for the LTR_par program :
I<A. Kalyanaraman, S. Aluru. 2006. Efficient algorithms and software for
detection of full-length LTR retrotransposons. Journal of
Bioinformatics and Computational Biology (JBCB), 4(2):197-216.>
=back
=head2 Required Perl Modules
=over
=item * File::Copy
This module is required to copy the BLAST results.
=item * Getopt::Long
This module is required to accept options at the command line.
=back
=head1 BUGS AND LIMITATIONS
=head2 Bugs
=over 2
=item * No bugs currently known
If you find a bug with this software, file a bug report on the DAWG-PAWS
Sourceforge website: http://sourceforge.net/tracker/?group_id=204962
=back
=head2 Limitations
=over
=item * Single Record FASTA Files
This program is currently limited to analyzing results from FASTA files
that contain a single record.
=back
=head1 SEE ALSO
The batch_blast.pl program is part of the DAWG-PAWS package of genome
annotation programs. See the DAWG-PAWS web page
( http://dawgpaws.sourceforge.net/ )
or the Sourceforge project page
( http://sourceforge.net/projects/dawgpaws )
for additional information about this package.
=head1 REFERENCE
A manuscript is being submitted describing the DAWGPAWS program.
Until this manuscript is published, please refer to the DAWGPAWS
SourceForge website when describing your use of this program:
JC Estill and JL Bennetzen. 2009.
The DAWGPAWS Pipeline for the Annotation of Genes and Transposable
Elements in Plant Genomes.
http://dawgpaws.sourceforge.net/
=head1 LICENSE
GNU GENERAL PUBLIC LICENSE, VERSION 3
http://www.gnu.org/licenses/gpl.html
THIS SOFTWARE COMES AS IS, WITHOUT ANY EXPRESS OR IMPLIED
WARRANTY. USE AT YOUR OWN RISK.
=head1 AUTHOR
James C. Estill E<lt>JamesEstill at gmail.comE<gt>
=head1 HISTORY
STARTED: 09/03/2007
UPDATED: 03/30/2009
VERSION: $Rev: 948 $
=cut
#-----------------------------------------------------------+
# HISTORY |
#-----------------------------------------------------------+
#
# 09/03/2007
# - Main body of program written
#
# 12/13/2007
# - Moved POD documentation to the end of the program
# - Updated POD documentation
# - Added print_help subfunction that extracts help
# and usage messages from the POD documentation
# 03/30/2009
# - Added param
# - Added program
# - Added STDIN support
# - Added STOUT support
# - Added proper feature names for LTR_retrotransposon,
# three_prime_LTR, and five_prime_LTR
# - Fixed error where vals starting at zero were
# set to null
# 03/02/2010
# - Added GFF3 format output