-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTIGARutils.py
1235 lines (958 loc) · 34.4 KB
/
TIGARutils.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
#!/usr/bin/env python
#########################################################
import functools
import operator
import os
import re
import subprocess
import sys
import traceback
from io import StringIO
from itertools import groupby
import pandas as pd
import numpy as np
#########################################################
## FUNCTIONS:
# error_handler
# calc_maf
# call_tabix
# call_tabix_header
# format_elapsed_time
# get_header
# exp_cols_dtype
# genofile_cols_dtype
# weight_cols_dtype
# zscore_cols_dtype
# gwas_cols_dtype
# MCOV_cols_dtype
## Handling covariance files:
# ld_cols
# get_ld_regions_list
# call_tabix_regions
# get_ld_regions_data
# get_ld_data
# get_ld_matrix
# get_snpIDs
# optimize_cols
# reformat_sample_vals
# reformat_vcf
# check_prep_vcf
# substr_in_strarray
#########################################################
# used to catch exceptions that dont require a traceback
class NoTargetDataError(Exception):
pass
# wrapper for thread_process functions; adds error catching/logging for failed targets
def error_handler(func):
@functools.wraps(func)
def wrapper(num, *args, **kwargs):
try:
return func(num, *args, **kwargs)
except NoTargetDataError:
return None
except Exception as e:
e_info = sys.exc_info()
e_type = e_info[0].__name__
# don't print traceback info for wrapper
e_tracebk = ''.join(traceback.format_tb(e_info[2])[1:])
print('Caught an exception for num={}:\n {}: {}\nTraceback:\n{}'.format(num, e_type, e, e_tracebk))
finally:
sys.stdout.flush()
return wrapper
def fatal_error_handler(func):
@functools.wraps(func)
def wrapper(num, *args, **kwargs):
try:
return func(num, *args, **kwargs)
except NoTargetDataError:
return None
except Exception as e:
e_info = sys.exc_info()
e_type = e_info[0].__name__
# don't print traceback info for wrapper
e_tracebk = ''.join(traceback.format_tb(e_info[2])[1:])
print('Caught an exception for num={}:\n {}: {}\nTraceback:\n{}'.format(num, e_type, e, e_tracebk))
sys.exit()
finally:
sys.stdout.flush()
return wrapper
# returns absolute path
def get_abs_path(x): return os.path.abspath(os.path.expanduser(os.path.expandvars(x)))
# wrapper for genotype functions; adds error handling for when an empty dataframe is read in during concatenation
def empty_df_handler(func):
@functools.wraps(func)
def wrapper(num, *args, **kwargs):
try:
return func(num, *args, **kwargs)
except (pd.errors.EmptyDataError, pd.errors.ParserError, AttributeError) as e:
e_info = sys.exc_info()
e_type = e_info[0].__name__
# don't print traceback info for wrapper
e_tracebk = ''.join(traceback.format_tb(e_info[2])[1:])
return None
return wrapper
def ped_startup(ped_path, pedinfo_path):
Asso_Info = pd.read_csv(
pedinfo_path,
sep='\t',
header=None,
names=['Ind','Var'])
pheno = Asso_Info[Asso_Info.Ind=='P'].Var.values
n_pheno = pheno.size
cov = Asso_Info[Asso_Info.Ind=='C'].Var.values
if not n_pheno:
raise SystemExit('No phenotypes provided by --PED_info.')
if not cov.size:
raise SystemExit('No covariates is provided by --PED_info.')
pheno = pheno.tolist()
cov = cov.tolist()
# get header of PED file
ped_header = get_header(ped_path)
ped_cols = np.intersect1d(ped_header, [*pheno, *cov])
# check that all phenos, covs in ped_header
missing_cols = [x for x in [*pheno, *cov] if not x in ped_cols]
if len(missing_cols):
print('WARNING: The following phenotypes and/or covariates given in PED_info are missing in the PED file: ' + ', '.join(missing_cols) + '\n')
pheno = [x for x in pheno if not x in missing_cols]
cov = [x for x in cov if not x in missing_cols]
ped_sampleid = pd.read_csv(
ped_path,
sep='\t',
usecols=['IND_ID'])['IND_ID'].drop_duplicates()
# ped_info = {'path':ped_path, 'file_cols':ped_header, 'cols':ped_cols, 'dtype': {x:object for x in ped_cols}, 'col_inds':tuple(sorted([ped_header.index(x) for x in ped_cols]))}
return ped_sampleid, ped_cols, n_pheno, pheno, cov
# def sampleid_startup(geno_path, sampleid_path, chrm, genofile_type, data_format, geneexp_path=None, **kwargs):
def sampleid_startup(chrm=None, genofile_type=None, data_format=None, sampleid_path=0, geno_path=0, geneexp_path=0, ped_path=0, pedinfo_path=0, **kwargs):
sampleid_lst = []
## PED files
if (ped_path and pedinfo_path):
ped_sampleid, ped_cols, n_pheno, pheno, cov = ped_startup(ped_path, pedinfo_path)
sampleid_lst.append(ped_sampleid)
## read in sampleids file
if sampleid_path:
print('Reading sampleID file.\n')
spec_sampleids = pd.read_csv(
sampleid_path,
sep='\t',
header=None)[0].drop_duplicates()
sampleid_lst.append(spec_sampleids)
## read in file headers
print('Reading file headers.\n')
## genotype file
if geno_path:
try:
geno_cols = call_tabix_header(geno_path)
except:
geno_cols = get_header(geno_path, zipped=True)
# get sampleids in the genotype file
geno_sampleids_strt_ind = {'dosage': 5, 'vcf': 9}[genofile_type]
geno_sampleids = geno_cols[geno_sampleids_strt_ind:]
sampleid_lst.append(geno_sampleids)
## expression file
if geneexp_path:
exp_cols = get_header(geneexp_path)
exp_sampleids = exp_cols[5:]
sampleid_lst.append(exp_sampleids)
## match sampleids between files
print('Matching sampleIDs.\n')
sampleID = np.array(functools.reduce(np.intersect1d, tuple(sampleid_lst)))
if not sampleID.size:
raise SystemExit('There are no overlapped sample IDs in the input files.')
print('Running job for ' + str(sampleID.size) + ' matched sampleIDs.')
## return values
return_lst = [sampleID, sampleID.size]
if geno_path:
geno_info = {'path': geno_path, 'chrm': chrm, 'genofile_type': genofile_type, 'data_format': data_format,
**get_cols_dtype(geno_cols, sampleid=sampleID,
cols=['CHROM','POS','REF','ALT'],
genofile_type=genofile_type,
ind_namekey=True)}
return_lst.append(geno_info)
if geneexp_path:
exp_info = {'geneexp_path': geneexp_path, 'chrm': chrm,
**get_cols_dtype(exp_cols, sampleid=sampleID,
cols=['CHROM','GeneStart','GeneEnd','TargetID','GeneName'])}
return_lst.append(exp_info)
if (ped_path and pedinfo_path):
return_lst = [*return_lst, ped_cols, n_pheno, pheno, cov]
return tuple(return_lst)
def gwas_file_info(gwas_path, chrm, **kwargs):
info_dict = get_cols_dtype(
get_header(gwas_path, zipped=True),
cols=['CHROM','POS','REF','ALT','BETA','SE'],
ind_namekey=True)
return {'path': gwas_path,
'chrm': chrm,
'sampleID': [],
'data_format': 'gwas',
'genofile_type': 'gwas',
**info_dict}
def bgw_weight_file_info(w_path, chrm, weight_threshold=0, add_cols=[], drop_cols=['ID'], **kwargs):
info_dict = get_cols_dtype(
get_header(w_path, zipped=True),
cols=['CHROM','POS','REF','ALT','Trans','PCP','beta'],
add_cols=add_cols,
drop_cols=drop_cols,
get_id=True,
ind_namekey=True)
return {'path': w_path,
'chrm': chrm,
'sampleID': [],
'target_ind': info_dict['file_cols'].index('Trans'),
'data_format': 'bgw_weight',
'genofile_type': 'bgw_weight',
'weight_threshold': weight_threshold,
**info_dict}
def weight_file_info(w_path, chrm, weight_threshold=0, add_cols=[], drop_cols=['ID'], **kwargs):
info_dict = get_cols_dtype(
get_header(w_path, zipped=True),
cols=['CHROM','POS','REF','ALT','TargetID','ES'],
add_cols=add_cols,
drop_cols=drop_cols,
get_id=True,
ind_namekey=True)
return {'path': w_path,
'chrm': chrm,
'sampleID': [],
'target_ind': info_dict['file_cols'].index('TargetID'),
'data_format': 'weight',
'genofile_type': 'weight',
'weight_threshold': weight_threshold,
**info_dict}
def zscore_file_info(z_path, chrm, **kwargs):
info_dict = get_cols_dtype(
get_header(z_path, zipped=True),
cols=['CHROM','POS','REF','ALT','Zscore'],
ind_namekey=True)
return {'path':z_path,
'chrm': chrm,
'sampleID': [],
'data_format': 'zscore',
'genofile_type': 'zscore',
**info_dict}
# def bgw_weight_cols_dtype(file_cols, add_cols=[], drop_cols=[], get_id=True, ret_dict=True, ind_namekey=True, **kwargs):
# return get_cols_dtype(file_cols,
# cols=['CHROM','POS','REF','ALT','Trans','PCP','beta'],
# add_cols=add_cols, drop_cols=drop_cols,
# get_id=get_id, ret_dict=ret_dict, ind_namekey=ind_namekey)
# read in annotation file or gene expression file
def read_gene_annot_exp(chrm=None, geneexp_path=None, annot_path=None, cols=['CHROM','GeneStart','GeneEnd','TargetID','GeneName'], col_inds=[0,1,2,3,4], dtype={'CHROM':object,'GeneStart':np.int64,'GeneEnd':np.int64,'TargetID':object,'GeneName':object}, **kwargs):
# set the path
path = geneexp_path if geneexp_path is not None else annot_path
if (chrm is not None):
try:
Gene_chunks = pd.read_csv(
path,
sep='\t',
iterator=True,
chunksize=10000,
usecols=cols,
dtype=dtype)
Gene = pd.concat([x[x['CHROM']==chrm] for x in Gene_chunks]).reset_index(drop=True)
except:
Gene_chunks = pd.read_csv(
path,
sep='\t',
iterator=True,
chunksize=10000,
usecols=cols)
Gene = pd.concat([x[x['CHROM']==chrm] for x in Gene_chunks]).reset_index(drop=True).astype(dtype)
# Gene.columns = [cols[i] for i in Gene.columns]
if Gene.empty:
raise SystemExit('There are no valid gene expression/annotation data for chromosome ' + chrm + '.\n')
else:
try:
Gene = pd.read_csv(
path,
sep='\t',
usecols=cols,
dtype=dtype)
except:
Gene = pd.read_csv(
path,
sep='\t',
header=None,
usecols=cols).reset_index(drop=True).astype(dtype)
# Gene.columns = [cols[i] for i in Gene.columns]
Gene = optimize_cols(Gene)
TargetID = Gene.TargetID
n_targets = TargetID.size
return Gene, TargetID, n_targets
## line filter functions for read_tabix
def filter_vcf_line(line: bytes, bformat, col_inds, split_multi_GT):
try:
# split line into list
row = line.split(b'\t')
# get index of data format
data_fmts = row[8].split(b':')
# may be multiallelic; only first alt allele will be used unless split_multi_GT; later data with bad values will be filtered out
alt_alleles = row[4].split(b',')
row[4] = alt_alleles[0]
# filter sample columns to include only data in desired format; sampleIDs start at file column index 9; in new row sampleIDs now start at 4, ALT is column 3
if (len(data_fmts) > 1):
data_ind = data_fmts.index(bformat)
row[8] = bformat
row = [row[x] if x <= 8 else row[x].split(b':')[data_ind] for x in col_inds]
else:
row = [row[x] for x in col_inds]
# turn multi-allelic lines into multiple biallelic lines
if split_multi_GT & (len(alt_alleles) > 1):
sample_str = b'\t'.join(row[4:])
line = bytearray()
for j in range(1, len(alt_alleles) + 1):
str_j = sample_str
for k in range(1, len(alt_alleles) + 1):
# substitute alt alleles besides the jth one with missing
str_j = re.sub(str(k).encode(), b'.', str_j) if (k != j) else str_j
# set jth alt allele to 1
str_j = re.sub(str(j).encode(), b'1', str_j)
# join row info information
line_j = b'\t'.join([*row[0:3], alt_alleles[j-1], str_j])
# append linebreak if needed
line_j = line_j if line_j.endswith(b'\n') else line_j + b'\n'
line += line_j
else:
# row to bytestring
line = b'\t'.join(row)
# append linebreak if needed
line += b'' if line.endswith(b'\n') else b'\n'
return line
except:
return b''
def filter_weight_line(line: bytes, btarget: bytes, target_ind, col_inds):
# split line into list
row = line.split(b'\t')
# check if row is for correct target
if (row[target_ind].startswith(btarget)):
line = b'\t'.join([row[x] for x in col_inds])
line += b'' if line.endswith(b'\n') else b'\n'
return line
else:
return b''
def filter_other_line(line: bytes, col_inds):
# split line into list
row = line.split(b'\t')
# filter out unneeded columns
line = b'\t'.join([row[x] for x in col_inds])
line += b'' if line.endswith(b'\n') else b'\n'
return line
def read_tabix(start, end, sampleID, chrm, path, file_cols, col_inds, cols, dtype, genofile_type=None, data_format=None, target_ind=5, target=None, weight_threshold=0, raise_error=True, **kwargs):
# subprocess command
command_str = ' '.join(['tabix', path, chrm + ':' + start + '-' + end])
proc = subprocess.Popen(
[command_str],
shell=True,
stdout=subprocess.PIPE,
bufsize=1)
# initialize bytearray
proc_out = bytearray()
# set correct filter function by file type
if genofile_type == 'vcf':
bformat = str.encode(data_format)
filter_line = functools.partial(filter_vcf_line, bformat=bformat, col_inds=col_inds, split_multi_GT=data_format == 'GT')
elif genofile_type == 'weight':
btarget = str.encode(target)
filter_line = functools.partial(filter_weight_line, btarget=btarget, target_ind=target_ind, col_inds=col_inds)
elif genofile_type == 'bgw_weight':
btarget = b'0'
filter_line = functools.partial(filter_weight_line, btarget=btarget, target_ind=target_ind, col_inds=col_inds)
else:
filter_line = functools.partial(filter_other_line, col_inds=col_inds)
# while subprocesses running, read lines into byte array
while proc.poll() is None:
line = proc.stdout.readline()
if len(line) == 0:
break
proc_out += filter_line(line)
# read in lines still remaining after subprocess completes
for line in proc.stdout:
proc_out += filter_line(line)
if not proc_out and raise_error:
print('No tabix data for target.\n')
raise NoTargetDataError
# read data into dataframe
df = pd.read_csv(
StringIO(proc_out.decode('utf-8')),
sep='\t',
low_memory=False,
header=None,
names=cols,
dtype=dtype)
# kill process
proc.kill()
# filter out rows where all sampleID values are nan
if len(sampleID):
df = df[df[sampleID].count(axis=1) != 0].reset_index(drop=True)
df = optimize_cols(df)
# get snpID
if (genofile_type != 'weight') or (not 'snpID' in cols):
df['snpID'] = get_snpIDs(df)
# filter out duplicated snpIDs
df = df.drop_duplicates(['snpID'], keep='first').reset_index(drop=True)
# weight file handling
if (genofile_type == 'weight'):
## figure out a way to handle target ids with decimal places when both files dont necessarily have those
# if not np.all(df['TargetID'] == target):
# partial_targetids = np.unique(df['TargetID'])
# VC_TWAS uses ES = b + beta
if (not 'ES' in cols):
if (('b' in cols) and ('beta' in cols)):
df['ES'] = df['b'] + df['beta']
if (('PCP' in cols) and ('beta' in cols)):
df['ES'] = np.prod(df['PCP'], df['beta'])
if weight_threshold:
# filter out weights below threshold
df = df[operator.gt(np.abs(df['ES']), weight_threshold)].reset_index(drop=True)
if df.empty and raise_error:
print('No test SNPs with cis-eQTL weights with magnitude that exceeds specified weight threshold for TargetID: ' + target + '.\n')
raise NoTargetDataError
# remove rows with non-valid GT values; ie those from multiallelic rows
if (data_format == 'GT'):
valid_GT = ['.|.', '0|0', '0|1', '1|0', '1|1',
'./.', '0/0', '0/1', '1/0', '1/1']
df = df[np.all(df[sampleID].isin(valid_GT), axis=1)].reset_index(drop=True)
# process dosage, vcf file sample df
if (data_format == 'GT') or (data_format == 'DS'):
df = reformat_sample_vals(df, data_format, sampleID)
if df.empty and raise_error:
print('No valid tabix data for target.\n')
raise NoTargetDataError
return df
def tabix_query_file(path, reg_str):
proc = subprocess.Popen(['tabix '+path+reg_str+' | head -n1 '],
shell=True, stdout=subprocess.PIPE, bufsize=1)
try:
outs, err = proc.communicate(timeout=15)
ret_val = len(outs) > 1
except TimeoutExpired:
proc.kill()
outs, errs = proc.communicate()
ret_val = len(outs) > 1
except:
proc.kill()
ret_val = False
finally:
# kill process
proc.kill()
return ret_val
def tabix_query_files(start, end, chrm, geno_path=None, gwas_path=None, w_path=None, z_path=None, **kwargs):
paths = [path for path in [geno_path, gwas_path, w_path, z_path] if path is not None]
reg_str = ' ' + chrm + ':' + start + '-' + end
# for each path test if length of first line != 0
return np.all([tabix_query_file(path, reg_str) for path in paths])
# Call tabix, read in lines into byte array
def call_tabix(path, chrm, start, end, add_command_str = ''):
regs_str = chrm + ':' + start + '-' + end
command_str = ' '.join(['tabix', path, regs_str, add_command_str])
# ["tabix " + path + " " + chrm + ":" + start + "-" + end]
proc = subprocess.Popen(
[command_str],
shell=True,
stdout=subprocess.PIPE,
bufsize=1)
proc_out = bytearray()
# while subprocesses running, read lines into byte array
while proc.poll() is None:
line = proc.stdout.readline()
if len(line) == 0:
break
proc_out += line
# read in lines still remaining after subprocess completes
for line in proc.stdout:
proc_out += line
# kill process
proc.kill()
return proc_out
# Call tabix to get header, read in lines into byte array;
# method for reading in headers for vcf files
def call_tabix_header(path, out='tuple', rename={}):
# create dictionary for renaming columns
# the first column in the vcf file is expected to be parsed as #CHROM; automatically add that to the rename list
rename = {**{'#CHROM':'CHROM'}, **rename}
proc = subprocess.Popen(
['tabix -H ' + path],
shell=True,
stdout=subprocess.PIPE,
bufsize=1)
proc_out = bytearray()
# while subprocesses running, read lines into byte array
while proc.poll() is None:
line = proc.stdout.readline()
if len(line) == 0:
break
# filter out lines starting with ##, which denotes comment in vcf file
if not line.startswith(b'##'):
proc_out += line
# read in lines still remaining after subprocess completes
for line in proc.stdout:
if not line.startswith(b'##'):
proc_out += line
# decode bytes, use pandas to read in, rename columns from dictionary
header = pd.read_csv(
StringIO(proc_out.decode('utf-8')),
sep='\t',
error_bad_lines=False).rename(columns=rename)
# kill process
proc.kill()
if out=='tuple':
return tuple(header)
elif out=='list':
return list(header)
return header
# return human readable elapsed time string
def format_elapsed_time(time_secs):
val = abs(int(time_secs))
day = val // (3600*24)
hour = val % (3600*24) // 3600
mins = val % 3600 // 60
secs = val % 60
res = '%02d:%02d:%02d:%02d' % (day, hour, mins, secs)
if int(time_secs) < 0:
res = "-%s" % res
return res
# get header from non-vcf file
def get_header(path, out='tuple', zipped=False, rename={}):
# zipped files assumed to be bgzipped, tabixed files
compress_type = 'gzip' if zipped else None
# the first column in the file is expected to be parsed as #CHROM; automatically add that to the rename list
rename = {**{'#CHROM':'CHROM'}, **rename}
# decode bytes, use pandas to read in, rename columns from dictionary
header = pd.read_csv(
path,
sep='\t',
header=0,
compression=compress_type,
low_memory=False,
nrows=0).rename(columns=rename)
if out=='tuple':
return tuple(header)
elif out=='list':
return list(header)
return header
# for testing on systems without tabix; not currently used by any scripts
def get_vcf_header(path, out='tuple'):
proc = subprocess.Popen(
["zgrep -m1 -E 'CHROM' "+path],
shell=True,
stdout=subprocess.PIPE,
bufsize=1)
proc_out = bytearray()
while proc.poll() is None:
line = proc.stdout.readline()
if len(line) == 0:
break
proc_out += line
for line in proc.stdout:
proc_out += line
header = pd.read_csv(
StringIO(proc_out.decode('utf-8')),
sep='\t',
error_bad_lines=False).rename(columns={'#CHROM':'CHROM'})
# kill process
proc.kill()
if out=='tuple':
return tuple(header)
elif out=='list':
return list(header)
return header
# determine indices of file cols to read in, dtype of each col
def get_cols_dtype(file_cols, cols, sampleid=None, genofile_type=None, add_cols=[], drop_cols=[], get_id=False, ind_namekey=False, **kwargs):
# sampleid handling
if sampleid is not None:
cols = cols + sampleid.tolist()
sampleid_dtype = object if genofile_type == 'vcf' else np.float64
sampleid_dict = {x:sampleid_dtype for x in sampleid}
else:
sampleid_dict = {}
dtype_dict = {
'ALT': object,
'b': np.float64,
'beta': np.float64,
'BETA': np.float64,
'CHROM': object,
'COV': object,
'ES': np.float64,
'FILTER': object,
'FORMAT': object,
'INFO': object,
'GeneEnd': np.int64,
'GeneName': object,
'GeneStart': np.int64,
'ID': object,
'MAF': np.float64,
'PCP': np.float64,
'POS': np.int64,
'QUAL': object,
'REF': object,
'SE': np.float64,
'snpID': object,
'TargetID': object,
'Trans': np.int64,
'Zscore': np.float64,
**sampleid_dict}
# cols set up
cols = cols + add_cols
# if type == 'vcf':
# cols.insert(4, 'snpID')
if get_id:
if ('snpID' in file_cols):
cols.append('snpID')
elif ('ID' in file_cols):
cols.append('ID')
cols = [x for x in cols if (x not in drop_cols)]
# create output
col_inds = tuple(sorted([file_cols.index(x) for x in cols]))
if ind_namekey:
# return dtype dict with keys as column names
out_dtype_dict = {x:dtype_dict[x] for x in cols}
else:
# return dtype dict with keys as column index
ind_dtype_dict = {file_cols.index(x):dtype_dict[x] for x in cols}
out_dtype_dict = ind_dtype_dict
# if ret_dict:
return {
'file_cols': file_cols,
'cols': [file_cols[i] for i in col_inds],
'col_inds': col_inds,
'dtype': out_dtype_dict}
# return col_inds, out_dtype_dict
# def exp_cols_dtype(file_cols, sampleid, ind_namekey=True, ret_dict=True, **kwargs):
# return get_cols_dtype(file_cols,
# cols=['CHROM', 'GeneStart', 'GeneEnd', 'TargetID', 'GeneName'],
# sampleid=sampleid, ind_namekey=ind_namekey, ret_dict=ret_dict)
# def genofile_cols_dtype(file_cols, genofile_type, sampleid, ind_namekey=True, ret_dict=True, **kwargs):
# return get_cols_dtype(file_cols,
# cols=['CHROM','POS','REF','ALT'],
# sampleid=sampleid, genofile_type=genofile_type,
# ind_namekey=ind_namekey, ret_dict=ret_dict)
# def weight_cols_dtype(file_cols, add_cols=[], drop_cols=[], get_id=True, ret_dict=True, ind_namekey=True, **kwargs):
# return get_cols_dtype(file_cols,
# cols=['CHROM','POS','REF','ALT','TargetID','ES'],
# add_cols=add_cols, drop_cols=drop_cols,
# get_id=get_id, ret_dict=ret_dict, ind_namekey=ind_namekey)
# def zscore_cols_dtype(file_cols, ret_dict=True, ind_namekey=True, **kwargs):
# return get_cols_dtype(file_cols,
# cols=['CHROM','POS','REF','ALT','Zscore'], ret_dict=ret_dict, ind_namekey=ind_namekey)
def gwas_cols_dtype(file_cols, ind_namekey=True, **kwargs):
return get_cols_dtype(file_cols,
cols=['CHROM','POS','REF','ALT','BETA','SE'], ind_namekey=ind_namekey)
def MCOV_cols_dtype(file_cols, add_cols=[], drop_cols=[], get_id=True, **kwargs):
return get_cols_dtype(file_cols,
cols=['CHROM','POS','REF','ALT','COV'],
add_cols=add_cols, drop_cols=drop_cols,
get_id=get_id)
# get header of ld file, get indices of columns to read in
def get_ld_cols(path):
# get header
file_cols = tuple(pd.read_csv(
path,
sep='\t',
header=0,
compression='gzip',
low_memory=False,
nrows=0).rename(columns={'#snpID':'snpID', '#ID':'snpID', 'ID':'snpID', '#0':'row', '#row':'row', '0':'row'}))
# ld files have gone through a lot of format revisions hence the possible need to rename
cols = ['row', 'snpID', 'COV']
file_cols_ind = tuple([file_cols.index(x) for x in cols])
return file_cols, file_cols_ind
# yields formatted tabix regions strings
def get_ld_regions_list(snp_ids):
# 'chrm:' prefix for region string
chrm = snp_ids[0].split(':')[0] + ':'
# snp pos values as integers
pos_vals = [int(snp.split(':')[1]) for snp in snp_ids]
# get intervals of start,end positions; convert to tabix string
for x, y in groupby(enumerate(pos_vals), lambda p: p[1]-p[0]):
y = list(y)
# chrm:start-end
yield chrm + str(y[0][1]) + '-' + str(y[-1][1])
# yields formatted tabix regions strings
def get_regions_list(snp_ids):
# split into groups by chromosome
for chrm, grp in groupby(snp_ids, lambda x: x.split(':')[0]):
# snp pos values as integers
pos_vals = [int(snp.split(':')[1]) for snp in grp]
# get intervals of start,end positions; convert to tabix string
for x, y in groupby(enumerate(pos_vals), lambda p: p[1]-p[0]):
y = list(y)
# chrm:start-end
yield chrm + ':' + str(y[0][1]) + '-' + str(y[-1][1])
# call tabix using regions string
def call_tabix_regions(path, regs_str, filter_line = lambda x:x ):
proc = subprocess.Popen(
['tabix '+path+' '+regs_str],
shell=True,
stdout=subprocess.PIPE,
bufsize=1)
proc_out = bytearray()
# process while subprocesses running
while proc.poll() is None:
line = proc.stdout.readline()
if len(line) == 0:
break
proc_out += filter_line(line)
# leftover lines
for line in proc.stdout:
proc_out += filter_line(line)
# kill process
proc.kill()
return proc_out
# get proc_out from function and parse data for regions
def get_ld_regions_data(regs_str, path, snp_ids, ld_cols, ld_cols_ind):
proc_out = call_tabix_regions(path, regs_str)
regs_data = pd.read_csv(
StringIO(proc_out.decode('utf-8')),
sep='\t',
low_memory=False,
header=None,
names=ld_cols,
usecols=ld_cols_ind,
dtype={
'snpID': object,
'row': np.int32,
'COV': object}
).drop_duplicates(['snpID'], keep='first')
regs_data = regs_data[regs_data.snpID.isin(snp_ids)]
return regs_data
# read in covariance data for snps
def get_ld_data(path, snp_ids):
# get columns names, indices for ld file
ld_cols, ld_cols_ind = get_ld_cols(path)
# format tabix regions from snp_ids; 'chrm:start-end'
regs_lst = list(get_ld_regions_list(snp_ids))
N = len(regs_lst)
# arguments to pass
regs_args = [path, snp_ids, ld_cols, ld_cols_ind]
try:
regs_str = ' '.join(regs_lst)
cov_data = get_ld_regions_data(regs_str, *regs_args)
except OSError:
# argument may be too long for OS; if so try subset instead of getting all regions at once
# print('Subseting regions to tabix.')
n = 2500
while n:
try:
regs_str_lst = [' '.join(regs_lst[i:i+n]) for i in range(0, N, n)]
cov_data = pd.concat([get_ld_regions_data(regs_str, *regs_args) for regs_str in regs_str_lst])
except OSError:
n -= 500
pass
else:
n = 0
return cov_data.set_index('row')
def get_ld_matrix(MCOV, return_diag=False):
MCOV = MCOV.copy()
MCOV['COV'] = MCOV['COV'].apply(lambda x:np.fromstring(x, dtype=np.float32, sep=','))
inds = MCOV.index
n_inds = inds.size
V_upper = np.zeros((n_inds, n_inds))
for i in range(n_inds):
cov_i = MCOV.COV.loc[inds[i]]
N = cov_i.size
for j in range(i,n_inds):
if inds[j] - inds[i] < N:
V_upper[i,j] = cov_i[inds[j] - inds[i]]
else:
V_upper[i,j] = 0
snp_Var = V_upper.diagonal()
V = V_upper + V_upper.T - np.diag(snp_Var)
snp_sd = np.sqrt(snp_Var)
if return_diag:
return snp_sd, V, snp_Var
else:
return snp_sd, V
# return snp ids; join CHROM, POS, REF, ALT columns into : separated string
def get_snpIDs(df: pd.DataFrame, flip=False):
chrms = df['CHROM'].astype('str').values
pos = df['POS'].astype('str').values
ref = df['REF'].values
alt = df['ALT'].values
if flip:
return [':'.join(i) for i in zip(chrms,pos,alt,ref)]
else:
return [':'.join(i) for i in zip(chrms,pos,ref,alt)]
# gives flipped snpIDs for an array/list of snpIDs
def flip_snpIDs(snpIDs):
return np.array([':'.join([y[0],y[1],y[3],y[2]]) for y in [x.split(':') for x in snpIDs]])
# Decrease memory by downcasting 'CHROM' column to integer, integer and float columns to minimum size that will not lose info
def optimize_cols(df: pd.DataFrame):
# if 'CHROM' not convertable to integer, assume it's 'X', 'Y', 'MT', etc.
if 'CHROM' in df.columns:
try:
df['CHROM'] = df['CHROM'].astype(str).astype(np.int8)