8
|
1 #!/usr/local/anaconda/bin/python
|
0
|
2
|
|
3 #########################################################################################
|
|
4 # #
|
|
5 # Name : vcf_snp.py #
|
6
|
6 # Version : 0.3 #
|
0
|
7 # Project : extract snp from vcf #
|
|
8 # Description : Script to exctract snps #
|
|
9 # Author : Brigida Rusconi #
|
6
|
10 # Date : November 06 2015 #
|
0
|
11 # #
|
|
12 #########################################################################################
|
|
13
|
2
|
14 # If the position is identical to the reference it does not print the nucleotide. I have to retrieve it from the ref column.
|
0
|
15
|
|
16
|
|
17 #------------------------------------------------------------------------------------------
|
|
18
|
|
19
|
|
20 import argparse, os, sys, csv, IPython
|
|
21 import pandas
|
|
22 import pdb
|
|
23 from pandas import *
|
4
|
24
|
0
|
25 #------------------------------------------------------------------------------------------
|
|
26
|
|
27
|
|
28 #output and input file name to give with the script
|
|
29 parser = argparse.ArgumentParser()
|
|
30
|
|
31 parser.add_argument('-o', '--output', help="snp tab")
|
|
32 parser.add_argument('-s', '--snp_table', help="vcf")
|
6
|
33 parser.add_argument('-p', '--positions', help="positions")
|
|
34
|
0
|
35
|
|
36
|
|
37 args = parser.parse_args()
|
|
38 output_file = args.output
|
|
39 input_file = args.snp_table
|
6
|
40 input_file2=args.positions
|
0
|
41 #------------------------------------------------------------------------------------------
|
|
42
|
|
43
|
|
44 #read in file as dataframe
|
|
45 df =read_csv(input_file,sep='\t', dtype=object)
|
6
|
46 df3=read_csv(input_file2,sep='\t', dtype=object, names=['#CHROM','POS'])
|
|
47 #pdb.set_trace()
|
|
48 #get the positions that are missing
|
|
49 if df.index.size!= df3.index.size:
|
|
50 df4=df3[~df3.POS.isin(df.POS)]
|
|
51 #pdb.set_trace()
|
|
52 df4=df4.set_index('POS')
|
|
53 df=df.set_index('POS')
|
|
54 df2=concat([df,df4], axis=1)
|
|
55 #if there is no information for a loci the position is dropped so it fills the nan with N
|
|
56 df5=df2.iloc[:, 2:4].fillna('N')
|
|
57 df5=df5.reindex(df3.POS)
|
|
58 else:
|
|
59 df5=df.iloc[:,3:5]
|
0
|
60
|
|
61 #------------------------------------------------------------------------------------------
|
|
62
|
6
|
63
|
0
|
64 #pdb.set_trace()
|
|
65
|
|
66 #------------------------------------------------------------------------------------------
|
6
|
67 #replaces the . with the nucleotide call of the reference also deals with multiallelic states calling them N
|
2
|
68 ref_list=[]
|
6
|
69 for i in range(0,df5.index.size):
|
|
70 if df5.iloc[i,1]==".":
|
|
71 ref_list.append(df5.iloc[i,0][0])
|
|
72 elif len(df5.iloc[i,1])>1:
|
|
73 ref_list.append('N')
|
2
|
74 else:
|
6
|
75 ref_list.append(df5.iloc[i,1][0])
|
2
|
76 #pdb.set_trace()
|
|
77 #
|
|
78 ##------------------------------------------------------------------------------------------
|
|
79 #
|
0
|
80 #save file with output name for fasta -o option and removes header and index
|
|
81 with open(output_file,'w') as output:
|
6
|
82 output.write('ALT' + '\t' + ''.join([str(i) for v,i in enumerate(ref_list)]))
|
2
|
83 ##------------------------------------------------------------------------------------------ |