-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bacterial_pipeline_part1.py
165 lines (113 loc) · 3.86 KB
/
Bacterial_pipeline_part1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import sys
import os
import operator
import numpy as np
import pickle
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
import pandas as pd
from Bio import SeqIO
import json
import argparse
# In[3]:
if (len(sys.argv) > 3):
print("Wrong usage")
base_path = "/data/MAB_alignment/"
bact_name = "iid_genome"
if (len(sys.argv) == 3):
base_path = sys.argv[1]
bact_name = sys.argv[2]
num_reads_to_keep = 1000
min_read_length = 7000
num_aln = 5
aln_threshold = 0.8
org_path = base_path+bact_name+"/"
print(org_path)
# In[6]:
aln_txt_org = org_path+"alignments.txt"
all_read_fasta = org_path+bact_name+"_all.fasta"
DBshow1 = ("DBshow " + org_path + bact_name + " > " + all_read_fasta)
print(DBshow1)
LAshow1 = ("LAshow " + org_path + bact_name + " " + org_path +
bact_name + " > " + aln_txt_org)
print(LAshow1)
os.system(DBshow1)
os.system(LAshow1)
# In[7]:
rd_lgths = {}
fasta_sequences = SeqIO.parse(open(all_read_fasta),'fasta')
for ind, fasta in enumerate(fasta_sequences):
rd_lgths[ind] = len(str(fasta.seq))
# In[9]:
read_no = 1
lst_alignments = []
aln_size = {}
reads_to_keep = set()
with open(aln_txt_org, 'r') as f:
for ind, line in enumerate(f):
if ind < 2:
continue
x = line.split()
y = line.replace('<', '[').split("[")
# print(y)
try:
z = y[1].strip().split("x")[0].strip().strip(">").strip("]").split("..")
except:
print (line, x, y)
raise
start_pos = int(z[0].strip().replace(',', ''))
end_pos = int(z[1].strip().replace(',', ''))
# print len(x)
if int(x [0].replace(',', '')) == read_no:
if x [2] == 'n':
read_b_n0 = int(x [1].replace(',', ''))
raw_aln_size = (end_pos-start_pos+0.0)
aln = raw_aln_size
# print read_b_n0, start_pos, end_pos, aln
if read_b_n0 in aln_size:
aln_size[read_b_n0] += aln
else:
aln_size[read_b_n0] = aln
# print x
else:
sorted_aln = sorted(aln_size.items(), key=operator.itemgetter(1),reverse=True)
if (rd_lgths[read_no-1] > min_read_length
and len(sorted_aln) > num_aln
and sorted_aln[4][1] > aln_threshold):
reads_to_keep = reads_to_keep.union(set([read_no]))
reads_to_keep = reads_to_keep.union(set([sorted_aln[i][0] for i in range(5)]))
if len(reads_to_keep) > num_reads_to_keep:
break
# with open(output_txt,'a') as f:
# for rd_nm in aln_size:
# ln_write = str(read_no)+"\t"+str(rd_nm)+"\t"+str(aln_size[rd_nm])+"\t"+str(rd_lgths[read_no-1])+"\t"+str(rd_lgths[rd_nm-1])+"\n"
# f.write(ln_write)
read_no = int(x [0].replace(',', ''))
aln_size = {}
if x [2] == 'n':
read_b_n0 = int(x [1].replace(',', ''))
raw_aln_size = (end_pos-start_pos+0.0)
aln = raw_aln_size
# print read_b_n0, start_pos, end_pos, aln
if read_b_n0 in aln_size:
aln_size[read_b_n0] += aln
else:
aln_size[read_b_n0] = aln
# In[12]:
reads_to_keep_file = org_path+"reads_filtered.txt"
np.savetxt(reads_to_keep_file,sorted(list(reads_to_keep)), fmt="%d")
# In[14]:
new_base_path = base_path+bact_name+"_filtered/"
mkdir_cmd = "mkdir -p "+new_base_path
print(mkdir_cmd)
os.system(mkdir_cmd)
# In[16]:
new_read_file = new_base_path+"reads.fasta"
DBshow_cmd2 = ("DBshow "+ org_path + bact_name + " "
+ reads_to_keep_file + " > " + new_read_file)
print(DBshow_cmd2)
os.system(DBshow_cmd2)
# In[ ]: