forked from dib-lab/MMETSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transdecoder.py
148 lines (135 loc) · 5.39 KB
/
transdecoder.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
import os
import os.path
from os.path import basename
from urllib import urlopen
from urlparse import urlparse
import subprocess
from subprocess import Popen, PIPE
import urllib
import shutil
import glob
# custom Lisa module
import clusterfunc
def get_data(thefile):
count=0
url_data={}
with open(thefile,"rU") as inputfile:
headerline=next(inputfile).split(',')
#print headerline
position_name=headerline.index("ScientificName")
position_reads=headerline.index("Run")
position_ftp=headerline.index("download_path")
for line in inputfile:
line_data=line.split(',')
name="_".join(line_data[position_name].split())
read_type=line_data[position_reads]
ftp=line_data[position_ftp]
name_read_tuple=(name,read_type)
print name_read_tuple
#check to see if Scientific Name and run exist
if name_read_tuple in url_data.keys():
#check to see if ftp exists
if ftp in url_data[name_read_tuple]:
print "url already exists:", ftp
else:
url_data[name_read_tuple].append(ftp)
else:
url_data[name_read_tuple] = [ftp]
return url_data
def fix_fasta(trinity_fasta,trinity_dir,sra):
# insert SRR before
#os.chdir(trinity_dir)
trinity_out=trinity_dir+sra+".Trinity.fixed.fa"
fix="""
sed -e "s/^>/>{}_/" {} | sed 's_|_-_g' | sed "s/\s.*$//" > {}
""".format(sra,trinity_fasta,trinity_out,trinity_out,trinity_out)
#print fix
#s=subprocess.Popen(fix,shell=True)
#s.wait()
#os.chdir("/home/ubuntu/MMETSP/")
return trinity_out
def transdecoder_LongOrf(transdecoderdir,trinity_fasta):
os.chdir(transdecoderdir)
print os.getcwd()
print os.path.isfile(trinity_fasta)
if os.path.isfile(trinity_fasta):
print "file exists:",trinity_fasta
trans_command="""
/home/ubuntu/TransDecoder-3.0.0/TransDecoder.LongOrfs -t {} -m 100
""".format(trinity_fasta)
print trans_command
s=subprocess.Popen(trans_command,shell=True)
s.wait()
print "Transdecoder finished."
os.chdir("/home/ubuntu/MMETSP/")
def transdecoder_Predict(transdecoderdir,trinity_fasta_prefix):
os.chdir(transdecoderdir)
print os.getcwd()
print os.path.isfile(trinity_fasta_prefix)
trans_predict_command="""
/home/ubuntu/TransDecoder-3.0.0/TransDecoder.Predict -t {}
""".format(trinity_fasta_prefix)
print trans_predict_command
s=subprocess.Popen(trans_predict_command,shell=True)
s.wait()
os.chdir("/home/ubuntu/MMETSP/")
def get_longest_ORF(transdecoderdir,trinity_fasta):
os.chdir(transdecoderdir)
print os.getcwd()
print os.path.isfile(trinity_fasta+".transdecoder.pep")
get_longest_orf_command="""
/home/ubuntu/TransDecoder-3.0.0/util/get_longest_ORF_per_transcript.pl {}.transdecoder.pep > {}.transdecoder.pep.longest.pep
""".format(trinity_fasta,trinity_fasta)
print get_longest_orf_command
s=subprocess.Popen(get_longest_orf_command,shell=True)
s.wait()
os.chdir("/home/ubuntu/MMETSP/")
def fix(transdecoderdir,trinity_fasta,sra,new_trinity_fasta):
os.chdir(transdecoderdir)
fix_command="""
sed -e 's/>.*::SRR/>SRR/' {}{}.transdecoder.pep.longest.pep | sed -e 's/::.*//' | sed 's/\*//g'i > {}{}.Trinity.pep.longest
""".format(transdecoderdir,trinity_fasta,new_trinity_fasta,sra)
print fix_command
s=subprocess.Popen(fix_command,shell=True)
s.wait()
os.chdir("/home/ubuntu/MMETSP/")
def copy_files(trinity_file,trinity_fasta,transdecoderdir,sra):
copy_command="cp "+transdecoderdir+sra+".Trinity.fixed.fa.transdecoder.pep.longest.pep"+" "+"/home/ubuntu/MMETSP/mmetsp_trinity_finished/"
print copy_command
#copy_command="cp "+trinity_file+" "+transdecoderdir+trinity_fasta
#print copy_command
#rm_command="rm -rf "+transdecoderdir+trinity_fasta+".transdecoder_dir"
#print rm_command
#s=subprocess.Popen(copy_command,shell=True)
#s.wait()
def execute(basedir,url_data):
for item in url_data.keys():
organism=item[0]
org_seq_dir=basedir+organism+"/"
url_list=url_data[item]
for url in url_list:
sra=basename(urlparse(url).path)
newdir=org_seq_dir+sra+"/"
trinitydir=newdir+"trinity/trinity_out/"
transdecoderdir=newdir+"transdecoder/"
clusterfunc.check_dir(transdecoderdir)
trinity_in_fasta=trinitydir+"Trinity.fasta"
#trinity_fasta_prefix=sra+".Trinity.fa"
trinity_fasta_prefix=sra+".Trinity.fixed.fa"
trinity_fasta=fix_fasta(trinity_in_fasta,trinitydir,sra)
#copy_files(trinity_fasta,trinity_fasta_prefix,transdecoderdir)
#transdecoder_LongOrf(transdecoderdir,trinity_fasta_prefix)
#transdecoder_Predict(transdecoderdir,trinity_fasta_prefix)
#get_longest_ORF(transdecoderdir,trinity_fasta_prefix)
#new_trinity_fasta="/mnt/mmetsp_trinity_finished/"
#clusterfunc.check_dir(new_trinity_fasta)
#fix(transdecoderdir,trinity_fasta_prefix,sra,new_trinity_fasta)
copy_files(trinity_fasta_prefix,trinity_fasta,transdecoderdir,sra)
file_locations={"/mnt2/mmetsp/":"MMETSP_SRA_Run_Info_subset_d.csv",
"/mnt3/mmetsp/":"MMETSP_SRA_Run_Info_subset_a.csv",
"/mnt4/mmetsp/":"MMETSP_SRA_Run_Info_subset_b.csv"}
for basedir in file_locations.keys():
datafile=file_locations[basedir]
url_data=get_data(datafile)
print url_data
execute(basedir,url_data)