-
Notifications
You must be signed in to change notification settings - Fork 1
/
CallHap_HapCallr.py
1323 lines (1268 loc) · 58.4 KB
/
CallHap_HapCallr.py
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
#!/bin/python
# CallHap_HapCallr V. 1.01.00
#
# A program for determining full-genome haplotype frequencies in pooled DNA
# samples based on SNP calls and limited known haplotypes.
# Takes as input a pair of VCF files describing haplotype identity and SNP
# frequency, as generated by CallHap VCF_Filt
#
# Import necessary modules
import numpy as np
from argparse import ArgumentParser
import time
import sys
import random
import os
from multiprocessing import Pool
from Modules.VCF_parser import *
from Modules.CorrHaps import *
from Modules.CallHap_LeastSquares import *
from Modules.General import *
from Modules.IO import *
from Modules.parallel import *
progVersion = "V1.01.00"
def MakeHaps(inSnpSets, inOldHaps, inInitialFreqs, InitialHaps):
# Module to create new haplotypes using input SNP sets and haplotype set.
# Figure out what the less common identity for this SNP is in the current
# haplotype set
snpIDs = [inOldHaps[x][inSnpSets[0]] for x in xrange(len(inOldHaps))]
numSnps = len(inOldHaps[0])
commonCounter = [snpIDs.count(0), snpIDs.count(1)]
if commonCounter[0] > commonCounter[1]:
rareAllele=1
else:
rareAllele=0
# Figure out which haplotypes contain the less common variant
containingHaps = [True if inOldHaps[x][inSnpSets[0]] == rareAllele
else False for x in xrange(len(inOldHaps))]
if True in containingHaps: # If this SNP is in a known haplotype
# Determine which SNPs can be legally changed in each haplotype
legalSnpsByHap = ValidSnpsFromPhylogeny(inOldHaps, InitialHaps)
# Check which haplotypes the target SNP can be legally changed in
# These are the ones that could be used to create new source haplotypes
usableHaps = [True if inSnpSets[0] in legalSnpsByHap[hap] else False
for hap in xrange(len(inOldHaps))]
else: # If this SNP is not in a known haplotype
# All haplotypes can be used to create new source haplotypes.
usableHaps = [True for x in containingHaps]
# Initialize lists of possible haplotype sets
possibleFreqs = [inInitialFreqs[:]]
possibleHaps = [inOldHaps]
initialHaps = len(inOldHaps)
freqSet = 0
testStop = len(possibleFreqs)
loopCtr1 = 0
# while there are still haplotypes to try adding this SNP to
while freqSet < testStop:
loopCtr1 += 1
baseFreq = []
for freq in xrange(len(possibleFreqs[freqSet])):
if possibleFreqs[freqSet][freq] > 0 and usableHaps[freq] == True:
baseFreq.append(freq)
newFreq = 0
loopCtr2 = 0
while newFreq < len(baseFreq):
loopCtr2 += 1
if loopCtr2 > 1000:
raise Exception(
"Too many iterations at line 342 with baseFreq = %s" %
len(baseFreq)
)
if baseFreq[newFreq] > initialHaps:
if newFreq == len(baseFreq) - 1:
# Change the original frequency set and haplotypes set
possibleFreqs[freqSet].append(1)
possibleHaps[freqSet].append(
np.copy(possibleHaps[freqSet][baseFreq[newFreq]])
)
for iter1 in inSnpSets:
possibleHaps[freqSet][-1][iter1] = 1 - possibleHaps[freqSet][-1][iter1]
else:
# make a copy of the original frequency set and haplotypes
# set
possibleFreqs.append([x for x in possibleFreqs[freqSet]])
possibleHaps.append([np.copy(x)
for x in possibleHaps[freqSet]])
# change the copy
possibleFreqs[-1].append(1)
possibleHaps[-1].append(
np.copy(possibleHaps[freqSet][newFreq])
)
for iter1 in inSnpSets:
possibleHaps[-1][-1][iter1] = 1 - possibleHaps[-1][-1][iter1]
else:
if newFreq == len(baseFreq) - 1:
# Change the origional frequency set and haplotypes set
possibleFreqs[freqSet].append(1)
possibleHaps[freqSet].append(
np.copy(possibleHaps[freqSet][baseFreq[newFreq]])
)
for iter1 in inSnpSets:
possibleHaps[freqSet][-1][iter1] = 1 - possibleHaps[freqSet][-1][iter1]
if (int(possibleFreqs[freqSet][baseFreq[newFreq]]) == 0
and baseFreq[newFreq] >= InitialHaps):
possibleFreqs[freqSet].pop(baseFreq[newFreq])
possibleHaps[freqSet].pop(baseFreq[newFreq])
else:
# make a copy of the original frequency set and haplotypes
# set
possibleFreqs.append([x for x in possibleFreqs[freqSet]])
possibleHaps.append([np.copy(x)
for x in possibleHaps[freqSet]])
# change the copy
possibleFreqs[-1].append(1)
possibleHaps[-1].append(
np.copy(possibleHaps[freqSet][baseFreq[newFreq]])
)
for iter1 in inSnpSets:
possibleHaps[-1][-1][iter1] = 1 - possibleHaps[-1][-1][iter1]
if (int(possibleFreqs[freqSet][baseFreq[newFreq]]) == 0
and baseFreq[newFreq] >= InitialHaps):
possibleFreqs[freqSet].pop(baseFreq[newFreq])
possibleHaps[freqSet].pop(baseFreq[newFreq])
newFreq += 1
freqSet += 1
return(possibleHaps)
def CallHapMain(OrderNumber, o, resume=False):
print("Starting Random Order %s/%s" % (str(OrderNumber + 1),
str(o.numRand)))
# Load haplotypes
KnownHaps, KnownNames = toNP_array(o.knownHaps, "GT")
# Invert haplotypes so that ref allele is 1
KnownHaps = invertArray(KnownHaps)
# Find unique haplotypes
inHapArray, UniqueNames = UniqueHaps(KnownHaps, KnownNames)
# Count number of unique haplotypes
numHapsInitial = len(UniqueNames)
# Count number of SNPs
numSNPs = inHapArray.shape[0]
# Add "dummy" SNP to ensure haplotype frequencies sum correctly
inHapArray = ExtendHaps(inHapArray)
# Store input haplotypes in bestArray
bestArray = np.copy(inHapArray)
# Load SNPs
SnpFreqs, poolNames = toNP_array(o.inFreqs, "RF")
# Add "dummy" SNP to ensure haplotype frequencies sum correctly
SnpFreqs = ExtendHaps(SnpFreqs)
# Count number of pools present
numPools = len(poolNames)
# Count number of haplotypes again to save initial number of known
# haplotypes for later
# May not be needed in random method
numHapsInitial1 = len(UniqueNames)
# Set baseNumHapSets to keep track of source haplotype set for each created
# haplotype set
baseNumHapSets = 1
# Convert haplotypes and SNPs arrays to decimal format to prevent rounding
# errors
bestArray = npToDecNp(bestArray)
SnpFreqs = npToDecNp(SnpFreqs)
# Find base SLSq
# Save base RSS
baseSLSq = []
# Save base haplotype frequencies
baseFreqs = []
# save base residuals
baseResiduals = [[]]
# Calculate RSS for each pool
for poolIter in xrange(numPools):
tmpSol = Find_Freqs(bestArray, SnpFreqs[:,poolIter], poolSizes[poolIter])
baseSLSq.append(tmpSol[1])
baseFreqs.append(tmpSol[0])
baseResiduals[0].append(
np.array([[x] for x in list(residuals(tmpSol[0][0],bestArray,
SnpFreqs[:,poolIter],poolSizes[poolIter]))])
)
# Calculate total per SNP RSS values for all SNPs; method for deterministic
# ordering
baseSnpResids = [sum([baseResiduals[0][pool][xSnp]
for pool in xrange(numPools)]) for xSnp in xrange(numSNPs)]
# Find overall SNP frequency in SSLs; method for deterministic ordering
snpFreqsTotal = np.sum(bestArray, axis=1) < bestArray.shape[1]
# Create random SNP ordering
if o.ordered:
snpFreqsTotal = np.sum(SnpFreqs, axis=1)
snpCombins3 = [[x] for x in sorted(range(numSNPs), key = lambda x: snpFreqsTotal[x], reverse=True)]
else:
snpCombins3 = [[x] for x in range(numSNPs)]
random.shuffle(snpCombins3)
snpCombins3 = [y for y in sorted(snpCombins3,
key = lambda x: snpFreqsTotal[x[0]], reverse = True)]
#Find base average RSS value
baseRSS = sum(baseSLSq)/len(baseSLSq)
fullFreqs = [[0 for x in xrange(numHapsInitial)]]
for testIter in xrange(numHapsInitial):
for testIter2 in xrange(numPools):
if baseFreqs[testIter2][0,testIter] > 0:
fullFreqs[0][testIter] = 1
# Break up haplotypes array into a list of arrays
potHapSets = [[np.copy(bestArray[:,x]) for x in xrange(numHapsInitial)]]
numHaps = [numHapsInitial]
# AIC and RSS are used somewhat interchangeably as variable names
# in this program at the moment, and I don't have the time to clean it up
# right now. It will be cleaned up in the future
bestAIC = [baseRSS]
usedSnps = 0
iterationsStartPoint=0
targetUsedSNPs = 0
if o.resume:
print("Restarting order %s" % OrderNumber)
#try resuming this order
# if this order cannot be resumed, pass
try:
print("Opening file %s_save%s.tmp" % (o.outPrefix, OrderNumber))
restartInput = open("%s_save%s.tmp" % (o.outPrefix, OrderNumber),"rb")
line = restartInput.readline().strip()
outputList = []
if line == "Outputs":
print("Order %s does not need restarting" % str(OrderNumber + 1))
line = restartInput.readline().strip()
while "#" not in line:
linebins = line.split()
outputList.append([])
outputList[-1].append([int(x) for x in linebins[:-1]])
outputList[-1].append(float(linebins[-1]))
line = restartInput.readline().strip()
restartInput.close()
print("Outputs from order %s/%s:\n%s" % (str(OrderNumber + 1),str(o.numRand),str(outputList)))
return(outputList)
while line != "":
if line=="Order":
print("Loading SNPs for Order %s" % str(OrderNumber))
line = restartInput.readline().strip().split()
snpCombins3 = [[int(x)] for x in line]
elif line == "Iter":
print("Loading iteration number for order %s" % str(OrderNumber))
line = restartInput.readline().strip()
iterationsStartPoint= int(line)
elif line=="Used":
print("Loading used SNPs for order %s" % str(OrderNumber))
line = restartInput.readline().strip()
targetUsedSNPs = int(line)
elif line=="potHapSets":
print("Loading pot hap sets for order %s" % str(OrderNumber))
line = restartInput.readline().strip()
potHapSets = []
while "#" not in line:
print(line)
potHapSets.append([DecHapToNPHap(int(x)) for x in line.split()])
line = restartInput.readline().strip()
elif line=="fullFreqs":
print("loading fullFreqs for order %s" % str(OrderNumber))
fullFreqs = []
line = restartInput.readline().strip()
while "#" not in line:
fullFreqs.append([int(x) for x in line.split()])
line = restartInput.readline().strip()
elif line=="AIC":
print("Loading AICs for order %s" % str(OrderNumber))
line=restartInput.readline().strip()
bestAIC = []
while "#" not in line:
bestAIC.append(float(line))
line=restartInput.readline().strip()
else:
pass
line = restartInput.readline().strip()
numHaps = [len(x) for x in potHapSets]
restartInput.close()
except IOError:
print("Order %s hasn't started yet" % (str(OrderNumber + 1)))
print("SNP Order %s/%s: \n%s" % (str(OrderNumber + 1),
str(o.numRand), snpCombins3))
# Start adding SNPs
# In the case of multiple iterations:
for iteration in xrange(iterationsStartPoint, o.numIterations):
# Legacy line from when I was grouping SNPs based on correlation,
# or residual, or frequency
for combin in snpCombins3:
if usedSnps < targetUsedSNPs:
usedSnps += 1
else:
# Keep track of where in the list of SNPs I am so the user knows
# something's happening
usedSnps += 1
# Test if this SNP combination has any non-zero residuals
useCombin = False
for hapSetIter in xrange(baseNumHapSets):
for population in xrange(numPools):
if abs(round(
20*baseResiduals[hapSetIter][population][combin[0]]
)) > 0:
useCombin = True
# If this SNP combination has non-zero residuals:
if useCombin:
newPotHapSets = []
potHapSetsAIC = []
newFullFreqs = []
# Find options for adding this SNP set:
currentHapSet = 0
snpRes = []
for hapSet in potHapSets:
newPotHaps = MakeHaps(combin, copy(hapSet),
fullFreqs[0], numHapsInitial)
SLSqs = []
Freqs = []
testAICList = []
maxRSSList = []
srcHap = []
# Find the average SLSq for each pot hap set
newPotHaps2 = []
if o.ordered:
tmpSols = easy_parallizeLS(newPotHaps, o.numProcesses, SnpFreqs, poolSizes)
else:
intermediate = []
for solverIter1 in xrange(len(newPotHaps)):
intermediate.append(
easyConcat(newPotHaps[solverIter1])
)
cleanedIntermediate = [x for x in intermediate
if not x is None]
func = partial(massFindFreqs, inSnpFreqs=SnpFreqs,
p=poolSizes)
result = []
for solverIter in xrange(len(cleanedIntermediate)):
result.append(func(cleanedIntermediate[solverIter]))
tmpSols = [x for x in result if not x is None]
# Determine which solutions (and thus haplotypes) produce
# an improvement in RSS value
testAICList = [x for x in xrange(len(tmpSols))
if tmpSols[x][2] <= bestAIC[currentHapSet]]
# Keep track of the source haplotye set for these solutions
srcHap = [currentHapSet for x in xrange(len(testAICList))]
# Calculate per SNP residuals to test if improvement was
# enough to keep this SNPs solutions
newResiduals = []
changedResids = []
SnpResiduals = []
solIter = 0
for sol in tmpSols:
newFullFreqs.append(
[0 for x in xrange(len(sol[1][0][0]))]
)
for testIter in xrange(len(newFullFreqs[-1])):
for testIter2 in xrange(numPools):
if sol[1][testIter2][0,testIter] > 0:
newFullFreqs[-1][testIter] = 1
newResiduals.append(
np.array([[x]
for x in list(residuals(sol[1][testIter2][0],
np.concatenate([np.transpose(y[np.newaxis])
for y in newPotHaps[solIter]], axis=1),
SnpFreqs[:,testIter2],poolSizes[testIter2]))])
)
# Calculate per SNP RSS values
SnpResiduals.append(
[sum([newResiduals[poolIter][x]**2
for poolIter in xrange(numPools)])/numPools
for x in xrange(numSNPs)]
)
solIter += 1
snpRes1 = [SnpResiduals[x][combin[0]]
for x in xrange(len(tmpSols))]
if len(testAICList) > 0:
# Filter to only the best solutions out of all proposed
# solutions based on this haplotype set
# Sort solutions better than starting RSS by RSS value,
# from lowest to highest
testIndex = sorted(testAICList,
key=lambda x: tmpSols[x][2])
# If no best solution for this SNP exists, the best
# solution for this
if len(potHapSetsAIC) == 0:
testFreq = tmpSols[testIndex[0]][2]
# If the best RSS from this solution is worse than the
# best RSS so far proposed, use the best RSS so far
# proposed
elif tmpSols[testIndex[0]][2] >= min(potHapSetsAIC):
testFreq = min(potHapSetsAIC)
# Othrewise, use the best RSS value from this SNP
else:
testFreq = tmpSols[testIndex[0]][2]
# If this RSS value represents an improvement, sort and
# save solutions
if testFreq < bestAIC[currentHapSet]:
iter1 = 0
minAICIndex = []
continueLoop = True
# Save all solutions (and thus potential haplotype
# sets) that represent an improvement in RSS value
while (iter1 < len(testIndex) and
tmpSols[testIndex[iter1]][2] <= testFreq):
newPotHapSets.append(
copy(newPotHaps[testIndex[iter1]])
)
potHapSetsAIC.append(
tmpSols[testIndex[iter1]][2]
)
snpRes.append(snpRes1[testIndex[iter1]])
iter1 += 1
else:
minAICIndex = []
# Next haplotype set
currentHapSet += 1
# Check if the ending residual values for a SNP are too high
continueCheck = [False if snpRes[x] >= o.highResidual else True
for x in xrange(len(snpRes))]
# Sort potential haplotype sets by RSS value
bestAICIdx = sorted(range(len(newPotHapSets)),
key=lambda x: potHapSetsAIC[x])
# Filter solutions based on RSS values, keeping only the lowest
# RSS values
if len(bestAICIdx) > 0 and True in continueCheck:
bestFreq = potHapSetsAIC[bestAICIdx[0]]
potHapSets = []
bestAIC = []
iter1 = 0
minCtr = 0
newSourceHap = []
potHapSetsMaxRSS = []
while iter1 < len(bestAICIdx):
if (potHapSetsAIC[bestAICIdx[iter1]] == bestFreq and
snpRes[bestAICIdx[iter1]] < o.highResidual):
minCtr += 1
potHapSets.append(
copy(newPotHapSets[bestAICIdx[iter1]])
)
bestAIC.append(potHapSetsAIC[bestAICIdx[iter1]])
iter1 += 1
fullFreqs = newFullFreqs[:]
bestRSS = bestFreq
numHaps = [len(x) for x in potHapSets]
if o.saveFreq > 0:
if usedSnps % o.saveFreq == 0:
# Save invormation after this ordering
saveFile = open("%s_save%s.tmp" % (o.outPrefix, OrderNumber),"wb")
saveFile.write("Order\n%s\n" % "\t".join([str(x[0]) for x in snpCombins3]))
saveFile.write("Iter\n%s\n" % iteration)
saveFile.write("Used\n%s\n" % usedSnps)
saveFile.write("# Potential Haplotype Sets\n")
saveFile.write("potHapSets\n")
for potSaveIter in xrange(len(potHapSets)):
tmpOutput = []
for hapSaveIter in xrange(len(potHapSets[potSaveIter])):
tmpOutput.append(int("1"+"".join([str(int(x))
for x in potHapSets[potSaveIter][hapSaveIter]]),2))
saveFile.write("%s\n" % "\t".join([str(x) for x in tmpOutput]))
saveFile.write("# Done\n")
saveFile.write("fullFreqs\n")
for potSaveIter in xrange(len(potHapSets)):
saveFile.write("%s\n" % "\t".join([str(x) for x in fullFreqs[potSaveIter]]))
saveFile.write("# Done\n")
saveFile.write("AIC\n%s\n" % "\n".join([str(x) for x in bestAIC]))
saveFile.write("# Done")
saveFile.close()
# Filter any solutions that made it through all SNPs. to only those
# with the lowest AIC (this time, really is AIC value)
SLSqs = []
Freqs = []
finFullFreqs = []
SolutionHapSets = []
SolutionAICs = []
# Remove unused haplotypes from each potential final hap set
intermediate = []
for solverIter1 in xrange(len(potHapSets)):
intermediate.append(easyConcat(potHapSets[solverIter1]))
cleanedIntermediate = [x for x in intermediate if not x is None]
func = partial(massFindFreqs, inSnpFreqs=SnpFreqs, p=poolSizes)
result = []
for solverIter in xrange(len(cleanedIntermediate)):
result.append(func(cleanedIntermediate[solverIter]))
tmpSols = [x for x in result if not x is None]
for sol in tmpSols:
finFullFreqs.append([0 for x in xrange(len(sol[1][0][0]))])
for testIter in xrange(len(finFullFreqs[-1])):
for testIter2 in xrange(numPools):
if sol[1][testIter2][0,testIter] > 0:
finFullFreqs[-1][testIter] = 1
# Calculate AIC values for each solution
SolutionAICs = [AIC_from_RSS(tmpSols[x][2],
sum(finFullFreqs[x]), numSNPs)
for x in xrange(len(tmpSols))]
# Create solution haplotype sets with only haplotypes present in
# initial haplotypes or with frequency in pools
# Known haplotypes should be a subset of haplotypes with frequency in
# the final solution, but this is just in case they aren't
SolutionHapSets = [[np.copy(potHapSets[x][y])
for y in xrange(len(finFullFreqs[x]))
if finFullFreqs[x][y] > 0 or y < numHapsInitial ]
for x in xrange(len(tmpSols))]
# If SNPs are being removed permenantly after the final iteration:
if o.dropFinal == True and iteration == o.numIterations - 1:
# Figure out which SNPs to remove for each proposed solution
newResiduals = []
snpsToRemove = []
solIter = 0
for sol in tmpSols:
for testIter in xrange(len(newFullFreqs)):
for testIter2 in xrange(numPools):
newResiduals.append(
np.array([[x] for x in list(
residuals(sol[1][testIter2][0],
np.concatenate([np.transpose(y[np.newaxis])
for y in potHapSets[solIter]], axis=1),
SnpFreqs[:,testIter2],poolSizes[testIter2])
)])
)
SnpResiduals = [sum([newResiduals[poolIter][x]**2
for poolIter in xrange(numPools)])
for x in xrange(numSNPs)]
snpsToRemove.append([])
for snpRemovalIter in xrange(numSNPs):
if SnpResiduals[snpRemovalIter] >= o.highResidual:
snpsToRemove[-1].append(snpRemovalIter)
solIter += 1
else:
snpsToRemove = [[] for x in xrange(len(tmpSols))]
# Figure out which solution(s) has (have) the lowest AIC
AIC_test_idx = sorted(range(len(SolutionAICs)),
key = lambda x: SolutionAICs[x])
finIndex = 0
testFreq = SolutionAICs[AIC_test_idx[0]]
iter1 = 0
minAICIndex = []
continueLoop = True
# Figure out how many solutions to output
while iter1 < len(AIC_test_idx) and continueLoop:
if SolutionAICs[AIC_test_idx[iter1]] == testFreq:
finIndex += 1
else:
continueLoop = False
iter1 += 1
# Start resetting base haplotype residuals
baseResiduals = []
# Output solutions
newPotHapSets = []
bestAIC = []
if iteration == o.numIterations - 1:
outputList = []
for outputIdx in xrange(finIndex):
if iteration == o.numIterations - 1:
outputList.append([])
# Create final haplotypes array
finSolution = np.concatenate(
[SolutionHapSets[
AIC_test_idx[outputIdx]][x][np.newaxis].transpose()
for x in xrange(len(
SolutionHapSets[AIC_test_idx[outputIdx]]
))]
, axis=1
)
# Remove any SNPs that need removing
finSolution = np.delete(finSolution, snpsToRemove[outputIdx], 0)
# Find (or make) haplotype names
myHapNames = []
newHapNumber = 1
for haplotypeIter in xrange(finSolution.shape[1]):
if haplotypeIter >= len(UniqueNames):
# For new haplotypes, build a new haplotype name, keeping
# track of iteration and new haplotype number
myHapNames.append(
"NewHap_%s.%s" % (str(iteration).zfill(2),
str(newHapNumber).zfill(2)))
newHapNumber += 1
else:
# For known haplotypes, use the original haplotype name
myHapNames.append(UniqueNames[haplotypeIter])
# Redo uniqueness of haplotypes in case removing a SNP merged two
# haplotypes
finSolution, finNames = UniqueHaps(finSolution, myHapNames)
# remove SNPs from SNP frequencies
finSNPs = np.delete(SnpFreqs,snpsToRemove[outputIdx],0)
# Create decimal haplotype identifiers
myDecHaps = []
for haplotypeIter in xrange(finSolution.shape[1]):
myDecHaps.append(int("1"+"".join([str(int(x))
for x in finSolution[:, haplotypeIter]]),2))
if iteration == o.numIterations - 1:
outputList[-1].append(myDecHaps)
SLSqs = []
Freqs = []
predSnpFreqs = []
newResiduals = []
for poolIter in xrange(numPools):
tmpSol = Find_Freqs(finSolution, finSNPs[:,poolIter],
poolSizes[poolIter])
SLSqs.append(tmpSol[1])
Freqs.append(tmpSol[0])
# Calculate residuals for this pool
newResiduals.append(
np.array([[x] for x in list(residuals(tmpSol[0][0],
finSolution,
finSNPs[:,poolIter],
poolSizes[poolIter]))])
)
# Calculate predicted SNP frequencies
predSnpFreqs = np.sum(finSolution * tmpSol[0][0],
axis = 1)/poolSizes[poolIter]
if iteration == o.numIterations - 1:
outputList[-1].append(average(SLSqs))
baseResiduals.append(newResiduals[:])
# Calculate per SNP RSS values for VCF output
SnpResiduals = [float(sum([newResiduals[poolIter][x]**2
for poolIter in xrange(numPools)])[0])
for x in xrange(numSNPs-len(snpsToRemove[outputIdx]))]
bestAIC.append(sum(SLSqs)/len(SLSqs))
# Save this haplotype set for the next iteration
newPotHapSets.append([np.copy(finSolution[:,x])
for x in xrange(finSolution.shape[1])])
# Setup for next iteration
usedSnps = 0
numHapsInitial = len(myHapNames) # may need some fixing
UniqueNames = myHapNames[:] # may need some fixing
numHaps = [numHapsInitial for x in xrange(len(newPotHapSets))]
outPrefix = "%s_Iteration%s" % (o.outPrefix, iteration + 2)
potHapSets = newPotHapSets[:]
fullFreqs = [[1 for x in xrange(len(potHapSets[y]))]
for y in xrange(len(potHapSets))]
# Go on to the next iteration
if iteration == o.numIterations - 1:
print("Finished Random Order %s/%s" % (str(OrderNumber + 1),
str(o.numRand)))
saveFile = open("%s_save%s.tmp" % (o.outPrefix, OrderNumber),"wb")
saveFile.write("Outputs\n")
for xIter in xrange(len(outputList)):
saveFile.write("%s\t%s\n" % ("\t".join([str(x) for x in outputList[xIter][0]]), str(outputList[xIter][1])))
saveFile.write("# Done")
saveFile.close()
print("Outputs from order %s/%s:\n%s" % (str(OrderNumber + 1),str(o.numRand),str(outputList)))
return(outputList)
if __name__ == "__main__":
# Load options
parser = ArgumentParser()
parser.add_argument(
'-i','--inputHaps',
action="store",
dest="knownHaps",
help = "A VCF-formatted file containing the known haplotypes encoded \
in the GT field. GT must be present in the FORMAT field, and \
ploidy must be 1. ",
required=True
)
parser.add_argument(
"-p", "--poolSizes",
action="store",
dest="poolSizesFile",
help="A file detailing the number of individuals in each pooled library. ",
required=True
)
parser.add_argument(
'-f','--inputFreqs',
action="store",
dest="inFreqs",
help="A VCF-formatted file containing the input pool frequencies \
encoded in the RF field. RF must be present in the FORMAT \
field. ",
required=True
)
parser.add_argument(
'-o','--outPrefix',
action="store",
dest="outPrefix",
required=True,
help="A prefix for output file names. "
)
parser.add_argument(
"-v", "--version",
action="store_true",
dest="v",
help="Displays the version number and exits."
)
parser.add_argument(
'-t', '--processes',
type=int,
action="store",
dest="numProcesses",
default=None,
help="The number of processes to use. Should not be more than the \
number of cores on your CPU. Defaults to using the number of \
cores on your CPU. "
)
parser.add_argument(
'-l','--numIterations',
type=int,
action="store",
dest="numIterations",
default=1,
help="Number of iterations to run within each random ordering."
)
parser.add_argument(
'-r','--highResidual',
type=float,
action="store",
dest="highResidual",
default=100,
help="Cutoff value for delaying processing of a SNP until after all \
other SNPs have been processed"
)
parser.add_argument(
'--dropFinal',
action="store_true",
dest="dropFinal",
help="If after delaying processing on a SNP, the solution isn't \
improved by keeping it, drop the SNP. If absent, the SNP will \
be processed as normal at the end. "
)
parser.add_argument(
'--genpop',
action="store_true",
dest="genpopOutput",
help="Output a genpop file of the resulting haplotype frequencies. "
)
parser.add_argument(
'--structure',
action="store_true",
dest="strOutput",
help="Output a Structure formatted file of the resulting haplotype \
frequencies. "
)
parser.add_argument(
'--numRandom',
type=int,
action="store",
dest="numRand",
help="The number of random orders to use for haplotype creation. \
More orders will yield more accurate results, but will also \
take longer. ",
default=1
)
parser.add_argument(
'--numTopRSS',
type=int,
action="store",
dest="topNum",
default=3,
help="The number of top RSS values you want to output files for. \
Increasing the size of this number may lead to a large number of \
outputs. "
)
parser.add_argument(
'--noSearch',
action="store_true",
dest="findHaps",
help="Use if you don't want to find new haplotypes."
)
parser.add_argument(
'--restart',
action="store_true",
dest="resume",
help="Restart the program from last saved points."
)
parser.add_argument(
'--saveFrequency',
action="store",
type=int,
dest="saveFreq",
help="how often to save; default is not to save",
default=0
)
parser.add_argument(
'--keepTemps',
action="store_true",
dest="keepTmp",
help="Do not delete temporary files after finishing"
)
parser.add_argument('--deterministic', '-d',
dest="ordered",
action="store_true",
help="Use deterministic SNP ordering. Ignores --numRandom. ")
o = parser.parse_args()
if o.ordered:
o.numRand = 1
# version output
if o.v:
print(progVersion)
exit()
# Print initialization text
print("Running CallHap on %s at %s:" % (time.strftime("%d/%m/%Y"),
time.strftime("%H:%M:%S")))
CommandStr = "python CallHap_VCF_Filt.py %s" % " ".join(sys.argv[1:])
print("Command = %s" % CommandStr)
# Generate poolSize related numbers:
poolSizes = []
inPoolSizes = open(o.poolSizesFile,"rb")
for line in inPoolSizes:
poolSizes.append(int(line.strip().split()[1]))
inPoolSizes.close()
# Set initial output prefix
outPrefix = "%s" % (o.outPrefix)
if o.findHaps:
print("Outputing initial solution")
print("Loading haplotypes")
# Load haplotypes
KnownHaps, KnownNames = toNP_array(o.knownHaps, "GT")
# Invert haplotypes so that ref allele is 1
KnownHaps = invertArray(KnownHaps)
# Find unique haplotypes
inHapArray, UniqueNames = UniqueHaps(KnownHaps, KnownNames)
# Count number of unique haplotypes
numHapsInitial = len(UniqueNames)
# Count number of SNPs
numSNPs = inHapArray.shape[0]
# Add "dummy" SNP to ensure haplotype frequencies sum correctly
inHapArray = ExtendHaps(inHapArray)
# Store input haplotypes in bestArray
bestArray = np.copy(inHapArray)
print("Loading Frequencies")
# Load SNPs
SnpFreqs, poolNames = toNP_array(o.inFreqs, "RF")
# Add "dummy" SNP to ensure haplotype frequencies sum correctly
SnpFreqs = ExtendHaps(SnpFreqs)
# Count number of pools present
numPools = len(poolNames)
# Count number of haplotypes again to save initial number of known
# haplotypes for later
# May not be needed in random method
numHapsInitial1 = len(UniqueNames)
# Set baseNumHapSets to keep track of source haplotype set for each created
# haplotype set
baseNumHapSets = 1
# Convert haplotypes and SNPs arrays to decimal format to prevent rounding
# errors
bestArray = npToDecNp(bestArray)
SnpFreqs = npToDecNp(SnpFreqs)
# Find base SLSq
# Save base RSS
baseSLSq = []
# Save base haplotype frequencies
baseFreqs = []
# save base residuals
baseResiduals = [[]]
# Calculate RSS for each pool
for poolIter in xrange(numPools):
tmpSol = Find_Freqs(bestArray, SnpFreqs[:,poolIter], poolSizes[poolIter])
baseSLSq.append(tmpSol[1])
baseFreqs.append(tmpSol[0])
baseResiduals[0].append(
np.array([[x] for x in list(residuals(tmpSol[0][0],bestArray,
SnpFreqs[:,poolIter],poolSizes[poolIter]))])
)
NexusWriter(KnownNames, KnownHaps, numSNPs, o.outPrefix,
"INITIAL", o.knownHaps)
NexusWriter(UniqueNames, inHapArray, numSNPs, o.outPrefix,
"Unique", o.knownHaps)
myDecHaps = []
for haplotypeIter in xrange(bestArray.shape[1]):
myDecHaps.append(int("1"+"".join([str(int(x))
for x in bestArray[:, haplotypeIter]]),2))
# Create final haplotypes array
finSolution = bestArray
# Find (or make) haplotype names
myHapNames = UniqueNames
print("Outputing base solution ")
# If requested, generate a structure formatted file
if o.strOutput:
outFile = open("%s_base.str" % (outPrefix), 'wb')
# Generate the haplotype frequencies file
outFile2 = open("%s_base_freqs.csv" % (outPrefix), 'wb')
outFile2.write("Population,")
# Create decimal haplotype identifiers
# Finish writing first line of haplotype frequencies file
outFile2.write(",".join(myHapNames))
outFile2.write(",RSS")
# Write decimal names of haplotypes
outFile2.write(
"\n,%s" % ",".join([str(x) for x in myDecHaps])
)
# Create genpop output, if requested
if o.genpopOutput:
genpopOut = open("%s_base.genpop" % (outPrefix), 'wb')
genpopOut.write(
",%s" % (",".join(["cp." + str(x)
for x in myDecHaps]))
)
SLSqs = []
Freqs = []
predSnpFreqs = []
# Create regression output
regressionOutput = open(
"%s_base_Regression.csv" % (outPrefix), 'wb'
)
regressionOutput.write(
"Pool,SNP,Observed Frequency,Predicted Frequency\n"
)
# Create predicted frequencies VCF output
tmpVCF = vcfReader(o.inFreqs)
output3 = vcfWriter(
"%s_base_PredFreqs.vcf" % (outPrefix),
source="CallHaps_HapCallr_%s" % progVersion,
commandLine=CommandStr,
baseHead=tmpVCF.headInfo,
FormatBlock=tmpVCF.headInfo["FORMAT"])
output3.writeHeader(poolNames)
output3.setFormat("RF")
output3.importLinesInfo(
tmpVCF.getData("chrom", lineTarget="a"),
tmpVCF.getData("pos", lineTarget="a"),
tmpVCF.getData("ref", lineTarget="a"),
tmpVCF.getData("alt", lineTarget="a"),
tmpVCF.getData("qual", lineTarget="a")
)
newResiduals = []
print("Finding haplotype frequencies...")
for poolIter in xrange(numPools):
tmpSol = Find_Freqs(bestArray, SnpFreqs[:,poolIter], poolSizes[poolIter])
SLSqs.append(tmpSol[1])
Freqs.append(tmpSol[0])
# Write haplotype frequencies and RSS values for this pool
outFile2.write(
"\n%s,%s" % (poolNames[poolIter],
",".join([str(x) for x in tmpSol[0][0]]))
)
outFile2.write(",%s" % tmpSol[1])
# Write genpop file text for this pool, if requested
if o.genpopOutput:
genpopOut.write(
"\n%s,%s" % (poolNames[poolIter],
",".join([str(x) for x in tmpSol[0][0]]))
)
# Write structure file text for this pool, if requested
if o.strOutput:
outputProt(UniqueNames, tmpSol[0], finSolution, poolSizes[poolIter],
poolNames, poolIter, outFile)
# Calculate residuals for this pool
newResiduals.append(
np.array([[x] for x in list(residuals(tmpSol[0][0],
finSolution,
SnpFreqs[:,poolIter],
poolSizes[poolIter]))])
)
# Calculate predicted SNP frequencies
predSnpFreqs = np.sum(
finSolution * tmpSol[0][0], axis = 1
)/poolSizes[poolIter]
#print("##DEBUG")
# Write regression file lines for this pool
regOutLines = zip(
[poolNames[poolIter]
for x in xrange(len(predSnpFreqs))],
[str(y) for y in xrange(len(predSnpFreqs))],
[str(z) for z in list(SnpFreqs[:,poolIter])],
[str(w) for w in list(predSnpFreqs)]
)
regressionOutput.write(
"\n".join([",".join(regOutLines[x])
for x in xrange(len(regOutLines))])
)
regressionOutput.write("\n") # add a new line between pools
# Add predicted SNP frequencies to VCF output
output3.importSampleValues(list(predSnpFreqs), poolNames[poolIter])
# Calculate per SNP RSS values for VCF output
SnpResiduals = [float(sum([newResiduals[poolIter][x]**2
for poolIter in xrange(numPools)])[0])
for x in xrange(numSNPs)]
output3.importInfo("RSS",SnpResiduals)
output3.writeSamples()
# Close output files
output3.close()
regressionOutput.close()
outFile2.close()
if o.strOutput: