| 1 | package Bio::Gonzales::Assembly::IO; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use List::MoreUtils qw/zip/; | ||
| 10 | use Bio::Gonzales::Seq::IO qw/fahash/; | ||
| 11 | use Bio::Gonzales::Matrix::IO qw(mslurp); | ||
| 12 | use Bio::Gonzales::Util::File qw/slurpc/; | ||
| 13 | |||
| 14 | use base 'Exporter'; | ||
| 15 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||
| 16 | our $VERSION = '0.058'; # VERSION | ||
| 17 | |||
| 18 | @EXPORT = qw(); | ||
| 19 | %EXPORT_TAGS = (); | ||
| 20 | @EXPORT_OK = qw(agpslurp agp2fasta); | ||
| 21 | |||
| 22 | our @AGP_COLUMN_NAMES = qw/ | ||
| 23 | object | ||
| 24 | object_beg | ||
| 25 | object_end | ||
| 26 | part_number | ||
| 27 | component_type | ||
| 28 | component_id | ||
| 29 | component_beg | ||
| 30 | component_end | ||
| 31 | orientation/; | ||
| 32 | |||
| 33 | sub INFO { say STDERR @_; } | ||
| 34 | |||
| 35 | sub agpslurp { | ||
| 36 | my ($file) = @_; | ||
| 37 | |||
| 38 | my @lines = slurpc( $file); | ||
| 39 | |||
| 40 | my @agp; | ||
| 41 | for my $l (@lines) { | ||
| 42 | my @a = split /\t/, $l; | ||
| 43 | |||
| 44 | #add last field in case somebody forgot to add it... | ||
| 45 | push @a, '' if ( @a == 8 ); | ||
| 46 | |||
| 47 | #but if sth is really going wrong, break | ||
| 48 | confess "error in agp file $file\n$l" unless ( @a == 9 ); | ||
| 49 | |||
| 50 | if ( $a[8] eq '-' ) { $a[8] = -1 } | ||
| 51 | elsif ( $a[8] eq '+' ) { $a[8] = 1 } | ||
| 52 | else { $a[8] = 0 } | ||
| 53 | |||
| 54 | push @agp, +{ zip @AGP_COLUMN_NAMES, @a }; | ||
| 55 | } | ||
| 56 | |||
| 57 | return \@agp; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub agp2fasta { | ||
| 61 | my ( $agp, $seq, $out ) = @_; | ||
| 62 | |||
| 63 | INFO("reading scf_seqs"); | ||
| 64 | my %scf_seqs = map { INFO(" $_"); ( fahash($_) ) } @$seq; | ||
| 65 | |||
| 66 | INFO("reading agp data"); | ||
| 67 | my %agp_data; | ||
| 68 | for my $agp_file (@$agp) { | ||
| 69 | INFO(" $agp_file"); | ||
| 70 | my $data = agpslurp($agp_file); | ||
| 71 | for my $e (@$data) { | ||
| 72 | $agp_data{ $e->{object} } //= []; | ||
| 73 | push @{ $agp_data{ $e->{object} } }, $e; | ||
| 74 | |||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | INFO("processing agp data"); | ||
| 79 | while ( my ( $chr_id, $objs ) = each %agp_data ) { | ||
| 80 | INFO("processing $chr_id"); | ||
| 81 | $objs = [ sort { $a->{part_number} <=> $b->{part_number} } @$objs ]; | ||
| 82 | |||
| 83 | open my $out_fh, '>', $out or confess "Can't open filehandle: $!"; | ||
| 84 | my $last_obj_end; | ||
| 85 | say $out_fh ">$chr_id"; | ||
| 86 | for my $o (@$objs) { | ||
| 87 | die "error" if ( $last_obj_end && ( $last_obj_end + 1 != $o->{object_beg} ) ); | ||
| 88 | if ( $o->{component_type} eq 'W' ) { | ||
| 89 | INFO( "processing scf " . $o->{component_id} ); | ||
| 90 | unless ( exists( $scf_seqs{ $o->{component_id} } ) ) { | ||
| 91 | die "could not find " . Dumper $o; | ||
| 92 | } | ||
| 93 | |||
| 94 | print $out_fh $scf_seqs{ $o->{component_id} } | ||
| 95 | ->subseq( [ @{$o}{qw/component_beg component_end orientation/} ] )->seq; | ||
| 96 | } elsif ( $o->{component_id} eq 'N' ) { | ||
| 97 | |||
| 98 | my $len = $o->{object_end} - $o->{object_beg} + 1; | ||
| 99 | INFO( "processing gap of length " . $len ); | ||
| 100 | print $out_fh 'N' x $len; | ||
| 101 | } elsif ( $o->{component_id} eq 'U' ) { | ||
| 102 | INFO("processing gap of UNKNOWN length 100"); | ||
| 103 | print $out_fh 'N' x 100 | ||
| 104 | |||
| 105 | } else { | ||
| 106 | die "unknown component type" . Dumper $o; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $out_fh->close; | ||
| 110 | } | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ | ||
| 117 | |||
| 118 | =head1 NAME | ||
| 119 | |||
| 120 | Bio::Gonzales::Assembly::IO - assembly related stuff | ||
| 121 | |||
| 122 | =head1 SYNOPSIS | ||
| 123 | |||
| 124 | use Bio::Gonzales::Assembly::IO qw(agpslurp); | ||
| 125 | |||
| 126 | =head1 DESCRIPTION | ||
| 127 | |||
| 128 | =head1 OPTIONS | ||
| 129 | |||
| 130 | =head1 SUBROUTINES | ||
| 131 | =head1 METHODS | ||
| 132 | |||
| 133 | =head1 SEE ALSO | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 138 | |||
| 139 | =cut |