| 1 | package Test::Chado::FixtureLoader::Flatfile; | ||
| 2 | { | ||
| 3 | $Test::Chado::FixtureLoader::Flatfile::VERSION = 'v4.1.1'; | ||
| 4 | } | ||
| 5 | |||
| 6 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::late; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Types::Standard qw/Str/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use YAML qw/LoadFile/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Test::Chado::Types qw/Twig Graph GraphT FixtureManager/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Test::Chado::FixtureManager::Flatfile; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use Graph; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use XML::Twig; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use XML::Twig::XPath; | ||
| 16 | use Graph::Traversal::BFS; | ||
| 17 | |||
| 18 | has 'namespace' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => Str, | ||
| 21 | lazy => 1, | ||
| 22 | default => 'test-chado' | ||
| 23 | ); | ||
| 24 | |||
| 25 | has 'fixture_manager' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => FixtureManager, | ||
| 28 | lazy => 1, | ||
| 29 | default => sub { | ||
| 30 | my $self = shift; | ||
| 31 | return Test::Chado::FixtureManager::Flatfile->new; | ||
| 32 | } | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'obo_xml' => ( | ||
| 36 | is => 'rw', | ||
| 37 | isa => Str | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'obo_xml_loader' => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => Twig, | ||
| 43 | lazy => 1, | ||
| 44 | builder => 1, | ||
| 45 | clearer => 1 | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub _build_obo_xml_loader { | ||
| 49 | my ($self) = @_; | ||
| 50 | XML::Twig->new( | ||
| 51 | twig_handlers => { | ||
| 52 | term => sub { $self->load_term(@_) }, | ||
| 53 | typedef => sub { $self->load_typedef(@_) } | ||
| 54 | } | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | |||
| 58 | has 'graph' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => Graph, | ||
| 61 | default => sub { Graph->new( directed => 1 ) }, | ||
| 62 | lazy => 1, | ||
| 63 | clearer => 1 | ||
| 64 | ); | ||
| 65 | |||
| 66 | has 'traverse_graph' => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => GraphT, | ||
| 69 | lazy => 1, | ||
| 70 | builder => 1, | ||
| 71 | clearer => 1, | ||
| 72 | handles => { store_relationship => 'bfs' } | ||
| 73 | ); | ||
| 74 | |||
| 75 | sub _build_traverse_graph { | ||
| 76 | my ($self) = @_; | ||
| 77 | Graph::Traversal::BFS->new( | ||
| 78 | $self->graph, | ||
| 79 | pre_edge => sub { | ||
| 80 | $self->handle_relationship(@_); | ||
| 81 | }, | ||
| 82 | back_edge => sub { | ||
| 83 | $self->handle_relationship(@_); | ||
| 84 | }, | ||
| 85 | down_edge => sub { | ||
| 86 | $self->handle_relationship(@_); | ||
| 87 | }, | ||
| 88 | non_tree_edge => sub { | ||
| 89 | $self->handle_relationship(@_); | ||
| 90 | }, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | has 'ontology_namespace' => ( | ||
| 95 | is => 'rw', | ||
| 96 | isa => Str, | ||
| 97 | lazy => 1, | ||
| 98 | builder => 1, | ||
| 99 | clearer => 1 | ||
| 100 | ); | ||
| 101 | |||
| 102 | sub _build_ontology_namespace { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | #which namespace to use incase it is not present for a particular node | ||
| 106 | my $twig = XML::Twig::XPath->new->parsefile( $self->obo_xml ); | ||
| 107 | my ($node) = $twig->findnodes('/obo/header/default-namespace'); | ||
| 108 | my $namespace = $node->getValue; | ||
| 109 | $twig->purge; | ||
| 110 | croak "no default namespace being set for this ontology" if !$namespace; | ||
| 111 | return $namespace; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub reset_all { | ||
| 115 | my ($self) = @_; | ||
| 116 | $self->clear_graph; | ||
| 117 | $self->clear_traverse_graph; | ||
| 118 | $self->clear_dbrow; | ||
| 119 | $self->clear_cvrow; | ||
| 120 | $self->clear_ontology_namespace; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub load_organism { | ||
| 124 | my $self = shift; | ||
| 125 | my $organism = LoadFile( $self->fixture_manager->organism_fixture ); | ||
| 126 | unshift @$organism, [qw/abbreviation genus species common_name/]; | ||
| 127 | |||
| 128 | my $schema = $self->schema; | ||
| 129 | $schema->txn_do( | ||
| 130 | sub { | ||
| 131 | $schema->populate( 'Organism::Organism', $organism ); | ||
| 132 | } | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub load_rel { | ||
| 137 | my ($self) = @_; | ||
| 138 | $self->clear_ontology_namespace; | ||
| 139 | $self->obo_xml( $self->fixture_manager->rel_fixture ); | ||
| 140 | $self->load_ontology; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub load_so { | ||
| 144 | my ($self) = @_; | ||
| 145 | $self->clear_ontology_namespace; | ||
| 146 | $self->obo_xml( $self->fixture_manager->so_fixture ); | ||
| 147 | $self->load_ontology; | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | sub load_ontology { | ||
| 152 | my ($self) = @_; | ||
| 153 | $self->reset_all; | ||
| 154 | my $loader = $self->obo_xml_loader; | ||
| 155 | $loader->parsefile( $self->obo_xml ); | ||
| 156 | $loader->purge; | ||
| 157 | $self->store_relationship; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub load_fixtures { | ||
| 161 | my $self = shift; | ||
| 162 | $self->load_organism; | ||
| 163 | $self->load_rel; | ||
| 164 | $self->load_so; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub handle_relationship { | ||
| 168 | my ( $self, $parent, $child, $traverse ) = @_; | ||
| 169 | my ( $relation_id, $parent_id, $child_id ); | ||
| 170 | |||
| 171 | # -- relation/edge | ||
| 172 | if ( $self->graph->has_edge_attribute( $parent, $child, 'id' ) ) { | ||
| 173 | $relation_id | ||
| 174 | = $self->graph->get_edge_attribute( $parent, $child, 'id' ); | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | |||
| 178 | # -- get the id from the storage | ||
| 179 | $relation_id = $self->name2id( | ||
| 180 | $self->graph->get_edge_attribute( | ||
| 181 | $parent, $child, 'relationship' | ||
| 182 | ), | ||
| 183 | ); | ||
| 184 | $self->graph->set_edge_attribute( $parent, $child, 'id', | ||
| 185 | $relation_id ); | ||
| 186 | } | ||
| 187 | |||
| 188 | # -- parent | ||
| 189 | if ( $self->graph->has_vertex_attribute( $parent, 'id' ) ) { | ||
| 190 | $parent_id = $self->graph->get_vertex_attribute( $parent, 'id' ); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $parent_id = $self->name2id($parent); | ||
| 194 | $self->graph->set_vertex_attribute( $parent, 'id', $parent_id ); | ||
| 195 | } | ||
| 196 | |||
| 197 | # -- child | ||
| 198 | if ( $self->graph->has_vertex_attribute( $child, 'id' ) ) { | ||
| 199 | $child_id = $self->graph->get_vertex_attribute( $child, 'id' ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $child_id = $self->name2id($child); | ||
| 203 | $self->graph->set_vertex_attribute( $child, 'id', $child_id ); | ||
| 204 | } | ||
| 205 | |||
| 206 | my $schema = $self->schema; | ||
| 207 | $schema->txn_do( | ||
| 208 | sub { | ||
| 209 | $schema->resultset('Cv::CvtermRelationship')->create( | ||
| 210 | { object_id => $parent_id, | ||
| 211 | subject_id => $child_id, | ||
| 212 | type_id => $relation_id | ||
| 213 | } | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub name2id { | ||
| 220 | my ( $self, $name ) = @_; | ||
| 221 | my $row = $self->schema->resultset('Cv::Cvterm') | ||
| 222 | ->search( { 'name' => $name, }, { rows => 1 } )->single; | ||
| 223 | |||
| 224 | if ( !$row ) { #try again in dbxref | ||
| 225 | $row | ||
| 226 | = $self->schema->resultset('General::Dbxref') | ||
| 227 | ->search( { accession => { -like => '%' . $name } }, | ||
| 228 | { rows => 1 } )->single; | ||
| 229 | if ( !$row ) { | ||
| 230 | my $namespace = $self->ontology_namespace; | ||
| 231 | $row = $self->schema->txn_do( | ||
| 232 | sub { | ||
| 233 | return $self->schema->resultset('Cv::Cvterm')->create( | ||
| 234 | { cv_id => $self->find_or_create_cv_id($namespace), | ||
| 235 | name => $name, | ||
| 236 | dbxref => { | ||
| 237 | db_id => | ||
| 238 | $self->find_or_create_db_id($namespace), | ||
| 239 | accession => $name, | ||
| 240 | } | ||
| 241 | } | ||
| 242 | ); | ||
| 243 | } | ||
| 244 | ); | ||
| 245 | return $row->cvterm_id; | ||
| 246 | } | ||
| 247 | return $row->cvterm->cvterm_id; | ||
| 248 | } | ||
| 249 | $row->cvterm_id; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub build_relationship { | ||
| 253 | my ( $self, $node, $cvterm_row ) = @_; | ||
| 254 | my $child = $cvterm_row->name; | ||
| 255 | for my $elem ( $node->children('is_a') ) { | ||
| 256 | my $parent = $self->normalize_name( $elem->text ); | ||
| 257 | $self->graph->set_edge_attribute( $parent, $child, 'relationship', | ||
| 258 | 'is_a' ); | ||
| 259 | } | ||
| 260 | |||
| 261 | for my $elem ( $node->children('relationship') ) { | ||
| 262 | my $parent = $self->normalize_name( $elem->first_child_text('to') ); | ||
| 263 | $self->graph->add_edge( $parent, $child ); | ||
| 264 | $self->graph->set_edge_attribute( $parent, $child, 'relationship', | ||
| 265 | $self->normalize_name( $elem->first_child_text('type') ) ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | sub load_typedef { | ||
| 270 | my ( $self, $twig, $node ) = @_; | ||
| 271 | |||
| 272 | my $name = $node->first_child_text('name'); | ||
| 273 | my $id = $node->first_child_text('id'); | ||
| 274 | my $is_obsolete = $node->first_child_text('is_obsolete'); | ||
| 275 | |||
| 276 | my $namespace | ||
| 277 | = $node->has_child('namespace') | ||
| 278 | ? $node->first_child_text('namespace') | ||
| 279 | : $self->ontology_namespace; | ||
| 280 | |||
| 281 | my $def_elem = $node->first_child('def'); | ||
| 282 | my $definition; | ||
| 283 | $definition = $def_elem->first_child_text('defstr') if $def_elem; | ||
| 284 | |||
| 285 | my $schema = $self->schema; | ||
| 286 | my $cvterm_row = $schema->txn_do( | ||
| 287 | sub { | ||
| 288 | return $schema->resultset('Cv::Cvterm')->create( | ||
| 289 | { cv_id => $self->find_or_create_cv_id($namespace), | ||
| 290 | is_relationshiptype => 1, | ||
| 291 | name => $self->normalize_name($name), | ||
| 292 | definition => $definition || '', | ||
| 293 | is_obsolete => $is_obsolete || 0, | ||
| 294 | dbxref => { | ||
| 295 | db_id => $self->find_or_create_db_id($namespace), | ||
| 296 | accession => $id, | ||
| 297 | } | ||
| 298 | } | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | ); | ||
| 302 | |||
| 303 | #hold on to the relationships between nodes | ||
| 304 | $self->build_relationship( $node, $cvterm_row ); | ||
| 305 | |||
| 306 | #no additional dbxref | ||
| 307 | return if !$def_elem; | ||
| 308 | |||
| 309 | $self->create_more_dbxref( $def_elem, $cvterm_row, $namespace ); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub load_term { | ||
| 313 | my ( $self, $twig, $node ) = @_; | ||
| 314 | |||
| 315 | my $name = $node->first_child_text('name'); | ||
| 316 | my $id = $node->first_child_text('id'); | ||
| 317 | my $is_obsolete = $node->first_child_text('is_obsolete'); | ||
| 318 | |||
| 319 | my $namespace | ||
| 320 | = $node->has_child('namespace') | ||
| 321 | ? $node->first_child_text('namespace') | ||
| 322 | : $self->ontology_namespace; | ||
| 323 | |||
| 324 | my $def_elem = $node->first_child('def'); | ||
| 325 | my $definition; | ||
| 326 | $definition = $def_elem->first_child_text('defstr') if $def_elem; | ||
| 327 | |||
| 328 | my $schema = $self->schema; | ||
| 329 | my $cvterm_row = $schema->txn_do( | ||
| 330 | sub { | ||
| 331 | return $schema->resultset('Cv::Cvterm')->create( | ||
| 332 | { cv_id => $self->find_or_create_cv_id($namespace), | ||
| 333 | name => $self->normalize_name($name), | ||
| 334 | definition => $definition || '', | ||
| 335 | is_obsolete => $is_obsolete || 0, | ||
| 336 | dbxref => { | ||
| 337 | db_id => $self->find_or_create_db_id($namespace), | ||
| 338 | accession => $id, | ||
| 339 | } | ||
| 340 | } | ||
| 341 | ); | ||
| 342 | } | ||
| 343 | ); | ||
| 344 | |||
| 345 | #hold on to the relationships between nodes | ||
| 346 | $self->build_relationship( $node, $cvterm_row ); | ||
| 347 | |||
| 348 | #no additional dbxref | ||
| 349 | return if !$def_elem; | ||
| 350 | |||
| 351 | $self->create_more_dbxref( $def_elem, $cvterm_row, $namespace ); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub normalize_name { | ||
| 355 | my ( $self, $name ) = @_; | ||
| 356 | return $name if $name !~ /:/; | ||
| 357 | my $value = ( ( split /:/, $name ) )[1]; | ||
| 358 | return $value; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub create_more_dbxref { | ||
| 362 | my ( $self, $def_elem, $cvterm_row, $namespace ) = @_; | ||
| 363 | my $schema = $self->schema; | ||
| 364 | |||
| 365 | # - first one goes with alternate id | ||
| 366 | my $alt_id = $def_elem->first_child_text('alt_id'); | ||
| 367 | if ($alt_id) { | ||
| 368 | $schema->txn_do( | ||
| 369 | sub { | ||
| 370 | $cvterm_row->create_related( | ||
| 371 | 'cvterm_dbxrefs', | ||
| 372 | { dbxref => { | ||
| 373 | accession => $alt_id, | ||
| 374 | db_id => $self->find_or_create_db_id($namespace) | ||
| 375 | } | ||
| 376 | } | ||
| 377 | ); | ||
| 378 | } | ||
| 379 | ); | ||
| 380 | } | ||
| 381 | |||
| 382 | #no more additional dbxrefs | ||
| 383 | my $def_dbx = $def_elem->first_child('dbxref'); | ||
| 384 | return if !$def_dbx; | ||
| 385 | |||
| 386 | my $dbname = $def_dbx->first_child_text('dbname'); | ||
| 387 | $schema->txn_do( | ||
| 388 | sub { | ||
| 389 | $cvterm_row->create_related( | ||
| 390 | 'cvterm_dbxrefs', | ||
| 391 | { dbxref => { | ||
| 392 | accession => $def_dbx->first_child_text('acc'), | ||
| 393 | db_id => $self->find_or_create_db_id($dbname) | ||
| 394 | } | ||
| 395 | } | ||
| 396 | ); | ||
| 397 | } | ||
| 398 | ); | ||
| 399 | } | ||
| 400 | |||
| 401 | with 'Test::Chado::Role::Helper::WithBcs'; | ||
| 402 | |||
| 403 | 1; | ||
| 404 | |||
| 405 | __END__ | ||