forked from polyatail/lincrna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooled_txome.py
163 lines (129 loc) · 6.39 KB
/
pooled_txome.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
162
163
# -*- coding: utf-8 -*-
__author__ = "Andrew Sczesnak"
__email__ = "andrew.sczesnak@med.nyu.edu"
__date__ = "9/20/2011"
__version__ = 0.0
from optparse import OptionParser
import sys
import os
import _common
from wrappers import cufflinks, cuffcompare, samtools, scripture
def parse_options(arguments):
global options, args
parser = OptionParser(usage="%prog [options] <assembly> <sample1.sam> [...sampleN.sam]",
version="%prog " + str(__version__))
parser.add_option("-o",
dest="output_dir",
metavar="[./txome_out]",
default="./txome_out",
help="write output files to this directory")
parser.add_option("-p",
dest="num_threads",
type="int",
metavar="[1]",
default=1,
help="number of threads used during analysis")
parser.add_option("-L",
dest="labels",
metavar="sample1,...sampleN",
default=None,
help="comma-separated list of condition labels")
parser.add_option("-r",
dest="reference",
metavar="reference.gtf",
default=None,
help="reference annotation (i.e. refGene) GTF file")
parser.add_option("-M",
dest="mask",
metavar="mask.gtf",
default=None,
help="ignore all alignment within transcripts in this file")
parser.add_option("--prerun",
dest="prerun",
action="store_true",
default=False,
help="perform only the 'discovery' Cufflinks/Scripture runs")
parser.add_option("--pool-transcripts",
dest="pool_transcripts",
action="store_true",
default=False,
help="pool transcripts with Cuffcompare")
parser.add_option("--realrun",
dest="realrun",
action="store_true",
default=False,
help="perform only the quantitation Cufflinks runs")
options, args = parser.parse_args()
if len(args) < 2:
print "Error: Not enough arguments"
parser.print_help()
sys.exit(0)
if options.labels != None:
options.labels = options.labels.split(",")
if len(options.labels) <> len(args) - 1:
print "Error: When using -L, must specify a label for every condition"
parser.print_help()
sys.exit(0)
else:
options.labels = ["sample%s" % (x,) for x in range(len(args))]
options.genome_dir = _common.genome_fasta[args[0]]
options.assembly, options.chroms = _common.find_genome_files(options.genome_dir)
if options.assembly != args[0]:
print "Error: Specified assembly does not match assembly found"
parser.print_help()
sys.exit(0)
def main():
parse_options(sys.argv[1:])
normal_run = not (options.realrun or options.prerun or options.pool_transcripts)
if not os.path.exists(options.output_dir):
os.mkdir(options.output_dir)
if normal_run or options.prerun:
# run cufflinks in 'discovery' mode
for label, input_bam in zip(options.labels, args[1:]):
run_out_dir = os.path.join(options.output_dir, "cufflinks_" + label)
cl = cufflinks.Cufflinks(run_out_dir, options.num_threads)
cl.run(input_bam, ["--min-frags-per-transfrag", "0"] + \
([] if not options.mask else ["-M", options.mask]))
# make sure there's a BAM index on every input file
st = samtools.SAMTools()
for input_bam in args[1:]:
if st.index(input_bam) == False:
raise ValueError("Failed to index %s" % (input_bam,))
# run scripture in 'discovery' mode
for label, input_bam in zip(options.labels, args[1:]):
run_out_dir = os.path.join(options.output_dir, "scripture_" + label)
run_out_bed = os.path.join(run_out_dir, "transcripts.bed")
run_out_gtf = os.path.join(run_out_dir, "transcripts.gtf")
sc = scripture.Scripture(options.genome_dir,
run_out_dir,
options.num_threads)
sc.all_chrom(input_bam)
scripture.bed_to_gtf(open(run_out_bed, "r"),
open(run_out_gtf, "w"))
# run cuffcompare to generate the unique intersection of all output GTFs
if normal_run or options.pool_transcripts:
input_gtfs = []
for label in options.labels:
input_gtfs.append(os.path.join(options.output_dir,
"cufflinks_" + label,
"transcripts.gtf"))
input_gtfs.append(os.path.join(options.output_dir,
"scripture_" + label,
"transcripts.gtf"))
run_out_dir = os.path.join(options.output_dir, "cuffcompare")
cc = cuffcompare.Cuffcompare(options.genome_dir,
run_out_dir)
cc.run(input_gtfs,
([] if not options.reference else ["-r", options.reference]))
# re-run each sample separately in abundance calculation mode
if normal_run or options.realrun:
for label, input_bam in zip(options.labels, args[1:]):
run_out_dir = os.path.join(options.output_dir, "FPKM_" + label)
cl = cufflinks.Cufflinks(run_out_dir, options.num_threads)
cl.run(input_bam, ["--min-frags-per-transfrag", "0",
"-G", os.path.join(options.output_dir,
"cuffcompare",
"cuffcompare.combined.gtf")] + \
([] if not options.mask else ["-M", options.mask]))
if __name__ == "__main__":
main()