File: gmod_gff2biomart5.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (1565 lines) | stat: -rw-r--r-- 53,880 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

=head1 NAME

  gff2biomart.pl -- create tables for BioMart from genome GFF annotations

=head1 SYNOPSIS

./gff2biomart.pl --species=scer --version=sgdlite_2005_08_23 --output tabscer/ \
 --fasta $scer/fasta/*-all-chromosome-*.fasta  \
 $scer/gff/scer-chr*.gff

  # add some extra tables here for more filters
./gff2biomart3.pl -dataset=11 -species=dper -version=br051028 -output tabdper \
 -table=cross_genome_match_dmelchr,match_tblastn_modDM \
 -fasta $dper/dper*.fa.gz $dper/gff/dper*scaffold*gff $dper/gff/dper*gff.gz

Example data sets from this tool are at:
http://insects.eugenes.org/BioMart/martview

=head1 Loading the results

Usage is for MySQL database and BioMart.org (0.3 version tested)
Please have installed and tested BioMart before trying
to use these data with it.

  # EXAMPLE LOADING INSTRUCTIONS ; use with care to existing databases  
  # LOAD tables TO MySQL: 
  
  mysqladmin create biomart
  cat tabdper//*.sql | mysql biomart 
  mysqlimport biomart `pwd`/tabdper//*.txt
  
  # LOAD xml to MySQL biomart.meta_configuration: 
  # BEST USE martj/bin/marteditor.sh to load tabdper//*.xml   
  # OR try this BUT NOT IF YOU HAVE EXISTING biomart
  # cat tabdper//meta.sql_example | mysql biomart 
  
  # NOTE: biomart is included in *.xml
  # Change datasetID's in xml, meta.sql if needed


=head1 ABOUT gff2biomart

gff2biomart creates

1. chromosome region__main tables for biomart
   with chr broken into nKb bins/regions (1kb default size?)

2. per-featuretype xfeature__dm link tables
    store feature attributes (id,dbxref,match stats,..)
    modify table __main add column feature_bool to indicate
    where features lie.

3.  create $species__chromosome__dm    
  with dna residues for fasta output from biomart
  
use in biomart:
  filter (include,exclude) features that exist in regions
  including joint filters (has homology in x
     but not homology in y,z; has gene/predict_gene/..)
  output: attributes = feature info, fasta of features
    in selected regions
Note: this means changing biomart's filter==attribute
paradigm; new perl module? 


=head1 VERSION NOTES

*** FIXME: gff2featdm dropped featuretype__dm, but want for some ??
e.g. dper__cross_genome_match_dmelchr__dm for dmelchr names
    dper__match_tblastn_modDM__dm for dmel Gene names; use instead __features__dm ?
**** add config for this;
****

Have gone thru several table variants to get biomart to
find both features and regions with feature matches (bool).
Version 3b works (finally) with proper xml; but drop extra per-feat
_dm tables.  Problems still at region-sequence output (where?)

See scer_mart3b_main.xml

Need this script to write proper xml config for biomart (martedit naive won't do).

Version 4 similar but added extra feat-region key
links, not working right.  Move however the combined feat info
from that to 3b.



=head1 NOTES

Needs lots of config choices for general use, esp. creating
UI parts.  Add as module to GMODTools Bulkfiles with various
config parts.  Add default biomart XML templates.

** ?? for biomart cross-table linking need the _dm tables to have
valid region_id_key entries for all such regions even if no/null values
otherwise attribute outputs do sql join that filters output to only thos
with region matches.

** need separate sequence Perl module to get feature_seq entries (as per Gbrowse)
** need new/revised GFF perl module - current needs Ensembl db fields.

** ??? add GO/OrthoMCL info based on prot. matches 

Used now with biomart martj/bin/marteditor to create
biomart metadata xml for interface after creating mart database.
Found martbuilder not useful enough at present for auto-deciphering
a genome database structure (e.g. chado db).

Mar06 (version 6): change sql to ?? sometimes want nulls, sometimes not
   NOT NULL default '' (or default 0 for int)
   
   
=head1 History
 
Needed genome seq. selector tool for sequence-regions,
rather than gene-centric, for new genomes, any seqregion
interests.  E.g. find all regions with homologs to mosquito but
not to Dmel fruitfly; find regions with SNAP gene predictions but
not Genscan/genewise/... or not homology to known genes (i.e. possible
new genes).

BioMart has useful userinterfaces for such but large time cost getting
data from anything else into its desired structure.

from seqblocks prelim. work at insects.eugenes.org, d.gilbert, aug 2005

cat *.gff | sort -k1,1 -k4,4n -k5,5rn | perl -n seqblockbin > seqblock.gfft
cat seqblock.gfft | sort -k2,2 -k1,1n | perl -n seqblockxml > seqblock.xml

=head1 AUTHOR
  
  Don Gilbert, gilbertd@indiana.edu, 2005/2006.
  
=head1 METHODS

=cut

use strict;
use Getopt::Long;
use FileHandle;
use POSIX;


use constant DEBUG => 1; # 
use constant DROSPEGE => 1;   # insects.eugenes.org rewriting code

use constant REGIONTAB => 1;  # always on now
use constant GFFTAB => 1;     # always on now
use constant SHORT_EXTRATAB => 0; # for $ftkey__dm : all fields or not?

my  $PROGRAM=$0; $PROGRAM =~ s,\S+/,,g,;
my  $BSIZE=5000; # blocksize, is 1Kb better default?
my  $DNACHUNKSIZE= 100000;  # dna chunks? NOTE: THIS NEEDS TO BE IN biomart.genome.xml
my  $MAX_FEATURE_RANGE = 1500000; # skip blocking things bigger than this ?


my @skipattr=qw();
my @chrtype=qw(chromosome chromosome_arm golden_path scaffold); #... more/choice
my @skiptype=qw(chromosome_band BAC match_part);  
my @skipattr=qw(qloc query version atID atsource loc noname species);
my @skipsource=qw( assembly:path );

my @tabletype=qw();  # THESE ARE 'type_source' syntax; FIXME;
                     # feat types to make _dm accessory table for

my %ftinfo=(); ## == main table info
my ($outpath,$version,$fasta);
my $species  =  $ENV{species} || 'noname';  # FIXME; full name
my $label    =  $ENV{label} || ""; #

my $RegionKey   = "Region"; # cant clash with valid feature type?
my $RegionIDKey = "region_id_key"; # in sql
my $StrucKey    = 'features'; #? NOW NOT _main, but _dm
my $StrucIDKey  = "feature_id_key"; # in sql
my $SequenceKey = 'genomic_sequence';  #? not main table, dm ? change to $tablehead_genomic_sequence__xxx__main ??
my $XmlKey      = "BioMart";
my $goldenpathType= 'chromosome'; 
my %goldenpathInfo= ();

my %MainKeys=( $RegionKey=>1, $StrucKey=>1, $SequenceKey => -1, $XmlKey => -1);
my @AllFtKeys=();

my @GFF3ATTR= qw(ID Name Parent Dbxref Target Note); # match structuresql order
my %GFF3ATTR= map{$_,1} @GFF3ATTR;
my @infiles;
my $databasename;
my $datasetid= 1;

my $optok= GetOptions(
  'blocksize=n'   => \$BSIZE,
  'dbname=s'     => \$databasename,
  'datasetid=n' => \$datasetid,
  'fasta=s'     => \$fasta,
  'input=s'     => \@infiles,
  'label=s'     => \$label,
  'output=s'    => \$outpath,
  'skipattr=s'  => \@skipattr,
  'skiptype=s'  => \@skiptype,
  'species=s'   => \$species,
  'tabletype=s'  => \@tabletype,
  'version=s'   => \$version,
  );
sub usage { return
"$PROGRAM: creates biomart table data from genome gff data
 usage:
  -input=gff[.gz] ... feature data including chromosomes: 
    @chrtype 
  -output=$outpath  .. put output tables in this folder
  -fasta=$fasta .. chromosome fasta file[.gz]
  -species=$species .. species prefix for tables
  -label=$label .. label for tables
  -dbname=biomart .. database name
  -datasetid=$datasetid .. datasest ID for biomart
  -version=$version .. version of data
  -blocksize=$BSIZE .. region size
  -tabletype=@tabletype .. list of feature types to make accessory _dm tables
  -skiptype=@skiptype .. list of feature types to skip
  -skipattr=@skipattr .. list of attributes to skip
  "; }
#  -format=main,features   data for main is chromosome.gff
#    'format=s'  => \$outformat,
  
push(@infiles,@ARGV); 
die usage() unless($optok && (@infiles || $fasta)); 

my %chrtype = map{ $_,1; } @chrtype;
my %skiptype= map{ $_,1; } map{ split /[,\s]+/; } @skiptype,@chrtype;
my %skipattr= map{ $_,1; } map{ split /[,\s]+/; } @skipattr;
my %skipsource= map{ $_,1; } map{ split /[,\s]+/; } @skipsource;
@tabletype= map{ split /[,\s]+/; } @tabletype;
my %tabletype= map{ $_,1; } @tabletype;

my $org= $species;
if ($org =~ /^(\w)[^_\s]*[_\s]+(\w{1,3})/) {
 $org= lc("$1$2"); # Gspp 4 letter abbrev.
 }
elsif (length($org)>10 && $org =~ /^(\w{1,4})/) {
 $org= lc("$1"); # Gspp 4 letter abbrev.
 }

my $tablehead= $org; $tablehead.= "_".$label if($label);
$version ||= $org."_1";
$databasename ||= "biomart";  ## FIXME
my $DATE=  POSIX::strftime("%F %T", localtime( $^T ));


#?? $outpath must be directory if exists ??
if($outpath) {
  $outpath .= "/" unless($outpath =~ m,/$,); # fixme
  mkdir($outpath) or die "bad output folder: $outpath" 
    unless(-d $outpath);
}


my $regionoid = 1;
my @regions=();
my %regionhash=();

if($fasta) {
  seqsql();
  seqtab( openin($fasta) );
}

if(@infiles) {
  foreach my $in (@infiles) { gff2featprescan( openin($in)); }
  
  regionsql();
  structuresql();
  featsql();
  
  foreach my $in (@infiles) { gff2featdm( openin($in)); }
  
  regiontab();  
  
  xml_config();
  meta_table_sql(); # ??
}

my $outh = new FileHandle(">${outpath}${tablehead}_loading.info"); 
my $info =  loadinfo();
print $outh $info; 
print $info;


#------------------------------------------------------------
sub loadinfo {
  return "
# EXAMPLE LOADING INSTRUCTIONS ; use with care to existing databases  
# LOAD tables TO MySQL: 

mysqladmin create $databasename
cat ${outpath}/*.sql | mysql $databasename 
mysqlimport $databasename `pwd`/${outpath}/*.txt

# LOAD xml to MySQL $databasename.meta_configuration: 
# BEST USE martj/bin/marteditor.sh to load ${outpath}/*.xml   

# OR try this BUT NOT IF YOU HAVE EXISTING $databasename
# cat ${outpath}${tablehead}_meta.sql_example | mysql $databasename 

# NOTE: dbname=$databasename is included in $SequenceKey.xml
# Change datasetID's in xml, meta.sql if needed
";
}

sub meta_table_sql {

  warn "meta_table_sql \n";
  my $maindataset= $tablehead; #?? 
  my $mainid    = $datasetid; # fixme
  my $dnatable=  $ftinfo{$SequenceKey}->{tabname}; ## need xml dataset name here  not tabname
  (my $dnadataset= $dnatable) =~ s/__(main|dm)$//g;
  my $dnaid    = $datasetid+1; # fixme
  my $mainname= $species; ## $maindataset; # species name; FiXME
  
  my $outsql = new FileHandle(">${outpath}${tablehead}_meta.sql_example"); 
  
  # $ftkey= $SequenceKey."_".$XmlKey;
  my $fullpath=$outpath; 
  unless($fullpath=~m,^/,) {$fullpath=`pwd`;chomp($fullpath);$fullpath.="/$outpath";} ## FiXME: use filetools
  
  my $maintab= $ftinfo{$XmlKey}->{tabname};
  my $dnatab = $ftinfo{$SequenceKey."_".$XmlKey}->{tabname};
 
  
print $outsql <<"EOF";

-- BioMart meta data tables
-- USE martj/bin/marteditor to create ; update

CREATE TABLE meta_interface (
  datasetID int(11) default NULL,
  interface varchar(100) default NULL,
  UNIQUE KEY datasetID (datasetID,interface)
);
INSERT INTO meta_interface values('$mainid','default'),('$dnaid','default');

CREATE TABLE meta_user (
  datasetID int(11) default NULL,
  martUser varchar(100) default NULL,
  UNIQUE KEY datasetID (datasetID,martUser)
);
INSERT INTO meta_user values('$mainid','default'),('$dnaid','default');

CREATE TABLE meta_table_info (
  table_name varchar(100) NOT NULL default '',
  column_name varchar(100) NOT NULL default '',
  column_count int(11) default NULL,
  KEY table_name (table_name),
  KEY table_name_2 (table_name,column_name)
);

-- this is the dataset table; needs XML loaded which is tricky without martj

CREATE TABLE meta_configuration (
  internalName varchar(100) default NULL,
  displayName varchar(100) default NULL,
  dataset varchar(100) default NULL,
  description varchar(200) default NULL,
  xml longblob,
  compressed_xml longblob,
  MessageDigest blob,
  type varchar(20) default NULL,
  visible int(1) unsigned default NULL,
  version varchar(25) default NULL,
  datasetID int(11) NOT NULL default '0',
  modified timestamp NOT NULL default CURRENT_TIMESTAMP
);

INSERT INTO meta_configuration (internalName,displayName,dataset,type,visible,version,datasetID,modified) values ('default','$mainname','$maindataset','TableSet','1','$version', $mainid, '$DATE');
INSERT INTO meta_configuration (internalName,displayName,dataset,type,visible,version,datasetID,modified) values ('default','$mainname DNA','$dnadataset','GenomicSequence','0','$version', $dnaid, '$DATE');

-- -- standard biomart also wants compressed_xml, but i haven't gotten that to work w/o corruption
-- CREATE TABLE tmpxml (xml longblob);
-- LOAD DATA INFILE '$fullpath$maintab.xml' INTO TABLE tmpxml LINES TERMINATED BY '~';
-- UPDATE meta_configuration set xml = (select xml from tmpxml) where datasetID = $mainid;
-- TRUNCATE table tmpxml;
-- LOAD DATA INFILE '$fullpath$dnatab.xml' INTO TABLE tmpxml LINES TERMINATED BY '~';
-- UPDATE meta_configuration set xml = (select xml from tmpxml) where datasetID = $dnaid;
-- DROP TABLE tmpxml;

-- GRANT select on $databasename.* to my_mart_user; -- FIX

EOF

}

sub featkey { 
  my($t, $s)= @_;
  my $ftkey= $t."_".$s; $ftkey =~ s/[.:;,\-]+/_/g;
  return (wantarray) ? ($ftkey, nickname($t,$s)) : $ftkey;
} 

sub nickname { 
  my($t, $s)= @_;
  my $nick= $s; 
  if($nick =~ m/^(\w+):(.+)$/) {
    $nick= substr($1,0,1).$2; #??
    }
  $nick =~ s/[:;,_-]+//g; ## fixme ; need configs
  return $nick;
}

sub openin {
  my @infiles= @_;
  my $infiles = join(" ", map{ split /[,\s]+/; } @infiles );
  (my $inshort= $infiles) =~ s,/\S+/,,g,;
  # warn "openin $inshort\n";
  my $inh;
  if($infiles =~ /\.gz/){
  open(IN, "gunzip -c $infiles  |") or warn "Error:openin gunzip -c $infiles\n"; 
  $inh= *IN;
  } else {
  open(IN, "cat $infiles |") or warn "Error:openin cat $infiles\n"; 
  $inh= *IN;
  }
  return ($inh,$inshort);
}



=item SQL writers

=cut

sub attrsql
{
  my($outh,$ftkey,$atkeys,$prefix)= @_;
  $prefix.="_" if($prefix && $prefix !~ m/_$/); #$prefix ||= "at_";
  foreach my $ak (@$atkeys) {
    print $outh "   ${prefix}${ak} "; ## change this 'at_' prefix; ends up in biomart views
    if($ftinfo{$ftkey}->{istext}->{$ak}) {
      print $outh "text default null"; #? use text instead?
    } elsif($ftinfo{$ftkey}->{ischar}->{$ak}) {
      print $outh "varchar(128) default null"; #? use text instead?
    } elsif($ftinfo{$ftkey}->{isreal}->{$ak}) {
      print $outh "double (32,5) default null";
    } else {
      print $outh "int(10) default null";
    }
    print $outh ",\n";
  }
}

sub structuresql
{
  # if(GFFTAB) 
  my $ftkey= $StrucKey;
  return unless(ref $ftinfo{$ftkey});
  my $outh= $ftinfo{$ftkey}->{outsql};
  my $tabname= $ftinfo{$ftkey}->{tabname} || "${tablehead}__${ftkey}__main"; # no longer __main
  
  ## match this field.txt
  # $oid,$c,$s,$t,$b,$e,$p,$o,$r,$a << gff order fields
  ## ** add GFFv3 ID,Name,Parent,Target,Notes,Dbxref parsed from attribs ..
  @GFF3ATTR= qw(ID Name Parent Dbxref Target Note); # match structuresql order
  %GFF3ATTR= map{$_,1} @GFF3ATTR;
  warn "structuresql $tabname\n";
  print $outh "
  -- written by: $PROGRAM
  -- date: $DATE 
  drop table if exists $tabname;
  create table $tabname (
    region_id_key   int(10) not null,
    feature_id_key   int(10)  not null,
    chr_name    varchar(32) not null,
    source      varchar(64) not null default '', 
    biotype     varchar(32) not null default '',
    type_source varchar(128) not null default '', 
    chr_start   int(10)  not null default 0,
    chr_end     int(10)  not null default 0,
    score       double(32,5)  not null default 0, 
    chr_strand  int(2)  not null default 0,
    chr_phase   int(2)  not null default 0,
    ID          varchar(128) not null default '',
    Name        varchar(128) not null default '',
    Parent      varchar(128) not null default '',
    Dbxref      text  not null default '',
    Target      text  not null default '',
    Note        text  not null default '',
    attributes  text  not null default '',
    ";
  print $outh "     key (region_id_key), \n"; #??
  print $outh "     key (feature_id_key) );\n"; #??
}


sub featsql
{
  ### NOT OFF for v.3b : user selected types for extra tables
  return unless(@tabletype);
  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    next unless(grep/^$ftkey/,@tabletype); ## ignore Source; only leading type?
    
    next  if ($MainKeys{$ftkey}); ##($ftkey eq $RegionKey);
    my $outh= $ftinfo{$ftkey}->{outsql};
    my $atkeys= $ftinfo{$ftkey}->{attrkeys};
    my $nickname= $ftinfo{$ftkey}->{nickname};
    my $tabname= $ftinfo{$ftkey}->{tabname} || "${tablehead}__${ftkey}__dm";
    
    ## match this field.txt order
    ##     print $outh join("\t", $oid, $c,$b,$e,$o,$t,$s);
    
    warn "featsql $tabname\n"; ##${tablehead}__${ftkey}__dm\n";
    #? short or long here?
if(SHORT_EXTRATAB) { ## (GFFTAB)  
    print $outh "
    drop table if exists $tabname;
    create table $tabname (
      region_id_key   int(10) not null ,
      feature_id_key  int(10)  not null,
      ";
  } else {
    print $outh "
    drop table if exists $tabname;
    create table $tabname (
      region_id_key   int(10)  not null,
      feature_id_key  int(10)  not null,
      chr_name    varchar(32) not null,
      chr_start   int(10) not null,
      chr_end     int(10) not null,
      chr_strand  int(2) not null  default 0,
      biotype     varchar(32) not null default '',
      source      varchar(64) not null default '', 
      ";
}
  
  ## see xml_extra_filters : use these fields as filters ??
    attrsql($outh,$ftkey,$atkeys,$nickname);
    print $outh "   key (region_id_key), \n"; #??
    print $outh "   key (feature_id_key) );\n"; #??
    }
}


sub regionsql
{
  my $ftkey= $RegionKey;
  return unless(ref $ftinfo{$ftkey});
  
  my $outh= $ftinfo{$ftkey}->{outsql};
  my $tabname= $ftinfo{$ftkey}->{tabname} || "${tablehead}__region__main";
  
  ## match this field.txt
  ## print $outh join("\t",$oid,"$c.$ib",$ib,$se,$c,$len);

  
  ## FIXME:
  ## region_name > region_id       varchar(64),
  ## drop chr_size
  warn "regionsql $tabname\n";
  print $outh "
  -- written by: $PROGRAM
  -- date: $DATE 
  drop table if exists $tabname;
  create table $tabname (
    region_id_key   int(10)  not null,
    region_name     varchar(64)  not null,
    region_start    int(10) not null,
    region_end      int(10) not null,
    chr_name        varchar(40)  not null,
    chr_size        int(10)  not null, 
    ";
  
  #my $atkeys= $ftinfo{$ftkey}->{attrkeys};  
  #skip for region# attrsql($outh,$ftkey,$atkeys);
    
    ## add feature_bool here ?
  print $outh "\n";
  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    ## next  if($MainKeys{$ftkey}); #($ftkey eq $RegionKey);
    my $sqlfield= "${ftkey}_bool";
    $ftinfo{$ftkey}->{regionfield}= $sqlfield;
    print $outh "   $sqlfield int(1) default null,\n"
    }
  print $outh "     key (region_id_key) );\n"; #??
}


sub seqsql
{
  my $ftkey= $SequenceKey;
  ## this should be instead
  ## my $tabname= lc("${tablehead}_genomic_sequence__sequence__main"); # ??
  my $tabname= lc("${tablehead}__${ftkey}__dm");  
  my $outh   = new FileHandle(">$outpath$tabname.txt");
  my $outsql = new FileHandle(">${outpath}$tabname.sql"); #$ftinfo{$ftkey}->{outsql};

  my $tabinfo= {
    tabname  => $tabname,
    nickname => $ftkey,
    outh   => $outh,
    outsql => $outsql,
    count => 0,
    oid => 1,
    attrs => {},
    isreal => {},
    isint => {},
    ischar => {},
    istext => {},
    locs => [],
    };
  $ftinfo{$ftkey}= $tabinfo;

  warn "seqsql $tabname\n";
  print $outsql "
  -- written by: $PROGRAM
  -- date: $DATE 
  drop table if exists $tabname;
  create table $tabname (
    region_id_key   int(10)  not null,
    name          varchar(128) not null default '',
    version       varchar(64) not null default '',
    biotype       varchar(255) not null default '',
    description   text not null default '',
    chr_size      int(10) not null, 
    md5checksum   character(32) not null default '',
    chr_start     int(10) not null,
    chunk_size    int(10) not null, 
    residues      longblob  not null default ''
    );";
  # timelastmodified   timestamp
  #print $outsql "     key (region_id_key) );\n"; #??
}



=item Table data subs

=cut

sub writetab
{
  my( $outh, @vals)= @_;
  foreach (@vals) { $_='\N' unless(defined $_); }
  print $outh join("\t",@vals);
}

sub writedna
{
  my( $outh, $oid, $id, $vers, $type, $desc, $md5, $dna)= @_;
  my $len= length($dna);
  # print $outh join("\t",$oid,$id,$vers,$type,$desc,$md5,1,$len,$dna),"\n";
  for (my $start=0; $start < $len;  ) {
    my $csize= $DNACHUNKSIZE;
    if($start+$csize > $len) { $csize= $len - $start; }
    writetab( $outh, $oid,$id,$vers,$type,$desc,$len,$md5, ($start+1), $csize);
    print $outh "\t", substr($dna,$start,$csize),"\n";
    $start += $csize;
    }
}


sub seqtab
{
  my($inh, $iname)= @_;
  warn "seqtab $iname\n";
  my $ftkey= $SequenceKey;
  
  # my $tabname= lc("${tablehead}__${ftkey}__dm");  
  # my $outh = new FileHandle(">${outpath}$tabname.txt"); #$ftinfo{$ftkey}->{outh};
  my $tabname= $ftinfo{$ftkey}->{tabname};
  my $outh   = $ftinfo{$ftkey}->{outh};
  
  my $oid = 1; # what ?? this is 'region_id_key' ?? 
  my $type= $goldenpathType || "chromosome"; # need input info
  my $start= 1; # biomart likes 1-origin
  
  my($id,$dna,$desc,$md5)=('','','','');
  while(<$inh>){
    chomp;
    if(/^>(\S+)\s*(.*)/) {
      my($newid,$newdesc)=($1,$2);
      if($id) {
        writedna( $outh, $oid, $id, $version, $type, $desc, $md5, $dna);
        # my $len= length($dna);
        # print $outh join("\t",$oid,$id,$version,$type,$desc,$md5,$start,$len,$dna),"\n";
        }
      $id= $newid; $desc= $newdesc; $dna='';
      if($desc =~ s/MD5=(\w+)[;]?//i){ $md5=$1; } else { $md5='\N'; }
      $desc =~ s/CRC64=\w+[;]?//; $desc =~ s/size=\w+[;]?//; # hack fix
      }
    elsif(/^\w/){
      $dna.= $_; #? check junk?
      }
    }
  if($id){
    writedna( $outh, $oid, $id, $version, $type, $desc, $md5, $dna);
    # my $len= length($dna);
    # print $outh join("\t",$oid,$id,$version,$type,$desc,$md5,$start,$len,$dna),"\n";
    }
  close($inh); close($outh);
}



sub attrhash 
{
  my @at= map { split (/;\s*/,$_) } @_;
  my %at= ();
  foreach (@at) {
    my($k,$v)=split(/=/,$_,2); 
    next unless(defined $v && $v ne '.' && $v ne '');
    if($k eq 'db_xref'){ $k='Dbxref'; }
    ## fixme for lowcase of GFF3 'name', 'id', 'db_xref', 'note'
    elsif($k =~ m/^[a-z]/ && $GFF3ATTR{ucfirst($k)}) { $k= ucfirst($k); }
    if(defined $at{$k}){ $at{$k} .=",".$v; } else { $at{$k} = $v ; }
    }
  attrib4drospege(\%at) if(DROSPEGE);
  return %at;
}

sub attrib4drospege
{
  my $at= shift;
  ## drop  Protein '-PA' tag from Name, ID like fields, otherwise ID search is messy
  ## biomart could use regexp/wildcard searches.
  $at->{ID}   =~ s/\-[RP]\w$// if($at->{ID});
  $at->{Name} =~ s/\-[RP]\w$// if($at->{Name});
  # also Dbxref ?
}

sub gffcols
{
  # my ($c,$s,$t,$b,$e,$p,$o,$r,$a)
  my @v= split "\t",$_[0],9; #gff cols
  foreach (@v) { $_=undef if($_ eq '.'); }
  unless($v[6]=~/\d/) { $v[6]= ($v[6] eq '+') ? 1 : ($v[6] eq '-') ? -1 : 0; }
  return @v;
}



## need 1-origin not 0-origin for blocks !!
sub calcregion { 
  if (wantarray) {
    my($b,$e)= @_;
    my @rg=();
    for (my $i= 1 + $BSIZE * int($b/$BSIZE) ; $i < $e; $i += $BSIZE) { push(@rg,$i); }
    return @rg;
  } else {
    return 1 + $BSIZE * int($_[0]/$BSIZE); 
  }
}

sub overlaps {
  my($sb,$se,$tb,$te)= @_;
  return ($tb <= $se && $te >= $sb);
}


sub newtab 
{
  my($ftkey,$nickname)= @_;

  my $ismain= ($MainKeys{$ftkey});
  my $suf= ($ftkey eq $StrucKey) ? "dm"
    : ($ismain) ? "main" : "dm"; ##($ftkey eq $RegionKey) 
  my $tabname= lc("${tablehead}__${ftkey}__${suf}");

  ## v4/3b drop some/all ftkey_dm tabs /
  my $makeout= ($ismain) || $tabletype{$ftkey};
  my $outh   = ($makeout) ? new FileHandle(">$outpath$tabname.txt") : undef;
  my $outsql = ($makeout) ? new FileHandle(">$outpath$tabname.sql") : undef;

  my $tabinfo= {
    tabname  => $tabname,
    nickname => $nickname,
    outh   => $outh,
    outsql => $outsql,
    count => 0,
    oid => 1,
    attrs => {},
    isreal => {},
    isint => {},
    ischar => {},
    istext => {},
    locs => [],
    };
  return $tabinfo;
}


=item GFF to Table data

=cut

=item gff2featprescan

  need to know about features before writing; read thru gff twice
  
=cut

sub gff2featprescan
{
  my($inh, $iname)= @_;
  warn "gff2featprescan $iname\n";
  
  # my %ftinfo=(); ## use global
  unless($ftinfo{$StrucKey}) {
    $ftinfo{$StrucKey}= newtab($StrucKey,$StrucKey);
    }

  while(<$inh>){
    next if(/^#/);
    chomp;
    my ($c,$s,$t,$b,$e,$p,$o,$r,$a)= gffcols($_); #split "\t",$_,9; #gff cols
    
    my $ftkey= featkey($t,$s);
    if($e && $chrtype{$t}) {
      $ftkey= $RegionKey; ## ??
      my $len= $e - $b + 1;
      $goldenpathType= $t; # could be many.
      $goldenpathInfo{$c}= { name => $c, type=>$t, source => $s, start=>$b, end=>$e, size=>$len };
      }
    elsif(!$e || $skiptype{$t} || $skipsource{$s}) {
      next; #?? should these be saved ??
      }

    ## $featureInfo{$ftkey}= { type=>$t, source => $s, } ## use ftinfo
    
    ## write new table for each "$t.$s" group
    unless($ftinfo{$ftkey}) {
      my $nickname= nickname($t,$s);
      $ftinfo{$ftkey}= newtab($ftkey,$nickname);
      $ftinfo{$ftkey}->{type}= $t; #?
      $ftinfo{$ftkey}->{source}= $s; #?
      }
    
    if($ftkey eq $RegionKey) {
      ##if(REGIONTAB)     
      # what? need to make region table now for crossrefs in _dm ?
      my $len= $e - $b + 1;
      my $links=""; # place holder; #5
      my @rblocks= calcregion($b,$e); 
      foreach my $ib (@rblocks) {
        my $ie= $ib + $BSIZE - 1; $ie= $e if ($ie>$e);
        # push(@regions, [$regionoid,$c,$ib,$ie,$len,$links]);
        my $regionkey= "$c.$ib";
        push(@regions, $regionkey); # keep input order
        $regionhash{$regionkey} = [$regionoid,$c,$ib,$ie,$len,$links]; # instead of @regions
        $regionoid++;
        }
    }
    
    $ftinfo{$ftkey}->{count}++;
    my %at= attrhash($a, ((defined $p)?"score=$p":"")); 
    foreach my $ak (sort keys %at) { 
      next if $skipattr{$ak};
      my $av= $at{$ak}; 
      next unless(defined $av && $av ne '.');
      $ftinfo{$ftkey}->{attrs}->{$ak}++; 
        if ($av =~ m/^[-+]?\d+$/) { $ftinfo{$ftkey}->{isint}->{$ak}++; }
      elsif($av =~ m/^[-+]?\d[\d\.e\-]+$/i) { $ftinfo{$ftkey}->{isreal}->{$ak}++; }
      elsif(length($av) > 128) { $ftinfo{$ftkey}->{istext}->{$ak}++; }
      elsif($av =~ m/\D/) { $ftinfo{$ftkey}->{ischar}->{$ak}++; }
      }
    }

  @AllFtKeys= grep { !$MainKeys{$_} } (sort keys %ftinfo);
    
  foreach my $ftkey (@AllFtKeys) { 
    my @at = sort keys %{ $ftinfo{$ftkey}->{attrs} };
    $ftinfo{$ftkey}->{attrkeys} = \@at;
    }
  close($inh); 
  ## return \%ftinfo;
}

=item regiontab

  write main table of region information
  
=cut

sub regiontab
{
  warn "regiontab\n";
  my $ftkey= $RegionKey;
  my $outh = $ftinfo{$ftkey}->{outh};
  ## use input order# my @regions= sort keys %regionhash; # sort not numeric
  foreach my $r (@regions) {
#   push(@regions, [$regionoid,$c,$ib,$ie,$len,$links]);
    my $rv= $regionhash{$r}; 
    my($regionoid,$c,$ib,$ie,$len,$links)= @$rv;
    writetab( $outh, $regionoid,"$c.$ib",$ib,$ie,$c,$len);

    ## ?? add all the _bool values here ? need region list in prescan then
    foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
      ## next  if($MainKeys{$ftkey}); #($ftkey eq $RegionKey);
      my $hits= ($links =~ m/\b$ftkey\b/) ? '1' : '\N'; ## urk value of NULL not 0 is off;
      print $outh "\t$hits"; 
      }
    print $outh "\n";
  }
}

=item gff2featdm

  write common feature table (gff) and maybe per-feature subtables;
  collect feature->region map info.
  
=cut

sub gff2featdm
{
  my($inh, $iname)= @_;
  warn "gff2featdm $iname\n";
    
  while(<$inh>){
    next if(/^#/);
    chomp;
    my ($c,$s,$t,$b,$e,$p,$o,$r,$a)= gffcols($_); 
    
    my $ftkey= featkey($t,$s);
    if( $e && $chrtype{$t}) {
      $ftkey= $RegionKey; 
      }
    
    elsif(!$e || $skiptype{$t} || $skipsource{$s}) {
      next; #?? should these be saved ??
      }

    # rows per attrib
    my %at= attrhash($a, ((defined $p)?"score=$p":"")); 
    my $featoid = 0;
    
    if(1) { # (GFFTAB)  
  # print struct main table of all features
    $featoid  = $ftinfo{$StrucKey}->{oid}++; #?? use this one for feat oid
    my $outh  = $ftinfo{$StrucKey}->{outh};

    my $rblock= calcregion($b,$e); # first only
    my $regionkey= "$c.$rblock";
    my $rvals= $regionhash{$regionkey};  
    my $regionoid= $rvals->[0] || '\N'; ## shouldnt be dupl.
    
    ## split out these gff3 tags from $a: ID,Name,Dbxref,Note,Parent,Target
    ## fixme for locase of GFF3 'name', 'id', 'db_xref', 'note'
    my %at2= attrhash($a); 
    my @atlist= map{ (defined $at2{$_}) ? delete $at2{$_} : '\N'; } @GFF3ATTR;
    my $amore = join(";", map { "$_=$at2{$_}" }(sort keys %at2));

    ## can we add 1 regionoid here that feature matches? will it help?
    writetab( $outh, $regionoid, $featoid, 
      $c,$s,$t,$ftkey,$b,$e,$p,$o,$r,@atlist,$amore); # same as gff order 
      ## BUT added $ftkey = type_source for feature filtering 
    print $outh "\n";
    }
    
  # -- collect region info and maybe write ftkey tab
  # print region dm tables for each feat type

    if($ftkey eq $RegionKey) {

    } else {  
      next  if($MainKeys{$ftkey});
      next if($e - $b > $MAX_FEATURE_RANGE); #? skip godawfullong ones/some errors?

      my $outh = $ftinfo{$ftkey}->{outh};
      my $atkeys= $ftinfo{$ftkey}->{attrkeys};

      my @hitblocks= calcregion($b,$e);
      foreach my $rblock (@hitblocks) { ## loop over feat row adding each regionoid
        my $regionkey= "$c.$rblock";
        my $rvals= $regionhash{$regionkey};  
        my $regionoid= $rvals->[0] || '\N'; ## shouldnt be dupl.
        $rvals->[5] .= $ftkey.",";
        
    # FIXME: dropped featuretype__dm, but want for some ??
    # e.g. dper__cross_genome_match_dmelchr__dm for dmelchr names
    
        next unless($tabletype{$ftkey}); 

  ## drop this extra table data? only need 3 tabs: region, feature, dna
if(SHORT_EXTRATAB) {
        writetab( $outh, $regionoid, $featoid);  
} else {
        writetab( $outh, $regionoid, $featoid, $c,$b,$e,$o,$t,$s);  
}        
        foreach my $ak (@$atkeys) {
          my $av= $at{$ak}; $av= '\N' unless(defined $av); 
          print $outh "\t$av";
          }
        print $outh "\n";
        } 
    }
  }
  close($inh); 
}




=item Biomart XML configs

=cut

sub xml_config
{
  my $xmlkey= $XmlKey;
  my $tabname= lc("${tablehead}__${xmlkey}");
  my $outh   = new FileHandle(">$outpath$tabname.xml");
  
  my $tabinfo= {
    tabname  => $tabname,
    nickname => $xmlkey,
    outh   => $outh,
    # outsql => $outsql,
    count => 0,
    oid => 1,
    attrs => {},
    isreal => {},
    isint => {},
    ischar => {},
    istext => {},
    locs => [],
    };
  $ftinfo{$xmlkey}= $tabinfo;

  warn "xml_config $tabname\n";
  xml_header($xmlkey);
  xml_filter_page($xmlkey);
  xml_attr_pages($xmlkey);
  xml_footer($xmlkey);

  #-- write dna dataset.xml
  $xmlkey= $SequenceKey."_".$XmlKey;
  $tabname= lc("${tablehead}__${xmlkey}");
  $outh   = new FileHandle(">$outpath$tabname.xml");
  $tabinfo= {
    tabname  => $tabname,
    nickname => $xmlkey,
    outh   => $outh,
    # outsql => $outsql,
    count => 0,
    oid => 1,
    attrs => {},
    isreal => {},
    isint => {},
    ischar => {},
    istext => {},
    locs => [],
    };

  $ftinfo{$xmlkey}= $tabinfo;
  warn "xml_config $tabname\n";
  xml_seqconfig($xmlkey);
}


sub xml_header
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  my $tabname=  $ftinfo{$RegionKey}->{tabname};
  my $dataset= $tablehead; #?? 
  my $did    = $datasetid; # fixme
  
print $outh <<"EOF";
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DatasetConfig>
<DatasetConfig 
  dataset="$dataset" 
  datasetID="$did" 
  displayName="$species" 
  version="$version"
  type="TableSet"  
  visible="1"
  interfaces="default" internalName="default" martUsers="default" modified="$DATE" 
  >
<!--  
written by: $PROGRAM
date: $DATE
-->
  
<MainTable>$tabname</MainTable>
<Key>region_id_key</Key>  

<Exportable attributes="rg_chr_name,rg_region_start,rg_region_end" 
  default="1" internalName="raw_sequence" linkName="raw_sequence" linkVersion="$version" 
  name="raw_sequence" orderBy="rg_chr_name" />
<Exportable attributes="rg_chr_name,rg_region_start,rg_region_end,ft_chr_strand" 
  default="0" internalName="oriented_raw_sequence" linkName="oriented_raw_sequence" linkVersion="$version" 
  name="oriented_raw_sequence" orderBy="rg_chr_name" />

<Exportable attributes="ft_id_key,ft_chr_name,ft_chr_start,ft_chr_end,ft_chr_strand" 
  internalName="gene_exon_intron" linkName="gene_exon_intron" linkVersion="$version" 
  name="gene_exon_intron"  orderBy="ft_id_key" default="1" />
<Exportable attributes="ft_id_key,ft_chr_name,ft_chr_start,ft_chr_end,ft_chr_strand" 
  internalName="gene_flank" linkName="gene_flank" linkVersion="$version" 
  name="gene_flank" orderBy="ft_id_key" default="0" />

EOF

}

sub xml_footer
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  print $outh <<"EOF";

</DatasetConfig>
EOF
}

sub xml_filter_page
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  print $outh <<"EOF";
<FilterPage displayName="FILTERS" internalName="filterpg">
EOF
  xml_feature_filter($xmlkey);
  xml_region_range_filter($xmlkey);
  xlm_region_contains_filter($xmlkey);
  xml_extra_filters($xmlkey) if(@tabletype);
  
  print $outh <<"EOF";
</FilterPage>

EOF
}

sub xml_extra_filters
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  
print $outh <<"EOF";

<FilterGroup displayName="EXTRA FILTERS:" internalName="exfilterg">
<FilterCollection displayName="Extra Features" internalName="exfeatures_col">
EOF

  foreach my $tabkey (@tabletype) {
    my $tabname=  $ftinfo{$tabkey}->{tabname};
    my $atkeys = $ftinfo{$tabkey}->{attrkeys};
    my $prefix= $ftinfo{$tabkey}->{nickname};
    $prefix.="_" if($prefix && $prefix !~ m/_$/);  
    foreach my $ak (@$atkeys) {
      my $atfld="${prefix}${ak}"; 
     (my $fname= ucfirst($atfld)) =~ s/_/ /g;
print $outh <<"EOF";
<FilterDescription displayName="$fname" field="$atfld" 
 internalName="${tabname}_${atfld}" key="region_id_key" tableConstraint="$tabname"
 legal_qualifiers="=" qualifier="="  type="text" />
EOF
      }
    }

print $outh <<"EOF";
</FilterCollection>
</FilterGroup>
EOF

}

sub xml_feature_filter
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  my $tabname=  $ftinfo{$StrucKey}->{tabname};
print $outh <<"EOF";

<FilterGroup displayName="FILTER by FEATURE:" internalName="ftfilterg">
<FilterCollection displayName="Feature Type(s)" internalName="Features_col">
<FilterDescription displayName="Feature type" field="type_source" 
 internalName="biotype_multi_list" key="region_id_key" tableConstraint="$tabname"
 legal_qualifiers="=,in" qualifier="="  type="list">
EOF

  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    #my $type  = $ftinfo{$ftkey}->{type};
    #my $source= $ftinfo{$ftkey}->{source} || ".";
    #my $fname= "$type:$source";
    (my $fname= ucfirst($ftkey)) =~ s/_/ /g;
    print $outh <<"EOF";
  <Option displayName="$fname" internalName="$ftkey"  value="$ftkey" />
EOF
  }

print $outh <<"EOF";
</FilterDescription>
</FilterCollection>
</FilterGroup>
EOF

}

sub kbsizeof {
  my($n)= shift;
  if($n>=1000000) { $n= int($n/1000000)."M"; }
  elsif($n>=1000) { $n= int($n/1000)."K"; }
  return $n;
}

sub xml_region_range_filter
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};

print $outh <<"EOF";

<FilterGroup displayName="FILTER GENOME REGION:" internalName="grfilters">
<FilterCollection displayName="Chromosome" internalName="rgchrcol">
<FilterDescription displayName="Chromosome" field="chr_name" 
 internalName="rgchr_multi_list" key="region_id_key" 
 legal_qualifiers="=,in" qualifier="=" 
 tableConstraint="main" type="list">
EOF

  my @chrs= (sort keys %goldenpathInfo);
  my $toomany= (@chrs > 50);
  if ($toomany) { # got a genome-in-progress ... pick only biggest
    my @topchr= sort{ $goldenpathInfo{$b}->{size} <=> $goldenpathInfo{$a}->{size} }  
       @chrs;
    @chrs= splice(@topchr,0,50);
    }
  foreach my $chr (@chrs) {  
    my $cname= $chr; 
    $cname .=" ".kbsizeof( $goldenpathInfo{$chr}->{size} ) if($toomany);
    # ($cname= ucfirst($cname)) =~ s/_/ /g;
print $outh <<"EOF";
    <Option displayName="$cname" internalName="$chr" value="$chr" />
EOF
  }
  
print $outh <<"EOF";
</FilterDescription>
<FilterDescription displayName="Region Start &gt;=" field="region_start" internalName="region_start" key="region_id_key" 
  legal_qualifiers="&gt;=" qualifier="&gt;="  tableConstraint="main" type="text" />
<FilterDescription displayName="Region End &lt;=" field="region_end" internalName="region_end" key="region_id_key" 
  legal_qualifiers="&lt;=" qualifier="&lt;=" tableConstraint="main" type="text" />

<FilterDescription displayName="Chrom. Size &gt;=" field="chr_size" internalName="hi_chr_size" key="region_id_key" 
  legal_qualifiers="&gt;=" qualifier="&gt;="  tableConstraint="main" type="text" />
<FilterDescription displayName="Chrom. Size &lt;=" field="chr_size" internalName="low_chr_size" key="region_id_key" 
  legal_qualifiers="&lt;=" qualifier="&lt;=" tableConstraint="main" type="text" />
</FilterCollection>
</FilterGroup>
EOF

}

sub xlm_region_contains_filter
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};

print $outh <<"EOF";

<FilterGroup  displayName="FILTER REGIONS WITH FEATURES: (include,exclude)" internalName="rgfeatflt">
<FilterCollection displayName="Feature LIST" internalName="id_list">
<FilterDescription  internalName="id_multilist_filters" type="boolean_list">
EOF
  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    my $fld= $ftinfo{$ftkey}->{regionfield};
    next unless($fld);
    (my $fname= ucfirst($fld)) =~ s/_/ /g;
    if($fname =~ s/ bool$//) { $fname="Has $fname";}
print $outh <<"EOF";
    <Option displayName="$fname" field="$fld" internalName="f_$fld" isSelectable="true" key="region_id_key" 
    legal_qualifiers="only,excluded" qualifier="only" tableConstraint="main" type="boolean" />
EOF
    }
print $outh <<"EOF";
</FilterDescription>
</FilterCollection>
</FilterGroup>
EOF

# and another one, diff iname
print $outh <<"EOF";

<FilterGroup  displayName="FILTER REGIONS WITH FEATURES:  (include,exclude)" internalName="rgfeatflt">
<FilterCollection displayName="Feature LIST" internalName="id2_list">
<FilterDescription  internalName="id2_multilist_filters" type="boolean_list">
EOF
  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    my $fld= $ftinfo{$ftkey}->{regionfield};
    next unless($fld);
    (my $fname= ucfirst($fld)) =~ s/_/ /g;
    if($fname =~ s/ bool$//) { $fname="Has $fname";}
    
print $outh <<"EOF";
    <Option displayName="$fname" field="$fld" internalName="f2_$fld" isSelectable="true" key="region_id_key" 
    legal_qualifiers="only,excluded" qualifier="only" tableConstraint="main" type="boolean" />
EOF
    }
print $outh <<"EOF";
</FilterDescription>
</FilterCollection>
</FilterGroup>
EOF

}

sub xml_attr_pages
{
  my($xmlkey)= @_;
  xml_featattr_page($xmlkey);
  xml_featseq_page($xmlkey);
  xml_regionattr_page($xmlkey);
  xml_regionseq_page($xmlkey);
}


sub xml_featattr_page
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  my $tabname=  $ftinfo{$StrucKey}->{tabname};
print $outh <<"EOF";

<AttributePage displayName="FEATURE TABLE" internalName="ftattr_page" outFormats="html,txt,csv,tsv,xls">
<AttributeGroup displayName="FEATURE TABLE" internalName="ftattr_grp">
<!-- not quite gff; biomart gff printer is unusable w/o rewrite  -->
<AttributeCollection displayName="Common Features" internalName="Features_ac">
<AttributeDescription default="true" displayName="Chromosome" field="chr_name" internalName="at_chr_name" key="region_id_key" tableConstraint="$tabname" maxLength="32" />
<AttributeDescription default="true" displayName="Source" field="source" internalName="at_source" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Biotype" field="biotype" internalName="at_biotype" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Start" field="chr_start" internalName="at_chr_start" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="End" field="chr_end" internalName="at_chr_end" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="Strand" field="chr_strand" internalName="at_chr_strand" key="region_id_key" tableConstraint="$tabname" maxLength="2" />
<AttributeDescription default="true" displayName="Score" field="score" internalName="at_score" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="ID" field="id" internalName="at_id" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Name" field="Name" internalName="at_Name" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Dbxref" field="Dbxref" internalName="at_Dbxref" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Note" field="Note" internalName="at_Note" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Other Attributes" field="attributes" internalName="at_attributes" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<!-- Parent; Target; -->
</AttributeCollection>
</AttributeGroup>
</AttributePage>
EOF
}


sub xml_featseq_page
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  my $tabname=  $ftinfo{$StrucKey}->{tabname};
  my $dnadataset=  $ftinfo{$SequenceKey}->{tabname}; ## need xml dataset name here  not tabname
  $dnadataset =~ s/__(main|dm)$//g;
  
print $outh <<"EOF";

<AttributePage displayName="FEATURE SEQUENCE" internalName="fsq_pg" outFormats="fasta">
<AttributeGroup displayName="FEATURE SEQUENCE" internalName="fsq_grp">

<AttributeCollection default="true"  displayName="Feature Sequence" internalName="seq_scope_type" maxSelect="1">
<AttributeDescription displayName="Feature span"  internalName="$dnadataset.gene_exon_intron" key="region_id_key" tableConstraint="$tabname" />
<AttributeDescription displayName="Feature flank" internalName="$dnadataset.gene_flank" key="region_id_key" tableConstraint="$tabname"  />
</AttributeCollection>

<AttributeCollection displayName="Upstream flank" internalName="upstream" maxSelect="0">
<AttributeDescription default="100" displayName="Upstream flank1" internalName="$dnadataset.filter.upstream_flank" />
</AttributeCollection>
<AttributeCollection displayName="Downstream flank" internalName="downstream" maxSelect="0">
<AttributeDescription default="100"  displayName="Downstream flank1" internalName="$dnadataset.filter.downstream_flank" />
</AttributeCollection>

<AttributeCollection displayName="Header Information" internalName="ft_header_info" maxSelect="0">
<AttributeDescription default="true" displayName="Chromosome" field="chr_name" internalName="ft_chr_name" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="Start" field="chr_start" internalName="ft_chr_start" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="End" field="chr_end" internalName="ft_chr_end" key="region_id_key" tableConstraint="$tabname" maxLength="10" />
<AttributeDescription default="true" displayName="Strand" field="chr_strand" internalName="ft_chr_strand" key="region_id_key" tableConstraint="$tabname" maxLength="2" />
  <!-- this is needed for link to seq by feature id ? -->
<AttributeDescription hideDisplay="true" default="true" displayName="Ft_ID (required)" 
  field="feature_id_key" internalName="ft_id_key" key="region_id_key" tableConstraint="$tabname" maxLength="10" /> 
<AttributeDescription default="true" displayName="Biotype" field="biotype" internalName="ft_biotype" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="Source" field="source" internalName="ft_source" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
<AttributeDescription default="true" displayName="ID" field="id" internalName="ft_id" key="region_id_key" maxLength="128" tableConstraint="$tabname" />
<AttributeDescription default="true" displayName="Name" field="Name" internalName="ft_Name" key="region_id_key" maxLength="128" tableConstraint="$tabname" />
<AttributeDescription default="true" displayName="Dbxref" field="Dbxref" internalName="ft_Dbxref" key="region_id_key" maxLength="128" tableConstraint="$tabname" />
<AttributeDescription displayName="Score" field="score" internalName="ft_score" key="region_id_key" tableConstraint="$tabname" maxLength="32" />
<AttributeDescription displayName="Note" field="Note" internalName="ft_Note" key="region_id_key" maxLength="128" tableConstraint="$tabname" />
<AttributeDescription displayName="Other Attributes" field="attributes" internalName="ft_attributes" key="region_id_key" tableConstraint="$tabname" maxLength="128" />
</AttributeCollection>
</AttributeGroup>
</AttributePage>

EOF
}

sub xml_regionattr_page
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  # my $tabname=  $ftinfo{$StrucKey}->{tabname};
print $outh <<"EOF";

<AttributePage displayName="REGION TABLE" internalName="rgattr_page" outFormats="html,txt,csv,tsv,xls">
<AttributeGroup displayName="REGION TABLE" internalName="rgattr_grp">

<AttributeCollection displayName="Region location" internalName="Region">
<AttributeDescription displayName="Region ID" field="region_name" internalName="region_name" key="region_id_key" maxLength="64" tableConstraint="main" />
<AttributeDescription default="true" displayName="Region chr" field="chr_name" internalName="rg_chr_name" key="region_id_key" maxLength="40" tableConstraint="main" />
<AttributeDescription default="true" displayName="Region start" field="region_start" internalName="region_start1" key="region_id_key" maxLength="10" tableConstraint="main" />
<AttributeDescription default="true" displayName="Region end" field="region_end" internalName="region_end1" key="region_id_key" maxLength="10" tableConstraint="main" />
</AttributeCollection>

<AttributeCollection displayName="Region contains" internalName="Regioncont">
EOF

  foreach my $ftkey (@AllFtKeys) { ## sort keys %ftinfo
    my $fld= $ftinfo{$ftkey}->{regionfield};
    next unless($fld);
    (my $fname= ucfirst($fld)) =~ s/_/ /g;
    if($fname =~ s/ bool$//) { $fname="Has $fname";}
print $outh <<"EOF";
  <AttributeDescription default="true" displayName="$fname" field="$fld" internalName="a_$fld" 
    key="region_id_key" maxLength="1" tableConstraint="main" />
EOF
    }

print $outh <<"EOF";
</AttributeCollection>
</AttributeGroup>
</AttributePage>

EOF
}

sub xml_regionseq_page
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  my $dnadataset=  $ftinfo{$SequenceKey}->{tabname}; ## need xml dataset name here  not tabname
  $dnadataset =~ s/__(main|dm)$//g;
print $outh <<"EOF";
<AttributePage displayName="REGION SEQUENCE" internalName="sequences" outFormats="fasta">
<AttributeGroup displayName="REGION SEQUENCE" internalName="sequence">

<!-- having problems with this selector -->
<AttributeCollection  displayName="REGION Sequence" internalName="seq_scope_type" maxSelect="1">
<AttributeDescription displayName="Region sequence" internalName="$dnadataset.raw_sequence" key="region_id_key"  tableConstraint="main" />
<!-- AttributeDescription  displayName="Oriented sequence"  internalName="$dnadataset.oriented_raw_sequence" key="region_id_key"  tableConstraint="main" / -->
</AttributeCollection>

<AttributeCollection displayName="Header Information" internalName="rg_header_info" maxSelect="0">
<AttributeDescription default="true" displayName="Scaffold" field="chr_name" internalName="rg_chr_name" key="region_id_key" maxLength="128" tableConstraint="main" />
<AttributeDescription default="true" displayName="Start" field="region_start" internalName="rg_region_start" key="region_id_key" maxLength="128" tableConstraint="main" />
<AttributeDescription default="true" displayName="End" field="region_end" internalName="rg_region_end" key="region_id_key" maxLength="128" tableConstraint="main" />
</AttributeCollection>

</AttributeGroup>
</AttributePage>
EOF
}

sub xml_seqconfig
{
  my($xmlkey)= @_;
  my $outh = $ftinfo{$xmlkey}->{outh};
  # my $databasename= $tablehead . "_" .$version;  ## FIXME - global
  my $dnatable=  $ftinfo{$SequenceKey}->{tabname}; ## need xml dataset name here  not tabname
  (my $dnadataset= $dnatable) =~ s/__(main|dm)$//g;
  my $did    = $datasetid + 1; # fixme
  
print $outh <<"EOF";
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DatasetConfig>
<DatasetConfig 
  dataset="$dnadataset" 
  datasetID="$did" 
  displayName="$species genomic DNA" 
  version="$version"
  type="GenomicSequence"  
  optional_parameters="$databasename.$dnatable,name,chr_start,residues,$DNACHUNKSIZE" 
  visible="0"
  interfaces="default" internalName="default" martUsers="default" modified="$DATE" 
  >
<!--  
written by: $PROGRAM 
date: $DATE
-->

<!-- note filter keys are tied to software: GenomicSequence.pm + dnaextractor -->
<Importable filters="pkey,chr,start,end,strand" internalName="gene_flank" 
  linkName="gene_flank" linkVersion="$version" name="gene_flank" orderBy="pkey" />
<Importable filters="pkey,chr,start,end,strand" internalName="gene_exon_intron" 
  linkName="gene_exon_intron" linkVersion="$version" name="gene_exon_intron" orderBy="pkey" />

<Importable filters="chr,start,end,strand" internalName="oriented_raw_sequence" 
  linkName="oriented_raw_sequence" linkVersion="$version" name="oriented_raw_sequence" />
<Importable filters="chr,start,end" internalName="raw_sequence" linkName="raw_sequence" 
  linkVersion="$version" name="raw_sequence" />

<!-- // no good for prots; need exon-rank; phase?; type??, codon,seq_edits are options -->
<!--
<Importable filters="pkey,chr,start,end,strand" internalName="gene_exon" 
  linkName="gene_exon" linkVersion="$version" name="gene_exon" orderBy="pkey" />
<Importable filters="pkey,chr,start,end,strand" 
  internalName="peptide" linkName="peptide" linkVersion="$version" name="peptide" orderBy="pkey" />
<Importable filters="pkey,chr,start,end,strand" internalName="coding" 
  linkName="coding" linkVersion="$version" name="coding" orderBy="pkey" />
-->

<FilterPage displayName="FILTERS FOR LINKS" internalName="link_filters">
<FilterGroup description="Filters for Links" internalName="link_filters">
<FilterCollection internalName="link_filters">
<FilterDescription internalName="phase" type="list" />
<FilterDescription internalName="codon_table_id" type="list" />
<FilterDescription internalName="pos" type="list" />
<FilterDescription internalName="allele" type="list" />
<FilterDescription internalName="pkey" type="list" />
<FilterDescription internalName="chr" type="list" />
<FilterDescription internalName="start" type="list" />
<FilterDescription internalName="end" type="list" />
<FilterDescription internalName="strand" type="list" />
<FilterDescription internalName="rank" type="list" />
<FilterDescription internalName="seq_edits" type="list" />
<FilterDescription internalName="type" type="list" />
<FilterDescription displayName="Upstream flank" internalName="upstream_flank" legal_qualifiers="=" qualifier="=" type="list" />
<FilterDescription displayName="Downstream flank" internalName="downstream_flank" legal_qualifiers="=" qualifier="=" type="list" />
</FilterCollection>
</FilterGroup>
</FilterPage>

<AttributePage displayName="ATTRIBUTES FOR LINKS" internalName="link_attributes">
<AttributeGroup description="Attributes for Links" internalName="link_attributes">
<AttributeCollection internalName="link_attributes">
<AttributeDescription internalName="codon_table_id" />

<AttributeDescription datasetLink="raw_sequence" internalName="raw_sequence" />
<AttributeDescription datasetLink="oriented_raw_sequence" internalName="oriented_raw_sequence" />
<AttributeDescription datasetLink="gene_exon_intron" displayName="Unspliced (Gene)" internalName="gene_exon_intron" />
<AttributeDescription datasetLink="gene_exon" displayName="Exon sequences (Gene)" internalName="gene_exon" />
<AttributeDescription datasetLink="gene_flank" displayName="Flank (Gene)" internalName="gene_flank" />
<AttributeDescription displayName="upstream_flank" internalName="upstream_flank" />
<AttributeDescription displayName="downstream_flank" internalName="downstream_flank" />
<AttributeDescription datasetLink="coding" displayName="Coding sequence" internalName="coding" />
<AttributeDescription datasetLink="coding_gene_flank" displayName="Flank-coding region (Gene)" internalName="coding_gene_flank" />
<AttributeDescription datasetLink="peptide" displayName="Peptide" internalName="peptide" />

<AttributeDescription displayName="pkey" internalName="pkey" />
<AttributeDescription displayName="chr" internalName="chr" />
<AttributeDescription displayName="start" internalName="start" />
<AttributeDescription displayName="end" internalName="end" />
<AttributeDescription displayName="strand" internalName="strand" />
<AttributeDescription displayName="rank" internalName="rank" />
<AttributeDescription internalName="seq_edits" />
<AttributeDescription internalName="type" />
</AttributeCollection>
</AttributeGroup>
</AttributePage>

</DatasetConfig>
EOF

}




1; # turn into perl module
##=================================