| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | wiggle2gff3.pl | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | wiggle2gff3.pl [options] WIG_FILE > load_data.gff3 | ||
| 10 | |||
| 11 | Converts UCSC WIG format files into gff3 files suitable for loading | ||
| 12 | into GBrowse databases. This is used for high-density quantitative | ||
| 13 | data such as CNV, SNP and expression arrays. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Use this converter when you have dense quantitative data to display | ||
| 18 | using the xyplot, density, or heatmap glyphs, and too many data items | ||
| 19 | (thousands) to load into GBrowse. It creates one or more space- | ||
| 20 | efficient binary files containing the quantitative data, as well as a | ||
| 21 | small GFF3 file that can be loaded into Chado or other GBrowse | ||
| 22 | databases. | ||
| 23 | |||
| 24 | Typical usage is as follows: | ||
| 25 | |||
| 26 | % wiggle2gff3.pl --method=microarray_oligo my_data.wig > my_data.gff3 | ||
| 27 | |||
| 28 | =head2 Options | ||
| 29 | |||
| 30 | The following options are accepted: | ||
| 31 | |||
| 32 | --method=<method name> Set the method for the GFF3 lines representing | ||
| 33 | each quantitative data point in the track. | ||
| 34 | The default is "microarray_oligo." | ||
| 35 | |||
| 36 | --source=<source> Set the source field for the GFF3 file. The default is | ||
| 37 | none. | ||
| 38 | |||
| 39 | --gff3 Create a GFF3-format file (the default) | ||
| 40 | |||
| 41 | --featurefile Create a "featurefile" format file -- this is the | ||
| 42 | simplified format used for GBrowse uploads. This | ||
| 43 | option is incompatible with the --gff3 option. | ||
| 44 | |||
| 45 | --sample If true, then very large files (>5 MB) will be sampled | ||
| 46 | to obtain minimum, maximum and standard deviation; otherwise | ||
| 47 | the entire file will be scanned to obtain these statistics. | ||
| 48 | This will process the files faster but may miss outlier | ||
| 49 | values. | ||
| 50 | |||
| 51 | --path=<path> Specify the directory in which to place the binary wiggle | ||
| 52 | files. The default is the current temporary directory | ||
| 53 | (/tmp or whatever is appropriate for your operating system). | ||
| 54 | |||
| 55 | --base=<path> Same as "--path". | ||
| 56 | |||
| 57 | |||
| 58 | --trackname specify the trackname base for the wigfile creation | ||
| 59 | |||
| 60 | --help This documentation. | ||
| 61 | |||
| 62 | This script will accept a variety of option styles, including | ||
| 63 | abbreviated options ("--meth=foo"), single character options ("-m | ||
| 64 | foo"), and other common variants. | ||
| 65 | |||
| 66 | =head2 Binary wiggle files | ||
| 67 | |||
| 68 | The binary "wiggle" files created by this utility are readable using | ||
| 69 | the L<Bio::Graphics::Wiggle> module. The quantitative data is scaled | ||
| 70 | to the range of 1-255 (losing lots of precision, but still more than | ||
| 71 | enough for data visualization), and stored in a packed format in which | ||
| 72 | each file corresponds to the length of a single chromosome or contig. | ||
| 73 | |||
| 74 | Once created, the binary files should not be moved or renamed, unless | ||
| 75 | you are careful to make corresponding changes to the pathnames given | ||
| 76 | by the "wigfile" attribute in the GFF3 file feature lines. You should | ||
| 77 | also be careful about using the cp command to copy the binary files; | ||
| 78 | they are formatted with "holes" in such a way that missing data does | ||
| 79 | not take up any space on disk. If you cp them, the holes will fill up | ||
| 80 | with zeroes and the space savings will be lost. Better to use the | ||
| 81 | "tar" command with its --sparse option to move the files from one | ||
| 82 | place to another. | ||
| 83 | |||
| 84 | =head2 Example WIG File | ||
| 85 | |||
| 86 | This example is from | ||
| 87 | L<http://genome.ucsc.edu/goldenPath/help/wiggle.html>: | ||
| 88 | |||
| 89 | |||
| 90 | # filename: example.wig | ||
| 91 | # | ||
| 92 | # 300 base wide bar graph, autoScale is on by default == graphing | ||
| 93 | # limits will dynamically change to always show full range of data | ||
| 94 | # in viewing window, priority = 20 positions this as the second graph | ||
| 95 | # Note, zero-relative, half-open coordinate system in use for bed format | ||
| 96 | track type=wiggle_0 name="Bed Format" description="BED format" \ | ||
| 97 | visibility=full color=200,100,0 altColor=0,100,200 priority=20 | ||
| 98 | chr19 59302000 59302300 -1.0 | ||
| 99 | chr19 59302300 59302600 -0.75 | ||
| 100 | chr19 59302600 59302900 -0.50 | ||
| 101 | chr19 59302900 59303200 -0.25 | ||
| 102 | chr19 59303200 59303500 0.0 | ||
| 103 | chr19 59303500 59303800 0.25 | ||
| 104 | chr19 59303800 59304100 0.50 | ||
| 105 | chr19 59304100 59304400 0.75 | ||
| 106 | chr19 59304400 59304700 1.00 | ||
| 107 | # 150 base wide bar graph at arbitrarily spaced positions, | ||
| 108 | # threshold line drawn at y=11.76 | ||
| 109 | # autoScale off viewing range set to [0:25] | ||
| 110 | # priority = 10 positions this as the first graph | ||
| 111 | # Note, one-relative coordinate system in use for this format | ||
| 112 | track type=wiggle_0 name="variableStep" description="variableStep format" \ | ||
| 113 | visibility=full autoScale=off viewLimits=0.0:25.0 color=255,200,0 \ | ||
| 114 | yLineMark=11.76 yLineOnOff=on priority=10 | ||
| 115 | variableStep chrom=chr19 span=150 | ||
| 116 | 59304701 10.0 | ||
| 117 | 59304901 12.5 | ||
| 118 | 59305401 15.0 | ||
| 119 | 59305601 17.5 | ||
| 120 | 59305901 20.0 | ||
| 121 | 59306081 17.5 | ||
| 122 | 59306301 15.0 | ||
| 123 | 59306691 12.5 | ||
| 124 | 59307871 10.0 | ||
| 125 | # 200 base wide points graph at every 300 bases, 50 pixel high graph | ||
| 126 | # autoScale off and viewing range set to [0:1000] | ||
| 127 | # priority = 30 positions this as the third graph | ||
| 128 | # Note, one-relative coordinate system in use for this format | ||
| 129 | track type=wiggle_0 name="fixedStep" description="fixed step" visibility=full \ | ||
| 130 | autoScale=off viewLimits=0:1000 color=0,200,100 maxHeightPixels=100:50:20 \ | ||
| 131 | graphType=points priority=30 | ||
| 132 | fixedStep chrom=chr19 start=59307401 step=300 span=200 | ||
| 133 | 1000 | ||
| 134 | 900 | ||
| 135 | 800 | ||
| 136 | 700 | ||
| 137 | 600 | ||
| 138 | 500 | ||
| 139 | 400 | ||
| 140 | 300 | ||
| 141 | 200 | ||
| 142 | 100 | ||
| 143 | |||
| 144 | You can convert this into a loadable GFF3 file with the following | ||
| 145 | command: | ||
| 146 | |||
| 147 | wiggle2gff3.pl --meth=example --so=example --path=/var/gbrowse/db example.wig \ | ||
| 148 | > example.gff3 | ||
| 149 | |||
| 150 | The output will look like this: | ||
| 151 | |||
| 152 | ##gff-version 3 | ||
| 153 | |||
| 154 | chr19 example example 59302001 59304700 . . . Name=Bed Format;wigfile=/var/gbrowse/db/track001.chr19.1199828298.wig | ||
| 155 | chr19 example example 59304701 59308020 . . . Name=variableStep;wigfile=/var/gbrowse/db/track002.chr19.1199828298.wig | ||
| 156 | chr19 example example 59307401 59310400 . . . Name=fixedStep;wigfile=/var/gbrowse/db/track003.chr19.1199828298.wig | ||
| 157 | |||
| 158 | =head1 PROBLEMS | ||
| 159 | |||
| 160 | This script has trouble with wig files from very fragmented genomes | ||
| 161 | (>100K scaffolds). In this case, you may wish to run split_wig.pl, | ||
| 162 | which splits the original wig file into a series of smaller files with | ||
| 163 | a maximum of 900 scaffolds each. It then runs wiggle2gff3.pl for each | ||
| 164 | subfile and stores the results in separate folders. | ||
| 165 | |||
| 166 | =head1 SEE ALSO | ||
| 167 | |||
| 168 | L<Bio::DB::GFF>, L<bp_bulk_load_gff.pl>, L<bp_fast_load_gff.pl>, | ||
| 169 | L<bp_load_gff.pl>, L<bp_seqfeature_load.pl> | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Lincoln Stein <lstein@cshl.org>. | ||
| 174 | |||
| 175 | Copyright (c) 2008 Cold Spring Harbor Laboratory | ||
| 176 | |||
| 177 | This package is free software; you can redistribute it and/or modify | ||
| 178 | it under the terms of the GPL (either version 1, or at your option, | ||
| 179 | any later version) or the Artistic License 2.0. Refer to LICENSE for | ||
| 180 | the full license text. See DISCLAIMER.txt for disclaimers of | ||
| 181 | warranty. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | |||
| 186 | use strict; | ||
| 187 | |||
| 188 | use Bio::Graphics::Wiggle::Loader; | ||
| 189 | use Getopt::Long; | ||
| 190 | use Pod::Usage; | ||
| 191 | use File::Spec; | ||
| 192 | use File::Temp 'tempfile',':seekable'; | ||
| 193 | |||
| 194 | my ($show_help, $method, $source, $use_gff3, $use_featurefile, | ||
| 195 | $base_directory, $trackname,$sample); | ||
| 196 | |||
| 197 | GetOptions( | ||
| 198 | 'h|help' => \$show_help, # Show help and exit | ||
| 199 | 'method=s' => \$method, | ||
| 200 | 'source=s' => \$source, | ||
| 201 | 'gff3' => \$use_gff3, | ||
| 202 | 'sample' => \$sample, | ||
| 203 | 'featurefile' => \$use_featurefile, | ||
| 204 | 'base|path=s' => \$base_directory, | ||
| 205 | 't|trackname:s' => \$trackname, | ||
| 206 | ) | ||
| 207 | or pod2usage(-verbose=>2); | ||
| 208 | pod2usage(-verbose=>2) if $show_help; | ||
| 209 | |||
| 210 | die "Only one of -gff3 or -featurefile options is allowed. Use -h for help" | ||
| 211 | if $use_gff3 && $use_featurefile; | ||
| 212 | |||
| 213 | unless (defined $base_directory) { | ||
| 214 | $base_directory = File::Spec->tmpdir(); | ||
| 215 | warn "Using $base_directory as base directory for binary files"; | ||
| 216 | } | ||
| 217 | |||
| 218 | -d $base_directory && -w _ | ||
| 219 | or die "$base_directory is not a writeable directory. Use -h for more help"; | ||
| 220 | |||
| 221 | my $loader = Bio::Graphics::Wiggle::Loader->new($base_directory) | ||
| 222 | or die "could not create loader"; | ||
| 223 | $loader->allow_sampling(1) if $sample && $loader->can('allow_sampling'); # newish feature | ||
| 224 | |||
| 225 | # specify the trackname base if provided | ||
| 226 | $loader->{trackname} = $trackname if defined $trackname; | ||
| 227 | my $type = $use_featurefile ? 'featurefile' : 'gff3'; | ||
| 228 | |||
| 229 | while (my $file = shift) { | ||
| 230 | |||
| 231 | my $fh; | ||
| 232 | |||
| 233 | if ($file =~ /\.(gz|bz2)$/) { | ||
| 234 | warn "creating tempfile"; | ||
| 235 | $fh = tempfile(); | ||
| 236 | my $unzipper = $file =~ /\.gz$/ ? 'gunzip -c' : 'bunzip2 -c'; | ||
| 237 | my $unzip = IO::File->new("$unzipper $file|"); | ||
| 238 | my $data; | ||
| 239 | while ($unzip->read($data,1024)) { | ||
| 240 | $fh->print($data); | ||
| 241 | } | ||
| 242 | $unzip->close; | ||
| 243 | seek($fh,0,SEEK_SET); | ||
| 244 | } else { | ||
| 245 | $fh = IO::File->new($file) or die "could not open $file: $!"; | ||
| 246 | } | ||
| 247 | print STDERR "Processing $file..."; | ||
| 248 | $loader->load($fh); | ||
| 249 | print STDERR "done.\n"; | ||
| 250 | print $loader->featurefile($type,$method,$source); | ||
| 251 | } | ||
| 252 | |||
| 253 | 1; | ||
| 254 | |||
| 255 |