annotate getalleleseq.py @ 0:c542b3075f29 draft

Uploaded repo.tar.gz
author boris
date Mon, 03 Feb 2014 13:07:13 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
1 #!/usr/bin/env python
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
2 # Boris Rebolledo-Jaramillo (boris-at-bx.psu.edu)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
3 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
4 #usage: getalleleseq.py [-h] [-l INT] [-j FILE] [-d DIR] alleles
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
5 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
6 #Given a table with minor and major alleles per position, it generates the
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
7 #minor and major allele sequences in FASTA format
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
8 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
9 #positional arguments:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
10 # alleles Table containing minor and major allele base per
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
11 # position. cols: [id, chr, pos, A, C, G, T, cvrg,
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
12 # plody, major, minor, freq_minor]
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
13 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
14 #optional arguments:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
15 # -h, --help show this help message and exit
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
16 # -l INT, --seq-length INT
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
17 # Background sequence length. Bases in an artifical
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
18 # all-N-sequence of length INT will be replaced by
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
19 # either the major or minor allele base accordingly
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
20 # -j FILE, --major-seq FILE
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
21 # File to write major allele sequences in FASTA multiple
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
22 # alignment format.
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
23 # -d DIR, --minor-dir DIR
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
24 # Per sample minor allele sequences will be written to
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
25 # this directory
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
26 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
27 # The expected columns in the alleles table follow Nicholas Stoler's
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
28 # Variant Annotator tool format. See Variant Annotator in Galaxy's tool shed
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
29 # http://testtoolshed.g2.bx.psu.edu/repos/nick/allele_counts_1 for more details
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
30 #
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
31 # Expected columns:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
32 # 1. sample_id
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
33 # 2. chr
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
34 # 3. position
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
35 # 4 counts for A's
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
36 # 5. counts for C's
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
37 # 6. counts for G's
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
38 # 7. counts for T's
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
39 # 8. Coverage
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
40 # 9. Number of alleles passing a given criteria
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
41 # 10. Major allele
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
42 # 11. Minor allele
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
43 # 12. Minor allele frequency in position
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
44
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
45 import sys
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
46 import os
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
47 import argparse
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
48
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
49 def createseq(sample, allele, seq_size, table):
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
50 """Generate major or minor allele sequence"""
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
51 out_sequence = ['N' for i in range(seq_size)]
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
52 sample_data = [line for line in table if line[0] == sample]
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
53
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
54 for entry in sample_data:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
55 position = int(entry[2])
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
56 number_of_alleles = int(entry[8])
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
57 major_allele = entry[9].strip()
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
58 minor_allele = entry[10].strip()
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
59
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
60 if allele == 'major':
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
61 out_sequence[position-1] = major_allele
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
62 elif allele == 'minor':
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
63 if number_of_alleles == 2:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
64 out_sequence[position-1] = minor_allele
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
65 else:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
66 out_sequence[position-1] = major_allele
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
67 return out_sequence
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
68
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
69 def printseq(sample,allele,seq,output):
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
70 """Print out sequence"""
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
71 #print >> output, '>{0}_{1}'.format(sample,allele)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
72 print >> output, '>{0}{1}'.format(sample,allele)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
73 for i in range(0,len(seq),70):
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
74 print >> output, ''.join(seq[i:i+70])
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
75
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
76 def main():
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
77 parser = argparse.ArgumentParser(description='Given a table with minor and major alleles per position, it generates the minor and major allele sequences in FASTA format', epilog='Boris Rebolledo-Jaramillo (boris-at-bx.psu.edu)')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
78 parser.add_argument('alleles', type=str, help='Table containing minor and major allele base per position. cols: [id, chr, pos, A, C, G, T, cvrg, plody, major, minor, freq_minor] ')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
79 parser.add_argument('-l','--seq-length', type=int, metavar='INT', help='Background sequence length. Bases in an artifical all-N-sequence of length INT will be replaced by either the major or minor allele base accordingly')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
80 parser.add_argument('-j','--major-seq', type=str, metavar='FILE', help='File to write major allele sequences in FASTA multiple alignment format.')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
81 parser.add_argument('-d', '--minor-dir', type=str, metavar='DIR', default='.', help="Per sample minor allele sequences will be written to this directory (Default: current directory)")
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
82 parser.add_argument('-p', '--minor-prefix', type=str, metavar='STR', nargs='?', const='', default='', help=argparse.SUPPRESS) #Galaxy compatibility
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
83 args = parser.parse_args()
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
84
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
85
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
86 try:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
87 table = [line.strip().split('\t') for line in list(open(args.alleles)) if "#" not in line]
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
88 samples = sorted(list(set([ line[0] for line in table ])))
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
89 except:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
90 sys.exit('\nERROR: Could not open %s\n' % args.alleles)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
91 try:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
92 major_out = open(args.major_seq, 'w+')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
93 except:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
94 sys.exit('\nCould not create %s\n' % args.major_seq)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
95
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
96 # Single file for all major allele sequences in FASTA multiple alignment
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
97 for sample in samples:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
98 sequence = createseq(sample,'major',args.seq_length,table)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
99 #printseq(sample,'major',sequence,major_out)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
100 printseq(sample,'',sequence,major_out)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
101 major_out.close()
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
102
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
103 # Sample specific minor allele sequence in FASTA format
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
104 try:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
105 os.makedirs(args.minor_dir)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
106 except:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
107 pass
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
108
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
109 for sample in samples:
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
110 if args.minor_prefix: # to fit Galaxy requirements
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
111 name = sample.replace('_','')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
112 minor_name = "%s_%s_%s" % ('primary',args.minor_prefix,name+'-minor_visible_fasta')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
113 else: # for non-Galaxy
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
114 minor_name = sample+'-minor.fa'
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
115 minor_out = open(os.path.join(args.minor_dir, minor_name), 'w+')
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
116 sequence = createseq(sample,'minor',args.seq_length,table)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
117 #printseq(sample,'minor',sequence,minor_out)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
118 printseq(sample,'_minor',sequence,minor_out)
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
119 minor_out.close()
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
120
c542b3075f29 Uploaded repo.tar.gz
boris
parents:
diff changeset
121 if __name__ == "__main__": main()