-
Notifications
You must be signed in to change notification settings - Fork 0
/
gene_snp_script.py
53 lines (39 loc) · 1.35 KB
/
gene_snp_script.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
#### Amelie gene/snp handling
# libraries
import pandas as pd
import argparse
# # argparse block
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="Input .txt file (incl path/to/file)", required=True)
parser.add_argument("-g", "--gene", help="Gene name (i.e. 'GeneA')",required=True)
parser.add_argument("-o", "--output", help="Output csv (i.e 'output.csv' and incl path/to/file)", required=True)
args = parser.parse_args()
# # read in data file
# x = pd.read_csv(args.input, sep="\t", header=0)
x = pd.read_csv(args.input, sep="\t", names=["CHROM", "POS", "DOT", "REF", "ALT", "QUAL", "FILTER", "DATA", "GUIDE", "GUIDE_DATA", "ANNO", "NAME"], index_col=False)
# split at . pull at NAME
names = []
for i in x.NAME:
names.append(i.split(".")[0])
snps = []
for i in x.ANNO:
y = i.split(",")[9]
snps.append(i.split(",")[9])
df = pd.DataFrame()
df['Names'] = names
df['SNPS'] = snps
# add a column for gene name
df['Gene'] = args.gene
# combine SNP and gene columns
df['SNP_Gene'] = df['Gene'] + "_" + df['SNPS']
# add a bool column all True
df['Bool'] = True
# drop sNp and gene column
df = df.drop(['SNPS', 'Gene'], axis=1)
# pivot table into a matrix
df = df.pivot(index='Names', columns='SNP_Gene', values='Bool')
# convert True to 1 and False to 0
df = df.fillna(0)
df = df.astype(int)
#generate csv
df.to_csv(args.output)