This file is indexed.

/usr/share/perl5/Bio/SeqIO/chadoxml.pm is in libbio-perl-perl 1.6.924-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
#
# BioPerl module for Bio::SeqIO::chadoxml
#
# Peili Zhang   <peili@morgan.harvard.edu>
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::SeqIO::chadoxml - chadoxml sequence output stream

=head1 SYNOPSIS

It is probably best not to use this object directly, but
rather go through the SeqIO handler system:

    $writer = Bio::SeqIO->new(-file => ">chado.xml",
                              -format => 'chadoxml');

    # assume you already have Sequence or SeqFeature objects
    $writer->write_seq($seq_obj);

    #after writing all seqs
    $writer->close_chadoxml();



=head1 DESCRIPTION

This object can transform Bio::Seq objects to chadoxml flat
file databases (for chadoxml DTD, see
http://gmod.cvs.sourceforge.net/gmod/schema/chado/dat/chado.dtd).

This is currently a write-only module.

    $seqio = Bio::SeqIO->new(-file => '>outfile.xml',
                             -format => 'chadoxml'
                             -suppress_residues => 1,
                             -allow_residues => 'chromosome',
                             );

    # we have a Bio::Seq object $seq which is a gene located on
    # chromosome arm 'X', to be written out to chadoxml
    # before converting to chadoxml, $seq object B<must> be transformed
    # so that all the coordinates in $seq are against the source
    # feature to be passed into Bio::SeqIO::chadoxml->write_seq()
    # -- chromosome arm X in the example below.

    $seqio->write_seq(-seq=>$seq,
                      -genus   => 'Homo',
                      -species => 'sapiens',
                      -seq_so_type=>'gene',
                      -src_feature=>'X',
                      -src_feat_type=>'chromosome_arm',
                        -nounflatten=>1,
                      -is_analysis=>'true',
                      -data_source=>'GenBank');

The chadoxml output of Bio::SeqIO::chadoxml-E<gt>write_seq() method can be
passed to the loader utility in XORT package
(http://gmod.cvs.sourceforge.net/gmod/schema/XMLTools/XORT/)
to be loaded into chado.

This object is currently implemented to work with sequence and
annotation data from whole genome projects deposited in GenBank. It
may not be able to handle all different types of data from all
different sources.

In converting a Bio::Seq object into chadoxml, a top-level feature is
created to represent the object and all sequence features inside the
Bio::Seq object are treated as subfeatures of the top-level
feature. The Bio::SeqIO::chadoxml object calls
Bio::SeqFeature::Tools::Unflattener to unflatten the flat feature list
contained in the subject Bio::Seq object, to build gene model
containment hierarchy conforming to chado central dogma model: gene
--E<gt> mRNA --E<gt> exons and protein.

Destination of data in the subject Bio::Seq object $seq is as following:

    *$seq->display_id:  name of the top-level feature;

    *$seq->accession_number: if defined, uniquename and
                 feature_dbxref of the top-level
                 feature if not defined,
                 $seq->display_id is used as the
                 uniquename of the top-level feature;

    *$seq->molecule: transformed to SO type, used as the feature
            type of the top-level feature if -seq_so_type
            argument is supplied, use the supplied SO type
            as the feature type of the top-level feature;

    *$seq->species: organism of the top-level feature;

    *$seq->seq: residues of the top-level feature;

    *$seq->is_circular, $seq->division: feature_cvterm;

    *$seq->keywords, $seq->desc, comments: featureprop;

    *references: pub and feature_pub;
        medline/pubmed ids: pub_dbxref;
        comments: pubprop;

    *feature "source" span: featureloc for top-level feature;

    *feature "source" db_xref: feature_dbxref for top-level feature;

    *feature "source" other tags: featureprop for top-level feature;

    *subfeature 'symbol' or 'label' tag: feature uniquename, if
                     none of these is present, the chadoxml object
                     generates feature uniquenames as:
                     <gene>-<feature_type>-<span>
                     (e.g. foo-mRNA--1000..3000);

    *gene model: feature_relationship built based on the
                     containment hierarchy;

    *feature span: featureloc;

    *feature accession numbers: feature_dbxref;

    *feature tags (except db_xref, symbol and gene): featureprop;

Things to watch out for:

    *chado schema change: this version works with the chado
                               version tagged chado_1_01 in GMOD CVS.

    *feature uniquenames: especially important if using XORT
                              loader to do incremental load into
                              chado. may need pre-processing of the
                              source data to put the correct
                              uniquenames in place.

    *pub uniquenames: chadoxml->write_seq() has the FlyBase policy
                          on pub uniquenames hard-coded, it assigns
                          pub uniquenames in the following way: for
                          journals and books, use ISBN number; for
                          published papers, use MEDLINE ID; for
                          everything else, use FlyBase unique
                          identifier FBrf#. need to modify the code to
                          implement your policy. look for the comments
                          in the code.

    *for pubs possibly existing in chado but with no knowledge of
         its uniquename:put "op" as "match", then need to run the
                        output chadoxml through a special filter that
                        talks to chado database and tries to find the
                        pub by matching with the provided information
                        instead of looking up by the unique key. after
                        matching, the filter also resets the "match"
                        operation to either "force" (default), or
                        "lookup", or "insert", or "update". the
                        "match" operation is for a special FlyBase use
                        case. please modify to work according to your
                        rules.

    *chado initialization for loading:

        cv & cvterm: in the output chadoxml, all cv's and
                             cvterm's are lookup only. Therefore,
                             before using XORT loader to load the
                             output into chado, chado must be
                             pre-loaded with all necessary CVs and
                             CVterms, including "SO" , "property
                             type", "relationship type", "pub type",
                             "pubprop type", "pub relationship type",
                             "sequence topology", "GenBank feature
                             qualifier", "GenBank division". A pub by
                             the uniquename 'nullpub' of type 'null
                             pub' needs to be inserted.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution.
Bug reports can be submitted via the web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Peili Zhang

Email peili@morgan.harvard.edu

=head1 APPENDIX

The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _

=cut

# Let the code begin...

package Bio::SeqIO::chadoxml;
use strict;
use English;

use Carp;
use Data::Dumper;
use XML::Writer;
use IO::File;
use IO::Handle;
use Bio::Seq;
use Bio::Seq::RichSeq;
use Bio::SeqIO::FTHelper;
use Bio::Species;
use Bio::Seq::SeqFactory;
use Bio::Factory::SequenceStreamI;
use Bio::SeqFeature::Generic;
use Bio::Annotation::Collection;
use Bio::Annotation::Comment;
use Bio::Annotation::Reference;
use Bio::Annotation::DBLink;
use Bio::SeqFeature::Tools::Unflattener;

#global variables
undef(my %finaldatahash); #data from Bio::Seq object stored in a hash
undef(my %datahash); #data from Bio::Seq object stored in a hash

my $chadotables = 'feature featureprop feature_relationship featureloc feature_cvterm cvterm cv feature_pub pub pub_dbxref pub_author author pub_relationship pubprop feature_dbxref dbxref db synonym feature_synonym';

my %fkey = (
    "cvterm.cv_id"          => "cv",
        "cvterm.dbxref_id"              => "dbxref",
    "dbxref.db_id"          => "db",
    "feature.type_id"       => "cvterm",
    "feature.organism_id"       => "organism",
    "feature.dbxref_id"         => "dbxref",
    "featureprop.type_id"       => "cvterm",
    "feature_pub.pub_id"        => "pub",
    "feature_cvterm.cvterm_id"  => "cvterm",
    "feature_cvterm.pub_id"     => "pub",
        "feature_cvterm.feature_id"     => "feature",
    "feature_dbxref.dbxref_id"  => "dbxref",
    "feature_relationship.object_id"    => "feature",
    "feature_relationship.subject_id"   => "feature",
    "feature_relationship.type_id"  => "cvterm",
    "featureloc.srcfeature_id"  => "feature",
    "pub.type_id"           => "cvterm",
    "pub_dbxref.dbxref_id"      => "dbxref",
    "pub_author.author_id"      => "author",
    "pub_relationship.obj_pub_id"   => "pub",
    "pub_relationship.subj_pub_id"  => "pub",
    "pub_relationship.type_id"  => "cvterm",
    "pubprop.type_id"       => "cvterm",
        "feature_synonym.feature_id"    => "feature",
        "feature_synonym.synonym_id"    => "synonym",
        "feature_synonym.pub_id"        => "pub",
        "synonym.type_id"               => "cvterm",
);

my %cv_name = (
        'relationship'                  => 'relationship',
        'sequence'                      => 'sequence',
        'feature_property'              => 'feature_property',
);

my %feattype_args2so = (
    "aberr"             => "aberration_junction",
#   "conflict"          => "sequence_difference",
#   "polyA_signal"          => "polyA_signal_sequence",
    "variation"         => "sequence_variant",
    "mutation1"         => "point_mutation",        #for single-base mutation
    "mutation2"         => "sequence_variant",      #for multi-base mutation
    "rescue"            => "rescue_fragment",
#   "rfrag"             => "restriction_fragment",
    "protein_bind"          => "protein_binding_site",
    "misc_feature"          => "region",
#   "prim_transcript"       => "primary_transcript",
    "CDS"               => "polypeptide",
    "reg_element"           => "regulatory_region",
    "seq_variant"           => "sequence_variant",
    "mat_peptide"           => "mature_peptide",
    "sig_peptide"           => "signal_peptide",
);

undef(my %organism);

use base qw(Bio::SeqIO);

sub _initialize {

    my($self,%args) = @_;

    $self->SUPER::_initialize(%args);
    unless( defined $self->sequence_factory ) {
        $self->sequence_factory(Bio::Seq::SeqFactory->new
                                (-verbose => $self->verbose(),
                                 -type => 'Bio::Seq::RichSeq'));
    }
    #optional arguments that can be passed in
    $self->suppress_residues($args{'-suppress_residues'})
        if defined $args{'-suppress_residues'};

    $self->allow_residues($args{'-allow_residues'})
        if defined $args{'-allow_residues'};
    return;
}

=head2 write_seq

 Title   : write_seq
 Usage   : $stream->write_seq(-seq=>$seq, -seq_so_type=>$seqSOtype,
                  -src_feature=>$srcfeature,
                  -src_feat_type=>$srcfeattype,
                  -nounflatten=>0 or 1,
                  -is_analysis=>'true' or 'false',
                  -data_source=>$datasource)
 Function: writes the $seq object (must be seq) into chadoxml.
 Returns : 1 for success and 0 for error
 Args     : A Bio::Seq object $seq, optional $seqSOtype, $srcfeature,
            $srcfeattype, $nounflatten, $is_analysis and $data_source.

When $srcfeature (a string, the uniquename of the source feature) is given, the
location and strand information of the top-level feature against the source
feature will be derived from the sequence feature called 'source' of the $seq
object, a featureloc record is generated for the top -level feature on
$srcfeature. when $srcfeature is given, $srcfeattype must also be present. All
feature coordinates in $seq should be against $srcfeature. $seqSOtype is the
optional SO term to use as the type of the top-level feature. For example, a
GenBank data file for a Drosophila melanogaster genome scaffold has the molecule
type of "DNA", when converting to chadoxml, a $seqSOtype argument of
"golden_path_region" can be supplied to save the scaffold as a feature of type
"golden_path_region" in chadoxml, instead of "DNA". a feature with primary tag
of 'source' must be present in the sequence feature list of $seq, to decribe the
whole sequence record.

In the current implementation:

=over 3

=item *

non-mRNA records

A top-level feature of type $seq-E<gt>alphabet is generated for the whole GenBank
record, features listed are unflattened for DNA records to build gene model
feature graph, and for the other types of records all features in $seq are
treated as subfeatures of the top-level feature.

=item *

mRNA records

If a 'gene' feature is present, it B<must> have a /symbol or /label tag to
contain the uniquename of the gene. a top-level feature of type 'gene' is
generated. the mRNA is written as a subfeature of the top-level gene feature,
and the other sequence features listed in $seq are treated as subfeatures of the
mRNA feature.

=back

=cut

sub write_seq {
    my $usage = <<EOUSAGE;
Bio::SeqIO::chadoxml->write_seq()
Usage   : \$stream->write_seq(-seq=>\$seq,
                  -seq_so_type=>\$SOtype,
                  -src_feature=>\$srcfeature,
                  -src_feat_type=>\$srcfeattype,
                  -nounflatten=>0 or 1,
                              -is_analysis=>'true' or 'false',
                              -data_source=>\$datasource)
Args    : \$seq     : a Bio::Seq object
      \$SOtype  : the SO term to use as the feature type of
                      the \$seq record, optional
      \$srcfeature  : unique name of the source feature, a string
              containing at least one alphabetical letter
              (a-z, A-Z), optional
      \$srcfeattype : feature type of \$srcfeature. one of SO terms.
              optional
      when \$srcfeature is given, \$srcfeattype becomes mandatory,
      \$datasource  : source of the sequence annotation data,
              e.g. 'GenBank' or 'GFF'.
EOUSAGE

    my ($self,@args) = @_;

    my ($seq, $seq_so_type, $srcfeature, $srcfeattype, $nounflatten, $isanalysis, $datasource, $genus, $species) =
       $self->_rearrange([qw(SEQ
                 SEQ_SO_TYPE
                 SRC_FEATURE
                 SRC_FEAT_TYPE
                 NOUNFLATTEN
                 IS_ANALYSIS
                 DATA_SOURCE
                                 GENUS
                                 SPECIES
                 )],
                  @args);
    #print "$seq_so_type, $srcfeature, $srcfeattype\n";

    if( !defined $seq ) {
        $self->throw("Attempting to write with no seq!");
    }

    if( ! ref $seq || ! $seq->isa('Bio::Seq::RichSeqI') ) {
       ## FIXME $self->warn(" $seq is not a RichSeqI compliant module. Attempting to dump, but may fail!");
    }

        # try to get the srcfeature from the seqFeature object
        # for this to work, the user has to pass in the srcfeature type
        if (!$srcfeature) {
            if ($seq->can('seq_id')) {
                $srcfeature=$seq->seq_id if ($seq->seq_id ne $seq->display_name);
            }
        }

    #$srcfeature, when provided, should contain at least one alphabetical letter
    if (defined $srcfeature)
    {
        if ($srcfeature =~ /[a-zA-Z]/)
        {
        chomp($srcfeature);
        } else {
        $self->throw( $usage );
        }

        #check for mandatory $srcfeattype
        if (! defined $srcfeattype)
        {
        $self->throw( $usage );
        #$srcfeattype must be a string of non-whitespace characters
        } else {
        if ($srcfeattype =~ /\S+/) {
            chomp($srcfeattype);
        } else {
            $self->throw( $usage );
        }
        }
    }

    # variables local to write_seq()
        my $div = undef;
    my $hkey = undef;
    undef(my @top_featureprops);
        undef(my @featuresyns);
        undef(my @top_featurecvterms);
    my $name = $seq->display_id if $seq->can('display_id');
        $name = $seq->display_name  if $seq->can('display_name');
    undef(my @feature_cvterms);
    undef(my %sthash);
    undef(my %dvhash);
    undef(my %h1);
    undef(my %h2);
    my $temp = undef;
    my $ann = undef;
    undef(my @references);
    undef(my @feature_pubs);
    my $ref = undef;
    my $location = undef;
    my $fbrf = undef;
    my $journal = undef;
    my $issue = undef;
    my $volume = undef;
    my $volumeissue = undef;
    my $pages = undef;
    my $year = undef;
    my $pubtype = undef;
#   my $miniref= undef;
    my $uniquename = undef;
    my $refhash = undef;
    my $feat = undef;
    my $tag = undef;
    my $tag_cv = undef;
    my $ftype = undef;
    my $subfeatcnt = undef;
    undef(my @top_featrels);
    undef (my %srcfhash);

    local($^W) = 0; # supressing warnings about uninitialized fields.

        if (!$name && $seq->can('attributes') ) {
            ($name) = $seq->attributes('Alias');
        }

    if ($seq->can('accession_number') && defined $seq->accession_number && $seq->accession_number ne 'unknown') {
        $uniquename = $seq->accession_number;
    } elsif ($seq->can('accession') && defined $seq->accession && $seq->accession ne 'unknown') {
        $uniquename = $seq->accession;
    } elsif ($seq->can('attributes')) {
                ($uniquename) = $seq->attributes('load_id');
        } else {
        $uniquename = $name;
    }
        my $len = $seq->length();
    if ($len == 0) {
        $len = undef;
    }

    undef(my $gb_type);
    if (!$seq->can('molecule') || ! defined ($gb_type = $seq->molecule()) ) {
        $gb_type = $seq->can('alphabet') ? $seq->alphabet : 'DNA';
    }
    $gb_type = 'DNA' if $ftype eq 'dna';
    $gb_type = 'RNA' if $ftype eq 'rna';

    if(length $seq_so_type > 0) {
        if (defined $seq_so_type) {
            $ftype = $seq_so_type;
        }
            elsif ($seq->type) {
                    $ftype = ($seq->type =~ /(.*):/)
                             ? $1
                             : $seq->type;
            }
        else {
            $ftype = $gb_type;
        }
    }
    else {
        $ftype = $gb_type;
    }

    my %ftype_hash = $self->return_ftype_hash($ftype);

        if ($species) {
            %organism = ("genus"=>$genus, "species" => $species);
        }
        else {
        my $spec = $seq->species();
        if (!defined $spec) {
        $self->throw("$seq does not know what organism it is from, which is required by chado. cannot proceed!\n");
        } else {
        %organism = ("genus"=>$spec->genus(), "species" => $spec->species());
        }
        }

        my $residues;
        if (!$self->suppress_residues ||
            ($self->suppress_residues && $self->allow_residues eq $ftype)) {
            $residues = $seq->seq->isa('Bio::PrimarySeq')
                        ? $seq->seq->seq
                        : $seq->seq;
        }
        else {
            $residues = '';
        }

    #set is_analysis flag for gene model features
    undef(my $isanal);
    if ($ftype eq 'gene' || $ftype eq 'mRNA' || $ftype eq 'exon' || $ftype eq 'protein' || $ftype eq 'polypeptide') {
        $isanal = $isanalysis;
        $isanal = 'false' if !defined $isanal;
    }

    %datahash = (
        "name"      => $name,
        "uniquename"    => $uniquename,
        "seqlen"    => $len,
        "residues"  => $residues,
        "type_id"   => \%ftype_hash,
        "organism_id"   => \%organism,
        "is_analysis"   => $isanal || 'false',
        );

        if (defined $srcfeature) {
                %srcfhash = $self->_srcf_hash($srcfeature,
                                              $srcfeattype,
                                              \%organism);

                my ($phase,$strand);
                if ($seq->can('phase')) {
                    $phase = $seq->phase;
                }

                if ($seq->can('strand')) {
                    $strand = $seq->strand;
                }
                my %fl = (
                                "srcfeature_id" => \%srcfhash,
                                "fmin"          => $seq->start - 1,
                                "fmax"          => $seq->end,
                                "strand"        => $strand,
                                "phase"         => $phase,
                                );

                $datahash{'featureloc'} = \%fl;

        }


    #if $srcfeature is not given, use the Bio::Seq object itself as the srcfeature for featureloc's
    if (!defined $srcfeature) {
        $srcfeature = $uniquename;
        $srcfeattype = $ftype;
    }

    #default data source is 'GenBank'
    if (!defined $datasource) {
        $datasource = 'GenBank';
    }

    if ($datasource =~ /GenBank/i) {
        #sequence topology as feature_cvterm
        if ($seq->can('is_circular') && $seq->is_circular) {
            %sthash = (
                "cvterm_id" => {'name' => 'circular',
                            'cv_id' => {
                            'name' => 'sequence topology',
                            },
                        },
                   "pub_id" => {'uniquename' => 'nullpub',
                            'type_id' => {
                            'name' => 'null pub',
                            'cv_id' => {
                                'name'=> 'pub type',
                            },
                            },
                        },
                );
        } else {
            %sthash = (
                "cvterm_id" => { 'name' => 'linear',
                             'cv_id' => {
                             'name' => 'sequence topology',
                             }
                         },
                "pub_id"    => {'uniquename' => 'nullpub',
                            'type_id' => {
                            'name' => 'null pub',
                            'cv_id' => {
                                'name'=> 'pub type',
                            },
                            },
                        },
                   );
        }
        push(@feature_cvterms, \%sthash);

        #division as feature_cvterm
            if ($seq->can('division') && defined $seq->division()) {
                $div = $seq->division();
            %dvhash = (
                "cvterm_id" => {'name' => $div,
                            'cv_id' => {
                            'name' => 'GenBank division'}},
                "pub_id"    => {'uniquename' => 'nullpub',
                            'type_id' => {
                            'name' => 'null pub',
                            'cv_id' => {
                                'name'=> 'pub type'},
                                }},
                );
            push(@feature_cvterms, \%dvhash);
        }

        $datahash{'feature_cvterm'} = \@feature_cvterms;
    } # closes if GenBank

    #featureprop's
    #DEFINITION
    if ($seq->can('desc') && defined $seq->desc()) {
        $temp = $seq->desc();

        my %prophash = (
            "type_id"   => {'name' => 'description',
                        'cv_id' => {
                        'name' =>
                                                 $cv_name{'feature_property'}
                                                       },
                                           },
            "value"     => $temp,
            );

        push(@top_featureprops, \%prophash);
        }

    #KEYWORDS
    if ($seq->can('keywords')) {
        $temp = $seq->keywords();

        if (defined $temp && $temp ne '.' && $temp ne '') {
        my %prophash = (
                "type_id"   => {'name' => 'keywords',
                            'cv_id' => {
                                                  'name' =>
                                                   $cv_name{'feature_property'}
                                                           }
                        },
                "value"     => $temp,
                            );

        push(@top_featureprops, \%prophash);
        }
        }

    #COMMENT
    if ($seq->can('annotation')) {
        $ann = $seq->annotation();
        foreach my $comment ($ann->get_Annotations('comment')) {
            $temp = $comment->as_text();
            #print "fcomment: $temp\n";
            my %prophash = (
                "type_id"   => {'name' => 'comment',
                            'cv_id' => {
                                                  'name' =>
                                                   $cv_name{'feature_property'}
                                                           }
                                               },
                "value"     => $temp,
                );

            push(@top_featureprops, \%prophash);
        }
    }

        my @top_dbxrefs = ();
        #feature object from Bio::DB::SeqFeature::Store
        if ($seq->can('attributes')) {
                my %attributes = $seq->attributes;
                for my $key (keys %attributes) {
                    next if ($key eq 'parent_id');
                    next if ($key eq 'load_id');

                    if ($key eq 'Alias') {
                        @featuresyns = $self->handle_Alias_tag($seq,@featuresyns);
                    }

                    ###FIXME deal with Dbxref, Ontology_term,source,
                    elsif ($key eq 'Ontology_term') {
                        @top_featurecvterms = $self->handle_Ontology_tag($seq,@top_featurecvterms);
                    }

                    elsif ($key eq 'dbxref' or $key eq 'Dbxref') {
                        @top_dbxrefs = $self->handle_dbxref($seq, $key, @top_dbxrefs);
                    }

                    elsif ($key =~ /^[a-z]/) {
                        @top_featureprops
                             = $self->handle_unreserved_tags($seq,$key,@top_featureprops);
                    }
                }
        }
        $datahash{'feature_synonym'} = \@featuresyns;

        if ($seq->can('source')) {
                @top_dbxrefs = $self->handle_source($seq,@top_dbxrefs);
        }

    #accession and version as feature_dbxref
    if ($seq->can('accession_number') && defined $seq->accession_number && $seq->accession_number ne 'unknown') {
        my $db = $self->_guess_acc_db($seq, $seq->accession_number);
        my %acchash = (
               "db_id"  => {'name' => $db},
               "accession"  => $seq->accession_number,
               "version"    => $seq->seq_version,
               );
        my %fdbx = ('dbxref_id' => \%acchash);
        push(@top_dbxrefs, \%fdbx);
    }

    if( $seq->isa('Bio::Seq::RichSeqI') && defined $seq->get_secondary_accessions() ) {
        my @secacc = $seq->get_secondary_accessions();
        my $acc;
        foreach $acc (@secacc) {
            my %acchash = (
                "db_id"         => {'name' => 'GB'},
                "accession" => $acc,
                );
            my %fdbx = ('dbxref_id' => \%acchash);
            push(@top_dbxrefs, \%fdbx);
        }
    }

    #GI number
    if( $seq->isa('Bio::Seq::RichSeqI') && defined ($seq->pid)) {
        my $id = $seq->pid;
        #print "reftype: ", ref($id), "\n";

        #if (ref($id) eq 'HASH') {
        my %acchash = (
            "db_id"     => {'name' => 'GI'},
            "accession" => $id,
            );
        my %fdbx = ('dbxref_id' => \%acchash);
        push (@top_dbxrefs, \%fdbx);
    }

    #REFERENCES as feature_pub
    if (defined $ann) {
        #get the references
        @references = $ann->get_Annotations('reference');
        foreach $ref (@references) {
        undef(my %pubhash);
        $refhash = $ref->hash_tree();
        $location = $ref->location || $refhash->{'location'};
        #print "location: $location\n";

        #get FBrf#, special for FlyBase SEAN loading
        if (index($location, ' ==') >= 0) {
            $location =~ /\s==/;
                #print "match: $MATCH\n";
                #print "prematch: $PREMATCH\n";
                #print "postmatch: $POSTMATCH\n";
            $fbrf = $PREMATCH;
            $location = $POSTMATCH;
            $location =~ s/^\s//;
        }

        #print "location: $location\n";
        #unpublished reference
        if ($location =~ /Unpublished/) {
            $pubtype = 'unpublished';
            %pubhash = (
                "title"     => $ref->title || $refhash->{'title'},
                #"miniref"  => substr($location, 0, 255),
                #"uniquename"   => $fbrf,
                "type_id"   => {'name' => $pubtype, 'cv_id' => {'name' =>'pub type'}}
                );
        }
        #submitted
        elsif ($location =~ /Submitted/) {
            $pubtype = 'submitted';

            %pubhash = (
                "title"     => $ref->title || $refhash->{'title'},
                #"miniref"  => substr($location, 0, 255),
                #"uniquename"   => $fbrf,
                "type_id"   => {'name' => $pubtype, 'cv_id' => {'name' =>'pub type'}}
                );

            undef(my $pyear);
            $pyear = $self->_getSubmitYear($location);
            if (defined $pyear) {
            $pubhash{'pyear'} = $pyear;
            }
        }

        #published journal paper
        elsif ($location =~ /\D+\s\d+\s\((\d+|\d+-\d+)\),\s(\d+-\d+|\d+--\d+)\s\(\d\d\d\d\)$/) {
            $pubtype = 'paper';

                #parse location to get journal, volume, issue, pages & year
            $location =~ /\(\d\d\d\d\)$/;

            $year = $MATCH;
            my $stuff = $PREMATCH;
            $year =~ s/\(//; #remove the leading parenthesis
            $year =~ s/\)//; #remove the trailing parenthesis

            $stuff =~ /,\s(\d+-\d+|\d+--\d+)\s$/;

            $pages = $MATCH;
            $stuff = $PREMATCH;
            $pages =~ s/^, //; #remove the leading comma and space
            $pages =~ s/ $//; #remove the last space

            $stuff =~ /\s\d+\s\((\d+|\d+-\d+)\)$/;

            $volumeissue = $MATCH;
            $journal = $PREMATCH;
            $volumeissue =~ s/^ //; #remove the leading space
            $volumeissue =~ /\((\d+|\d+-\d+)\)$/;
            $issue = $MATCH;
            $volume = $PREMATCH;
            $issue =~ s/^\(//; #remove the leading parentheses
            $issue =~ s/\)$//; #remove the last parentheses
            $volume =~ s/^\s//; #remove the leading space
            $volume =~ s/\s$//; #remove the last space

            %pubhash = (
                "title"     => $ref->title || $refhash->{'title'},
                "volume"    => $volume,
                "issue"     => $issue,
                "pyear"     => $year,
                "pages"     => $pages,
                #"miniref"  => substr($location, 0, 255),
                #"miniref"  => ' ',
                #"uniquename"   => $fbrf,
                "type_id"   => {'name' => $pubtype, 'cv_id' => {'name' =>'pub type'}},
                "pub_relationship" => {
                    'obj_pub_id' => {
                    'uniquename' => $journal,
                    'title' => $journal,
                    #'miniref' => substr($journal, 0, 255),
                    'type_id' =>{'name' => 'journal',
                             'cv_id' =>
                             {'name' => 'pub type'
                              },
                         },
                             #'pubprop' =>{'value'=> $journal,
                             #       'type_id'=>{'name' => 'abbreviation', 'cv_id' => {'name' => 'pubprop type'}},
                             #      },
                         },
                       'type_id' => {
                           'name' => 'published_in',
                           'cv_id' => {
                           'name' => 'pub relationship type'},
                       },
                },
                );
        }

        #other references
        else {
            $pubtype = 'other';
            %pubhash = (
                "title"     => $ref->title || $refhash->{'title'},
                #"miniref"  => $fbrf,
                "type_id"   => {
                    'name' => $pubtype,
                    'cv_id' => {'name' =>'pub type'}
                }
                );
        }

        #pub_author
        my $autref = $self->_getRefAuthors($ref);
        if (defined $autref) {
            $pubhash{'pub_author'} = $autref;
        }
        # if no author and is type 'submitted' and has submitter address, use the first 100 characters of submitter address as the author lastname.
        else {
            if ($pubtype eq 'submitted') {
            my $autref = $self->_getSubmitAddr($ref);
            if (defined $autref) {
                $pubhash{'pub_author'} = $autref;
            }
            }
        }

        #$ref->comment as pubprop
        #print "ref comment: ", $ref->comment, "\n";
        #print "ref comment: ", $refhash->{'comment'}, "\n";
        if (defined $ref->comment || defined $refhash->{'comment'}) {
            my $comnt = $ref->comment || $refhash->{'comment'};
                #print "remark: ", $comnt, "\n";
            $pubhash{'pubprop'} = {
            "type_id"       => {'name' => 'comment', 'cv_id' => {'name' => 'pubprop type'}},
            "value"     => $comnt,
            };
        }

        #pub_dbxref
        undef(my @pub_dbxrefs);
        if (defined $fbrf) {
            push(@pub_dbxrefs, {dbxref_id => {accession => $fbrf, db_id => {'name' => 'FlyBase'}}});
        }
        if (defined ($temp = $ref->medline)) {
            push(@pub_dbxrefs, {dbxref_id => {accession => $temp, db_id => {'name' => 'MEDLINE'}}});
                #use medline # as the pub's uniquename
            $pubhash{'uniquename'} = $temp;
        }
        if (defined ($temp = $ref->pubmed)) {
            push(@pub_dbxrefs, {dbxref_id => {accession => $temp, db_id => {'name' => 'PUBMED'}}});
        }
        $pubhash{'pub_dbxref'} = \@pub_dbxrefs;

        #if the pub uniquename is not defined or blank, put its FBrf# as its uniquename
        #this is unique to FlyBase
        #USERS OF THIS MODULE: PLEASE MODIFY HERE TO IMPLEMENT YOUR POLICY
        # ON PUB UNIQUENAME!!!
        if (!defined $pubhash{'uniquename'} || $pubhash{'uniquename'} eq '') {
            if (defined $fbrf) {
            $pubhash{'uniquename'} = $fbrf;
            }
                #else {
                #   $pubhash{'uniquename'} = $self->_CreatePubUname($ref);
                #}
        }

        #add to collection of references
        #if the pub covers the entire sequence of the top-level feature, add it to feature_pubs
        if (($ref->start == 1 && $ref->end == $len) || (!defined $ref->start && !defined $ref->end)) {
            push(@feature_pubs, {"pub_id" => \%pubhash});
        }
        #the pub is about a sub-sequence of the top-level feature
        #create a feature for the sub-sequence and add pub as its feature_pub
        #featureloc of this sub-sequence is against the top-level feature, in interbase coordinates.
        else {
            my %parf = (
                'uniquename'    => $uniquename . ':' . $ref->start . "\.\." . $ref->end,
                'organism_id'   =>\%organism,
                'type_id'   =>{'name' =>'region', 'cv_id' => {'name' => $cv_name{'sequence'} }},
                );
            my %parfsrcf = (
                    'uniquename'    => $uniquename,
                    'organism_id'   =>\%organism,
                    );
            my %parfloc = (
                   'srcfeature_id'  => \%parfsrcf,
                   'fmin'       => $ref->start - 1,
                   'fmax'       => $ref->end,
                   );
            $parf{'featureloc'} = \%parfloc;
            $parf{'feature_pub'} = {'pub_id' => \%pubhash};
            my %ffr = (
                   'subject_id' => \%parf,
                   'type_id'        => { 'name' => 'partof', 'cv_id' => { 'name' => $cv_name{'relationship'}}},
                   );
            push(@top_featrels, \%ffr);
        }
        }
        $datahash{'feature_pub'} = \@feature_pubs;
    }

    ##construct srcfeature hash for use in featureloc
    if (defined $srcfeature) {
                %srcfhash = $self->_srcf_hash($srcfeature,
                                              $srcfeattype,
                                              \%organism);
    #   my %fr = (
    #       "object_id" => \%srcfhash,
    #       "type_id"   => { 'name' => 'partof', 'cv_id' => { 'name' => 'relationship type'}},
    #       );

    #   push (@top_featrels, \%fr);
    }

    #unflatten the seq features in $seq if $seq is a gene or a DNA sequence
    if (($gb_type eq 'gene' || $gb_type eq 'DNA') &&
        !$nounflatten) {
        my $u = Bio::SeqFeature::Tools::Unflattener->new;
        $u->unflatten_seq(-seq=>$seq, -use_magic=>1);
    }

    my @top_sfs = $seq->get_SeqFeatures;
    #print $#top_sfs, "\n";

    #SUBFEATURES

    if ($datasource =~ /GenBank/i) {
        $tag_cv = 'GenBank feature qualifier';
    } elsif ($datasource =~ /GFF/i) {
        $tag_cv = 'feature_property';
    } else {
        $tag_cv = $cv_name{'feature_property'};
    }

    my $si = 0;
    foreach $feat (@top_sfs) {
        #$feat = $top_sfs[$si];
        #print "si: $si\n";
        my $prim_tag = $feat->primary_tag;
        #print $prim_tag, "\n";

        # get all qualifiers of the 'source' feature, load these as top_featureprops of the top level feature
        if ($prim_tag eq 'source') {
            foreach $tag ($feat->all_tags()) {
                #db_xref
                if ($tag eq 'db_xref'
                                 or $tag eq 'Dbxref'
                                 or $tag eq 'dbxref')   {
                    my @t1 = $feat->each_tag_value($tag);
                    foreach $temp (@t1) {
                       $temp =~ /([^:]*?):(.*)/;
                                           my $db = $1;
                                           my $xref = $2;
                                           #PRE/POST very inefficent
                       #my $db = $PREMATCH;
                       #my $xref = $POSTMATCH;
                       my %acchash = (
                        "db_id"     => {'name' => $db},
                        "accession" => $xref,
                        );
                       my %fdbx = ('dbxref_id' => \%acchash);
                       push (@top_dbxrefs, \%fdbx);
                    }
                                #Ontology_term
                                } elsif ($tag eq 'Ontology_term') {
                                        my @t1 = $feat->each_tag_value($tag);
                                        foreach $temp (@t1) {
                                            ###FIXME
                                        }
                #other tags as featureprop
                } elsif ($tag ne 'gene') {
                    my %prophash = undef;
                    %prophash = (
                                    "type_id"       => {'name' => $tag, 'cv_id' => {'name' => $tag_cv}},
                        "value"     => join(' ',$feat->each_tag_value($tag)),
                        );
                    push(@top_featureprops, \%prophash);
                }
            }

                        if ($feat->can('source')) {
                            my $source = $feat->source();
                            @top_dbxrefs = $self->handle_source($feat, @top_dbxrefs);
                        }

            #featureloc for the top-level feature
            my $fmin = undef;
            my $fmax = undef;
            my $strand = undef;
                        my $phase = undef;
            my %fl = undef;

            $fmin = $feat->start - 1;
            $fmax = $feat->end;
            $strand = $feat->strand;

                        if ($feat->can('phase')) {
                            $phase = $feat->phase;
                        }

            %fl = (
                "srcfeature_id" => \%srcfhash,
                "fmin"      => $fmin,
                "fmax"      => $fmax,
                "strand"    => $strand,
                                "phase"         => $phase,
                );

            $datahash{'featureloc'} = \%fl;

            #delete 'source' feature from @top_sfs
            splice(@top_sfs, $si, 1);
        }
        $si ++;
    #close loop over top_sfs
    }

    #the top-level features other than 'source'
    foreach $feat (@top_sfs) {
        #print $feat->primary_tag, "\n";

        my $r = $self->_subfeat2featrelhash($name, $ftype, $feat, \%srcfhash, $tag_cv, $isanalysis);

        if (!($ftype eq 'mRNA' && $feat->primary_tag eq 'gene')) {
            my %fr = %$r;
            push(@top_featrels, \%fr);
        } else {
            %finaldatahash = %$r;
        }
    }

    if (@top_dbxrefs) {
        $datahash{'feature_dbxref'} = \@top_dbxrefs;
    }

    if (@top_featureprops) {
        $datahash{'featureprop'} = \@top_featureprops;
    }

    if (@top_featrels) {
        $datahash{'feature_relationship'} = \@top_featrels;
    }

        if (@top_featurecvterms) {
                $datahash{'feature_cvterm'} = \@top_featurecvterms;
        }

    if ($ftype eq 'mRNA' && %finaldatahash) {
        $finaldatahash{'feature_relationship'} = {
                        'subject_id'    => \%datahash,
                        'type_id'   => { 'name' => 'partof', 'cv_id' => { 'name' => $cv_name{'relationship'} }},
                             };
    } else {
        %finaldatahash = %datahash;
    }

    my $mainTag = 'feature';
    $self->_hash2xml(undef, $mainTag, \%finaldatahash);

    return 1;
}

sub _hash2xml {
    my $self = shift;
    my $isMatch = undef;
    $isMatch = shift;
    my $ult = shift;
    my $ref = shift;
    my %mh = %$ref;
    my $key;
    my $v;
    my $sh;
    my $xx;
    my $yy;
    my $nt;
    my $ntref;
    my $output;
    my $root = shift if (@_);
    #print "ult: $ult\n";
    if (!defined $self->{'writer'}) {
    $root = 1;
        $self->_create_writer();
    }
    my $temp;
    my %subh = undef;

    #start opeing tag
    #if pub record of type 'journal', form the 'ref' attribute for special pub lookup
    #requires that the journal name itself is also stored as a pubprop record for the journal with value equal
    #to the journal name and type of 'abbreviation'.
    if ($ult eq 'pub' && $mh{'type_id'}->{'name'} eq 'journal') {
    $self->{'writer'}->startTag($ult, 'ref' => $mh{'title'} . ':journal:abbreviation');
    }

    #special pub match if pub uniquename not known
    elsif ($ult eq 'pub' && !defined $mh{'uniquename'}) {
    $self->{'writer'}->startTag($ult, 'op' => 'match');
    #set the match flag, all the sub tags should also have "op"="match"
    $isMatch = 1;
    }

    #if cvterm or cv, lookup only
    elsif (($ult eq 'cvterm') || ($ult eq 'cv')) {
    $self->{'writer'}->startTag($ult, 'op' => 'lookup');
    }

    #if nested tables of match table, match too
    elsif ($isMatch) {
    $self->{'writer'}->startTag($ult, 'op' => 'match');
    }

    else {
    $self->{'writer'}->startTag($ult);
    }

    #first loop to produce xml for all the table columns
    foreach $key (keys %mh)
    {
    #print "key: $key\n";
    $xx = ' ' . $key;
    $yy = $key . ' ';
    if (index($chadotables, $xx) < 0 && index($chadotables, $yy) < 0)
    {
        if ($isMatch) {
        $self->{'writer'}->startTag($key, 'op' => 'match');
        } else {
        $self->{'writer'}->startTag($key);
        }

        my $x = $ult . '.' . $key;
        #the column is a foreign key
        if (defined $fkey{$x})
        {
        $nt = $fkey{$x};
        $sh = $mh{$key};
        $self->_hash2xml($isMatch, $nt, $sh, 0);
        } else
        {
        #print "$key: $mh{$key}\n";
        $self->{'writer'}->characters($mh{$key});
        }
        $self->{'writer'}->endTag($key);
    }
    }

    #second loop to produce xml for all the nested tables
    foreach $key (keys %mh)
    {
    #print "key: $key\n";
    $xx = ' ' . $key;
    $yy = $key . ' ';
    #a nested table
    if (index($chadotables, $xx) > 0 || index($chadotables, $yy) > 0)
    {
        #$writer->startTag($key);
        $ntref = $mh{$key};
        #print "$key: ", ref($ntref), "\n";
        if (ref($ntref) =~ 'HASH') {
        $self->_hash2xml($isMatch, $key, $ntref, 0);
        } elsif (ref($ntref) =~ 'ARRAY') {
        #print "array dim: ", $#$ntref, "\n";
        foreach $ref (@$ntref) {
                #print "\n";
            $self->_hash2xml($isMatch, $key, $ref, 0);
        }
        }
        #$writer->endTag($key);
    }
    }

    #end tag
    $self->{'writer'}->endTag($ult);

    #if ($root == 1) {
#   $self->{'writer'}->endTag('chado');
#    }
}

sub _guess_acc_db {
    my $self = shift;
    my $seq = shift;
    my $acc = shift;
    #print "acc: $acc\n";

    if ($acc =~ /^NM_\d{6}/ || $acc =~ /^NP_\d{6}/ || $acc =~ /^NT_\d{6}/ || $acc =~ /^NC_\d{6}/) {
        return "RefSeq";
    } elsif ($acc =~ /^XM_\d{6}/ || $acc =~ /^XP_\d{6}/ || $acc =~ /^XR_\d{6}/) {
        return "RefSeq";
    } elsif ($acc =~ /^[a-zA-Z]{1,2}\d{5,6}/) {
        return "GB";
    } elsif ($seq->molecule() eq 'protein' && $acc =~ /^[a-zA-z]\d{5}/) {
        return "PIR";
    } elsif ($seq->molecule() eq 'protein' && $acc =~ /^\d{6,7}[a-zA-Z]/) {
        return "PRF";
    } elsif ($acc =~ /\d+/ && $acc !~ /[a-zA-Z]/) {
        return "LocusID";
    } elsif ($acc =~ /^CG\d+/ || $acc =~ /^FB[a-z][a-z]\d+/) {
        return "FlyBase";
    } else {
        return "unknown";
    }
}

sub _subfeat2featrelhash {
    my $self = shift;
    my $genename = shift;
    my $seqtype = shift;
    my $feat = shift;
    my $r = shift;
    my %srcf = %$r;     #srcfeature hash for featureloc.srcfeature_id
    my $tag_cv = shift;
    my $isanalysis = shift;

    my $prim_tag = $feat->primary_tag;

    my $sfunique = undef;       #subfeature uniquename
    my $sfname = undef;     #subfeature name
    my $sftype = undef;     #subfeature type

    if ($feat->has_tag('symbol')) {
        ($sfunique) = $feat->each_tag_value("symbol");
    } elsif ($feat->has_tag('label')) {
        ($sfunique) = $feat->each_tag_value("label");
    } else {
        #$self->throw("$prim_tag at " . $feat->start . "\.\." . $feat->end . " does not have symbol or label! To convert into chadoxml, a seq feature must have a /symbol or /label tag holding its unique name.");
        #generate feature unique name as <genename>-<feature-type>-<span>
        $sfunique = $self->_genFeatUniqueName($genename, $feat);
    }

    if ($feat->has_tag('Name')) {
        ($sfname) = $feat->each_tag_value("Name");
    }

    #feature type translation
    if (defined $feattype_args2so{$prim_tag}) {
        $sftype = $feattype_args2so{$prim_tag};
    } else {
        $sftype = $prim_tag;
    }

    if ($prim_tag eq 'mutation') {
        if ($feat->start == $feat->end) {
            $sftype = $feattype_args2so{'mutation1'};
        } else {
            $sftype = $feattype_args2so{'mutation2'};
        }
    }

    #set is_analysis flag for gene model features
    undef(my $isanal);
    if ($sftype eq 'gene' || $sftype eq 'mRNA' || $sftype eq 'exon' || $sftype eq 'protein' || $sftype eq 'polypeptide') {
        $isanal = $isanalysis;
    }

    my %sfhash = (
        "name"          => $sfname,
        "uniquename"        => $sfunique,
        "organism_id"       => \%organism,
        "type_id"       => { 'name' => $sftype, 'cv_id' => { 'name' => $cv_name{'sequence'} }},
        "is_analysis"           => $isanal || 'false',
        );

    #make a copy of %sfhash for passing to this method when recursively called
    #my %srcfeat = (
        #        "name"                  => $sfname,
        #        "uniquename"            => $sfunique,
        #        "organism_id"           => \%organism,
        #        "type_id"               => { 'name' => $sftype, 'cv_id' => { 'name' => 'SO'}},
        #        );

    #featureloc for subfeatures
    undef(my $sfmin);
    undef(my $sfmax);
    undef(my $is_sfmin_partial);
    undef(my $is_sfmax_partial);
    undef(my $sfstrand);
        undef(my $sfphase);
    $sfmin = $feat->start - 1;
    $sfmax = $feat->end;
    $sfstrand = $feat->strand();

        if ($feat->can('phase')) {
            $sfphase = $feat->phase;
        }

    #if the gene feature in an mRNA record, cannot use its coordinates, omit featureloc
    if ($seqtype eq 'mRNA' && $sftype eq 'gene') {
    } else {
        if ($feat->location->isa('Bio::Location::FuzzyLocationI')) {
            if ($feat->location->start_pos_type() ne 'EXACT') {
                $is_sfmin_partial = 'true';
            }
            if ($feat->location->end_pos_type() ne 'EXACT') {
                $is_sfmax_partial = 'true';
            }
        }

        my %sfl = (
            "srcfeature_id" => \%srcf,
            "fmin"      => $sfmin,
            "is_fmin_partial" => $is_sfmin_partial || 'false',
            "fmax"      => $sfmax,
            "is_fmax_partial" => $is_sfmax_partial || 'false',
            "strand"    => $sfstrand,
                        "phase"         => $sfphase,
            );

        $sfhash{'featureloc'} = \%sfl;
    }


    #subfeature tags
    undef(my @sfdbxrefs);       #subfeature dbxrefs
    undef(my @sub_featureprops);    #subfeature props
        undef(my @sub_featuresyns);     #subfeature synonyms
        undef(my @sub_featurecvterms);  #subfeature cvterms
    foreach my $tag ($feat->all_tags()) {
        #feature_dbxref for features
        if ($tag eq 'db_xref' or $tag eq 'dbxref' or $tag eq 'Dbxref')   {
            my @t1 = $feat->each_tag_value($tag);
            #print "# of dbxref: @t1\n";
            for my $temp (@t1) {
               $temp =~ /:/;
               my $db = $PREMATCH;
               my $xref = $POSTMATCH;
               #print "db: $db; xref: $xref\n";
               my %acchash = (
                "db_id"     => {'name' => $db},
                "accession" => $xref,
                );
               my %sfdbx = ('dbxref_id' => \%acchash);
               push (@sfdbxrefs, \%sfdbx);
            }
                #Alias tags
                } elsif ($tag eq 'Alias') {
                        @sub_featuresyns = $self->handle_Alias_tag($feat, @sub_featuresyns);
                } elsif ($tag eq 'Ontology_term') {
                        @sub_featurecvterms = $self->handle_Ontology_tag($feat, @sub_featurecvterms);
        #featureprop for features, excluding GFF Name & Parent tags
        } elsif ($tag ne 'gene' && $tag ne 'symbol' && $tag ne 'Name' && $tag ne 'Parent') {
                        next if ($tag eq 'parent_id');
                        next if ($tag eq 'load_id');
            foreach my $val ($feat->each_tag_value($tag)) {
                my %prophash = undef;
                %prophash = (
                                "type_id"       => {'name' => $tag, 'cv_id' => {'name' => $tag_cv}},
                    "value"     => $val,
                );
                push(@sub_featureprops, \%prophash);
            }
        }
    }

        if ($feat->can('source')) {
                @sfdbxrefs = $self->handle_source($feat,@sfdbxrefs);
        }

    if (@sub_featureprops) {
        $sfhash{'featureprop'} = \@sub_featureprops;
    }
    if (@sfdbxrefs) {
        $sfhash{'feature_dbxref'} = \@sfdbxrefs;
    }
        if (@sub_featuresyns) {
                $sfhash{'feature_synonym'} = \@sub_featuresyns;
        }
        if (@sub_featurecvterms) {
                $sfhash{'feature_cvterm'} = \@sub_featurecvterms;
        }

    undef(my @ssfeatrel);
    if ($feat->has_tag('locus_tag')) {
        ($genename)= $feat->each_tag_value('locus_tag');
    } elsif ($feat->has_tag('gene')) {
        ($genename)= $feat->each_tag_value('gene');
    }

    foreach my $sf ($feat->get_SeqFeatures()) {
        #print $sf->primary_tag, "\n";
        my $rref = $self->_subfeat2featrelhash($genename, $sftype, $sf, \%srcf, $tag_cv, $isanalysis);
        if (defined $rref) {
            push(@ssfeatrel, $rref);
        }
    }

    if (@ssfeatrel) {
        $sfhash{'feature_relationship'} = \@ssfeatrel;
    }

    #subj-obj relationship type
    undef(my $reltypename);
        $reltypename = return_reltypename($sftype);

    my %fr = (
        "subject_id"    => \%sfhash,
        "type_id"       => { 'name' => $reltypename,
                                             'cv_id' => { 'name' => $cv_name{'relationship'} }},
        );

    if ($seqtype eq 'mRNA' && $sftype eq 'gene') {
        return \%sfhash;
    } else {
        return \%fr;
    }

}

#generate uniquename for feature as: <genename>-<feature-type>-<span> (foo-mRNA-10..1000)
sub _genFeatUniqueName {
    my $self = shift;
    my $genename = shift;
    my $feat = shift;
    undef(my $uniquename);
    my $ftype = $feat->primary_tag;
    my $start = $feat->start;
    my $end = $feat->end;

    if ($feat->has_tag('locus_tag')) {
        ($genename) = $feat->each_tag_value("locus_tag");
    } elsif ($feat->has_tag('gene')) {
        ($genename) = $feat->each_tag_value("gene");
    }

    $uniquename = $genename . '-' . $ftype . '-' . $start . "\.\." . $end;

    return $uniquename;
}

#create uniquename for pubs with no medline id and no FBrf#
#use "<authors>, <year>, <type>" as the uniquename (same as miniref)
#<authors> is <sole-author-surname>    if one author,
#  or <first-author-surname> and <second-author-surname>   if two,
#  or <first-author-surname> et al.   if more
#sub _CreatePubUname {
#   my $self = shift;
#   my $pub = shift;
#   undef(my $pubuname);
#
#   return $pubuname;
#}

#get authors of a reference
#returns ref to the array of author hashes
sub _getRefAuthors {
    my $self = shift;
    my $ref = shift;

    my $temp = $ref->authors;
    undef(my @authors);
    undef(my @aut);

    #there are authors
    if ($temp ne '.') {
        if (index($temp, ' and ') > 0) {
            $temp =~ / and /;
            my $lastauthor = $POSTMATCH;
            @authors = split(/\, /, $PREMATCH);
            push (@authors, $lastauthor);
        } else {
            @authors = split(/\, /, $temp);
        }

        my $a;
        my $i = 0;
        foreach $a (@authors) {
            $i ++;
            #parse the author lastname and givennames
            undef(my $last);
            undef(my $given);
            if (index($a, ',') > 0) {   #genbank format, last,f.m.
                ($last, $given) = split(/\,/, $a);
            } elsif (index($a, ' ') > 0) {  #embl format, last f.m.
                ($last, $given) = split(/ /, $a);
            }
            my %au = (
                'surname'   => $last,
                'givennames'    => $given,
                );
            push(@aut, {author_id => \%au, arank => $i});
        }

        return \@aut;
    }

    #no authors, Bio::SeqIO::genbank doesn't pick up 'CONSRTM' line.
    else {
        return;
    }

}

#extract submission year from the citation of the submitted reference
#genbank format for the submitted citation: JOURNAL   Submitted (DD-MON-YYYY) submitter address
sub _getSubmitYear {
    my $self = shift;
    my $citation = shift;

    if ($citation !~ /Submitted/) {
    $self->warn("not citation for a submitted reference. cannot extract submission year.");
    return;
    } else {
    $citation =~ /Submitted \(\d\d-[a-zA-Z]{3}-\d{4}\)/;
    my $a = $MATCH;
    $a =~ /\d{4}/;
    my $year = $MATCH;

    return $year;
    }
}

sub _getSubmitAddr {
    my $self = shift;
    my $ref = shift;
    undef(my %author);

    my $citation = $ref->location;
    if ($citation !~ /Submitted/) {
    $self->warn("not citation for a submitted reference. cannot extract submission year.");
    return;
    } else {
    $citation =~ /Submitted \(\d\d-[a-zA-Z]{3}-\d{4}\)/;
    my $a = $POSTMATCH;
    if (defined $a) {
        $a =~ s/^\s//;
        %author = (
               'author_id'  => {'surname'   => substr($a, 0, 100)},
               );
        return \%author;
    } else {
        return;
    }
    }
}

=head2 suppress_residues

 Title    : suppress_residues
 Usage    : $obj->suppress_residues()        #get existing value
            $obj->suppress_residues($newval) #set new value
 Function : Keep track of the flag to suppress printing of residues in the
            chadoxml file. The default it to allow all residues to go into the
            file.
 Returns  : value of suppress_residues (a scalar)
 Args     : new value of suppress_residues (to set)

=cut

sub suppress_residues {
    my $self = shift;
    my $suppress_residues = shift if @_;
    return $self->{'suppress_residues'} = $suppress_residues if defined($suppress_residues);
    return $self->{'suppress_residues'};
}

=head2 allow_residues

 Title    : allow_residues
 Usage    : $obj->allow_residues()        #get existing value
            $obj->allow_residues($feature_type) #set new value
 Function : Track the allow_residues type.  This can be used in conjunction
            with the suppress_residues flag to only allow residues from a
            specific feature type to be printed in the xml file, for example,
            only printing chromosome residues. When suppress_residues is set to
            true, then only chromosome features would would go into the xml
            file. If suppress_residues is not set, this function has no effect
            (since the default is to put all residues in the xml file).
 Returns  : value of allow_residues (string that corresponds to a feature type)
 Args     : new value of allow_residues (to set)
 Status   :

=cut

sub allow_residues {
    my $self = shift;
    my $allow_residues = shift if @_;
    return $self->{'allow_residues'} = $allow_residues if defined($allow_residues);
    return $self->{'allow_residues'};
}

=head2 return_ftype_hash

 Title    : return_ftype_hash
 Usage    : $obj->return_ftype_hash()
 Function : A simple hash where returning it has be factored out of the main
            code to allow subclasses to override it.
 Returns  : A hash that indicates what the name of the SO term is and what
            the name of the Sequence Ontology is in the cv table.
 Args     : The string that represents the SO term.
 Status   :

=cut

sub return_ftype_hash {
    my $self  = shift;
    my $ftype = shift;
    my %ftype_hash = ( "name" => $ftype,
                       "cv_id" => {"name" => $cv_name{'sequence'} });
    return %ftype_hash;
}

=head2 return_reltypename

 Title    : return_reltypename
 Usage    : $obj->return_reltypename
 Function : Return the appropriate relationship type name depending on the
            feature type (typically part_of, but derives_from for polypeptide).
 Returns  : A relationship type name.
 Args     : A SO type name.
 Status   :

=cut

sub return_reltypename {
    my $self   = shift;
    my $sftype = shift;

    my $reltypename;
    if ($sftype eq 'protein' || $sftype eq 'polypeptide') {
        $reltypename = 'derives_from';
    } else {
        $reltypename = 'part_of';
    }

    return $reltypename;
}

=head2 next_seq

 Title    : next_seq
 Usage    : $obj->next_seq
 Function :
 Returns  :
 Args     :
 Status   : Not implemented (write only adaptor)

=cut

sub next_seq {
    my ($self, %argv) = @_;

    $self->throw('next_seq is not implemented; this is a write-only adapter.');

}

=head2 _create_writer

 Title    : _create_writer
 Usage    : $obj->_create_writer
 Function : Creates XML::Writer object and writes start tag
 Returns  : Nothing, though the writer persists as part of the chadoxml object
 Args     : None
 Status   :

=cut

sub _create_writer {
    my $self = shift;

    $self->{'writer'} = XML::Writer->new(OUTPUT => $self->_fh,
                                         DATA_MODE => 1,
                                         DATA_INDENT => 3);

    #print header
    $self->{'writer'}->xmlDecl("UTF-8");
    $self->{'writer'}->comment("created by Peili Zhang, Flybase, Harvard University\n".
                               "and Scott Cain, GMOD, Cold Spring Harbor Laboratory");

    #start chadoxml
    $self->{'writer'}->startTag('chado');

    return;
}

=head2 close_chadoxml

 Title    : close_chadoxml
 Usage    : $obj->close_chadoxml
 Function : Writes the closing xml tag
 Returns  : None
 Args     : None
 Status   :

=cut

sub close_chadoxml {
    my $self = shift;

    $self->{'writer'}->endTag('chado');
    return;
}

=head2 handle_unreserved_tags

 Title    : handle_unreserved_tags
 Usage    : $obj->handle_unreserved_tags
 Function : Converts tag value pairs to xml-ready hashrefs
 Returns  : The array containing the hashrefs
 Args     : In order: the Seq or SeqFeature object, the key, and the hasharray
 Status   :

=cut

sub handle_unreserved_tags {
    my $self = shift;
    my $seq  = shift;
    my $key  = shift;
    my @arr  = @_;

    my @values = $seq->attributes($key);
    for my $value (@values) {
        my %prophash = (
           "type_id"     => {'name' => $key,
                             'cv_id' => { 'name' => $cv_name{'feature_property'} }
                            },
                            "value"       => $value,
                       );
        push(@arr, \%prophash);
    }

    return @arr;
}

=head2 handle_Alias_tag

 Title    : handle_Alias_tag
 Usage    : $obj->handle_Alias_tag
 Function : Convert Alias values to synonym hash refs
 Returns  : An array of synonym hash tags
 Args     : The seq or seqFeature object and the synonym hash array
 Status   :

=cut

sub handle_Alias_tag {
    my $self = shift;
    my $seq  = shift;
    my @arr  = @_;

    my @Aliases = $seq->attributes('Alias');
    for my $Alias (@Aliases) {
        my %synhash = (
                  "type_id"   => { 'name' => 'exact',
                                  'cv_id' => { 'name'  => 'synonym_type' } },
                                 "name"         => $Alias,
                                 "synonym_sgml" => $Alias,
                      );
        push(@arr, {'synonym_id' => \%synhash,
                    'pub_id'     => {'uniquename' => 'null',
                                     'type_id'    => { 'name' => 'null',
                                                       'cv_id' => {
                                                            'name' => 'null',
                                                                  },
                                                     },
                                    },
                   });
    }

    return @arr;
}

=head2 handle_Ontology_tag

 Title    : handle_Ontology_tag
 Usage    : $obj->handle_Ontology_tag
 Function : Convert Ontology_term values to ontology term hash refs
 Returns  : An array of ontology term hash refs
 Args     : The seq or seqFeature object and the ontology term array
 Status   :

=cut

sub handle_Ontology_tag  {
    my $self = shift;
    my $seq  = shift;
    my @arr  = @_;

    my @terms = $seq->attributes('Ontology_term');
    for my $term (@terms) {
        my $hashref;
        if ($term =~ /(\S+):(\S+)/) {
            my $db  = $1;
            my $acc = $2;
            $hashref = {
                    'cvterm_id' => {
                        'dbxref_id' => {
                           'db_id' => { 'name' => $db },
                           'accession' => $acc
                                      },
                                   },
                       };
        }
        push(@arr, {cvterm_id => $hashref});
    }

    return @arr;
}

=head2 handle_dbxref

 Title    : handle_dbxref
 Usage    : $obj->handle_dbxref
 Function : Convert Dbxref values to dbxref hashref
 Returns  : An array of dbxref hashrefs
 Args     : A seq or seqFeature object and the dbxref array
 Status   :

=cut

sub handle_dbxref {
    my $self = shift;
    my $seq  = shift;
    my $tag  = shift;
    my @arr  = @_;

    my @terms = $seq->attributes($tag);
    for my $term (@terms) {
        my $hashref;
        if ($term =~ /(\S+):(\S+)/) {
            my $db = $1;
            my $acc= $2;
            my $version = 1;
            if ($acc =~ /(\S+)\.(\S+)/) {
                $acc = $1;
                $version = $2;
            }
            $hashref = {
                         'dbxref_id' => {
                               'db_id' => { 'name' => $db },
                               'accession' => $acc,
                               'version'   => $version,
                                        },
                       };
        }
        else {
            $self->throw("I don't know how to handle a dbxref like $term");
        }
        push(@arr, {'dbxref_id' => $hashref});
    }
    return @arr;
}

=head2 handle_source

 Title    : handle_source
 Usage    : $obj->handle_source
 Function :
 Returns  :
 Args     :
 Status   :

=cut

sub handle_source {
    my $self = shift;
    my $seq  = shift;
    my @arr  = @_;

    my $source = $seq->source();
    return @arr unless $source;

    my $hashref = {
               'dbxref_id' => {
                       'db_id' => {'name' => 'GFF_source'},
                       'accession' => $source,
                              }
                  };

    push(@arr, {'dbxref_id' => $hashref});
    return @arr;
}

=head2 _srcf_hash

 Title    : _srcf_hash
 Usage    : $obj->_srcf_hash
 Function : Creates the srcfeature hash for use in featureloc hashes
 Returns  : The srcfeature hash
 Args     : The srcfeature name, the srcfeature type and a reference to the
            organism hash.
 Status   :

=cut

sub _srcf_hash {
    my $self = shift;
    my $srcf = shift;
    my $stype= shift;
    my $orgref = shift;

    my %hash = ('uniquename'    => $srcf,
                'organism_id'   => $orgref,
                'type_id'       => {'name' => $stype,
                                    'cv_id' =>
                                       {'name' => $cv_name{'sequence'} }},
               );

    return %hash;
}


1;