-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
397 lines (311 loc) · 13.3 KB
/
main.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
import argparse
import networkx as nx
import random as rd
import pandas as pd
import sys
class city:
def __init__(self, numberOfGeneration):
self.generations = {}
self.familyList = {}
self.famID = 0
self.numberOfGeneration = numberOfGeneration
for gen in range(numberOfGeneration):
self.generations[gen] = {}
def changeParentsToGetHalf(self, generation):
keys = self.generations[generation].keys()
first = min(keys)
last = max(keys)
random = rd.randint(0, 1)
toChange = "m"
if random == 1:
toChange = "f"
family1 = rd.randint(first, last)
while len(self.generations[generation][family1].children) == 0:
family1 = rd.randint(first, last)
indToChange1 = self.generations[generation][family1].father
if toChange == "m":
indToChange1 = self.generations[generation][family1].mother
family2 = family1
while(family1 == family2):
family2 = rd.randint(first, last)
indToChange2 = self.generations[generation][family2].mother
if toChange == "m":
indToChange2 = self.generations[generation][family2].father
if(self.areSiblings(indToChange1, indToChange2, generation)):
family2 = family1
else:
if len(self.generations[generation][family2].children) == 0:
family2 = family1
#print(f"I found two individuals non related to create half brothers: {indToChange1} and {indToChange2}")
if(toChange == "m"):
removed = self.generations[generation][family2].changeMother(indToChange1)
else:
removed = self.generations[generation][family2].changeFather(indToChange1)
return (len(self.generations[generation][family2].children)+len(self.generations[generation][family1].children))
def changeParent(self, ind, generation, role):
oldFamily = -1
keys = self.generations[generation].keys()
first = min(keys)
last = max(keys)
for family in self.generations[generation]:
mother, father = self.generations[generation][family].getParents()
#======================#
toCompare = mother
if(role == "f"):
toCompare = father
# ======================#
if toCompare == ind:
oldFamily = family
#print(f"My old family is {oldFamily}")
toChange = oldFamily
while (toChange == oldFamily):
toChange = rd.randint(first, last)
#print(f"Changing the {ind} from family {oldFamily} to {toChange}")
if(oldFamily == -1):
getchar()
if(role == "f"):
toChangeInd = self.generations[generation][toChange].changeFather(ind)
toChangeInd = self.generations[generation][oldFamily].changeFather(toChangeInd)
if (role == "m"):
toChangeInd = self.generations[generation][toChange].changeMother(ind)
toChangeInd = self.generations[generation][oldFamily].changeMother(toChangeInd)
#print(f"=================== Saindo ({generation}) ======================")
def getFamiliesForGeneration(self, generation):
return list(self.generations[generation].keys())
def addFamily(self, male, female, generation):
id = self.famID
self.famID = self.famID + 1
self.generations[generation][id] = family(male, female)
self.familyList[id] = generation
def setChildToFamily(self, generation, famID, child):
self.generations[generation][famID].addChildToFamily(child)
def printCity(self):
for gen in range(len(self.generations)):
print(f" =========================== Generation {gen} ==========================")
for id in self.generations[gen]:
print(f"\tFamily {id}: ")
self.generations[gen][id].printFamily()
def areSiblings(self, ind1, ind2, generation):
for family in self.generations[generation]:
if self.generations[generation][family].areSiblings(ind1, ind2):
return True
return False
def getParents(self, generation):
mothersList = []
fathersList = []
for family in self.generations[generation]:
mother, father = self.generations[generation][family].getParents()
mothersList.append(mother)
fathersList.append(father)
return mothersList, fathersList
class family:
def __init__(self, father, mother):
#print(f"Creating a family with {father} and {mother}")
self.father = father
self.mother = mother
self.children = []
def changeFather(self, ind):
toReturn = self.father
self.father = ind
return toReturn
def changeMother(self, ind):
toReturn = self.mother
self.mother = ind
return toReturn
def addChildToFamily(self, child):
self.children.append(child)
def printFamily(self):
print(f"\t\tMother : {self.mother} \t Father : {self.father}")
print("\t\t\t", end="")
for child in self.children:
print(f"{child} ", end= "")
print("\n*********************************")
def getParents(self):
return self.mother, self.father
def areSiblings(self, ind1, ind2):
if ind1 in self.children and ind2 in self.children:
return True
return False
def mergeGenealogies(inputFile, myCity):
numberOfTries = 50
for generation in range(len(myCity.generations)-1):
actual = generation
next = generation+1
previous = generation - 1
motherList, fatherList = myCity.getParents(next)
siblings = True
while siblings and numberOfTries > 0:
siblings = False
for i in range(len(motherList)):
if(myCity.areSiblings(motherList[i], fatherList[i], actual)):
siblings = True
random = rd.randint(0,1)
if random == 1:
myCity.changeParent(fatherList[i], next, "f")
else:
myCity.changeParent(motherList[i], next, "m")
numberOfTries = numberOfTries - 1
if numberOfTries <= 0:
return True
#Now, half brothers
if generation != 0:
numberOfHalf = inputFile[generation]['half'] * (inputFile[generation]['male']+inputFile[generation]['female'])
if int(numberOfHalf) > 0:
halfBrothers = 0
while halfBrothers < numberOfHalf:
created = myCity.changeParentsToGetHalf(previous)
halfBrothers = halfBrothers + created
numberOfTries = 50
return False
def getchar():
c = sys.stdin.read(1)
def createCouples(myCity, maleList, femaleList, generation):
maxCouple = min(len(maleList), len(femaleList))
for couple in range(maxCouple):
maleIndex = rd.randint(0, len(maleList)-1)
femaleIndex = rd.randint(0, len(femaleList)-1)
male = maleList.pop(maleIndex)
female = femaleList.pop(femaleIndex)
myCity.addFamily(male, female, generation)
return myCity
def giveChildrenToCouples(myCity, childrenList, unrelatedProportion, generation):
numberOfUnrelated = int(len(childrenList)*unrelatedProportion)
#print(f"{len(childrenList)}*{unrelatedProportion}")
#print(f"We will have {numberOfUnrelated} unrelateds")
for unrelated in range(numberOfUnrelated):
unrelatedIndex = rd.randint(0,len(childrenList)-1)
removed = childrenList.pop(unrelatedIndex)
#print(f"The {removed} will be unrelated")
families = myCity.getFamiliesForGeneration(generation)
for child in childrenList:
familyIndex = unrelatedIndex = rd.randint(0,len(families)-1)
myCity.setChildToFamily(generation, families[familyIndex], child)
return(myCity)
def getList (individuals, type, generation):
if type != "all":
toReturn = individuals.query(f"Sex == {type} and Generation == {generation}")["ID"].to_list()
else:
toReturn = individuals.query(f"Generation == {generation}")["ID"].to_list()
return toReturn
def isValid(path, individuals):
valid = True
first = path[0]
genFirst = individuals.iloc[first]["Generation"]
genNext = individuals.iloc[path[1]]["Generation"]
# setting descendant
if genFirst < genNext:
genNow = genNext
for i in range(2, len(path)):
genNext = individuals.iloc[path[i]]["Generation"]
if genNow > genNext:
valid = False
genNow = genNext
# setting ascendant
else:
up = True
genNow = genNext
for i in range(2, len(path)):
genNext = individuals.iloc[path[i]]["Generation"]
if genNow > genNext and not up:
valid = False
elif genNow < genNext and up:
up = False
genNow = genNext
return valid
def createNetwork(city, individuals, outputFile):
file = open(outputFile, 'w')
N = nx.Graph()
N.add_nodes_from(individuals.ID)
for generation in city.generations:
for family in city.generations[generation]:
father = city.generations[generation][family].father
mother = city.generations[generation][family].mother
for children in city.generations[generation][family].children:
N.add_edge(father, children)
N.add_edge(mother, children)
for source in N.nodes():
for target in N.nodes():
validPath = []
if source != target:
#print(f"{source} and {target}")
try:
allPaths = nx.all_shortest_paths(N, source, target)
for path in allPaths:
if isValid(path, individuals):
validPath.append(path)
except:
pass
sumKinship = 0
#print(validPath)
if(validPath):
for path in validPath:
kinship = 1/(2 ** len(path))
sumKinship = sumKinship + kinship
file.write(f"{path[0]}\t{path[-1]}\t{sumKinship}\n")
file.close()
def createSubGenealogies(inputFile, myCity):
individuals = pd.DataFrame(columns=['ID', 'Sex', 'Generation'])
id = 0
print(len(inputFile))
for generation in range(0,len(inputFile)):
maleList = []
femaleList =[]
for ind in range(0, inputFile[generation]['male']):
temp = pd.DataFrame([[id, 1, generation]], columns=['ID', 'Sex', 'Generation'])
individuals = pd.concat([individuals, temp])
id = id+1
for ind in range(0, inputFile[generation]['female']):
temp = pd.DataFrame([[id, 2, generation]], columns=['ID', 'Sex', 'Generation'])
individuals = pd.concat([individuals, temp])
id = id+1
for generation in range(0, len(inputFile) - 1):
maleList = getList(individuals, 1, generation)
femaleList = getList(individuals, 2, generation)
childrenList = getList(individuals, 'all', generation+1)
myCity = createCouples(myCity, maleList, femaleList, generation)
myCity = giveChildrenToCouples(myCity, childrenList, inputFile[generation+1]['unrelated'], generation)
#myCity.printCity()
return myCity, individuals
#input
#men #woman %unrelated %half
def readInputFile(inputFile):
file = open(inputFile, 'r')
gen = 0
inputData = {}
for line in file:
splitted = line.split('\t')
inputData[gen] = {}
inputData[gen]["male"] = int(splitted[0])
inputData[gen]["female"] = int(splitted[1])
inputData[gen]["unrelated"] = float(splitted[2])
inputData[gen]["half"] = float(splitted[3])
gen = gen+1
return inputData, gen
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='3A script: Automated Ancestry Analysis Script')
required = parser.add_argument_group("Required arguments")
required.add_argument('-i', '--input', help='Name of the input file', required=True)
required.add_argument('-o', '--output', help='Name fo the output file',
required=True)
args = parser.parse_args()
inputFile, gen = readInputFile (args.input)
outputFile = args.output
loop = True
numberOfTries = 0
while loop and numberOfTries < 50:
loop = False
print(f"Creating city ({numberOfTries})")
myCity = city(gen)
print(f"Creating the sub-genealogies ({numberOfTries})")
myCity, individuals = createSubGenealogies(inputFile, myCity)
print(f"Merging the sub-genealogies ({numberOfTries})")
loop = mergeGenealogies(inputFile, myCity)
if(loop):
numberOfTries = numberOfTries + 1
if numberOfTries == 50:
print("I tried 50 times to get a valid genealogy. It was not possible, please change your input file and try again")
else:
myCity.printCity()
N = createNetwork(myCity, individuals, outputFile)
#print(f"Sai do loop com {numberOfTries} tentativas")
#myCity.printCity()