forked from johnandrea/ancestry-matches-to-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-matches-to-csv.py
201 lines (162 loc) · 6.75 KB
/
extract-matches-to-csv.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
import sys
import re
import glob
import argparse
'''
Ancestry DNA shared matches to a csv file, intended to create a matrix of matches to each other.
For your own, and from your matches "shared matches" pages, use browser to
"save page as text".
Name each one as .txt and place all together with this program.
Options:
--out-file default matches.csv
--skip-id default true
--id-with-name default false
--skip-header default false
--min-cm default 22
Assumptions: English pages, names contain only ascii
Needs: Python 3.6+
This code is released under the MIT License:
https://opensource.org/licenses/MIT
Copyright (c) 2022 John A. Andrea
v2.0
No support provided.
'''
def get_program_options():
results = dict()
# defaults
results['out-file'] = 'matches.csv'
results['min-cm'] = 22
results['id-with-name'] = False
# with names flipped into 'positive' verbs
results['add-id'] = True
results['add-header'] = True
arg_help = 'Convert Ancestry matches output to CSV list.'
parser = argparse.ArgumentParser( description=arg_help )
arg_help = 'Do not include a header in the output file.'
arg_default = not results['add-header']
parser.add_argument( '--skip-header', default=arg_default, action='store_true', help=arg_help )
arg_help = 'Do not include account unique ids in the output file.'
arg_default = not results['add-id']
parser.add_argument( '--skip-id', default=arg_default, action='store_true', help=arg_help )
arg_help = 'Put id in name column. Otherwise in a separate column.'
arg_default = results['id-with-name']
parser.add_argument( '--id-with-name', default=arg_default, action='store_true', help=arg_help )
arg_help = 'Minimum cM match to consider.'
arg_help += ' Default is ' + str(results['min-cm'])
arg_default = int(results['min-cm'])
parser.add_argument( '--min-cm', default=arg_default, type=int, help=arg_help )
arg_help = 'Output file. Default is ' + results['out-file']
arg_default = results['out-file']
parser.add_argument( '--out-file', default=arg_default, type=argparse.FileType('w'), help=arg_help )
args = parser.parse_args()
results['out-file'] = args.out_file.name
results['min-cm'] = args.min_cm
results['id-with-name'] = args.id_with_name
results['add-id'] = not args.skip_id
results['add-header'] = not args.skip_header
return results
def get_owner_name( s, pattern1, pattern2 ):
result = None
m = pattern1.match( s )
if m:
result = m.group(1)
else:
m = pattern2.match( s )
if m:
result = m.group(1)
return result
def get_url( s, line_pattern ):
ids = []
m = line_pattern.match( s )
if m:
ids.append( m.group(1) )
ids.append( m.group(2) )
return ids
def get_cm( s, line_pattern ):
result = None
m = line_pattern.match( s.lower() )
if m:
# also remove the hundreds separator (,)
result = m.group(1).replace( ',', '' )
return result
def escape_quote( s ):
return str(s).replace( '"', '\"').replace( "'", "\'" )
def output( one, two, three, four, five, f ):
# as csv
out = '"' + one + '"'
out += ',"' + two + '"'
out += ',"' + three + '"'
if four:
out += ',"' + four + '","' + five + '"'
print( out, file=f )
def is_int( s ):
try:
x = int( s )
return True
except ValueError:
return False
options = get_program_options()
name_pattern1 = re.compile( r'^(.*)\'s DNA Matches$' ) # account owner
name_pattern2 = re.compile( r'^You and (.*)$' ) # matches of account owner
url_pattern = re.compile( r'^.*discoveryui-matches/compare/([A-Za-z0-9-]*)/with/([A-Za-z0-9-]*)>' )
cm_pattern = re.compile( r'^(.*) cm | ' )
not_names = [] # must be lower case
not_names.append( 'view match' )
not_names.append( 'common ancestor' )
not_names.append( 'do you recognize them?' )
with open( options['out-file'], 'w' ) as outf:
if options['add-header']:
header = '"name 1","name 2","cM"'
if options['add-id'] and not options['id-with-name']:
header += ',"id 1","id 2"'
print( header, file=outf )
# these are the case labels for the loop over lines
first_step = 'owner'
person_url = 'person url'
person_name = 'person name'
cm_tag = 'cm'
for filename in glob.glob( '*.txt' ):
#print( filename, file=sys.stderr )
owner_name = None
match_name = None
url_ids = []
look_for = first_step
with open( filename ) as inf:
for line in inf:
line = line.strip()
if line:
if look_for == first_step:
name = get_owner_name( line, name_pattern1, name_pattern2 )
if name:
owner_name = escape_quote( name )
look_for = person_url
elif look_for == person_url:
url_ids = get_url( line, url_pattern )
if url_ids:
look_for = person_name
elif look_for == person_name:
# this will be the next line after the url
if line.lower() in not_names:
# this is a clickable item, keep looking for the person start line
look_for = person_url
else:
match_name = line
look_for = cm_tag
elif look_for == cm_tag:
cm = get_cm( line, cm_pattern )
if cm:
if is_int( cm ) and int(cm) >= options['min-cm']:
name1 = owner_name
name2 = escape_quote(match_name)
if options['add-id']:
if options['id-with-name']:
output( name1 + '/' + url_ids[0], name2 + '/' + url_ids[1], str(cm), None, None, outf )
else:
output( name1, name2, str(cm), url_ids[0], url_ids[1], outf )
else:
output( name1, name2, str(cm), None, None, outf )
look_for = person_url
if not owner_name:
print( filename, ':reached and of file without finding owner', file=sys.stderr )
if look_for != person_url:
print( filename, 'didnt complete search', look_for, file=sys.stderr )