annotate Iterative_mapping/iterative_map.py @ 114:e269e4c6818e draft

Uploaded
author tyty
date Tue, 14 Apr 2015 14:12:35 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
e269e4c6818e Uploaded
tyty
parents:
diff changeset
1 #!/usr/bin/env python
e269e4c6818e Uploaded
tyty
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e269e4c6818e Uploaded
tyty
parents:
diff changeset
3
e269e4c6818e Uploaded
tyty
parents:
diff changeset
4 import sys
e269e4c6818e Uploaded
tyty
parents:
diff changeset
5 import os
e269e4c6818e Uploaded
tyty
parents:
diff changeset
6 from read_file import *
e269e4c6818e Uploaded
tyty
parents:
diff changeset
7 from read_s_file import *
e269e4c6818e Uploaded
tyty
parents:
diff changeset
8 import random
e269e4c6818e Uploaded
tyty
parents:
diff changeset
9 import string
e269e4c6818e Uploaded
tyty
parents:
diff changeset
10
e269e4c6818e Uploaded
tyty
parents:
diff changeset
11 type_input = sys.argv[1]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
12 seq_file = sys.argv[2]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
13 ref_file = sys.argv[3]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
14 shift = sys.argv[4]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
15 length = sys.argv[5]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
16 t_end = sys.argv[6]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
17 map_type = sys.argv[7]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
18 output_file = sys.argv[8]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
19
e269e4c6818e Uploaded
tyty
parents:
diff changeset
20
e269e4c6818e Uploaded
tyty
parents:
diff changeset
21 if map_type!="default":
e269e4c6818e Uploaded
tyty
parents:
diff changeset
22 s = ""
e269e4c6818e Uploaded
tyty
parents:
diff changeset
23 sm = ""
e269e4c6818e Uploaded
tyty
parents:
diff changeset
24 s = s+"-v "+sys.argv[9]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
25 sm = sm+"-v "+sys.argv[9]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
26 sm = sm+" -5 "+sys.argv[10]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
27 sm = sm+" -3 "+sys.argv[11]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
28 s = s+" -k "+sys.argv[12]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
29 sm = sm+" -k "+sys.argv[12]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
30 if sys.argv[13]:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
31 s = s+" -a"
e269e4c6818e Uploaded
tyty
parents:
diff changeset
32 sm = sm+" -a"
e269e4c6818e Uploaded
tyty
parents:
diff changeset
33 if int(sys.argv[14])>=1:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
34 s = s+" -m "+sys.argv[14]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
35 sm = sm+" -m "+sys.argv[14]
e269e4c6818e Uploaded
tyty
parents:
diff changeset
36 if sys.argv[15]:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
37 s = s+" --best --strata "
e269e4c6818e Uploaded
tyty
parents:
diff changeset
38 sm = sm+" --best --strata "
e269e4c6818e Uploaded
tyty
parents:
diff changeset
39
e269e4c6818e Uploaded
tyty
parents:
diff changeset
40 else:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
41 s = "-v 3 -a --best --strata "
e269e4c6818e Uploaded
tyty
parents:
diff changeset
42 sm = "-v 3 -a --best --strata "
e269e4c6818e Uploaded
tyty
parents:
diff changeset
43
e269e4c6818e Uploaded
tyty
parents:
diff changeset
44 ospath = os.path.realpath(sys.argv[0])
e269e4c6818e Uploaded
tyty
parents:
diff changeset
45 ost = ospath.split('/')
e269e4c6818e Uploaded
tyty
parents:
diff changeset
46 syspath = ""
e269e4c6818e Uploaded
tyty
parents:
diff changeset
47 for i in range(len(ost)-1):
e269e4c6818e Uploaded
tyty
parents:
diff changeset
48 syspath = syspath+ost[i].strip()
e269e4c6818e Uploaded
tyty
parents:
diff changeset
49 syspath = syspath+'/'
e269e4c6818e Uploaded
tyty
parents:
diff changeset
50
e269e4c6818e Uploaded
tyty
parents:
diff changeset
51 syspathrs = os.getcwd()
e269e4c6818e Uploaded
tyty
parents:
diff changeset
52 syspathrs = syspathrs+'/'
e269e4c6818e Uploaded
tyty
parents:
diff changeset
53
e269e4c6818e Uploaded
tyty
parents:
diff changeset
54 os.system("bowtie-build -f "+ref_file+" "+syspathrs+"ref > "+syspathrs+"log.txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
55
e269e4c6818e Uploaded
tyty
parents:
diff changeset
56 os.system("cp "+seq_file+" "+syspathrs+"seq0.fa")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
57
e269e4c6818e Uploaded
tyty
parents:
diff changeset
58 if type_input == "fasta":
e269e4c6818e Uploaded
tyty
parents:
diff changeset
59 tp = 'fasta'
e269e4c6818e Uploaded
tyty
parents:
diff changeset
60 if type_input == "fastq":
e269e4c6818e Uploaded
tyty
parents:
diff changeset
61 tp = 'fastq'
e269e4c6818e Uploaded
tyty
parents:
diff changeset
62
e269e4c6818e Uploaded
tyty
parents:
diff changeset
63 k = 0
e269e4c6818e Uploaded
tyty
parents:
diff changeset
64
e269e4c6818e Uploaded
tyty
parents:
diff changeset
65 if type_input == "fasta":
e269e4c6818e Uploaded
tyty
parents:
diff changeset
66 os.system("bowtie "+sm+"-f "+syspathrs+"ref"+" "+syspathrs+"seq"+str(k)+".fa --quiet -S > "+syspathrs+"map"+str(k)+".sam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
67 if type_input == "fastq":
e269e4c6818e Uploaded
tyty
parents:
diff changeset
68 os.system("bowtie "+sm+"-q "+syspathrs+"ref"+" "+syspathrs+"seq"+str(k)+".fa --quiet -S > "+syspathrs+"map"+str(k)+".sam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
69
e269e4c6818e Uploaded
tyty
parents:
diff changeset
70 while(True):
e269e4c6818e Uploaded
tyty
parents:
diff changeset
71 os.system("samtools view -Sb -F 0xfff "+syspathrs+"map"+str(k)+".sam > "+syspathrs+"mapped"+str(k)+".bam 2>"+syspathrs+"log.txt") #get mapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
72 os.system("samtools view -Sb -f 0x4 "+syspathrs+"map"+str(k)+".sam > "+syspathrs+"umapped"+str(k)+".bam 2>"+syspathrs+"log.txt") #get unmapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
73 os.system("samtools view -Sb -f 0x10 "+syspathrs+"map"+str(k)+".sam > "+syspathrs+"rmapped"+str(k)+".bam 2>"+syspathrs+"log.txt") #get reversed mapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
74 os.system("samtools merge -f "+syspathrs+"unmapped"+str(k)+".bam "+syspathrs+"umapped"+str(k)+".bam "+syspathrs+"rmapped"+str(k)+".bam") #get reversed mapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
75 os.system("samtools view -h -o "+syspathrs+"unmapped"+str(k)+".sam "+syspathrs+"unmapped"+str(k)+".bam") #get reversed mapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
76 if k>0:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
77 os.system("samtools view -h -o "+syspathrs+"mapped"+str(k)+".sam "+syspathrs+"mapped"+str(k)+".bam") #get reversed mapped reads
e269e4c6818e Uploaded
tyty
parents:
diff changeset
78 os.system("cut -f 1 "+syspathrs+"unmapped"+str(k)+".sam > "+syspathrs+"unmapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
79 os.system("cut -f 1 "+syspathrs+"mapped"+str(k)+".sam > "+syspathrs+"mapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
80 os.system("python "+syspath+"remove_map.py "+syspathrs+"unmapped"+str(k)+".txt "+syspathrs+"mapped"+str(k)+".txt "+syspathrs+"runmapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
81 os.system("rm "+syspathrs+"mapped"+str(k)+".sam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
82 os.system("rm "+syspathrs+"mapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
83 os.system("rm "+syspathrs+"unmapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
84 else:
e269e4c6818e Uploaded
tyty
parents:
diff changeset
85 os.system("cut -f 1 "+syspathrs+"unmapped"+str(k)+".sam > "+syspathrs+"runmapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
86
e269e4c6818e Uploaded
tyty
parents:
diff changeset
87 os.system("rm "+syspathrs+"unmapped"+str(k)+".bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
88 os.system("rm "+syspathrs+"umapped"+str(k)+".bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
89 os.system("rm "+syspathrs+"rmapped"+str(k)+".bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
90 os.system("python "+syspath+"seq_track.py "+syspathrs+"runmapped"+str(k)+".txt "+syspathrs+"seq"+str(k)+".fa "+syspathrs+"unmap_seq"+str(k)+".fa "+tp) #get unmapped sequence
e269e4c6818e Uploaded
tyty
parents:
diff changeset
91 os.system("python "+syspath+"truncate.py "+syspathrs+"unmap_seq"+str(k)+".fa "+shift+" "+syspathrs+"seq"+str(k+1)+".fa "+length+" "+t_end) #truncate unmapped sequence
e269e4c6818e Uploaded
tyty
parents:
diff changeset
92 os.system("rm "+syspathrs+"seq"+str(k)+".fa") #Remove sequences being mapped
e269e4c6818e Uploaded
tyty
parents:
diff changeset
93 os.system("rm "+syspathrs+"map"+str(k)+".sam") #Remove mapping file
e269e4c6818e Uploaded
tyty
parents:
diff changeset
94 os.system("rm "+syspathrs+"unmap_seq"+str(k)+".fa") #Remove unmapped sequnce
e269e4c6818e Uploaded
tyty
parents:
diff changeset
95 os.system("rm "+syspathrs+"runmapped"+str(k)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
96 os.system("rm "+syspathrs+"unmapped"+str(k)+".sam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
97
e269e4c6818e Uploaded
tyty
parents:
diff changeset
98 os.system("wc -l "+syspathrs+"seq"+str(k+1)+".fa > "+syspathrs+"count"+str(k+1)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
99 c = read_sp_file(syspathrs+"count"+str(k+1)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
100 if c[0][0] == '0': #If no reads is in the sequence file, stop
e269e4c6818e Uploaded
tyty
parents:
diff changeset
101 os.system("rm "+syspathrs+"count"+str(k+1)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
102 os.system("rm "+syspathrs+"seq"+str(k+1)+".fa")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
103 break
e269e4c6818e Uploaded
tyty
parents:
diff changeset
104 os.system("rm "+syspathrs+"count"+str(k+1)+".txt")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
105 k = k+1
e269e4c6818e Uploaded
tyty
parents:
diff changeset
106 os.system("bowtie "+s+"-f "+syspathrs+"ref"+" "+syspathrs+"seq"+str(k)+".fa --quiet -S > "+syspathrs+"map"+str(k)+".sam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
107
e269e4c6818e Uploaded
tyty
parents:
diff changeset
108
e269e4c6818e Uploaded
tyty
parents:
diff changeset
109 ss = ""
e269e4c6818e Uploaded
tyty
parents:
diff changeset
110 for i in range(0,k+1):
e269e4c6818e Uploaded
tyty
parents:
diff changeset
111 ss = ss+" "+syspathrs+"mapped"+str(i)+".bam"
e269e4c6818e Uploaded
tyty
parents:
diff changeset
112
e269e4c6818e Uploaded
tyty
parents:
diff changeset
113
e269e4c6818e Uploaded
tyty
parents:
diff changeset
114 os.system("samtools merge -f "+syspathrs+"combine.bam"+" "+ss)
e269e4c6818e Uploaded
tyty
parents:
diff changeset
115 os.system("samtools sort "+syspathrs+"combine.bam sorted")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
116 os.system("samtools view -b -h sorted.bam > " + output_file)
e269e4c6818e Uploaded
tyty
parents:
diff changeset
117 #print("samtools merge mapped_all.bam"+ss)
e269e4c6818e Uploaded
tyty
parents:
diff changeset
118 os.system("rm "+syspathrs+"mapped*.bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
119 os.system("rm "+syspathrs+"combine.bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
120 os.system("rm "+syspathrs+"sorted.bam")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
121 os.system("rm "+syspathrs+"ref*")
e269e4c6818e Uploaded
tyty
parents:
diff changeset
122 #os.system("rm -r "+syspathrs)
e269e4c6818e Uploaded
tyty
parents:
diff changeset
123
e269e4c6818e Uploaded
tyty
parents:
diff changeset
124