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
|
#!/usr/bin/env perl
use strict;
use warnings;
use Bio::FeatureIO;
use Getopt::Long;
use FileHandle;
#use lib '/home/cain/cvs_stuff/schema/chado/lib';
#use lib '/home/scott/cvs_stuff/schema/chado/lib';
use Bio::GMOD::DB::Adapter;
use Bio::GMOD::Config;
use Bio::GMOD::DB::Config;
=head1 NAME
$0 - Prepares a GFF3 file for bulk loading into a chado database.
=head1 SYNOPSIS
% gmod_gff_preprocessor [options] --gfffile <filename>
=head1 COMMAND-LINE OPTIONS
--gfffile The file containing GFF3 (optional, can read
from stdin)
--outfile The name kernel that will be used for naming result files
--splitfile Split the files into more manageable chunks, providing
an argument to control splitting
--onlysplit Split the files and then quit (ie, don't sort)
--nosplit Don't split the files (ie, only sort)
--hasrefseq Set this if the file contains a reference sequence line
(Only needed if not splitting files)
--dbprofile Specify a gmod.conf profile name (otherwise use default)
--inheritance_tiers How many levels of inheritance do you expect tis file
to have (default: 3)
=head1 DESCRIPTION
splitfile -- Just setting this flag to 1 will cause the file to be split
by reference sequence. If you provide an optional argument, it will be
further split according to these rules:
source=1 Splits files according to the value in the source column
source=a,b,c Puts lines with sources that match (via regular expression)
'a', 'b', or 'c' in a separate file
type=a,b,c Puts lines with types that match 'a', 'b', or 'c' in a
separate file
For example, if you wanted all of your analysis results to go in a separate
file, you could indicate '--splitfile type=match', and all cDNA_match,
EST_match and cross_genome_match features would go into separate files
(separate by reference sequence).
inheritence_tiers -- The number of levels of inheritance this file has.
For example, if the file has "central dogma" genes in it (gene/mRNA/
exon,polypeptide), then it has 3. Up to 4 is supported but the higher
the number, the more slowly it performs. If you don't know, 3 is a
reasonable guess.
=head2 FASTA sequence
If the GFF3 file contains FASTA sequence at the end, the sequence
will be placed in a separate file with the extension '.fasta'. This
fasta file can be loaded separately after the split and/or sorted
GFF3 files are loaded, using the command:
gmod_bulk_load_gff3.pl -g <fasta file name>
=head1 AUTHOR
Scott Cain E<lt>cain@cshl.orgE<gt>
Copyright (c) 2006-2007
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
my (@GFFFILE, $OUTFILE, $SPLITFILE,$ONLYSPLIT,$NOSPLIT,$HASREFSEQ,
$DBPROFILE, $INHERITANCE_TIERS);
GetOptions(
'gfffile=s' => \@GFFFILE,
'outfile=s' => \$OUTFILE,
'splitfile=s' => \$SPLITFILE,
'onlysplit' => \$ONLYSPLIT,
'nosplit' => \$NOSPLIT,
'hasrefseq' => \$HASREFSEQ,
'dbprofile=s' => \$DBPROFILE,
'inheritance_tiers=i' => \$INHERITANCE_TIERS,
) or ( system( 'pod2text', $0 ), exit -1 );
@GFFFILE = split(/,/,join(',',@GFFFILE));
$DBPROFILE ||='default';
$INHERITANCE_TIERS ||= 3;
my ($split_on_source, $split_on_type, $split_on_ref);
if ($SPLITFILE) {
if ($SPLITFILE and $SPLITFILE !~ /=/ and $SPLITFILE == 1) {
$split_on_ref = 1;
}
else {
my @splits = split /;/, $SPLITFILE;
for (@splits) {
my ($tag, $value) = split /=/;
$value =~ s/,/|/g;
if ($tag eq 'source') {
$split_on_source = $value;
}
elsif ($tag eq 'type') {
$split_on_type = $value;
}
else {
die "unsupported splitfile tag: $tag\n";
}
}
}
}
my %has_ref_seq;
my @gfffiles;
for my $GFFFILE (@GFFFILE) {
$GFFFILE ||='-';
$OUTFILE ||="$GFFFILE.out.gff3";
my $FASTA = "$OUTFILE.fasta";
if ($SPLITFILE && !$NOSPLIT) {
open GFFIN, "<", $GFFFILE or die "couldn't open $GFFFILE for reading: $!";
open FASTA, ">", $FASTA or die " couldn't open $FASTA for writing: $!";
my $fasta_flag = 0;
my %files;
while ( <GFFIN> ) {
if (/^##FASTA/) {
$fasta_flag = 1;
print FASTA;
next;
} elsif ($fasta_flag) {
print FASTA;
next;
}
next if /^#/;
my @la = split /\t/;
(warn "ignored gff line: $_" && next) if (scalar @la != 9);
my $has_ref_seq;
chomp $la[8];
if ( $la[8] =~ /ID=([^;]+);*.*$/ ) {
my $id = $1;
if ( $id eq $la[0] ) {
$has_ref_seq = $id;
}
}
if ( $split_on_source && $split_on_source eq 1 ) {
my $source = $la[1];
my $filename = "$la[0].$la[1].$OUTFILE";
unless ( defined $files{ $filename } ) {
$files{ $filename } = new FileHandle $filename, "w";
push @gfffiles, $filename;
}
$files{ $filename }->print( $_ );
push @{$has_ref_seq{ $filename }}, $has_ref_seq if $has_ref_seq;
}
elsif ( $split_on_source && $la[1] =~ /$split_on_source/) {
my $filename = "$la[0].source.$OUTFILE";
unless ( defined $files{ $filename } ) {
$files{ $filename } = new FileHandle $filename, "w";
push @gfffiles, $filename;
}
$files{ $filename }->print( $_ );
push @{$has_ref_seq{ $filename }}, $has_ref_seq if $has_ref_seq;
}
elsif ( $split_on_type && $la[2] =~ /$split_on_type/) {
my $filename = $la[0].'.type.'.$OUTFILE;
unless ( defined $files{ $filename } ) {
$files{ $filename } = new FileHandle $filename, "w";
push @gfffiles, $filename;
}
$files{ $filename }->print( $_ );
push @{$has_ref_seq{ $filename }}, $has_ref_seq if $has_ref_seq;
}
else {
my $filename = $la[0].'.'.$OUTFILE;
unless ( defined $files{ $filename } ) {
$files{ $filename } = new FileHandle $filename, "w";
push @gfffiles, $filename;
}
$files{ $filename }->print( $_ );
push @{$has_ref_seq{ $filename }}, $has_ref_seq if $has_ref_seq;
}
}
for my $key (keys %files) {
$files{$key}->close;
}
}
else {
push @gfffiles, $GFFFILE;
push @{ $has_ref_seq{ $GFFFILE } }, $GFFFILE if $HASREFSEQ;
}
}
exit(0) if $ONLYSPLIT;
my $gmod_conf = Bio::GMOD::Config->new();
my $db_conf = Bio::GMOD::DB::Config->new($gmod_conf, $DBPROFILE);
my $db = Bio::GMOD::DB::Adapter->new(
dbuser => $db_conf->user,
dbpass => $db_conf->password || '',
dbhost => $db_conf->host,
dbport => $db_conf->port,
dbname => $db_conf->name,
notransact => 1,
skipinit => 1,
);
$db->sorter_create_table;
for my $gfffile (@gfffiles) {
$db->sorter_delete_from_table;
my $outfile = $gfffile.'.sorted';
my $fasta = "$outfile.fasta";
open IN, "<", $gfffile or die "couldn't open $gfffile for reading: $!\n";
my $fasta_flag = 0;
print STDERR "Sorting the contents of $gfffile ...\n";
while( <IN> ) {
if (/^##FASTA/) {
$fasta_flag = 1;
open FASTA,
">", $fasta or die "couldn't open $fasta for writing: $!\n";
#print FASTA "##gff-version 3\n";
#print FASTA;
#print FASTA "\n"; #extra cr works around bug in Bio::FeatureIO::gff
next;
}
elsif ($fasta_flag) {
print FASTA;
next;
}
my $line = $_;
my @line_array = split /\t/, $line;
if ($line =~ /^#/ or scalar @line_array != 9) {
next;
}
my $refseq = $line_array[0];
my ($id, @parents,@derives_froms);
if ( $line_array[8] =~ /ID=([^;]+);*.*$/ ) {
$id = $1;
chomp $id;
}
if ( $line_array[8] =~ /Parent=([^;]+);*.*$/ ) {
@parents = split /,/, $1;
}
if ( $line_array[8] =~ /Derives_from=([^;]+);*.*$/ ) {
@derives_froms = split /,/, $1;
}
if (@parents > 0 || @derives_froms > 0) {
for my $parent ( (@parents,@derives_froms) ) {
chomp $parent;
$db->sorter_insert_line($refseq, $id, $parent, $line);
}
}
elsif ($id) {
$db->sorter_insert_line($refseq, $id, undef, $line);
}
else {
$db->sorter_insert_line($refseq, undef, undef, $line);
}
}
close IN;
close FASTA if $fasta_flag;
print STDERR "Writing sorted contents to $outfile ...\n";
open OUT,">", $outfile or die "couldn't open $outfile for writing: $!\n";
#to print:
# -get ref seqs (refseq == id)
# -get things with no parent
print OUT "##gff-version 3\n";
my @refseqs = $db->sorter_get_refseqs();
for my $refseq (@refseqs) {
print OUT $refseq; #already has the line feed
}
my @no_parents = $db->sorter_get_no_parents();
for my $no_parent (@no_parents) {
print OUT $no_parent;
}
@no_parents = '';
if ($INHERITANCE_TIERS >= 2) {
my @second_tiers = $db->sorter_get_second_tier();
for my $second_tier (@second_tiers) {
print OUT $second_tier;
}
}
if ($INHERITANCE_TIERS >= 3) {
my @third_tiers = $db->sorter_get_third_tier();
for my $third_tier (@third_tiers) {
print OUT $third_tier;
}
}
#yes, four tiers can happen, like transposible_element->te_gene->mRNA->exon
if ($INHERITANCE_TIERS >= 4) {
my @forth_tiers = $db->sorter_get_fourth_tier();
for my $fourth_tier (@forth_tiers) {
print OUT $fourth_tier;
}
}
close OUT;
}
|