0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 #########################################################################################
|
|
4 # #
|
|
5 # Name : vcf_snp.py #
|
2
|
6 # Version : 0.2 #
|
0
|
7 # Project : extract snp from vcf #
|
|
8 # Description : Script to exctract snps #
|
|
9 # Author : Brigida Rusconi #
|
2
|
10 # Date : November 02 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")
|
|
33
|
|
34
|
|
35 args = parser.parse_args()
|
|
36 output_file = args.output
|
|
37 input_file = args.snp_table
|
|
38 #------------------------------------------------------------------------------------------
|
|
39
|
|
40
|
|
41 #read in file as dataframe
|
|
42 df =read_csv(input_file,sep='\t', dtype=object)
|
|
43
|
|
44 #------------------------------------------------------------------------------------------
|
|
45
|
|
46 # only columns with qbase and refbase in table
|
2
|
47 #count_qbase=list(df.columns.values)
|
|
48 #qindexes=[]
|
|
49 #for i, v in enumerate(count_qbase):
|
|
50 # if 'ALT' in v:
|
|
51 # qindexes.append(i)
|
|
52 df2=df.iloc[:,3:5]
|
0
|
53 #pdb.set_trace()
|
|
54
|
|
55 #------------------------------------------------------------------------------------------
|
2
|
56 ref_list=[]
|
|
57 for i in range(0,df2.index.size):
|
|
58 if df2.iloc[i,1]==".":
|
|
59 ref_list.append(df2.iloc[i,0][0])
|
|
60 else:
|
|
61 ref_list.append(df2.iloc[i,1][0])
|
|
62 #pdb.set_trace()
|
|
63 #
|
|
64 ##------------------------------------------------------------------------------------------
|
|
65 #
|
0
|
66 #save file with output name for fasta -o option and removes header and index
|
|
67 with open(output_file,'w') as output:
|
2
|
68 output.write(df.columns.values[4] + '\t' + ''.join([str(i) for v,i in enumerate(ref_list)]))
|
|
69 ##------------------------------------------------------------------------------------------ |