| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 8 | use Pod::Usage; | ||
| 9 | use Bio::ToolBox::Data; | ||
| 10 | |||
| 11 | our $VERSION = '2.00'; | ||
| 12 | |||
| 13 | print "\n This program will convert a data file to fasta\n\n"; | ||
| 14 | |||
| 15 | ### Quick help | ||
| 16 | unless (@ARGV) { | ||
| 17 | |||
| 18 | # when no command line options are present | ||
| 19 | # print SYNOPSIS | ||
| 20 | pod2usage( | ||
| 21 | { | ||
| 22 | '-verbose' => 0, | ||
| 23 | '-exitval' => 1, | ||
| 24 | } | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | ### Get command line options and initialize values | ||
| 29 | my ( | ||
| 30 | $infile, $outfile, $database, $feature, $subfeature, | ||
| 31 | $id_i, $desc_i, $seq_i, $chr_i, $start_i, | ||
| 32 | $stop_i, $strand_i, $extend, $concatenate, $pad, | ||
| 33 | $gz, $help, $print_version, | ||
| 34 | ); | ||
| 35 | |||
| 36 | # Command line options | ||
| 37 | GetOptions( | ||
| 38 | 'i|in=s' => \$infile, # the solexa data file | ||
| 39 | 'o|out=s' => \$outfile, # name of output file | ||
| 40 | 'd|db|fasta=s' => \$database, # database name or genomic fasta file | ||
| 41 | 'f|feature=s' => \$feature, # feature from input file | ||
| 42 | 'u|subfeature=s' => \$subfeature, # subfeature to collect sequence | ||
| 43 | 'n|name|id=i' => \$id_i, # id index | ||
| 44 | 'desc=i' => \$desc_i, # description index | ||
| 45 | 's|seq=i' => \$seq_i, # sequence index | ||
| 46 | 'c|chr=i' => \$chr_i, # chromosome index | ||
| 47 | 'b|begin|start=i' => \$start_i, # start index | ||
| 48 | 'e|stop|end=i' => \$stop_i, # stop index | ||
| 49 | 't|strand=i' => \$strand_i, # strand index | ||
| 50 | 'x|extend=i' => \$extend, # extend sequence by given bp | ||
| 51 | 'cat!' => \$concatenate, # concatenate sequences into one | ||
| 52 | 'pad=i' => \$pad, # pad concatenate sequences with given N bp | ||
| 53 | 'z|gz!' => \$gz, # compress output | ||
| 54 | 'h|help' => \$help, # request help | ||
| 55 | 'v|version' => \$print_version, # print the version | ||
| 56 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 57 | |||
| 58 | # Print help | ||
| 59 | if ($help) { | ||
| 60 | |||
| 61 | # print entire POD | ||
| 62 | pod2usage( | ||
| 63 | { | ||
| 64 | '-verbose' => 2, | ||
| 65 | '-exitval' => 1, | ||
| 66 | } | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | # Print version | ||
| 71 | if ($print_version) { | ||
| 72 | print " Biotoolbox script data2fasta.pl, version $VERSION\n"; | ||
| 73 | eval { | ||
| 74 | require Bio::ToolBox; | ||
| 75 | my $v = Bio::ToolBox->VERSION; | ||
| 76 | print " Biotoolbox package version $v\n"; | ||
| 77 | }; | ||
| 78 | exit; | ||
| 79 | } | ||
| 80 | |||
| 81 | ### Check for requirements | ||
| 82 | unless ($infile) { | ||
| 83 | if (@ARGV) { | ||
| 84 | $infile = shift @ARGV; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 88 | exit 1; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | unless ( defined $gz ) { | ||
| 92 | $gz = 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | ### Open file #### | ||
| 96 | my $Data = Bio::ToolBox::Data->new( | ||
| 97 | file => $infile, | ||
| 98 | parse => 1, | ||
| 99 | feature => $feature, | ||
| 100 | subfeature => $subfeature, | ||
| 101 | ) or die " unable to load input file '$infile'\n"; | ||
| 102 | unless ($database) { | ||
| 103 | $database = $Data->database; | ||
| 104 | } | ||
| 105 | |||
| 106 | ### Identify columns #### | ||
| 107 | unless ($id_i) { | ||
| 108 | $id_i = $Data->name_column; | ||
| 109 | } | ||
| 110 | unless ($seq_i) { | ||
| 111 | $seq_i = $Data->find_column('sequence'); | ||
| 112 | } | ||
| 113 | unless ($desc_i) { | ||
| 114 | $desc_i = $Data->find_column('description|note'); | ||
| 115 | } | ||
| 116 | my $coords = 0; | ||
| 117 | if ( $chr_i and $start_i and $stop_i ) { | ||
| 118 | |||
| 119 | # user defined coordinates | ||
| 120 | $coords = 1; | ||
| 121 | } | ||
| 122 | elsif ( $Data->feature_type eq 'coordinate' ) { | ||
| 123 | |||
| 124 | # Input has coordinate columns | ||
| 125 | $coords = 1; | ||
| 126 | } | ||
| 127 | elsif ( $Data->feature_type eq 'named' ) { | ||
| 128 | |||
| 129 | # Input has named features that presumably have coordinates in a database | ||
| 130 | $coords = 1; | ||
| 131 | } | ||
| 132 | |||
| 133 | ### Determine mode ### | ||
| 134 | if ( $id_i and $seq_i and $concatenate ) { | ||
| 135 | |||
| 136 | # sequence is already in the source file | ||
| 137 | printf " Found Sequence column %s\n", defined $seq_i ? $seq_i : '-'; | ||
| 138 | printf " Found Description column %s\n", defined $desc_i ? $desc_i : '-'; | ||
| 139 | print " writing a single concatenated fasta with the provided sequence\n"; | ||
| 140 | write_direct_single_fasta(); | ||
| 141 | } | ||
| 142 | elsif ( $id_i and $seq_i ) { | ||
| 143 | |||
| 144 | # sequence is already in the source file | ||
| 145 | printf " Found Sequence column %s\n", defined $seq_i ? $seq_i : '-'; | ||
| 146 | printf " Found Description column %s\n", defined $desc_i ? $desc_i : '-'; | ||
| 147 | print " writing a multi-fasta with the provided sequence\n"; | ||
| 148 | write_direct_multi_fasta(); | ||
| 149 | } | ||
| 150 | elsif ( $coords and $concatenate ) { | ||
| 151 | |||
| 152 | # collect sequences and concatenate into single | ||
| 153 | print " fetching sequence from $database and writing a concatenated fasta\n"; | ||
| 154 | fetch_seq_and_write_single_fasta(); | ||
| 155 | } | ||
| 156 | elsif ($coords) { | ||
| 157 | |||
| 158 | # need to collect sequence | ||
| 159 | print " fetching sequence from $database and writing a multi-fasta\n"; | ||
| 160 | fetch_seq_and_write_multi_fasta(); | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | print STDERR " FATAL: unable to identify appropriate columns! see help\n"; | ||
| 164 | exit 1; | ||
| 165 | } | ||
| 166 | |||
| 167 | ### Finished | ||
| 168 | print " wrote file '$outfile'\n"; | ||
| 169 | |||
| 170 | ######################## Subroutines ################################### | ||
| 171 | |||
| 172 | sub write_direct_single_fasta { | ||
| 173 | |||
| 174 | # concatenate each of the provided sequences | ||
| 175 | my $concat_seq = q(); | ||
| 176 | |||
| 177 | $Data->iterate( | ||
| 178 | sub { | ||
| 179 | my $row = shift; | ||
| 180 | $concat_seq .= $row->value($seq_i); | ||
| 181 | $concat_seq .= 'N' x $pad if $pad; | ||
| 182 | } | ||
| 183 | ); | ||
| 184 | |||
| 185 | # write out | ||
| 186 | my $seq_fh = open_output_fasta(); | ||
| 187 | write_fasta_seq( $seq_fh, $Data->basename, 'Concatenated sequences', $concat_seq ); | ||
| 188 | $seq_fh->close; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub write_direct_multi_fasta { | ||
| 192 | |||
| 193 | # write multi-fasta with the provided sequences | ||
| 194 | my $seq_fh = open_output_fasta(); | ||
| 195 | $Data->iterate( | ||
| 196 | sub { | ||
| 197 | my $row = shift; | ||
| 198 | my $id = $row->value($id_i); | ||
| 199 | my $seq = $row->value($seq_i); | ||
| 200 | my $desc = q(); | ||
| 201 | if ( defined $desc_i ) { | ||
| 202 | $desc = $row->value($desc_i); | ||
| 203 | } | ||
| 204 | write_fasta_seq( $seq_fh, $id, $desc, $seq ); | ||
| 205 | } | ||
| 206 | ); | ||
| 207 | $seq_fh->close; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub fetch_seq_and_write_single_fasta { | ||
| 211 | |||
| 212 | # fetch sequence from database and write concatenated fasta file | ||
| 213 | |||
| 214 | # Open fasta database | ||
| 215 | my $db; | ||
| 216 | if ($database) { | ||
| 217 | $db = $Data->open_new_database($database); | ||
| 218 | unless ($db) { | ||
| 219 | die " Could not open database '$database' to use!\n"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | elsif ( $Data->database ) { | ||
| 223 | |||
| 224 | # cool, database defined in metadata, we'll use that | ||
| 225 | # hope it works.... | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | print STDERR | ||
| 229 | " FATAL: A sequence or fasta database must be provided to collect sequence!\n"; | ||
| 230 | exit 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | # collect concatenated sequences and write | ||
| 234 | my $concat_seq = q(); | ||
| 235 | $Data->iterate( | ||
| 236 | sub { | ||
| 237 | my $row = shift; | ||
| 238 | |||
| 239 | # collect provided arguments for generating sequence | ||
| 240 | my @args; | ||
| 241 | push @args, ( 'db', $db ) if $db; | ||
| 242 | push @args, ( 'start', $row->value($start_i) ) if defined $start_i; | ||
| 243 | push @args, ( 'stop', $row->value($stop_i) ) if defined $stop_i; | ||
| 244 | push @args, ( 'seq_id', $row->value($chr_i) ) if defined $chr_i; | ||
| 245 | push @args, ( 'strand', $row->value($strand_i) ) if defined $strand_i; | ||
| 246 | push @args, ( 'extend', $extend ) if $extend; | ||
| 247 | |||
| 248 | # collect sequence using provided arguments as necessary | ||
| 249 | my $sequence = $row->get_sequence(@args); | ||
| 250 | unless ($sequence) { | ||
| 251 | printf "no sequence for line %d, %s", $row->line_number, | ||
| 252 | $row->name || $row->coordinate; | ||
| 253 | next; | ||
| 254 | } | ||
| 255 | |||
| 256 | # concatenate the sequence | ||
| 257 | $concat_seq .= $sequence; | ||
| 258 | $concat_seq .= 'N' x $pad if $pad; | ||
| 259 | } | ||
| 260 | ); | ||
| 261 | |||
| 262 | # write out | ||
| 263 | my $seq_fh = open_output_fasta(); | ||
| 264 | my $id = $Data->basename; | ||
| 265 | my $desc = 'Concatenated sequences'; | ||
| 266 | write_fasta_seq( $seq_fh, $id, $desc, $concat_seq ); | ||
| 267 | $seq_fh->close; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub fetch_seq_and_write_multi_fasta { | ||
| 271 | |||
| 272 | # fetch sequence from database and write multi-fasta file | ||
| 273 | |||
| 274 | # Open fasta database | ||
| 275 | my $db; | ||
| 276 | if ($database) { | ||
| 277 | $db = $Data->open_new_database($database); | ||
| 278 | unless ($db) { | ||
| 279 | die " Could not open database '$database' to use!\n"; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | elsif ( $Data->database ) { | ||
| 283 | |||
| 284 | # cool, database defined in metadata, we'll use that | ||
| 285 | # hope it works.... | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | print STDERR | ||
| 289 | " FATAL: A sequence or fasta database must be provided to collect sequence!\n"; | ||
| 290 | exit 1; | ||
| 291 | } | ||
| 292 | |||
| 293 | # open output file | ||
| 294 | my $seq_fh = open_output_fasta(); | ||
| 295 | |||
| 296 | # collect sequences and write | ||
| 297 | $Data->iterate( | ||
| 298 | sub { | ||
| 299 | my $row = shift; | ||
| 300 | |||
| 301 | # collect provided arguments for generating sequence | ||
| 302 | my @args; | ||
| 303 | push @args, ( 'subfeature', $subfeature ) if $subfeature; | ||
| 304 | push @args, ( 'db', $db ) if $db; | ||
| 305 | push @args, ( 'start', $row->value($start_i) ) if $start_i; | ||
| 306 | push @args, ( 'stop', $row->value($stop_i) ) if $stop_i; | ||
| 307 | push @args, ( 'seq_id', $row->value($chr_i) ) if $chr_i; | ||
| 308 | push @args, ( 'strand', $row->value($strand_i) ) if $strand_i; | ||
| 309 | push @args, ( 'extend', $extend ) if $extend; | ||
| 310 | |||
| 311 | # collect sequence based on values obtained above | ||
| 312 | my $seq = $row->get_sequence(@args); | ||
| 313 | unless ($seq) { | ||
| 314 | printf "no sequence for line %d, %s", $row->line_number, | ||
| 315 | $row->name || $row->coordinate; | ||
| 316 | next; | ||
| 317 | } | ||
| 318 | |||
| 319 | # write out sequence | ||
| 320 | my $id = $row->name || $row->coordinate; | ||
| 321 | my $desc = q(); | ||
| 322 | if ( defined $desc_i ) { | ||
| 323 | $desc = $row->value($desc_i); | ||
| 324 | } | ||
| 325 | write_fasta_seq( $seq_fh, $id, $desc, $seq ); | ||
| 326 | } | ||
| 327 | ); | ||
| 328 | $seq_fh->close; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub open_output_fasta { | ||
| 332 | |||
| 333 | # get filename | ||
| 334 | unless ($outfile) { | ||
| 335 | $outfile = $Data->path . $Data->basename . '.fa'; | ||
| 336 | } | ||
| 337 | unless ( $outfile =~ /\.fa (?:sta)? (?:\.gz)? $/xi ) { | ||
| 338 | $outfile .= '.fasta'; | ||
| 339 | } | ||
| 340 | if ( $gz and $outfile !~ /\.gz$/i ) { | ||
| 341 | $outfile .= '.gz'; | ||
| 342 | } | ||
| 343 | |||
| 344 | # open for writing | ||
| 345 | my $out_fh = Bio::ToolBox::Data->open_to_write_fh( $outfile, $gz ) | ||
| 346 | or die "unable to open '$outfile' for writing!\n"; | ||
| 347 | return $out_fh; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub write_fasta_seq { | ||
| 351 | my ( $fh, $id, $desc, $seq ) = @_; | ||
| 352 | if ($desc) { | ||
| 353 | $fh->printf( ">%s %s\n", $id, $desc ); | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | $fh->printf( ">%s\n", $id ); | ||
| 357 | } | ||
| 358 | while ( length($seq) > 60 ) { | ||
| 359 | my $line = substr $seq, 0, 60, q(); | ||
| 360 | $fh->printf( "%s\n", $line ); | ||
| 361 | } | ||
| 362 | if ( length $seq ) { | ||
| 363 | $fh->printf( "%s\n", $seq ); | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | __END__ |