comparison vcf_snp.py @ 0:75cedeb179aa draft

Uploaded
author brigidar
date Mon, 02 Nov 2015 12:45:11 -0500
parents
children ea2f686dfd4a
comparison
equal deleted inserted replaced
-1:000000000000 0:75cedeb179aa
1 #!/usr/bin/env python
2
3 #########################################################################################
4 # #
5 # Name : vcf_snp.py #
6 # Version : 0.1 #
7 # Project : extract snp from vcf #
8 # Description : Script to exctract snps #
9 # Author : Brigida Rusconi #
10 # Date : October 30 2015 #
11 # #
12 #########################################################################################
13 #for replacement of a given value with NaN
14 #http://stackoverflow.com/questions/18172851/deleting-dataframe-row-in-pandas-based-on-column-value
15
16 # to remove any symbol and replace it with nan
17 #http://stackoverflow.com/questions/875968/how-to-remove-symbols-from-a-string-with-python
18
19 # for isin information
20 #http://pandas.pydata.org/pandas-docs/stable/indexing.html
21
22 # for selecting rows that have an indel:
23 #http://stackoverflow.com/questions/14247586/python-pandas-how-to-select-rows-with-one-or-more-nulls-from-a-dataframe-without
24
25
26
27 #------------------------------------------------------------------------------------------
28
29
30 import argparse, os, sys, csv, IPython
31 import pandas
32 import pdb
33 import numpy as np
34 from pandas import *
35 from IPython import get_ipython
36 import matplotlib.pyplot as plt
37 from pandas.util.testing import assert_frame_equal
38 #------------------------------------------------------------------------------------------
39
40
41 #output and input file name to give with the script
42 parser = argparse.ArgumentParser()
43
44 parser.add_argument('-o', '--output', help="snp tab")
45 parser.add_argument('-s', '--snp_table', help="vcf")
46
47
48 args = parser.parse_args()
49 output_file = args.output
50 input_file = args.snp_table
51 #------------------------------------------------------------------------------------------
52
53
54 #read in file as dataframe
55 df =read_csv(input_file,sep='\t', dtype=object)
56 df=df.set_index(['#CHROM','POS'])
57
58 #need to fill na otherwise it cannot do boolean operations
59 df=df.fillna('--')
60 print "vcf " + str(df.index.size)
61 #------------------------------------------------------------------------------------------
62
63 # only columns with qbase and refbase in table
64 count_qbase=list(df.columns.values)
65 qindexes=[]
66 for i, v in enumerate(count_qbase):
67 if 'ALT' in v:
68 qindexes.append(i)
69 df2=df.iloc[:,qindexes]
70
71 #------------------------------------------------------------------------------------------
72 #pdb.set_trace()
73
74 #------------------------------------------------------------------------------------------
75
76 #save file with output name for fasta -o option and removes header and index
77 with open(output_file,'w') as output:
78 df2.T.to_csv(output, sep='\t',header=False)
79 #------------------------------------------------------------------------------------------
80
81
82