annotate Iterative_mapping/iterative_map.py @ 88:40b8071a44f7 draft

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