| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Pod::Usage; | ||
| 8 | use Getopt::Long; | ||
| 9 | |||
| 10 | my %a; # commandline %a rguments | ||
| 11 | |||
| 12 | GetOptions( \%a, 'help|h|?', 'switch-query-reference|switch|s' ) or pod2usage( -verbose => 2 ); | ||
| 13 | |||
| 14 | pod2usage( -verbose => 2 ) if ( $a{help} ); | ||
| 15 | |||
| 16 | my ( $coords_file, $gff_file ) = @ARGV; | ||
| 17 | pod2usage( | ||
| 18 | -msg => "ERROR: Coordinate file not supplied or non-existent\n", | ||
| 19 | -verbose => 1, | ||
| 20 | -noperldoc => 1 | ||
| 21 | ) unless ( $coords_file && -f $coords_file ); | ||
| 22 | |||
| 23 | use Bio::Gonzales::Feat::IO::GFF3; | ||
| 24 | |||
| 25 | my $fout = Bio::Gonzales::Feat::IO::GFF3->new( file => $gff_file, mode => '>' ); | ||
| 26 | |||
| 27 | open my $coords_fh, '<', $coords_file or confess "Can't open filehandle: $!"; | ||
| 28 | |||
| 29 | #fast forward to NUCMER tag | ||
| 30 | while ( <$coords_fh> !~ /^NUCMER/ ) { } | ||
| 31 | |||
| 32 | my @header; | ||
| 33 | my $i = 0; | ||
| 34 | while ( my $c = <$coords_fh> ) { | ||
| 35 | $c =~ s/\r\n/\n/; | ||
| 36 | chomp $c; | ||
| 37 | |||
| 38 | #skip empty lines | ||
| 39 | next if ( $c =~ /^\s*$/ ); | ||
| 40 | |||
| 41 | #we found the header line | ||
| 42 | if ( $c =~ /^\[/ ) { | ||
| 43 | $c =~ y/[]//d; | ||
| 44 | @header = split /\t/, $c; | ||
| 45 | next; | ||
| 46 | } | ||
| 47 | |||
| 48 | #skip lines that don't start with coordinates(numbers) | ||
| 49 | next unless ( $c =~ /^\d/ ); | ||
| 50 | |||
| 51 | #split line into columns | ||
| 52 | my @contents = split /\t/, $c; | ||
| 53 | |||
| 54 | if ( $a{'switch-query-reference'} ) { | ||
| 55 | #FIXME quick and dirty, reorder fields, so that seq1 => seq2 and seq2 => seq1 | ||
| 56 | @contents = @contents[ 2, 3, 0, 1, 5, 4, 6, 8, 7 ]; | ||
| 57 | } | ||
| 58 | |||
| 59 | my %coords; | ||
| 60 | #figure out correct orientation | ||
| 61 | if ( $contents[0] > $contents[1] && $contents[2] > $contents[3] ) { | ||
| 62 | %coords = ( r_range => [ 1, 0 ], strand => 1, q_range => [ 3, 2 ] ); | ||
| 63 | } elsif ( $contents[0] < $contents[1] && $contents[2] > $contents[3] ) { | ||
| 64 | %coords = ( r_range => [ 0, 1 ], strand => -1, q_range => [ 3, 2 ] ); | ||
| 65 | } elsif ( $contents[0] < $contents[1] && $contents[2] < $contents[3] ) { | ||
| 66 | %coords = ( r_range => [ 0, 1 ], strand => 1, q_range => [ 2, 3 ] ); | ||
| 67 | } elsif ( $contents[0] > $contents[1] && $contents[2] < $contents[3] ) { | ||
| 68 | %coords = ( r_range => [ 1, 0 ], strand => -1, q_range => [ 2, 3 ] ); | ||
| 69 | } else { | ||
| 70 | die "error in coordinate parsing"; | ||
| 71 | } | ||
| 72 | |||
| 73 | my $b = create_feat( \@contents, \%coords ); | ||
| 74 | $b->id( sprintf( "align%09d", $i++ ) ); | ||
| 75 | $fout->write_feat($b); | ||
| 76 | } | ||
| 77 | $fout->close; | ||
| 78 | |||
| 79 | sub create_feat { | ||
| 80 | my ( $c, $ranges ) = @_; | ||
| 81 | |||
| 82 | my $qid = $c->[8]; | ||
| 83 | my $rid = $c->[7]; | ||
| 84 | my $score = $c->[6]; | ||
| 85 | |||
| 86 | my $strand = $ranges->{'strand'}; | ||
| 87 | |||
| 88 | my ( $qstart, $qend ) = @{$c}[ @{ $ranges->{'q_range'} } ]; | ||
| 89 | my ( $rstart, $rend ) = @{$c}[ @{ $ranges->{'r_range'} } ]; | ||
| 90 | |||
| 91 | my $f = Bio::Gonzales::Feat->new( | ||
| 92 | seq_id => $rid, | ||
| 93 | source => 'coords2gff', | ||
| 94 | type => 'match', | ||
| 95 | start => $rstart, | ||
| 96 | end => $rend, | ||
| 97 | strand => $strand, | ||
| 98 | attributes => { Target => ["\"$qid\" $qstart $qend"], Ontology_term => ['SO:0000343'], }, | ||
| 99 | ); | ||
| 100 | |||
| 101 | return $f; | ||
| 102 | } | ||
| 103 | __END__ | ||
| 104 | |||
| 105 | =head1 NAME | ||
| 106 | |||
| 107 | coords2gff.pl - convert mummer coords files to gff3 format | ||
| 108 | |||
| 109 | =head1 SYNOPSIS | ||
| 110 | |||
| 111 | # store results in OUTPUT.gff3 | ||
| 112 | perl coords2gff.pl [OPTIONS] <MUMMER_ALIGNMENT.coords> <OUTPUT.gff3> | ||
| 113 | |||
| 114 | # print results to standard output | ||
| 115 | perl coords2gff.pl [OPTIONS] <MUMMER_ALIGNMENT.coords> | ||
| 116 | # or | ||
| 117 | perl coords2gff.pl [OPTIONS] <MUMMER_ALIGNMENT.coords> - | ||
| 118 | |||
| 119 | =head1 DESCRIPTION | ||
| 120 | |||
| 121 | This script converts mummer output in coords files to gff3 formatted output. | ||
| 122 | |||
| 123 | =head1 OPTIONS | ||
| 124 | |||
| 125 | Alternative option names are separated by "|". | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =item B<< --switch-query-reference|switch|s >> | ||
| 130 | |||
| 131 | Take the query (2nd sequence in the alignment) as reference and | ||
| 132 | the reference (1st sequence in the alignment) as query. Adjust coordinates | ||
| 133 | accordingly. | ||
| 134 | |||
| 135 | =item B<< --help|h|? >> | ||
| 136 | |||
| 137 | Complete help. | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | L<Bio::FeatureIO>, L<Bio::SeqFeature::Generic> | ||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 151 | |||
| 152 | =cut |