annotate Iterative_mapping/iterative_map.py @ 62:58df1060fea1 draft

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