annotate vcf_snp.py @ 7:b617219a70b5 draft

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