-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePedFile.py
289 lines (211 loc) · 7.83 KB
/
generatePedFile.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
import time
import re
import sys
import argparse
def generatePed(b):
if(len(b) < 1):
raise ValueError('Error: Cannot parse empty string into boolean')
if(b=='True'):
return True
if(b=='False'):
return False
raise ValueError('Error: Cannot parse string into boolean')
def Main():
listOfArguements=[]
parser=argparse.ArgumentParser(description='This is a tool similar to vcftools written in python')
parser.add_argument('--vcf',nargs='?')
parser.add_argument('--generatePed',type=generatePed)
parser.add_argument('--out',nargs='?')
##parser.add_argument('--getTsTv',type=)
args=parser.parse_args()
return args
if __name__=='__main__':
Main()
def readTestFile(): ## Working fine
allReturned=[]
allReturned=Main()
vcfSupplied=allReturned.vcf
handle=open(vcfSupplied,'r')
lines="\n"
for line in handle:
lines+=line
handle.close()
return lines
vcfRead = readTestFile() ## a multi sample vcf
def extractLinesWithVariants(): ## Working fine
allReturned=[]
allReturned=Main()
vcfLinesToSplit=readTestFile()
linesRead = re.split("\n",vcfLinesToSplit)
return linesRead
linesToAList=extractLinesWithVariants()
print('\n')
def getOnlyVariantsFromList(): ## Working fine
OnlyVariants=[]
indexOfHeaderLine=0
linesToAList=extractLinesWithVariants()
##print("\n")
for eachLine in linesToAList:
if('#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT' in eachLine):
indexOfHeaderLine=linesToAList.index(eachLine)
for anIndex in range(indexOfHeaderLine,len(linesToAList),1):
splittingTheLinesToAList=re.split('\t',linesToAList[anIndex])
OnlyVariants.append(splittingTheLinesToAList)
return OnlyVariants
def getAllLinesBeforeChromLine(): ## Working fine
indexOfHeaderLine=0
linesToAList=extractLinesWithVariants()
##print("\n")
OnlyLines=[]
indexOfHeaderLine=0
for eachLine in linesToAList:
if('#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT' in eachLine):
indexOfHeaderLine=linesToAList.index(eachLine)
OnlyLines=linesToAList[0:indexOfHeaderLine+1]
return OnlyLines
def convertRemoveChrWordAndMakeLociInfoInt():
variantsReturned=getOnlyVariantsFromList()
variantsReturned=variantsReturned[1:len(variantsReturned)-1]
allVariants=[]
for aVariant in variantsReturned:
eachVarList=[]
chrWord=aVariant[0]
chrWordSplit=re.findall("\d+",chrWord)
intChr=int(chrWordSplit[0])
intLoci=int(aVariant[1])
eachVarList.extend([intChr,intLoci]) ## This is chr start and stop as integer
listWithOutLoci=aVariant[2:len(aVariant)] ## This is the list without chr start and stop
varListToReturn=eachVarList+listWithOutLoci
allVariants.append(varListToReturn)
return allVariants
def sortByChrLoci(listWithQualScoreFloatAndLociInt): ## not sure how to do it especially with the recursion
if(len(listWithQualScoreFloatAndLociInt) < 2):
return listWithQualScoreFloatAndLociInt
listSortedByChrAndPos=[]
mid=int(len(listWithQualScoreFloatAndLociInt)/2)
leftList=sortByChrLoci(listWithQualScoreFloatAndLociInt[:mid])
rightList=sortByChrLoci(listWithQualScoreFloatAndLociInt[mid:])
while(len(leftList) > 0) and (len(rightList) > 0):
if(leftList[0][0] >= rightList[0][0]):
if(leftList[0][1] > rightList[0][1]):
listSortedByChrAndPos.append(rightList[0])
rightList.pop(0)
else:
if(leftList[0][1] < rightList[0][1]):
listSortedByChrAndPos.append(leftList[0])
leftList.pop(0)
else:
if(leftList[0][0] <= rightList[0][0]):
listSortedByChrAndPos.append(leftList[0])
leftList.pop(0)
listSortedByChrAndPos=listSortedByChrAndPos+leftList
listSortedByChrAndPos=listSortedByChrAndPos+rightList
return listSortedByChrAndPos
def retrieveGenotypeForFormingPedAllVar(): ### keeping missing genotypes as well, working fine
allLinesBeforeChromLine=getAllLinesBeforeChromLine()
headerLine=allLinesBeforeChromLine[-1]
headerLineSplitted=re.split('\t',headerLine)
headerLinePartToAppend=headerLineSplitted[0:2]+headerLineSplitted[3:5]+headerLineSplitted[8:]
listWithQualScoreFloatAndLociInt=convertRemoveChrWordAndMakeLociInfoInt()
listSortedByChrLoci=sortByChrLoci(listWithQualScoreFloatAndLociInt)
listToReturn=[]
genotypesAllVar=[]
for eachVar in listSortedByChrLoci:
genoTypes=eachVar[8:]
variantLoci=eachVar[0:2]
variantAlleles=eachVar[3:5]
newGenotypeList=[]
for eachGenotype in genoTypes:
newGenotypeList.append(eachGenotype)
genotypeInfoPerVar=variantLoci+variantAlleles+newGenotypeList
genotypesAllVar.append(genotypeInfoPerVar)
listToReturn.append(headerLinePartToAppend)
listToReturn.extend(genotypesAllVar)
return listToReturn
def functionToFormDictOfGenotypeBySampleInPlinkStyle(oneIndv):
genotypeInfoRetrieved=retrieveGenotypeForFormingPedAllVar()
onlyVariantInfo=genotypeInfoRetrieved[1:]
onlyHeader=genotypeInfoRetrieved[0]
listOfAllGenoPerIndiv=[]
noOfVariants=0
indexOfindiv=onlyHeader.index(oneIndv)
for aVariant in onlyVariantInfo:
refAllele=aVariant[2]
altAllele=aVariant[3]
if(len(refAllele)==len(altAllele)):
noOfVariants=noOfVariants+1
genotypeOfVariant=aVariant[indexOfindiv]
listOfAllelePerGenotype=[]
if(genotypeOfVariant!='.'):
genotypeSplitted=re.split(':',genotypeOfVariant)
actualGenotype=genotypeSplitted[0]
if(actualGenotype=='0/0'):
listOfAllelePerGenotype.extend([refAllele,refAllele])
elif(actualGenotype=='0/1'):
listOfAllelePerGenotype.extend([refAllele,altAllele])
else:
if(actualGenotype=='1/1'):
listOfAllelePerGenotype.extend([altAllele,altAllele])
else:
listOfAllelePerGenotype.extend(['0','0'])
listOfAllGenoPerIndiv.append(listOfAllelePerGenotype)
return listOfAllGenoPerIndiv
def formingTheMapFile():
genotypeInfoRetrieved=retrieveGenotypeForFormingPedAllVar()
onlyVariantInfo=genotypeInfoRetrieved[1:]
mapFormatAllVariants=[]
for aVariant in onlyVariantInfo:
if(len(aVariant[2])==len(aVariant[3])):
eachLineOfMap=[]
chrNo=aVariant[0]## the chromosome
chrNoAsString=str(chrNo)
posOnChr=str(aVariant[1])
secondColOfMap="chr"+str(chrNo)+":"+posOnChr
eachLineOfMap.extend([chrNoAsString,secondColOfMap,'0',posOnChr])
mapFormatAllVariants.append(eachLineOfMap)
return mapFormatAllVariants
def functionToReturnPedForAllSample():
genotypeInfoRetrieved=retrieveGenotypeForFormingPedAllVar()
onlyHeader=genotypeInfoRetrieved[0]
allSampleId=onlyHeader[5:]
dictOfGenoForPlink={}
for aSampleId in allSampleId:
plinkGenoFormatForASample=functionToFormDictOfGenotypeBySampleInPlinkStyle(aSampleId)
dictOfGenoForPlink[aSampleId]=plinkGenoFormatForASample
return dictOfGenoForPlink
def finalPedFormatToWriteToFile():
pedDataForAllSamples= functionToReturnPedForAllSample()
listOfAllPedToWriteToFile=[]
for sampleId,genotypeInfo in pedDataForAllSamples.items():
pedLinePerSample=[]
pedLinePerSample.extend([sampleId,sampleId,'0','0','0','0'])
for eachGenoList in genotypeInfo:
pedLinePerSample.extend(eachGenoList)
listOfAllPedToWriteToFile.append(pedLinePerSample)
return listOfAllPedToWriteToFile
def writeAnyNonVCFToFile(dataToWriteToFile,choiceOfFileName,choiceOfFileExt):
lineList=[]
for anyLine in dataToWriteToFile:
anyLineAsString=[str(anyLine[i]) for i in range(0,len(anyLine))]
aLineAsStringTabbed='\t'.join(anyLineAsString)
lineList.append(aLineAsStringTabbed)
f=open('{}.{}' .format(choiceOfFileName,choiceOfFileExt),'w')
for line in lineList:
f.write(line+'\n')
f.close()
def writePedDataToFile():
allReturned=[]
allReturned=Main()
fileName=allReturned.out
pedDataToWrite= finalPedFormatToWriteToFile()
writeAnyNonVCFToFile(pedDataToWrite,fileName,'ped')
def writeTheMapToFile():
allReturned=[]
allReturned=Main()
fileName=allReturned.out
mapToWriteToFile=formingTheMapFile()
writeAnyNonVCFToFile(mapToWriteToFile,fileName,'map')
def callThePedAndMapWriteFunctions():
writePedDataToFile()
writeTheMapToFile()
callThePedAndMapWriteFunctions()