/
generate_ltm_inputs.py
176 lines (125 loc) · 5.91 KB
/
generate_ltm_inputs.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
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import os
import pandas as pd
import subprocess
import numpy as np
directory = os.path.join(
os.path.realpath(
os.path.dirname(__file__)))
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('input_file', help = 'Path to input .csv file.')
parser.add_argument('sample_id', help = 'Sample ID of the tree.')
parser.add_argument('--output_dir', help = 'Output directory for the sample', default = 'output/')
args = parser.parse_args()
return args
def get_analysis_ploidy(analysis_ploidy):
if 'autoploidy' in analysis_ploidy:
return '00'
elif 'diploid' in analysis_ploidy:
return '02'
raise Exception('Unrecognized ploidy: {}'.format(analysis_ploidy))
def get_sftp_ploidy(analysis_ploidy):
if 'autoploidy' in analysis_ploidy:
return 'hmmcopy_autoploidy'
elif 'diploid' in analysis_ploidy:
return 'hmmcopy_diploid'
raise Exception('Unrecognized ploidy: {}'.format(analysis_ploidy))
def get_cn_matrix_from_hdf(hmmcopy_hdf_file, ploidy):
df = get_reads_from_hdf(hmmcopy_hdf_file, ploidy)
df["bin"] = list(zip(df.chr, df.start, df.end))
df = df.pivot(index='cell_id', columns='bin', values='state')
chromosomes = map(str, range(1, 23)) + ['X', 'Y']
bins = pd.DataFrame(df.columns.values.tolist(),
columns=['chr', 'start', 'end'])
bins["chr"] = pd.Categorical(bins["chr"], chromosomes)
bins = bins.sort_values(['start', ])
bins = [tuple(v) for v in bins.values.tolist()]
df = df.sort_values(bins, axis=0).T
dropped_cells = df.columns[df.isna().all()].tolist()
print 'Dropping {} cells: {}'.format(len(dropped_cells), dropped_cells)
df = df.loc[:, ~df.isna().all()].astype(int)
df.columns = df.columns.astype(str)
df = df.reset_index()
chrom = []
start = []
end = []
width = []
for i, b in df['bin'].iteritems():
chrom.append(b[0])
start.append(b[1])
end.append(b[2])
width.append(b[2] - b[1] + 1)
df['chr'] = chrom
df['start'] = start
df['end'] = end
df['width'] = width
return df, dropped_cells
def get_quality_from_hdf(hmmcopy_hdf_file, alignment_hdf_file, ploidy, merged_csv):
hmmcopy_metrics = pd.read_hdf(hmmcopy_hdf_file, '/hmmcopy/metrics/' + ploidy[-1]) # Keep total_mapped_reads from this
alignment_metrics = pd.read_hdf(alignment_hdf_file, '/alignment/metrics')
alignment_metrics = alignment_metrics.drop(columns = 'total_mapped_reads')
merged_df = pd.merge(hmmcopy_metrics, alignment_metrics, how = 'outer',
on = ['cell_id', 'row', 'column', 'primer_i5', 'primer_i7', 'img_col',
'sample_type', 'experimental_condition', 'cell_call',
'index_i5', 'index_i7'])
merged_df = merged_df.rename({'log_likelihood': 'loglikehood'}, axis = 'columns')
merged_df.to_csv(merged_csv)
def classify_metrics(infile, outfile):
script = os.path.join(directory, 'classify.R')
cmd = ['Rscript', script, infile, outfile]
print ' '.join(cmd)
subprocess.check_call(cmd)
def get_reads_from_hdf(hdf_file, ploidy):
return pd.read_hdf(hdf_file, '/hmmcopy/reads/' + ploidy[-1])
def main():
args = get_args()
output_dir = os.path.join(args.output_dir, args.sample_id)
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
# Read contents of Jira ticket table
df = pd.read_csv(args.input_file, index_col = 0)
cn_list = []
for jira_ticket, row in df.iterrows():
print 'Filtering ticket {}'.format(jira_ticket)
analysis_ploidy = get_analysis_ploidy(row['analysis_ploidy'])
sftp_ploidy = get_sftp_ploidy(row['analysis_ploidy'])
genesis_dir_path = os.path.join('/genesis/shahlab/danlai/SC-803/hmmcopy/merged_output', jira_ticket, analysis_ploidy)
cn_matrix_file = os.path.join(genesis_dir_path, row['library_id'] + '_cn_matrix.csv')
all_metrics_summary_file = os.path.join(genesis_dir_path, row['library_id'] + '_all_metrics_summary.csv')
reads_file = os.path.join(genesis_dir_path, row['library_id'] + '_reads.csv')
hmmcopy_hdf_file = os.path.join('/projects/sftp/shahlab/singlecell', jira_ticket, sftp_ploidy, row['library_id'] + '_hmmcopy.h5')
alignment_hdf_file = os.path.join('/projects/sftp/shahlab/singlecell', jira_ticket, 'alignment', row['library_id'] + '_alignment_metrics.h5')
if os.path.exists(cn_matrix_file):
cn_df = pd.read_csv(cn_matrix_file, dtype = {'chr': str})
dropped_cells = None
elif os.path.exists(hmmcopy_hdf_file):
cn_df, dropped_cells = get_cn_matrix_from_hdf(hmmcopy_hdf_file, analysis_ploidy)
merged_csv = os.path.join(output_dir, '{}_all_metrics_summary.csv'.format(jira_ticket))
all_metrics_summary_file = os.path.join(output_dir, '{}_all_metrics_summary_classified.csv'.format(jira_ticket))
get_quality_from_hdf(hmmcopy_hdf_file, alignment_hdf_file, analysis_ploidy, merged_csv)
classify_metrics(merged_csv, all_metrics_summary_file)
cn_df = cn_df.drop(columns = 'bin')
else:
raise Exception('Unable to find results for ticket {}'.format(jira_ticket))
# Filter out cells with quality < 0.75
all_metrics_summary_df = pd.read_csv(all_metrics_summary_file)
if dropped_cells:
all_metrics_summary_df = all_metrics_summary_df.drop(index = all_metrics_summary_df.loc[all_metrics_summary_df['cell_id'].isin(dropped_cells)].index)
cn_df = cn_df.drop(columns = all_metrics_summary_df[all_metrics_summary_df['quality'] < 0.75]['cell_id'])
cn_list.append(cn_df)
# Concatenate all copy number matrices together
cn_matrix = pd.concat(cn_list, axis = 1, join = 'outer')
cn_matrix = cn_matrix.T.drop_duplicates().T # Get rid of any duplicate columns
cn_matrix = cn_matrix.set_index(['chr', 'start', 'end', 'width'])
if os.path.exists(reads_file):
reads_df = pd.read_csv(reads_file)
else:
reads_df = get_reads_from_hdf(hmmcopy_hdf_file, analysis_ploidy)
# Filter out bins with mappability < 0.99 and write to csv file
i = reads_df.loc[(reads_df['cell_id'] == reads_df.iloc[0]['cell_id']) & (reads_df['map'] >= 0.99)].index
cn_matrix = cn_matrix.iloc[i]
cn_matrix_path = os.path.join(output_dir, 'cn_matrix.csv')
cn_matrix.to_csv(cn_matrix_path)
if __name__ == "__main__":
main()