-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidhelp.cpp
2251 lines (1727 loc) · 54.2 KB
/
idhelp.cpp
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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2009 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
// Rules:
// ID cannot have any spaces, tabs, commas (,) or plus (+) signs
// Attributes can have commas
#include "idhelp.h"
#include "options.h"
#include "helper.h"
#include "nlist.h"
#include <iomanip>
extern Plink * PP;
map<IDField*,set<IDValue> > IDHelper::parseQuery(string q)
{
map<IDField*,set<IDValue> > lookup;
// Convert a query string into a searchable form
NList tlist(0);
vector<string> ids = tlist.deparseStringList( q );
// Consider each term (comma-delimited list)
map<string,string> qmap;
map<string,string> qjoint;
for ( int i = 0 ; i < ids.size() ; i++)
{
string s = ids[i];
if ( s.find("=") == string::npos )
error("Query should be: ID=value or ATTRIB=value[,ATTRIB=value]");
string fs = s.substr(0, s.find("="));
string vs = s.substr(s.find("=")+1);
// Is this a joint field?
if ( fs.find("+") != string::npos )
{
// We need to split both fs and vs: they must have the same
// number of entries on both sides
vector<string> flist =
tlist.deparseStringList( searchAndReplace( fs,"+",",") );
vector<string> vlist =
tlist.deparseStringList( searchAndReplace( vs,"+",",") );
if ( flist.size() != vlist.size() )
error("Joint query wrong: " + fs + " = " + vs);
for (int i=0; i<flist.size(); i++)
{
qmap.insert(make_pair(flist[i],vlist[i]));
qjoint.insert(make_pair(flist[i],vs));
}
}
else
{
qmap.insert(make_pair(fs,vs));
}
}
// Check that all named fields exist
map<string,string>::iterator i = qmap.begin();
while ( i != qmap.end() )
{
string fs = i->first;
map<string,IDField*>::iterator f = fieldMap.find( fs );
if ( f == fieldMap.end() )
error("Cannot find field " + fs );
IDField * thisField = f->second;
IDValue t;
t.field = thisField;
t.value = i->second;
if ( t.field->equiv )
t.updateAlias();
//////////////////////////////////////////////////
// Is this query being framed as a joint field?
map<string,string>::iterator k = qjoint.find( fs );
if ( k != qjoint.end() )
{
t.jointValue = k->second;
t.field->joint = true;
}
else
{
t.field->joint = false;
}
map<IDField*,set<IDValue> >::iterator j = lookup.find( thisField );
if ( j == lookup.end() )
{
set<IDValue> ts;
ts.insert(t);
lookup.insert(make_pair(thisField,ts) );
}
else
j->second.insert(t);
++i;
}
return lookup;
}
bool IDHelper::matchIndividual(IDGroup * group,
map<IDField*,set<IDValue> > & matchTemplate )
{
bool match = true;
// Compare
// map<IDField*, set<IDValue> > lookupValues;
// with this group
int found = 0;
for (int g=0; g<group->values.size(); g++)
{
IDField * f = group->values[g]->field;
map<IDField*, set<IDValue> >::iterator mf = matchTemplate.find( f );
if ( mf == matchTemplate.end() )
continue;
++found;
// The observed value
IDValue * myValue = group->values[g];
set<IDValue>::iterator j = mf->second.begin();
bool matchField = false;
while ( j != mf->second.end() )
{
if ( *myValue == *j )
{
matchField = true;
}
++j;
}
if ( ! matchField )
return false;
}
// Did we get a look at all the match fields?
if ( found != matchTemplate.size() )
{
return false;
}
// If still here, we must match
return true;
}
void IDHelper::setJointValues( set<IDValue> & val )
{
// Make a dummy ID group, with pointers to the originals
IDGroup g;
set<IDValue>::iterator i = val.begin();
while ( i != val.end() )
{
g.values.push_back( (IDValue*)&(*i) );
++i;
}
// Use main joint-value update function
setJointValues( &g );
// We will add "jointValue" attribs to val, but no need
// to remove any items
}
void IDHelper::setJointValues( IDGroup * group )
{
// Find the joint fields and edit in values
// Similar to the above function, except here if the joint values
// are completely missing, then we need to remove the entire set
// from the ID group
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
vector<IDField*> & jo = jointOrder[j];
// Does this group contain one of these
// joint fields?
bool hasJoint = false;
map<IDField*,int> mapback;
for (int j = 0 ; j < group->values.size(); j++)
{
if ( jf.find( group->values[j]->field ) != jf.end() )
{
hasJoint = true;
mapback.insert( make_pair( group->values[j]->field , j ) );
}
}
if ( ! hasJoint )
continue;
string jointValue = "";
bool doneFirst = false;
bool jointMissing = false;
bool jointOneSeen = false;
set<IDField*>::iterator k = jf.begin();
while ( k != jf.end() )
{
map<IDField*,int>::iterator mi = mapback.find( *k );
if ( mi == mapback.end() )
{
jointMissing = true;
if ( doneFirst )
jointValue += "+.";
else
{
jointValue += ".";
doneFirst = true;
}
}
else
{
jointOneSeen = true;
if ( doneFirst )
jointValue += "+" + group->values[ mi->second ]->value;
else
{
jointValue += group->values[mi->second]->value;
doneFirst = true;
}
}
++k;
}
// Update values accordingly
// Except, remove entirely missing values
vector<bool> mask( group->values.size(), false);
set<IDField*>::iterator k2 = jf.begin();
while ( k2 != jf.end() )
{
map<IDField*,int>::iterator mi = mapback.find( *k2 );
if ( mi != mapback.end() )
{
int j = mi->second;
if ( jointMissing && ! jointOneSeen )
mask[j] = true;
else
group->values[j]->jointValue = jointValue;
}
++k2;
}
// Remove entirely missing values
if ( jointMissing && ! jointOneSeen )
{
vector<IDValue*> newValues = group->values;
group->values.clear();
for ( int i = 0 ; i < mask.size() ; i++)
{
if ( ! mask[i] )
group->values.push_back( newValues[i] );
}
}
}
return;
}
IDGroup * IDHelper::findUniqueIndividual( set<IDValue> & matchTemplate )
{
set<IDGroup *> thisGroup;
set<IDValue>::iterator k = matchTemplate.begin();
while ( k != matchTemplate.end() )
{
// Hmm need to decide how to handle: is default joint or single lookup?
// for IDValues?
map<IDValue,set<IDGroup*> >::iterator i = idmap.find( *k );
if ( i != idmap.end() )
{
set<IDGroup*>::iterator j = i->second.begin();
while ( j != i->second.end() )
{
if ( ! (*j)->resolved )
thisGroup.insert( *j );
++j;
}
}
++k;
}
if ( thisGroup.size() > 1 )
error("Internal problem: found more than one match when expecting a unique match");
return *thisGroup.begin();
}
set<IDGroup*> IDHelper::findAllIndividuals( map<IDField*,set<IDValue> > & matchTemplate )
{
set<IDGroup*> matched;
for ( int g = 0 ; g < idgroup.size(); g++ )
{
IDGroup * group = idgroup[g];
if ( matchIndividual( group , matchTemplate ) )
matched.insert( group );
}
return matched;
}
bool IDHelper::setAlias(IDField * myField, string val, int f, map<IDField*,string> & originalEquivalence)
{
bool needToStore = true;
map<IDField*,string>::iterator i = originalEquivalence.find( myField );
if ( i == originalEquivalence.end() )
{
set<string>::iterator k = myField->aliasList.find( val );
if ( k != myField->aliasList.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
myField->masterList.insert( val );
originalEquivalence.insert(make_pair( myField, val ));
}
else
{
// We need to add an equivalence value to this field?
map<string, string>::iterator k = myField->eqid.find( val );
// We only let each alias be specified once
if ( k != myField->eqid.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
k = myField->eqid.find( i->second );
if ( k != myField->eqid.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + i->second);
if ( myField->masterList.find( val ) != myField->masterList.end() )
error("Alias specified more than once in "
+ files[f].filename + " : "
+ myField->name + " " + val );
// Keep track of this alias
myField->eqid.insert( make_pair ( val , i->second ) );
myField->aliasList.insert( val );
// And now we do not need to store this particular value
// as a distinct field
needToStore = false;
}
return needToStore;
}
void IDHelper::idHelp()
{
// Turn off the "-" initiated range-delimiting (i.e. so that
// IDs can have hyphens)
par::range_delimiter = " ";
// Are we only performing a "simple match", performed without
// a dictionary being specified? If so, jump there now, creating
// the dictionary on the fly.
if ( par::idhelp_match && par::idhelp_no_dict )
{
idMatch();
return;
}
// 1. Read in dictionary, and make the fields and files
// Contains files (and can be full path) and description of each field
// {file name} {col names } : { rules }
// e.g.
// ../files/id1.txt FID IID FID1 FID2 : uniq=FID,IID uniq=FID1,IID2
// ../names/id.lst ID2 ID23 : missing=NA,---,0
// ../names/id2.lst ID3 : equiv
//
// note: for "equiv" files, assume all IDs on same line are equivalent,
// only one ID can be specified here
PP->printLOG("ID helper, with dictionary [ " + par::idhelp_dictionary + " ]\n");
checkFileExists( par::idhelp_dictionary );
ifstream DICT( par::idhelp_dictionary.c_str() , ios::in );
while ( ! DICT.eof() )
{
vector<string> tokens = tokenizeLine( DICT );
// Needs atleast three fields:
// filename, and two IDs
if ( tokens.size() == 0 )
continue;
if ( tokens.size() < 2 || tokens[1] == ":" )
error("Expecting at least 2 fields w/ 1 ID in every dictionary row\n");
IDFile d;
checkFileExists( tokens[0] );
d.filename = tokens[0];
int p = 1;
while (1)
{
if ( tokens[p] == ":" )
{
++p;
break;
}
// Is this a new field name?
IDField f;
f.name = tokens[p];
// Set to skip this field?
if ( f.name == "." )
f.null = f.attribute = true;
iField = fields.find( f );
if ( iField == fields.end() )
{
fields.insert( f );
iField = fields.find( f );
}
// Track that this field was found in this file
IDField * ip = (IDField*)&( *iField );
d.fields.push_back( ip );
++d.uniqFieldCount;
// Consider the next field in this file
if ( ++p == tokens.size() )
break;
}
bool seenJoint = false;
bool seenAttrib = false;
// Now read any rules
if ( p < tokens.size() )
{
while (1)
{
string cmd = tokens[p];
// Commands
// attrib=X,Y,Z
// joint=X,Y
// set:X=Y
// header
// missing=NA,-9
// alias-delimit=|
bool parsed = false;
if ( cmd.size() > 6 && cmd.substr(0,6) == "joint=" )
{
parsed = true;
bool seenJoint = true;
string u = cmd.substr(6);
string jName = searchAndReplace(u,","," ");
// This should contain at least two ID fields
// These fields must always then appear together in
// any file that features at least one
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
if ( ids.size() < 2 )
error("Problem with specification of : " + cmd );
set<string> t;
for (int i = 0 ; i < ids.size() ; i++)
{
t.insert(ids[i]);
}
for (int i = 0 ; i < ids.size() ; i++)
jointMap.insert( make_pair( ids[i] , t ) );
set<IDField*> jointf;
vector<IDField*> jointo;
for (int i = 0 ; i < ids.size() ; i++)
{
IDField f;
f.name = ids[i];
iField = fields.find( f );
if ( iField == fields.end() )
{
error("Could not find field " + ids[i]
+ " which is specified as joint");
}
IDField * ip = (IDField*)&( *iField );
ip->joint = true;
ip->jointName = jName;
jointf.insert( ip );
jointo.push_back( ip );
}
jointField.push_back( jointf );
jointOrder.push_back( jointo );
}
if ( cmd.size() > 7 && cmd.substr(0,7) == "attrib=" )
{
parsed = true;
seenAttrib = true;
string u = cmd.substr(7);
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
for (int i = 0 ; i < ids.size() ; i++)
attribFields.insert( ids[i] );
}
if ( cmd.size() > 8 && cmd.substr(0,8) == "missing=" )
{
parsed = true;
string u = cmd.substr(8);
NList tlist(0);
vector<string> ids = tlist.deparseStringList( u );
for (int i = 0 ; i < ids.size() ; i++)
d.missingValues.insert( ids[i] );
}
if ( cmd.size() > 4 && cmd.substr(0,4) == "set:" )
{
parsed = true;
if ( seenJoint )
error("Must specify set:X=Y before joint=X,Z");
if ( seenAttrib )
error("Must specify set:X=Y before attrib=X");
string u = cmd.substr(4);
bool okay = true;
if ( u.find("=") == string::npos )
okay = false;
else
{
string u1 = u.substr(0,u.find("="));
string u2 = u.substr(u.find("=")+1);
if ( u1.size() < 1 )
okay = false;
if ( u2.size() < 1 )
okay = false;
if ( okay )
{
d.injections.insert(make_pair(u1,u2));
IDField f;
f.name = u1;
if ( f.name == "." )
error("Cannot set field value name to .");
iField = fields.find( f );
if ( iField == fields.end() )
{
fields.insert( f );
iField = fields.find( f );
}
// Track that this field was found in this file
IDField * ip = (IDField*)&( *iField );
d.fields.push_back( ip );
++d.uniqFieldCount;
}
}
if ( ! okay )
error("Badly formed set:X=V command");
}
if( cmd == "header" || cmd == "hasHeader" )
{
parsed = true;
d.hasHeader = true;
}
// if ( cmd.size() > 10 && cmd.substr(0,10) == "delimiter=" )
// {
// string u = cmd.substr(10);
// if ( u.size() != 1 )
// error("Delimiters can only be a single character length");
// d.delimit = u;
// }
if ( cmd.size() > 16 && cmd.substr(0,16) == "alias-delimiter=" )
{
parsed = true;
string u = cmd.substr(16);
if ( u.size() != 1 )
error("Delimiters can only be a single character length");
if ( u == d.delimit )
error("Delimiter and alias delimiter cannot be the same value");
d.alias_delimit = u;
}
if ( ! parsed )
error("Could not parse the following rule in the ID dictionary: " + cmd );
if ( ++p == tokens.size() )
break;
}
}
files.push_back(d);
// Next line
}
DICT.close();
PP->printLOG("Read " + int2str( fields.size() ) + " unique fields\n");
set<IDField>::iterator i = fields.begin();
while ( i != fields.end() )
{
fieldMap.insert(make_pair( i->name , (IDField*)&(*i) ));
++i;
}
/////////////////////////////////////////////////
// Set flags for any attribute fields
if ( attribFields.size() > 0 )
{
PP->printLOG(" Attribute fields: ");
set<string>::iterator i = attribFields.begin();
while ( i != attribFields.end() )
{
if ( fieldMap.find( *i ) == fieldMap.end() )
error("Cannot find specified attribute "
+ *i
+ " -- please check your dictionary file");
// Set as an attribute field
fieldMap.find(*i)->second->attribute = true;
PP->printLOG( *i + " " );
++i;
}
PP->printLOG("\n");
}
/////////////////////////////////////////////////
// If set fields, check either joint or attrib
for ( int f = 0 ; f < files.size() ; f++ )
{
map<string,string>::iterator i = files[f].injections.begin();
while ( i != files[f].injections.end() )
{
IDField * f = fieldMap.find( i->first )->second;
if ( ! ( f->joint || f->attribute ) )
error("Any set:field=value should be an attribute or a joint field");
++i;
}
}
////////////////////////////////////////////////
// If joint fields, check always all specified
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
for ( int f = 0 ; f < files.size() ; f++ )
{
for ( int j = 0; j < files[f].fields.size(); j++)
{
if ( jointMap.find( files[f].fields[j]->name ) != jointMap.end() )
{
map<string,set<string> >::iterator i = jointMap.find( files[f].fields[j]->name );
set<string> & ss = i->second;
set<string>::iterator is = ss.begin();
while ( is != ss.end() )
{
bool okay = false;
for ( int j = 0; j < files[f].fields.size(); j++)
if ( *is == files[f].fields[j]->name )
okay =true;
if ( ! okay )
error("Need to specify all joint fields in dictionary, [" + files[f].filename + " ]");
++is;
}
}
}
}
}
if ( jointField.size() > 0 )
{
PP->printLOG(" Joint fields:");
for (int j = 0 ; j < jointField.size(); j++ )
{
set<IDField*> & jf = jointField[j];
set<IDField*>::iterator j = jf.begin();
PP->printLOG(" { ");
while ( j != jf.end() )
{
PP->printLOG( (*j)->name + " " );
++j;
}
PP->printLOG(" }");
}
PP->printLOG("\n");
}
/////////////////////////////////
// 2. Read in and index all IDs
for ( int f = 0 ; f < files.size() ; f++ )
{
IDFile * file = &files[f];
PP->printLOG("Reading [ " + files[f].filename + " ] with fields : ");
for ( int j = 0 ; j < files[f].fields.size(); j++ )
{
if (j>0 )
PP->printLOG(", ");
PP->printLOG( files[f].fields[j]->name );
}
////////////////////////////
// Find any equivalence sets
bool foundEquiv = false;
map<string, vector<int> > equiv;
map<string, map<int,int> > equivMap;
for ( int j = 0 ; j < files[f].fields.size(); j++)
{
string n = files[f].fields[j]->name;
map<string, vector<int> >::iterator i = equiv.find( n );
if ( i == equiv.end() )
{
vector<int> t;
t.push_back(j);
equiv.insert(make_pair( n , t ));
}
else
{
i->second.push_back(j);
files[f].fields[j]->equiv = true;
foundEquiv = true;
}
}
if ( foundEquiv )
{
PP->printLOG(" : ");
map<string, vector<int> >::iterator i = equiv.begin();
while ( i != equiv.end() )
{
if ( i->second.size() > 1 )
{
map<int,int> tmap;
PP->printLOG(" "+files[f].fields[i->second[0]]->name + "(");
for (int k = 0 ; k < i->second.size(); k++)
{
if ( k>0 )
{
if ( k==1 )
PP->printLOG("<-");
else
PP->printLOG(",");
tmap.insert(make_pair( i->second[k] , i->second[0] ) );
}
PP->printLOG(int2str( i->second[k]+1 ));
}
PP->printLOG(")");
equivMap.insert( make_pair( files[f].fields[i->second[0]]->name , tmap ));
}
++i;
}
}
PP->printLOG("\n");
////////////////////////////////////
// Read the raw data
ifstream ID1( files[f].filename.c_str() , ios::in );
if ( files[f].hasHeader )
{
vector<string> header = tokenizeLine( ID1 );
}
while ( !ID1.eof() )
{
vector<string> tokens = tokenizeLine( ID1 );
if ( tokens.size() == 0 )
continue;
// Insert SET:X=Y values here
map<string,string>::iterator i = files[f].injections.begin();
while ( i != files[f].injections.end() )
{
tokens.push_back( i->second );
// Note -- this won't be same order -- need to check/fix this???
++i;
}
if ( tokens.size() != file->uniqFieldCount )
{
PP->printLOG("\n\nIn [ " + file->filename
+ " ] encountered a row with the wrong number of fields\n");
PP->printLOG("Found " + int2str( tokens.size() )
+ " fields but expecting " + int2str( file->fields.size() ) + "\n");
int mx = tokens.size() > file->uniqFieldCount ? tokens.size() : file->uniqFieldCount ;
for (int j = 0 ; j < mx ; j++)
{
if ( j < file->uniqFieldCount )
PP->printLOG( " " + file->fields[j]->name + ":\t" );
else
PP->printLOG( " {?}:\t" );
if ( j < tokens.size() )
PP->printLOG( " " + tokens[j] + "\n" );
else
PP->printLOG( " {?}\n" );
}
error("Problem with [ " + file->filename + " ]\n" );
}
IDGroup * g = new IDGroup;
// Track which file this group of IDs came form
g->file = file;
// Track what the original eq-value is for this line
map<IDField*,string> originalEquivalence;
for ( int j = 0 ; j < files[f].fields.size(); j++ )
{
// Is this a missing value?
if ( files[f].missingValues.find( tokens[j] ) != files[f].missingValues.end() )
continue;
IDField * myField = files[f].fields[j];
if ( myField->null )
continue;
string val = tokens[j];
// Handle equivalence specifications (aliases)
bool needToStore = true;
if ( val.find( files[f].alias_delimit ) != string::npos )
{
// This is now an equiv. field
myField->equiv = true;
NList nl(0);
nl.setDelimiter( files[f].alias_delimit );
nl.setRangeChar(" "); // allow hyphens
vector<string> atoken = nl.deparseStringList( val );
string first = "";
for ( int i=0; i<atoken.size(); i++)
{
if ( files[f].missingValues.find( atoken[i] ) == files[f].missingValues.end() )
{
setAlias( myField, atoken[i] , f, originalEquivalence );
if ( first=="" ) first = atoken[i];
}
}
// Store the first value as the main value
if ( first != "" )
tokens[j] = first;
else
continue; // if all missing
}
else if ( myField->equiv )
needToStore = setAlias( myField, tokens[j] , f , originalEquivalence );
if ( needToStore )
{
IDValue * v = new IDValue;
v->field = myField;
v->value = tokens[j];
g->values.push_back(v);
// for pretty-printing
if ( v->value.size() + 3 > myField->width )
myField->width = v->value.size() + 3 ;
}
}
idgroup.push_back(g);
}
ID1.close();
}
/////////////////////////////////
// Done reading in the raw data
// cout << "DISPLAY LEVEL 0\n";
// for ( int g = 0 ; g < idgroup.size(); g++ )
// idgroup[g]->display();
// cout << "-------------------------------------------\n";
//////////////////////////////////////////////////////////////
// 1. Swap in preferred values for any equiv fields
for ( int g = 0 ; g < idgroup.size(); g++ )