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
|
package STAR::Writer;
use STAR::DataBlock;
use STAR::Dictionary;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
$VERSION = '0.01';
# $Id: Writer.pm,v 1.2 2000/12/19 22:54:56 helgew Exp $ RCS Identification
####################
# Constructor: new #
####################
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
bless ($self,$class);
return $self;
}
##########################
# Class method write_cif #
##########################
sub write_cif {
my ($self,@parameters) = @_;
my ($file, $data);
my $options = "";
# single unnamed parameter doesn't make sense here,
# need -dataref and -file
while ($_ = shift @parameters) {
$file = shift @parameters if /-file/;
$data = shift @parameters if /-dataref/;
$options = shift @parameters if /-options/;
}
my ($d, $s, $c, $i); #data, save, category, item
my ($m); # loop counter
my ($string, $dict, $log, $debug);
my ($cat, $item, @cats, @items, $next);
$dict = 1 if ( $data->type eq 'dictionary' );
$log = 1 if ( $options =~ /l/ );
$debug = 1 if ( $options =~ /d/ );
$data->add_quotes;
open (OUT, ">$file");
print STDERR "writing $file\n" if ( $log );
foreach $d ( sort keys %{$data->{DATA}} ) {
print OUT "data_$d\n";
foreach $s ( sort keys %{$data->{DATA}{$d}} ) {
print STDERR "$s\n" if $debug ; # for debugging
unless ( $s eq '-' ) {
print OUT "\n","#"x(length($s)+4),"\n";
print OUT "# $s #\n";
print OUT "#"x(length($s)+4),"\n\n";
print OUT "save_$s\n";
}
if ( $dict && ( $s eq '-' ) ) {
print OUT "\n##############\n";
print OUT "# DICTIONARY #\n";
print OUT "##############\n\n";
}
foreach $c ( sort keys %{$data->{DATA}{$d}{$s}} ) {
print STDERR "\t$c\n" if $debug ; #for debugging
unless ( $dict ) {
print OUT "\n","#"x(length($c)+4),"\n";
print OUT "# $c #\n";
print OUT "#"x(length($c)+4),"\n\n";
}
@items = sort keys %{$data->{DATA}{$d}{$s}{$c}};
if ( $#{$data->{DATA}{$d}{$s}{$c}{$items[0]}} == 0 ) {
print STDERR "\t\tin items if\n" if $debug; #debugging
foreach $item ( @items ) {
print STDERR "\t\t$item\n" if $debug; #for debugging
print OUT $item, " ";
$next = $data->{DATA}{$d}{$s}{$c}{$item}[0];
if ( $next =~ /^;/ ||
length($item.$next) >= 77 ) {
print OUT "\n";
}
print OUT "$next\n";
}
}
else { #loop
print STDERR "\t\tin items else\n" if $debug ; #debugging
print OUT "loop_\n";
foreach $item ( @items ) { #items in loop
print OUT "$item\n";
}
#values in loop:
foreach $m (0..$#{$data->{DATA}{$d}{$s}{$c}{$items[0]}}) {
$string='';
foreach $i ( @items ) {
$next = $data->{DATA}{$d}{$s}{$c}{$i}[$m];
if ( $next =~ /^;/ ) {
$string .="\n" unless ( $string eq '' );
print OUT $string,$next;
$string = '';
}
elsif ( length($string.$next) >= 80 ) {
$string .= "\n";
print OUT $string;
$string = $next.' ';
}
else {
$string .= $next;
$string .= ' ';
}
}
print OUT "$string\n";
}
}
}
print OUT "save_\n" unless ( $s eq '-' );
}
}
print STDERR "writing $file\n" if $log;
close (OUT);
}
##########################
# Class method write_xml #
##########################
# This is very premature and "undocumented".
# Just keeping the code here for convenience.
sub write_xml {
my ($self,@parameters) = @_;
my ($file, $data);
my $options = "";
# single unnamed parameter doesn't make sense here,
# need -dataref and -file
while ($_ = shift @parameters) {
$file = shift @parameters if /-file/;
$data = shift @parameters if /-dataref/;
$options = shift @parameters if /-options/;
}
my ($d, $s, $c, $i); #data, save, category, item
my ($dx, $sx, $cx, $ix); #xml compatible
my ($m); # loop counter
my ($string, $dict, $log);
my ($cat, $item, @cats, @items, $next);
$dict = 1 if ( $data->type eq 'dictionary' );
$log = 1 if ( $options =~ /l/ );
open (OUT, ">$file");
print STDERR "writing $file\n" if ( $log );
foreach $d ( sort keys %{$data->{DATA}} ) {
$dx = $d;
print OUT "<data_$dx>\n";
foreach $s ( sort keys %{$data->{DATA}{$d}} ) {
$sx = $s;
print OUT "\t<save_$sx>\n";
foreach $c ( sort keys %{$data->{DATA}{$d}{$s}} ) {
$cx = xml_tag($c);
print OUT "\t\t<$cx>\n";
@items = sort keys %{$data->{DATA}{$d}{$s}{$c}};
foreach $m (0..$#{$data->{DATA}{$d}{$s}{$c}{$items[0]}}) {
foreach $item ( @items ) {
$item =~ /^\S+?\.(.*)/;
$ix = xml_tag($1);
$next = $data->{DATA}{$d}{$s}{$c}{$item}[$m];
$next = xml_data($next);
if ( $next =~ /\n/s ) {
print OUT "\n";
}
print OUT "\t\t\t<$ix i=\"$m\">";
print OUT "$next";
if ( $next =~ /\n/s ) {
print OUT "\t\t\t</$ix>\n";
}
else {
print OUT "</$ix>\n";
}
}
}
print OUT "\t\t</$cx>\n";
}
print OUT "\t</save_$sx>\n";
}
print OUT "</data_$dx>\n";
}
print STDERR "writing $file\n" if $log;
close (OUT);
}
###########
# xml_tag #
###########
sub xml_tag {
my $string = shift;
$string =~ s/[^A-Za-z0-9_\.-]/_/g;
$string =~ s/^(\d.*)/_$1/;
return $string;
}
############
# xml_data #
############
sub xml_data {
my $string = shift;
$string =~ s/&/&/gs;
$string =~ s/</</gs;
$string =~ s/>/>/gs;
$string =~ s/'/'/gs;
$string =~ s/"/"/gs;
return $string;
}
1;
__END__
=head1 NAME
STAR::Writer - Perl extension for writing STAR::DataBlock objects
as files.
=head2 Version
This documentation refers to version 0.01 of this module.
=head1 SYNOPSIS
use STAR::Writer;
STAR::Writer->write_cif( -dataref=>$data, -file=>$file );
=head1 DESCRIPTION
This module will provide several methods for writing STAR::DataBlocks
as files in different format. Currently, there is a write_cif method,
which writes a STAR::DataBlock or STAR::Dictionary object as a file
in CIF (STAR) format.
=head1 CLASS METHODS
=head2 write_cif
Usage: STAR::Writer->write_cif( -dataref=>$data,
-file=>$file [,
-options=>$options ] );
Write the STAR::DataBlock object referenced by $data to the file specified
by $file. C<$options> are C<'l'> for logging activity (to STDERR) and
C<'d'> for debugging.
=head1 COMMENTS
Categories and items are currently written out in alphabetical
order. Obviously, this is of no importance to automated
parsing. However, it may not be desirable for visual inspection
of files.
=head1 AUTHOR
Wolfgang Bluhm, mail@wbluhm.com
=head2 Acknowledgments
Thanks to Phil Bourne, Helge Weissig, Anne Kuller, Doug Greer,
Michele Bluhm, and others for support, help, and comments.
=head1 COPYRIGHT
A full copyright statement is provided with the distribution
Copyright (c) 2000 University of California, San Diego
=head1 SEE ALSO
STAR::Parser, STAR::Dictionary.
=cut
|