annotate vcf_snp.py @ 9:a9433c403e28 draft default tip

anaconda as default python
author brigidar
date Fri, 06 Nov 2015 15:31:03 -0500
parents e9dcf64ef8ec
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
e9dcf64ef8ec changed python call up
brigidar
parents: 6
diff changeset
1 #!/usr/local/anaconda/bin/python
0
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 #
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
6 # Version : 0.3 #
0
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 #
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
10 # Date : November 06 2015 #
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
11 # #
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
12 #########################################################################################
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
13
2
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
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
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
15
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 import argparse, os, sys, csv, IPython
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
21 import pandas
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
22 import pdb
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
23 from pandas import *
4
866cd9ce1cbd update anaconda python
brigidar
parents: 2
diff changeset
24
0
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 #output and input file name to give with the script
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
29 parser = argparse.ArgumentParser()
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
30
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
31 parser.add_argument('-o', '--output', help="snp tab")
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
32 parser.add_argument('-s', '--snp_table', help="vcf")
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
33 parser.add_argument('-p', '--positions', help="positions")
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
34
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
35
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
36
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
37 args = parser.parse_args()
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
38 output_file = args.output
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
39 input_file = args.snp_table
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
40 input_file2=args.positions
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
41 #------------------------------------------------------------------------------------------
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
42
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
43
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
44 #read in file as dataframe
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
45 df =read_csv(input_file,sep='\t', dtype=object)
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
46 df3=read_csv(input_file2,sep='\t', dtype=object, names=['#CHROM','POS'])
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
47 #pdb.set_trace()
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
48 #get the positions that are missing
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
49 if df.index.size!= df3.index.size:
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
50 df4=df3[~df3.POS.isin(df.POS)]
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
51 #pdb.set_trace()
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
52 df4=df4.set_index('POS')
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
53 df=df.set_index('POS')
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
54 df2=concat([df,df4], axis=1)
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
55 #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
56 df5=df2.iloc[:, 2:4].fillna('N')
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
57 df5=df5.reindex(df3.POS)
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
58 else:
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
59 df5=df.iloc[:,3:5]
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
60
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
61 #------------------------------------------------------------------------------------------
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
62
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
63
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
64 #pdb.set_trace()
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
65
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
66 #------------------------------------------------------------------------------------------
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
67 #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
68 ref_list=[]
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
69 for i in range(0,df5.index.size):
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
70 if df5.iloc[i,1]==".":
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
71 ref_list.append(df5.iloc[i,0][0])
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
72 elif len(df5.iloc[i,1])>1:
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
73 ref_list.append('N')
2
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
74 else:
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
75 ref_list.append(df5.iloc[i,1][0])
2
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
76 #pdb.set_trace()
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
77 #
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
78 ##------------------------------------------------------------------------------------------
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
79 #
0
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
80 #save file with output name for fasta -o option and removes header and index
75cedeb179aa Uploaded
brigidar
parents:
diff changeset
81 with open(output_file,'w') as output:
6
3a352cb57117 new script updates
brigidar
parents: 4
diff changeset
82 output.write('ALT' + '\t' + ''.join([str(i) for v,i in enumerate(ref_list)]))
2
ea2f686dfd4a Uploaded
brigidar
parents: 0
diff changeset
83 ##------------------------------------------------------------------------------------------