Skip to content

Instantly share code, notes, and snippets.

@CholoTook
Created June 17, 2021 11:40
Show Gist options
  • Save CholoTook/070896cca14f1aa6889c2c4df0fd139f to your computer and use it in GitHub Desktop.
Save CholoTook/070896cca14f1aa6889c2c4df0fd139f to your computer and use it in GitHub Desktop.
import pathlib
import csv
from collections import namedtuple
Marker = namedtuple('Marker', 'name, snp, chr, pos')
path = pathlib.Path('Data/PlinkFormat/Samples')
coord_file = 'Data/CanineHD_B.csv'
def get_coord_map(coord_file):
coord_map = dict()
with open(coord_file) as cf:
csv_reader = csv.reader(cf, delimiter=',')
col_names = []
data_section = False
for row in csv_reader:
if row[0] == '[Assay]':
data_section = True
continue
if data_section:
if len(col_names) == 0:
col_names = row
continue
if row[0] == '[Controls]':
data_section = False
break
# Trying to be explicit, probably ends up obtuse
row_dict = {
col_names[j]: row[j] for j in range(len(row))
}
coord_map[row_dict['Name']] = Marker(
name=row_dict['Name'],
snp=row_dict['SNP'],
chr=row_dict['Chr'],
pos=row_dict['MapInfo']
)
return coord_map
coord_map = get_coord_map(coord_file)
print(f"Got {len(coord_map)} markers from '{coord_file}'")
bim_cols = ['chr', 'name', 'idk', 'pos', 'a0', 'a1']
for f in path.glob('*.bim'):
print(f)
with open(f) as fp:
csv_reader = csv.reader(fp, delimiter='\t')
for row in csv_reader:
# Trying to be explicit, probably ends up obtuse
row_dict = {
bim_cols[j]: row[j] for j in range(len(row))
}
try:
coord_map[row_dict['name']]
except KeyError:
print(f"Oh drat... {row_dict['name']}")
# break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment