forked from mvneves/ncbitaxcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncbitc.c
More file actions
1004 lines (834 loc) · 22.2 KB
/
ncbitc.c
File metadata and controls
1004 lines (834 loc) · 22.2 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define NCBITC_FILE_DMP_GI_TAXID "gi_taxid_nucl.dmp"
#define NCBITC_FILE_DMP_NODES "nodes.dmp"
#define NCBITC_FILE_DMP_NAMES "names.dmp"
#define NCBITC_FILE_BIN_GI_TAXID "gi_taxid_nucl.dmp.bin"
#define NCBITC_FILE_BIN_NODES "nodes.dmp.bin"
#define NCBITC_FILE_BIN_NAMES "names.dmp.bin"
#undef NCBITC_WITH_COMMENTS
#undef NCBITC_WITH_GIINDEX
enum ncbitc_name_class {
NCBITC_CLASS_ACRONYM,
NCBITC_CLASS_ANAMORPH,
NCBITC_CLASS_AUTHORITY,
NCBITC_CLASS_BLAST_NAME,
NCBITC_CLASS_COMMON_NAME,
NCBITC_CLASS_EQUIVALENT_NAME,
NCBITC_CLASS_GENBANK_ACRONYM,
NCBITC_CLASS_GENBANK_ANAMORPH,
NCBITC_CLASS_GENBANK_COMMON_NAME,
NCBITC_CLASS_GENBANK_SYNONYM,
NCBITC_CLASS_INCLUDES,
NCBITC_CLASS_IN_PART,
NCBITC_CLASS_MISNOMER,
NCBITC_CLASS_MISSPELLING,
NCBITC_CLASS_SCIENTIFIC_NAME,
NCBITC_CLASS_SYNONYM,
NCBITC_CLASS_TELEOMORPH,
NCBITC_CLASS_UNPUBLISHED_NAME,
};
enum ncbitc_rank {
NCBITC_RANK_CLASS,
NCBITC_RANK_FAMILY,
NCBITC_RANK_FORMA,
NCBITC_RANK_GENUS,
NCBITC_RANK_INFRACLASS,
NCBITC_RANK_INFRAORDER,
NCBITC_RANK_KINGDOM,
NCBITC_RANK_NO_RANK,
NCBITC_RANK_ORDER,
NCBITC_RANK_PARVORDER,
NCBITC_RANK_PHYLUM,
NCBITC_RANK_SPECIES,
NCBITC_RANK_SPECIES_GROUP,
NCBITC_RANK_SPECIES_SUBGROUP,
NCBITC_RANK_SUBCLASS,
NCBITC_RANK_SUBFAMILY,
NCBITC_RANK_SUBGENUS,
NCBITC_RANK_SUBKINGDOM,
NCBITC_RANK_SUBORDER,
NCBITC_RANK_SUBPHYLUM,
NCBITC_RANK_SUBSPECIES,
NCBITC_RANK_SUBTRIBE,
NCBITC_RANK_SUPERCLASS,
NCBITC_RANK_SUPERFAMILY,
NCBITC_RANK_SUPERKINGDOM,
NCBITC_RANK_SUPERORDER,
NCBITC_RANK_SUPERPHYLUM,
NCBITC_RANK_TRIBE,
NCBITC_RANK_VARIETAS
};
#define NCBITC_MAX_SHORT_NAME 32
#define NCBITC_MAX_LONG_NAME 64
#define NCBITC_MAX_HUGE_NAME 256
#define NCBITC_LINE_SIZE 512
/**
nodes.dmp
---------
This file represents taxonomy nodes. The description for each node includes
the following fields:
tax_id -- node id in GenBank taxonomy database
parent tax_id -- parent node id in GenBank taxonomy database
rank -- rank of this node (superkingdom, kingdom, ...)
embl code -- locus-name prefix; not unique
division id -- see division.dmp file
inherited div flag (1 or 0) -- 1 if node inherits division from parent
genetic code id -- see gencode.dmp file
inherited GC flag (1 or 0) -- 1 if node inherits genetic code from parent
mitochondrial genetic code id -- see gencode.dmp file
inherited MGC flag (1 or 0) -- 1 if node inherits mitochondrial gencode from parent
GenBank hidden flag (1 or 0) -- 1 if name is suppressed in GenBank entry lineage
hidden subtree root flag (1 or 0) -- 1 if this subtree has no sequence data yet
comments -- free-text comments and citations
*/
struct nodes_dmp {
int tax_id;
int parent_tax_id;
char rank;
char embl_code[3];
short division_id;
char inherited_div_flag;
short genetic_code_id;
char inherited_GC_flag;
int mitochondrial_genetic_code_id;
char inherited_MGC_flag;
char GenBank_hidden_flag;
char hidden_subtree_root_flag;
#ifdef NCBITC_WITH_COMMENTS
char comments[255];
#endif
};
/**
names.dmp
---------
Taxonomy names file has these fields:
tax_id -- the id of node associated with this name
name_txt -- name itself
unique name -- the unique variant of this name if name not unique
name class -- (synonym, common name, ...)
*/
struct names_dmp {
int tax_id;
char name_txt[NCBITC_MAX_LONG_NAME];
char unique_name[NCBITC_MAX_LONG_NAME];
char name_class[NCBITC_MAX_LONG_NAME];
};
struct gi_taxid_nucl_dmp {
#ifdef NCBITC_WITH_GIINDEX
int gi;
#endif
int tax_id;
};
/* Flag set by ‘--verbose’. */
static int verbose_flag = 0;
int ncbitc_bin_to_str_class(int id, char *str)
{
switch (id) {
case NCBITC_CLASS_ACRONYM:
sprintf(str, "acronym");
break;
case NCBITC_CLASS_ANAMORPH:
sprintf(str, "anamorph");
break;
case NCBITC_CLASS_AUTHORITY:
sprintf(str, "authority");
break;
case NCBITC_CLASS_BLAST_NAME:
sprintf(str, "blast name");
break;
case NCBITC_CLASS_COMMON_NAME:
sprintf(str, "common name");
break;
case NCBITC_CLASS_EQUIVALENT_NAME:
sprintf(str, "equivalent name");
break;
case NCBITC_CLASS_GENBANK_ACRONYM:
sprintf(str, "genbank acronym");
break;
case NCBITC_CLASS_GENBANK_ANAMORPH:
sprintf(str, "genbank anamorph");
break;
case NCBITC_CLASS_GENBANK_COMMON_NAME:
sprintf(str, "genbank common name");
break;
case NCBITC_CLASS_GENBANK_SYNONYM:
sprintf(str, "genbank synonym");
break;
case NCBITC_CLASS_INCLUDES:
sprintf(str, "includes");
break;
case NCBITC_CLASS_IN_PART:
sprintf(str, "in-part");
break;
case NCBITC_CLASS_MISNOMER:
sprintf(str, "misnomer");
break;
case NCBITC_CLASS_MISSPELLING:
sprintf(str, "misspelling");
break;
case NCBITC_CLASS_SCIENTIFIC_NAME:
sprintf(str, "scientific name");
break;
case NCBITC_CLASS_SYNONYM:
sprintf(str, "synonym");
break;
case NCBITC_CLASS_TELEOMORPH:
sprintf(str, "teleomorph");
break;
case NCBITC_CLASS_UNPUBLISHED_NAME:
sprintf(str, "unpublished name");
break;
default:
sprintf(str, "invalid id");
return -1;
}
return 0;
};
int ncbitc_str_to_bin_class(char *str, int *id)
{
if (strncmp(str, "acronym", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_ACRONYM;
else if (strncmp(str, "anamorph", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_ANAMORPH;
else if (strncmp(str, "authority", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_AUTHORITY;
else if (strncmp(str, "blast name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_BLAST_NAME;
else if (strncmp(str, "common name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_COMMON_NAME;
else if (strncmp(str, "equivalent name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_EQUIVALENT_NAME;
else if (strncmp(str, "genbank acronym", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_GENBANK_ACRONYM;
else if (strncmp(str, "genbank anamorph", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_GENBANK_ANAMORPH;
else if (strncmp(str, "genbank common name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_GENBANK_COMMON_NAME;
else if (strncmp(str, "genbank synonym", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_GENBANK_SYNONYM;
else if (strncmp(str, "includes", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_INCLUDES;
else if (strncmp(str, "in-part", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_IN_PART;
else if (strncmp(str, "misnomer", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_MISNOMER;
else if (strncmp(str, "misspelling", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_MISSPELLING;
else if (strncmp(str, "scientific name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_SCIENTIFIC_NAME;
else if (strncmp(str, "synonym", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_SYNONYM;
else if (strncmp(str, "teleomorph", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_TELEOMORPH;
else if (strncmp(str, "unpublished name", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_CLASS_UNPUBLISHED_NAME;
else {
*id = -1;
}
return *id;
}
int ncbitc_bin_to_str_rank(int id, char *str)
{
switch (id) {
case NCBITC_RANK_CLASS:
sprintf(str, "class");
break;
case NCBITC_RANK_FAMILY:
sprintf(str, "family");
break;
case NCBITC_RANK_FORMA:
sprintf(str, "forma");
break;
case NCBITC_RANK_GENUS:
sprintf(str, "genus");
break;
case NCBITC_RANK_INFRACLASS:
sprintf(str, "infraclass");
break;
case NCBITC_RANK_INFRAORDER:
sprintf(str, "infraorder");
break;
case NCBITC_RANK_KINGDOM:
sprintf(str, "kingdom");
break;
case NCBITC_RANK_NO_RANK:
sprintf(str, "no rank");
break;
case NCBITC_RANK_ORDER:
sprintf(str, "order");
break;
case NCBITC_RANK_PARVORDER:
sprintf(str, "parvorder");
break;
case NCBITC_RANK_PHYLUM:
sprintf(str, "phylum");
break;
case NCBITC_RANK_SPECIES:
sprintf(str, "species");
break;
case NCBITC_RANK_SPECIES_GROUP:
sprintf(str, "species group");
break;
case NCBITC_RANK_SPECIES_SUBGROUP:
sprintf(str, "species subgroup");
break;
case NCBITC_RANK_SUBCLASS:
sprintf(str, "subclass");
break;
case NCBITC_RANK_SUBFAMILY:
sprintf(str, "subfamily");
break;
case NCBITC_RANK_SUBGENUS:
sprintf(str, "subgenus");
break;
case NCBITC_RANK_SUBKINGDOM:
sprintf(str, "subkingdom");
break;
case NCBITC_RANK_SUBORDER:
sprintf(str, "suborder");
break;
case NCBITC_RANK_SUBPHYLUM:
sprintf(str, "subphylum");
break;
case NCBITC_RANK_SUBSPECIES:
sprintf(str, "subspecies");
break;
case NCBITC_RANK_SUBTRIBE:
sprintf(str, "subtribe");
break;
case NCBITC_RANK_SUPERCLASS:
sprintf(str, "superclass");
break;
case NCBITC_RANK_SUPERFAMILY:
sprintf(str, "superfamily");
break;
case NCBITC_RANK_SUPERKINGDOM:
sprintf(str, "superkingdom");
break;
case NCBITC_RANK_SUPERORDER:
sprintf(str, "superorder");
break;
case NCBITC_RANK_SUPERPHYLUM:
sprintf(str, "superphylum");
break;
case NCBITC_RANK_TRIBE:
sprintf(str, "tribe");
break;
case NCBITC_RANK_VARIETAS:
sprintf(str, "varietas");
break;
default:
sprintf(str, "invalid id");
return -1;
}
return 0;
}
int ncbitc_str_to_bin_rank(char *str, int *id)
{
if (strncmp(str, "class", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_CLASS;
else if (strncmp(str, "family", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_FAMILY;
else if (strncmp(str, "forma", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_FORMA;
else if (strncmp(str, "genus", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_GENUS;
else if (strncmp(str, "infraclass", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_INFRACLASS;
else if (strncmp(str, "infraorder", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_INFRAORDER;
else if (strncmp(str, "kingdom", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_KINGDOM;
else if (strncmp(str, "no rank", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_NO_RANK;
else if (strncmp(str, "order", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_ORDER;
else if (strncmp(str, "parvorder", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_PARVORDER;
else if (strncmp(str, "phylum", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_PHYLUM;
else if (strncmp(str, "species", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SPECIES;
else if (strncmp(str, "species group", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SPECIES_GROUP;
else if (strncmp(str, "species subgroup", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SPECIES_SUBGROUP;
else if (strncmp(str, "subclass", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBCLASS;
else if (strncmp(str, "subfamily", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBFAMILY;
else if (strncmp(str, "subgenus", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBGENUS;
else if (strncmp(str, "subkingdom", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBKINGDOM;
else if (strncmp(str, "suborder", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBORDER;
else if (strncmp(str, "subphylum", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBPHYLUM;
else if (strncmp(str, "subspecies", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBSPECIES;
else if (strncmp(str, "subtribe", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUBTRIBE;
else if (strncmp(str, "superclass", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUPERCLASS;
else if (strncmp(str, "superfamily", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUPERFAMILY;
else if (strncmp(str, "superkingdom", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUPERKINGDOM;
else if (strncmp(str, "superorder", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUPERORDER;
else if (strncmp(str, "superphylum", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_SUPERPHYLUM;
else if (strncmp(str, "tribe", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_TRIBE;
else if (strncmp(str, "varietas", NCBITC_MAX_SHORT_NAME) == 0)
*id = NCBITC_RANK_VARIETAS;
else {
*id = -1;
}
return *id;
}
int ncbitc_print_node_entry(struct nodes_dmp *node)
{
char rank[NCBITC_MAX_SHORT_NAME];
/*
if (node->tax_id == 0) {
printf("0\n");
return 0;
}
*/
ncbitc_bin_to_str_rank(node->rank, rank);
printf
("%d | %d | %s | %s | %d | %d | %d | %d | %d | %d | %d | %d | %s |\n",
node->tax_id, node->parent_tax_id, rank, node->embl_code,
node->division_id, node->inherited_div_flag, node->genetic_code_id,
node->inherited_GC_flag, node->mitochondrial_genetic_code_id,
node->inherited_MGC_flag, node->GenBank_hidden_flag,
node->hidden_subtree_root_flag,
#ifdef NCBITC_WITH_COMMENTS
node->comments
#else
""
#endif
);
return 0;
}
int ncbitc_cleanup_str(char *str, int max)
{
int len;
len = strnlen(str, max);
if (len <= 2) {
str[0] = '\0';
} else {
str[0] = ' ';
str[len - 1] = '\0';
}
return 0;
}
int ncbitc_read_node_entry(char *line, struct nodes_dmp *node)
{
char rank[NCBITC_MAX_SHORT_NAME];
char embl_code[NCBITC_MAX_SHORT_NAME];
char comments[255];
sscanf(line,
"%d | %d |%[^|]|%[^|]| %d | %d | %d | %d | %d | %d | %d | %d |%[^|]|",
&node->tax_id, &node->parent_tax_id, rank, embl_code,
(int *)&node->division_id, (int *)&node->inherited_div_flag,
(int *)&node->genetic_code_id, (int *)&node->inherited_GC_flag,
(int *)&node->mitochondrial_genetic_code_id,
(int *)&node->inherited_MGC_flag,
(int *)&node->GenBank_hidden_flag,
(int *)&node->hidden_subtree_root_flag, comments);
ncbitc_cleanup_str(rank, 32);
ncbitc_str_to_bin_rank(rank + 1, (int *)&node->rank);
ncbitc_cleanup_str(embl_code, 32);
if (embl_code[0] != '\0') {
node->embl_code[0] = embl_code[1];
node->embl_code[1] = embl_code[2];
node->embl_code[2] = '\0';
} else {
node->embl_code[0] = '\0';
}
ncbitc_cleanup_str(comments, 255);
#ifdef NCBITC_WITH_COMMENTS
strncpy(node->comments, comments, 255);
#endif
return 0;
};
int ncbitc_read_name_entry(char *line, struct names_dmp *name)
{
sscanf(line, "%d |%[^|]|%[^|]|%[^|]|", &name->tax_id,
name->name_txt, name->unique_name, name->name_class);
ncbitc_cleanup_str(name->name_txt, NCBITC_MAX_LONG_NAME);
ncbitc_cleanup_str(name->unique_name, NCBITC_MAX_LONG_NAME);
ncbitc_cleanup_str(name->name_class, NCBITC_MAX_SHORT_NAME);
return 0;
}
int ncbitc_print_name_entry(struct names_dmp *name)
{
printf("%d | %s | %s | %s |\n", name->tax_id,
name->name_txt, name->unique_name, name->name_class);
return 0;
}
int ncbitc_search_tax_id(char *filename, int gi, int *tax_id)
{
FILE *fp;
long offset;
struct gi_taxid_nucl_dmp entry;
fp = fopen(filename, "rb");
if (fp == NULL) {
perror("fopen");
return -1;
}
/* seek to line */
offset = (gi - 1) * sizeof(struct gi_taxid_nucl_dmp);
if (fseek(fp, offset, SEEK_SET) < 0) {
perror("fseek");
fclose(fp);
return -1;
}
fread(&entry, sizeof(struct gi_taxid_nucl_dmp), 1, fp);
if (verbose_flag) {
#ifdef NCBITC_WITH_GIINDEX
printf("%d\t%d\n", entry.gi, entry.tax_id);
#else
printf("%d\t%d\n", gi, entry.tax_id);
#endif
}
*tax_id = entry.tax_id;
fclose(fp);
return 0;
}
int ncbitc_search_node(char *filename, int tax_id, struct nodes_dmp *node)
{
FILE *fp;
long offset;
fp = fopen(filename, "rb");
if (fp == NULL) {
perror("fopen");
return -1;
}
/* seek to line */
offset = (tax_id - 1) * sizeof(struct nodes_dmp);
if (fseek(fp, offset, SEEK_SET) < 0) {
perror("fseek");
fclose(fp);
return -1;
}
fread(node, sizeof(struct nodes_dmp), 1, fp);
if (verbose_flag) {
printf("%d\n", node->tax_id);
}
fclose(fp);
return 0;
}
int ncbitc_seek_name(FILE * fp, int pos, struct names_dmp *name)
{
long offset;
/* seek to line */
offset = (pos - 1) * sizeof(struct names_dmp) + sizeof(int);
if (fseek(fp, offset, SEEK_SET) < 0) {
perror("fseek");
fclose(fp);
return -1;
}
fread(name, sizeof(struct names_dmp), 1, fp);
return name->tax_id;
}
int ncbitc_search_name(char *filename, int tax_id)
{
FILE *fp;
int val = 0;
int l1, i, j, flag = 0;
l1 = 0;
int num;
struct names_dmp name;
fp = fopen(filename, "rb");
if (fp == NULL) {
perror("fopen");
return -1;
}
fread(&num, sizeof(int), 1, fp);
/* binary search */
i = num - 1;
while (l1 <= i) {
j = (l1 + i) / 2;
val = ncbitc_seek_name(fp, j, &name);
//ncbitc_print_name_entry(&name);
if (val == tax_id) {
flag = 1;
break;
} else if (val < tax_id)
l1 = j + 1;
else
i = j - 1;
}
if (flag == 0) {
printf("0\n");
return 0;
}
for (i = j - 1; i > 0; i--) {
val = ncbitc_seek_name(fp, i, &name);
if (val != tax_id)
break;
}
for (i = i + 1; i < num; i++) {
val = ncbitc_seek_name(fp, i, &name);
if (val != tax_id)
break;
ncbitc_print_name_entry(&name);
}
fclose(fp);
return 0;
}
int ncbitc_create_gi_taxid(char *filename)
{
FILE *fp;
FILE *fpnew;
char line[NCBITC_LINE_SIZE];
int i, diff, last;
char new[128];
struct gi_taxid_nucl_dmp zero;
struct gi_taxid_nucl_dmp entry;
int gi;
memset(&zero, '\0', sizeof(struct gi_taxid_nucl_dmp));
sprintf(new, "%s.bin", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return -1;
}
fpnew = fopen(new, "wb");
if (fp == NULL) {
perror("fopen");
return -1;
}
last = 0;
while (fgets(line, NCBITC_LINE_SIZE, fp) != NULL) {
sscanf(line, "%d\t%d", &gi, &entry.tax_id);
diff = gi - last;
if (diff != 1) {
for (i = 1; i < diff; i++)
fwrite(&zero, sizeof(struct gi_taxid_nucl_dmp),
1, fpnew);
}
#ifdef NCBITC_WITH_GIINDEX
entry.gi = gi;
#endif
fwrite(&entry, sizeof(struct gi_taxid_nucl_dmp), 1, fpnew);
last = gi;
}
fclose(fp);
fclose(fpnew);
return 0;
}
int ncbitc_create_nodes(char *filename)
{
FILE *fp;
FILE *fpnew;
char line[NCBITC_LINE_SIZE];
int i, diff, last;
char new[128];
struct nodes_dmp node;
struct nodes_dmp zero;
memset(&zero, 0, sizeof(struct nodes_dmp));
sprintf(new, "%s.bin", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return -1;
}
fpnew = fopen(new, "wb");
if (fp == NULL) {
perror("fopen");
return -1;
}
last = 0;
while (fgets(line, NCBITC_LINE_SIZE, fp) != NULL) {
ncbitc_read_node_entry(line, &node);
//ncbitc_print_node_entry(&node);
diff = node.tax_id - last;
if (diff != 1) {
for (i = 1; i < diff; i++)
fwrite(&zero, sizeof(struct nodes_dmp), 1,
fpnew);
}
fwrite(&node, sizeof(struct nodes_dmp), 1, fpnew);
last = node.tax_id;
}
fclose(fp);
fclose(fpnew);
return 0;
}
int ncbitc_create_names(char *filename)
{
FILE *fp;
FILE *fpnew;
char line[NCBITC_LINE_SIZE];
//int i, diff, last;
char new[128];
struct names_dmp name;
struct names_dmp zero;
int num = 0;
memset(&zero, 0, sizeof(struct names_dmp));
sprintf(new, "%s.bin", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return -1;
}
fpnew = fopen(new, "wb");
if (fp == NULL) {
perror("fopen");
return -1;
}
fwrite(&num, sizeof(int), 1, fpnew);
while (fgets(line, NCBITC_LINE_SIZE, fp) != NULL) {
ncbitc_read_name_entry(line, &name);
//ncbitc_print_name_entry(&name);
fwrite(&name, sizeof(struct names_dmp), 1, fpnew);
num++;
}
rewind(fpnew);
fwrite(&num, sizeof(int), 1, fpnew);
fclose(fp);
fclose(fpnew);
return 0;
}
#define NCBITC_OPER_SEARCH 0x0
#define NCBITC_OPER_SEARCH_GI 0x1
#define NCBITC_OPER_SEARCH_NODE 0x2
#define NCBITC_OPER_SEARCH_NAME 0x3
#define NCBITC_OPER_CREATE 0x4
void print_help()
{
printf("Usage: ncbitc [options]\n");
printf("Options:\n");
printf(" -s --search id search all tree using a gi index\n");
printf(" -g --search-gi id search a tax id using a gi index\n");
printf(" -t --search-node id search a node entry using a tax id\n");
printf(" -n --search-name id search a name entry using a tax id\n");
printf(" -v --verbose turn on verbose output\n");
printf(" -h --help print this help message\n");
exit(0);
}
int main(int argc, char *argv[])
{
int index;
int tax_id;
int ret;
int c;
int oper = -1;
int option_index = 0;
while (1) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"create", no_argument, 0, 'c'},
{"search", required_argument, 0, 's'},
{"search-gi", required_argument, 0, 'g'},
{"search-name", required_argument, 0, 'n'},
{"search-node", required_argument, 0, 't'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "hcs:n:vt:g:", long_options,
&option_index);
if (c == -1)
break;
switch (c) {
case 'c':
oper = NCBITC_OPER_CREATE;
break;
case 's':
oper = NCBITC_OPER_SEARCH;
index = atoi(optarg);
break;
case 'g':
oper = NCBITC_OPER_SEARCH_GI;
index = atoi(optarg);
break;
case 'v':
verbose_flag = 1;
break;
case 'n':
oper = NCBITC_OPER_SEARCH_NAME;
tax_id = atoi(optarg);
break;
case 't':
oper = NCBITC_OPER_SEARCH_NODE;
tax_id = atoi(optarg);
break;
case '?':
case 'h':
print_help();
break;
default:
print_help();
}
}
if (verbose_flag)
printf("verbose flag is set\n");
if (oper == NCBITC_OPER_SEARCH) {
struct nodes_dmp node;
ret = ncbitc_search_tax_id(NCBITC_FILE_BIN_GI_TAXID, index, &tax_id);
if (ret < 0) {
printf("Error.\n");
return -1;
}
if (tax_id == 0) {
printf("0\n");
return 0;
}
while (tax_id != 1) {
ret = ncbitc_search_node(NCBITC_FILE_BIN_NODES, tax_id, &node);
if (ret < 0) {
printf("Error.\n");
return -1;
}
tax_id = node.parent_tax_id;
if (tax_id != 1)
ncbitc_print_node_entry(&node);
}
} else if (oper == NCBITC_OPER_SEARCH_GI) {
struct nodes_dmp node;
ret = ncbitc_search_tax_id(NCBITC_FILE_BIN_GI_TAXID, index, &tax_id);
if (ret < 0) {
printf("Error.\n");
return -1;
}
if (tax_id == 0) {
printf("0\n");
return 0;
}
ret = ncbitc_search_node(NCBITC_FILE_BIN_NODES, tax_id, &node);
if (ret < 0) {
printf("Error.\n");
return -1;
}
ncbitc_print_node_entry(&node);
} else if (oper == NCBITC_OPER_SEARCH_NODE) {
struct nodes_dmp node;
ret = ncbitc_search_node(NCBITC_FILE_BIN_NODES, tax_id, &node);
if (ret < 0) {
printf("Error.\n");
return -1;
}
ncbitc_print_node_entry(&node);
} else if (oper == NCBITC_OPER_SEARCH_NAME) {
ret = ncbitc_search_name(NCBITC_FILE_BIN_NAMES, tax_id);
if (ret < 0) {
printf("Error.\n");
return -1;
}
} else if (oper == NCBITC_OPER_CREATE) {
ncbitc_create_gi_taxid(NCBITC_FILE_DMP_GI_TAXID);
ncbitc_create_nodes(NCBITC_FILE_DMP_NODES);
ncbitc_create_names(NCBITC_FILE_DMP_NAMES);
} else {
print_help();