@@ -360,19 +360,55 @@ WHERE NOT EXISTS (
360360 }
361361
362362 // Taxonomia + link a genome
363+ // Inserta la cadena completa (ancestros + hoja)
364+ // Pero la BD "indexa" hasta species: si el leaf es strain/subspecies, enlazamos el genoma a la species.
363365 const taxByAcc = taxonomyData ?. byAccession || { } ;
364366
367+ const rankScore = ( r ) => {
368+ const x = String ( r || "no rank" ) . toLowerCase ( ) ;
369+ const order = {
370+ "no rank" : 0 ,
371+ superkingdom : 1 ,
372+ kingdom : 2 ,
373+ phylum : 3 ,
374+ class : 4 ,
375+ order : 5 ,
376+ family : 6 ,
377+ genus : 7 ,
378+ species : 8 ,
379+ subspecies : 9 ,
380+ strain : 10 ,
381+ } ;
382+ return order [ x ] ?? 0 ;
383+ } ;
384+
365385 for ( const acc of accessions ) {
366386 const tInfo = taxByAcc ?. [ acc ] ;
367- const chain = Array . isArray ( tInfo ?. chain ) ? tInfo . chain : [ ] ;
368- if ( ! chain . length ) continue ;
387+ const chainRaw = Array . isArray ( tInfo ?. chain ) ? tInfo . chain : [ ] ;
388+ if ( ! chainRaw . length ) continue ;
389+
390+ // Aseguramos orden de general -> específico.
391+ // Si viene al revés, invertimos (p.ej. empieza en strain y termina en phylum).
392+ const chainOrdered = [ ...chainRaw ] ;
393+ if ( chainOrdered . length >= 2 ) {
394+ const first = rankScore ( chainOrdered [ 0 ] ?. rank ) ;
395+ const last = rankScore ( chainOrdered [ chainOrdered . length - 1 ] ?. rank ) ;
396+ if ( first > last ) chainOrdered . reverse ( ) ;
397+ }
398+
399+ // Si NCBI devuelve strain/subspecies, recortamos la cadena para enlazar el genoma a species.
400+ let chain = chainOrdered ;
401+ const idxSpecies = chainOrdered . findIndex ( ( n ) => String ( n ?. rank || "" ) . toLowerCase ( ) === "species" ) ;
402+ if ( idxSpecies >= 0 ) chain = chainOrdered . slice ( 0 , idxSpecies + 1 ) ;
369403
404+ // Insert/Update de cada nodo y su parent_id (FK a core_taxonomy.id)
370405 for ( let i = 0 ; i < chain . length ; i ++ ) {
371406 const node = chain [ i ] ;
372407 const taxid = String ( node . taxonomy_id || "" ) . trim ( ) ;
408+ if ( ! taxid ) continue ;
409+
373410 const name = String ( node . name || "" ) . trim ( ) ;
374411 const rank = String ( node . rank || "no rank" ) . trim ( ) ;
375- if ( ! taxid ) continue ;
376412
377413 const parentTaxid = i > 0 ? String ( chain [ i - 1 ] . taxonomy_id || "" ) . trim ( ) : "" ;
378414 const parentIdExpr = parentTaxid
@@ -401,6 +437,7 @@ WHERE taxonomy_id='${esc(taxid)}';
401437 ` . trim ( ) ) ;
402438 }
403439
440+ // Enlazamos el genoma al leaf elegido (species si existe; si no, el último del chain)
404441 const leafTaxid = String ( chain [ chain . length - 1 ] ?. taxonomy_id || "" ) . trim ( ) ;
405442 if ( leafTaxid ) {
406443 sql . push ( `
0 commit comments