| 1 | package InterMine::Item::Document; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | InterMine::Item::Document - a module for writing InterMine-Items-XML files | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | use InterMine::Model; | ||||||||||||||
| 10 | use InterMine::Item::Document; | ||||||||||||||
| 11 | |||||||||||||||
| 12 | my ($model_file, $output_file) = @ARGV; | ||||||||||||||
| 13 | |||||||||||||||
| 14 | my $model = InterMine::Model->new(file => $model_file); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | my $doc = InterMine::Item::Document->new( | ||||||||||||||
| 17 | model => $model, | ||||||||||||||
| 18 | output => $output_file, # defaults to STDOUT | ||||||||||||||
| 19 | auto_write => 1, # automatically write each item as it is made | ||||||||||||||
| 20 | ); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | my $organism = $doc->add_item( | ||||||||||||||
| 23 | "Organism", | ||||||||||||||
| 24 | "taxonId" => 7227 | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | my $pub1 = $doc->add_item( | ||||||||||||||
| 28 | "Publication", | ||||||||||||||
| 29 | "pubMedId" => 11700288, | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | my $pub2 = $doc->make_item( | ||||||||||||||
| 32 | "Publication", | ||||||||||||||
| 33 | "pubMedId" => 16496002, | ||||||||||||||
| 34 | ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | $doc->add_item( | ||||||||||||||
| 37 | 'Gene', | ||||||||||||||
| 38 | 'identifier' => "CG10811", | ||||||||||||||
| 39 | 'organism' => $organism, | ||||||||||||||
| 40 | 'publications' => [$pub1, $pub2], | ||||||||||||||
| 41 | ); | ||||||||||||||
| 42 | |||||||||||||||
| 43 | $doc->close; # writes the end tags - only needed when auto_write is on | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||
| 46 | |||||||||||||||
| 47 | This module allows you to make and write out InterMine-Items-XML for | ||||||||||||||
| 48 | integrating data into InterMine databases | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head1 AUTHOR | ||||||||||||||
| 51 | |||||||||||||||
| 52 | FlyMine C<< <support@flymine.org> >> | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 BUGS | ||||||||||||||
| 55 | |||||||||||||||
| 56 | Please report any bugs or feature requests to C<support@flymine.org>. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head1 SUPPORT | ||||||||||||||
| 59 | |||||||||||||||
| 60 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 61 | |||||||||||||||
| 62 | perldoc InterMine::ItemFactory | ||||||||||||||
| 63 | |||||||||||||||
| 64 | You can also look for information at: | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =over 4 | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =item * FlyMine | ||||||||||||||
| 69 | |||||||||||||||
| 70 | L<http://www.flymine.org> | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =item * Documentation | ||||||||||||||
| 73 | |||||||||||||||
| 74 | L<http://www.intermine.org/wiki/ItemsAPIPerl> | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =back | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 79 | |||||||||||||||
| 80 | Copyright 2006,2007,2008 FlyMine, all rights reserved. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 83 | under the same terms as Perl itself. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head1 FUNCTIONS | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =cut | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 90 | |||||||||||||||
| 91 | 1 | ▶ | use XML::Writer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 92 | 1 | ▶ | use InterMine::Item; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | =head2 new | ||||||||||||||
| 95 | |||||||||||||||
| 96 | return a factory that can be used to create Item objects. | ||||||||||||||
| 97 | |||||||||||||||
| 98 | $document = new InterMine::Item::Document(model => $model); | ||||||||||||||
| 99 | |||||||||||||||
| 100 | Args: | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =over 4 | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =item model - the InterMine::Model object to use to check field validity | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =item output - [optional] The file to write to (defaults to standard out) | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =item ignore_null - [optional] Whether or not to be tolerant of undefined field values (defaults to intolerant) | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =back | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =cut | ||||||||||||||
| 113 | |||||||||||||||
| 114 | sub new { | ||||||||||||||
| 115 | 0 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 116 | 0 | my %opts = @_; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | ▶ | if ( !exists $opts{model} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 0 | die "model argument missing in Item::Document constructor\n"; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | 0 | my %writer_args = ( | |||||||||||||
| 122 | DATA_MODE => 1, | ||||||||||||||
| 123 | DATA_INDENT => 3, | ||||||||||||||
| 124 | ); | ||||||||||||||
| 125 | 0 | ▶ | if ( my $out_file = delete $opts{output} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 0 | ▶ | open( my $output, '>', $out_file ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | or die "Cannot open $out_file for writing, $!"; | ||||||||||||||
| 128 | 0 | $writer_args{OUTPUT} = $output; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | 0 | $opts{writer} = new XML::Writer(%writer_args); | |||||||||||||
| 131 | 0 | my $model = $opts{model}; | |||||||||||||
| 132 | 0 | $opts{unwritten_items} = []; | |||||||||||||
| 133 | |||||||||||||||
| 134 | 0 | my $self = | |||||||||||||
| 135 | { id_counter => 0, %opts, package_name => $model->package_name() }; | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | bless $self, $class; | |||||||||||||
| 138 | 0 | return $self; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head2 writer | ||||||||||||||
| 142 | |||||||||||||||
| 143 | Function : Return the writer for this document | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =cut | ||||||||||||||
| 146 | |||||||||||||||
| 147 | sub writer { | ||||||||||||||
| 148 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
writer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 149 | 0 | return $self->{writer}; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | |||||||||||||||
| 153 | =head2 add_default_fields(key => value, key => value) | ||||||||||||||
| 154 | |||||||||||||||
| 155 | Function : add the given key value pairs to the default fields list | ||||||||||||||
| 156 | that will be appplied to every item made. If a default field | ||||||||||||||
| 157 | is not valid for a particular item, it will not be set. | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =cut | ||||||||||||||
| 160 | |||||||||||||||
| 161 | sub add_default_fields { | ||||||||||||||
| 162 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
add_default_fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 163 | 0 | my %new_defaults = @_; | |||||||||||||
| 164 | 0 | my @keys = keys %new_defaults; | |||||||||||||
| 165 | 0 | @{$self->{defaults}}{@keys} = @new_defaults{@keys}; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head2 get_default_fields() | ||||||||||||||
| 169 | |||||||||||||||
| 170 | Function : get the current default fields | ||||||||||||||
| 171 | Returns : a hash in list context, or a hash-reference in scalar context | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =cut | ||||||||||||||
| 174 | |||||||||||||||
| 175 | sub get_default_fields { | ||||||||||||||
| 176 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
get_default_fields: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 177 | 0 | ▶ | if (wantarray) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | 0 | return %{$self->{defaults}}; | |||||||||||||
| 179 | } else { | ||||||||||||||
| 180 | 0 | return $self->{defaults}; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =head2 write | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Function : write all unwritten items to the xml output. This is | ||||||||||||||
| 187 | called automatically on item creation if auto_write is | ||||||||||||||
| 188 | set. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =cut | ||||||||||||||
| 191 | |||||||||||||||
| 192 | sub write { | ||||||||||||||
| 193 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
write: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 194 | 0 | push @{ $self->{unwritten_items} }, @_; | |||||||||||||
| 195 | |||||||||||||||
| 196 | 0 | ▶ | return unless @{ $self->{unwritten_items} }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | 0 | my $writer = $self->{writer}; | |||||||||||||
| 198 | |||||||||||||||
| 199 | 0 | ▶ | unless ( $writer->within_element('items') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 0 | $writer->startTag('items'); | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | 0 | ▶ | while ( my $item = shift @{ $self->{unwritten_items} } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 203 | 0 | $item->as_xml($writer, $self->{ignore_null}); | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | 0 | return; | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | sub DESTROY { | ||||||||||||||
| 209 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||
| 210 | 0 | $self->close; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =head2 close | ||||||||||||||
| 214 | |||||||||||||||
| 215 | Function : close the document by writing any unwritten | ||||||||||||||
| 216 | items and closing the items tag. | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =cut | ||||||||||||||
| 219 | |||||||||||||||
| 220 | sub close { | ||||||||||||||
| 221 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 222 | 0 | $self->write(); | |||||||||||||
| 223 | 0 | ▶ | if ( $self->writer->within_element('items') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | 0 | $self->writer->endTag('items'); | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | 0 | return; | |||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =head2 make_item | ||||||||||||||
| 230 | |||||||||||||||
| 231 | Title : make_item | ||||||||||||||
| 232 | Usage : $item = $doc->make_item("Gene", [%attributes]); | ||||||||||||||
| 233 | Function: return a new Item object of the given type | ||||||||||||||
| 234 | while not storing it | ||||||||||||||
| 235 | Args : the classname of the new Item | ||||||||||||||
| 236 | Any attributes for the item | ||||||||||||||
| 237 | |||||||||||||||
| 238 | =cut | ||||||||||||||
| 239 | |||||||||||||||
| 240 | sub make_item { | ||||||||||||||
| 241 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
make_item: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||
| 242 | 0 | my %args; | |||||||||||||
| 243 | my %attr; | ||||||||||||||
| 244 | |||||||||||||||
| 245 | 0 | ▶ | if ( @_ == 1 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 246 | 0 | $args{classname} = shift; | |||||||||||||
| 247 | } | ||||||||||||||
| 248 | elsif ( @_ % 2 == 1 ) { | ||||||||||||||
| 249 | 0 | $args{classname} = shift; | |||||||||||||
| 250 | 0 | %attr = @_; | |||||||||||||
| 251 | } | ||||||||||||||
| 252 | else { | ||||||||||||||
| 253 | 0 | %args = @_; | |||||||||||||
| 254 | } | ||||||||||||||
| 255 | |||||||||||||||
| 256 | 0 | $self->{id_counter}++; | |||||||||||||
| 257 | |||||||||||||||
| 258 | 0 | ▶ | my $classname = $args{classname} || ''; | ||||||||||||
|
Condition0%0/2
$args{'classname'} || ''
MC/DC0%0/1
$args{'classname'} || ''
$args{'classname'}
Truth table0%0/2
A
A= $args{'classname'}
| |||||||||||||||
| 259 | 0 | ▶ | my $implements = $args{implements} || ''; | ||||||||||||
|
Condition0%0/2
$args{'implements'} || ''
MC/DC0%0/1
$args{'implements'} || ''
$args{'implements'}
Truth table0%0/2
A
A= $args{'implements'}
| |||||||||||||||
| 260 | |||||||||||||||
| 261 | my $item = new InterMine::Item( | ||||||||||||||
| 262 | classname => $classname, | ||||||||||||||
| 263 | implements => $implements, | ||||||||||||||
| 264 | model => $self->{model}, | ||||||||||||||
| 265 | id => $self->{id_counter}, | ||||||||||||||
| 266 | ignore_null => $self->{ignore_null}, | ||||||||||||||
| 267 | 0 | ); | |||||||||||||
| 268 | |||||||||||||||
| 269 | 0 | ▶ | while ( my ( $k, $v ) = each %attr ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 270 | 0 | $item->set( $k, $v ); | |||||||||||||
| 271 | } | ||||||||||||||
| 272 | 0 | ▶ | if ($self->get_default_fields) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 273 | 0 | ▶ | while ( my ( $k, $v ) = each %{$self->get_default_fields} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 274 | 0 | ▶ | if ($item->has_field_called($k)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 275 | 0 | $item->set($k, $v); | |||||||||||||
| 276 | } | ||||||||||||||
| 277 | } | ||||||||||||||
| 278 | } | ||||||||||||||
| 279 | 0 | return $item; | |||||||||||||
| 280 | } | ||||||||||||||
| 281 | |||||||||||||||
| 282 | =head2 add_item | ||||||||||||||
| 283 | |||||||||||||||
| 284 | Title : add_item | ||||||||||||||
| 285 | Usage : $item = $doc->add_item("Gene"); | ||||||||||||||
| 286 | Function: return a new Item object of the given type, | ||||||||||||||
| 287 | while storing it in an internal record for writing | ||||||||||||||
| 288 | out later | ||||||||||||||
| 289 | Args : the classname of the new Item | ||||||||||||||
| 290 | Any attributes for the item | ||||||||||||||
| 291 | |||||||||||||||
| 292 | =cut | ||||||||||||||
| 293 | |||||||||||||||
| 294 | sub add_item { | ||||||||||||||
| 295 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
add_item: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 296 | 0 | my $item = $self->make_item(@_); | |||||||||||||
| 297 | |||||||||||||||
| 298 | 0 | push @{ $self->{unwritten_items} }, $item; | |||||||||||||
| 299 | 0 | ▶ | if ( $self->{auto_write} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 300 | 0 | $self->write(); | |||||||||||||
| 301 | } | ||||||||||||||
| 302 | 0 | return $item; | |||||||||||||
| 303 | } | ||||||||||||||
| 304 | |||||||||||||||
| 305 | 1 | 1; | |||||||||||||