annotate get_reads/get_read.py @ 40:d5e10cdd486d draft

Uploaded
author tyty
date Mon, 20 Oct 2014 14:57:57 -0400
parents 297cdb01d656
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
297cdb01d656 Uploaded
tyty
parents:
diff changeset
1 #!/usr/bin/env python
297cdb01d656 Uploaded
tyty
parents:
diff changeset
2 # -*- coding: utf-8 -*-
297cdb01d656 Uploaded
tyty
parents:
diff changeset
3
297cdb01d656 Uploaded
tyty
parents:
diff changeset
4 import sys
297cdb01d656 Uploaded
tyty
parents:
diff changeset
5 #from galaxy.tools.read_file import *
297cdb01d656 Uploaded
tyty
parents:
diff changeset
6 from Bio import SeqIO
297cdb01d656 Uploaded
tyty
parents:
diff changeset
7 import os
297cdb01d656 Uploaded
tyty
parents:
diff changeset
8 from read_file import *
297cdb01d656 Uploaded
tyty
parents:
diff changeset
9
297cdb01d656 Uploaded
tyty
parents:
diff changeset
10 fasta_file = sys.argv[1]
297cdb01d656 Uploaded
tyty
parents:
diff changeset
11 map_file = sys.argv[2]
297cdb01d656 Uploaded
tyty
parents:
diff changeset
12 result_file = sys.argv[3]
297cdb01d656 Uploaded
tyty
parents:
diff changeset
13
297cdb01d656 Uploaded
tyty
parents:
diff changeset
14 os.system("samtools view -F 0xfff "+map_file+"|cut -f 3,4 > map_info.txt")
297cdb01d656 Uploaded
tyty
parents:
diff changeset
15
297cdb01d656 Uploaded
tyty
parents:
diff changeset
16 fasta_sequences = SeqIO.parse(open(fasta_file),'fasta');
297cdb01d656 Uploaded
tyty
parents:
diff changeset
17 length_seq = {};
297cdb01d656 Uploaded
tyty
parents:
diff changeset
18 for seq in fasta_sequences:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
19 nuc = seq.id;
297cdb01d656 Uploaded
tyty
parents:
diff changeset
20 length_seq[nuc] = len(seq.seq.tostring());
297cdb01d656 Uploaded
tyty
parents:
diff changeset
21
297cdb01d656 Uploaded
tyty
parents:
diff changeset
22
297cdb01d656 Uploaded
tyty
parents:
diff changeset
23
297cdb01d656 Uploaded
tyty
parents:
diff changeset
24 mapping = {}
297cdb01d656 Uploaded
tyty
parents:
diff changeset
25 transcripts = []
297cdb01d656 Uploaded
tyty
parents:
diff changeset
26
297cdb01d656 Uploaded
tyty
parents:
diff changeset
27 f = open("map_info.txt");
297cdb01d656 Uploaded
tyty
parents:
diff changeset
28 for aline in f.readlines():
297cdb01d656 Uploaded
tyty
parents:
diff changeset
29 tline = aline.strip();
297cdb01d656 Uploaded
tyty
parents:
diff changeset
30 tl = tline.split('\t');
297cdb01d656 Uploaded
tyty
parents:
diff changeset
31 if tl[0].strip() not in transcripts:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
32 transcripts.append(tl[0].strip());
297cdb01d656 Uploaded
tyty
parents:
diff changeset
33 mapping[tl[0].strip()] = [];
297cdb01d656 Uploaded
tyty
parents:
diff changeset
34
297cdb01d656 Uploaded
tyty
parents:
diff changeset
35 mapping[tl[0].strip()].append(tl[1].strip());
297cdb01d656 Uploaded
tyty
parents:
diff changeset
36
297cdb01d656 Uploaded
tyty
parents:
diff changeset
37 distribution = {};
297cdb01d656 Uploaded
tyty
parents:
diff changeset
38 coverage = {};
297cdb01d656 Uploaded
tyty
parents:
diff changeset
39 for transcript in length_seq:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
40 distribution[transcript] = [];
297cdb01d656 Uploaded
tyty
parents:
diff changeset
41 for i in range(0, length_seq[transcript]):
297cdb01d656 Uploaded
tyty
parents:
diff changeset
42 distribution[transcript].append(0);
297cdb01d656 Uploaded
tyty
parents:
diff changeset
43 sum_count = float(0);
297cdb01d656 Uploaded
tyty
parents:
diff changeset
44 if transcript in mapping:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
45 for j in range(0, len(mapping[transcript])):
297cdb01d656 Uploaded
tyty
parents:
diff changeset
46 index = mapping[transcript][j];
297cdb01d656 Uploaded
tyty
parents:
diff changeset
47 #count = reads[mapping[transcript][j][0]];
297cdb01d656 Uploaded
tyty
parents:
diff changeset
48 sum_count = sum_count + 1;
297cdb01d656 Uploaded
tyty
parents:
diff changeset
49 distribution[transcript][int(index)-1] = distribution[transcript][int(index)-1] + 1;
297cdb01d656 Uploaded
tyty
parents:
diff changeset
50 coverage[transcript] = float(sum_count)/float(length_seq[transcript]);
297cdb01d656 Uploaded
tyty
parents:
diff changeset
51 else:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
52 coverage[transcript] = 0
297cdb01d656 Uploaded
tyty
parents:
diff changeset
53
297cdb01d656 Uploaded
tyty
parents:
diff changeset
54
297cdb01d656 Uploaded
tyty
parents:
diff changeset
55
297cdb01d656 Uploaded
tyty
parents:
diff changeset
56
297cdb01d656 Uploaded
tyty
parents:
diff changeset
57
297cdb01d656 Uploaded
tyty
parents:
diff changeset
58 h = file(result_file, 'w')
297cdb01d656 Uploaded
tyty
parents:
diff changeset
59 for transcript in length_seq:
297cdb01d656 Uploaded
tyty
parents:
diff changeset
60 h.write(transcript);
297cdb01d656 Uploaded
tyty
parents:
diff changeset
61 h.write('\n')
297cdb01d656 Uploaded
tyty
parents:
diff changeset
62 for i in range(0, length_seq[transcript]):
297cdb01d656 Uploaded
tyty
parents:
diff changeset
63 h.write(str(distribution[transcript][i]))
297cdb01d656 Uploaded
tyty
parents:
diff changeset
64 h.write('\t')
297cdb01d656 Uploaded
tyty
parents:
diff changeset
65 h.write('\n')
297cdb01d656 Uploaded
tyty
parents:
diff changeset
66 h.write('\n')
297cdb01d656 Uploaded
tyty
parents:
diff changeset
67
297cdb01d656 Uploaded
tyty
parents:
diff changeset
68
297cdb01d656 Uploaded
tyty
parents:
diff changeset
69
297cdb01d656 Uploaded
tyty
parents:
diff changeset
70
297cdb01d656 Uploaded
tyty
parents:
diff changeset
71
297cdb01d656 Uploaded
tyty
parents:
diff changeset
72 f.close();
297cdb01d656 Uploaded
tyty
parents:
diff changeset
73 h.close()
297cdb01d656 Uploaded
tyty
parents:
diff changeset
74
297cdb01d656 Uploaded
tyty
parents:
diff changeset
75
297cdb01d656 Uploaded
tyty
parents:
diff changeset
76
297cdb01d656 Uploaded
tyty
parents:
diff changeset
77