annotate vcf_snp.py @ 0:75cedeb179aa draft

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