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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
import sys
import numpy as np
class UnknownChrom(Exception):
pass
def my_showwarning(message, category, filename, lineno=None, file=None,
line=None):
sys.stderr.write("Warning: %s\n" % message)
def invert_strand(iv):
iv2 = iv.copy()
if iv2.strand == "+":
iv2.strand = "-"
elif iv2.strand == "-":
iv2.strand = "+"
else:
raise ValueError("Illegal strand")
return iv2
def _merge_counts(
results,
attributes,
additional_attributes,
sparse=False,
dtype=np.float32,
):
barcodes = 'cell_barcodes' in results
if barcodes:
cbs = results['cell_barcodes']
counts = results['counts']
feature_attr = sorted(attributes.keys())
other_features = [
('__no_feature', 'empty'),
('__ambiguous', 'ambiguous'),
('__too_low_aQual', 'lowqual'),
('__not_aligned', 'notaligned'),
('__alignment_not_unique', 'nonunique'),
]
fea_names = [fea for fea in feature_attr] + [fea[0] for fea in other_features]
L = len(fea_names)
if barcodes:
n = len(cbs)
else:
n = len(results)
if not sparse:
table = np.zeros(
(n, L),
dtype=dtype,
)
else:
from scipy.sparse import lil_matrix
table = lil_matrix((n, L), dtype=dtype)
if not barcodes:
fea_ids = [fea for fea in feature_attr] + [fea[1] for fea in other_features]
for j, r in enumerate(results):
for i, fn in enumerate(fea_ids):
if i < len(feature_attr):
countji = r['counts'][fn]
else:
countji = r[fn]
if countji > 0:
table[j, i] = countji
else:
for j, cb in enumerate(cbs):
for i, fn in enumerate(fea_names):
countji = counts[cb][fn]
if countji > 0:
table[j, i] = countji
if sparse:
table = table.tocsr()
feature_metadata = {
'id': fea_names,
}
for iadd, attr in enumerate(additional_attributes):
feature_metadata[attr] = [attributes[fn][iadd] for fn in feature_attr]
return {
'feature_metadata': feature_metadata,
'table': table,
}
def _count_results_to_tsv(
results,
samples_name,
attributes,
additional_attributes,
output_filename,
output_delimiter,
output_append=False,
add_tsv_header=False
):
barcodes = 'cell_barcodes' in results
pad = ['' for attr in additional_attributes]
if barcodes:
cbs = results['cell_barcodes']
counts = results['counts']
# Print or write header
fields = [''] + pad + cbs
line = output_delimiter.join(fields)
if output_filename == '':
print(line)
else:
with open(output_filename, 'w') as f:
f.write(line)
f.write('\n')
elif add_tsv_header:
# Write the header.
# Only get here if we don't have cell barcodes, i.e. this is not called by htseq-count-barcode,
# and user wants the tsv header
file_header = output_delimiter.join([''] + pad + samples_name)
if output_filename == '':
print(file_header)
else:
# If append to existing file, then open as a
file_open_opt = 'a' if output_append else 'w'
with open(output_filename, file_open_opt) as f:
f.write(file_header)
f.write('\n')
# Each feature is a row with feature id, additional attrs, and counts
feature_attr = sorted(attributes.keys())
for ifn, fn in enumerate(feature_attr):
if not barcodes:
fields = [fn] + attributes[fn] + [str(r['counts'][fn]) for r in results]
else:
fields = [fn] + attributes[fn] + [str(counts[cb][fn]) for cb in cbs]
line = output_delimiter.join(fields)
if output_filename == '':
print(line)
else:
omode = 'a' if output_append or (ifn > 0) or barcodes or add_tsv_header else 'w'
with open(output_filename, omode) as f:
f.write(line)
f.write('\n')
# Add other features (unmapped, etc.)
other_features = [
('__no_feature', 'empty'),
('__ambiguous', 'ambiguous'),
('__too_low_aQual', 'lowqual'),
('__not_aligned', 'notaligned'),
('__alignment_not_unique', 'nonunique'),
]
for title, fn in other_features:
if not barcodes:
fields = [title] + pad + [str(r[fn]) for r in results]
else:
fields = [title] + pad + [str(counts[cb][title]) for cb in cbs]
line = output_delimiter.join(fields)
if output_filename == '':
print(line)
else:
with open(output_filename, 'a') as f:
f.write(line)
f.write('\n')
def _count_table_to_mtx(
filename,
table,
feature_metadata,
samples,
):
if not str(filename).endswith('.mtx'):
raise ValueError('Matrix Marker filename should end with ".mtx"')
try:
from scipy.io import mmwrite
except ImportError:
raise ImportError('Install scipy for mtx support')
filename_pfx = str(filename)[:-4]
filename_feature_meta = filename_pfx+'_features.tsv'
filename_samples = filename_pfx+'_samples.tsv'
# Write main matrix (features as columns)
mmwrite(
filename,
table,
)
# Write input filenames
with open(filename_samples, 'wt') as fout:
for fn in samples:
fout.write(fn+'\n')
# Write feature metadata (ids and additional attributes)
with open(filename_feature_meta, 'wt') as fout:
nkeys = len(feature_metadata)
for ik, key in enumerate(feature_metadata):
if ik != nkeys - 1:
fout.write(key+'\t')
else:
fout.write(key+'\n')
nfeatures = len(feature_metadata[key])
for i in range(nfeatures):
for ik, key in enumerate(feature_metadata):
if ik != nkeys - 1:
fout.write(feature_metadata[key][i]+'\t')
else:
fout.write(feature_metadata[key][i]+'\n')
def _count_table_to_h5ad(
filename,
table,
feature_metadata,
samples,
):
try:
import anndata
except ImportError:
raise ImportError('Install the anndata package for h5ad support')
# If they have anndata, they have scipy and pandas too
import pandas as pd
# We don't have additional attribute (e.g. gene name) for htseq specific features like __no_feature.
# Hence the trick is to convert the array to series so the value for htseq specific features like __no_feature
# column is set NaN.
# See: https://stackoverflow.com/questions/19736080/creating-dataframe-from-a-dictionary-where-entries-have-different-lengths
feature_metadata = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in feature_metadata.items()]))
feature_metadata.set_index(feature_metadata.columns[0], inplace=True)
adata = anndata.AnnData(
X=table,
obs=pd.DataFrame([], index=samples),
var=feature_metadata,
)
adata.write_h5ad(filename)
def _count_table_to_loom(
filename,
table,
feature_metadata,
samples,
):
try:
import loompy
except ImportError:
raise ImportError('Install the loompy package for loom support')
# Loom uses features as rows...
layers = {'': table.T}
row_attrs = feature_metadata
col_attrs = {'_index': samples}
loompy.create(
filename,
layers=layers,
row_attrs=row_attrs,
col_attrs=col_attrs,
)
def _write_output(
results,
samples,
attributes,
additional_attributes,
output_filename,
output_delimiter,
output_append,
sparse=False,
dtype=np.float32,
add_tsv_header=False
):
"""
Export the gene counts as tsv/csv, mtx, loom, h5ad files.
Note, need to update the parameter documentations.
Parameters
----------
results : list
List of dictionaries with each element representing the counts for an input BAM file.
Note, the list is in order of the samples parameter. So the first element in the list corresponds to
the first file in samples parameter.
samples : list
List of input BAM files.
"""
# Write output to stdout or TSV/CSV
if output_filename == '':
_count_results_to_tsv(
results,
samples,
attributes,
additional_attributes,
output_filename,
output_delimiter,
output_append=False,
add_tsv_header=add_tsv_header
)
return
# Get file extension/format
output_sfx = output_filename.split('.')[-1].lower()
if output_sfx in ('csv', 'tsv'):
_count_results_to_tsv(
results,
samples,
attributes,
additional_attributes,
output_filename,
output_delimiter,
output_append,
add_tsv_header=add_tsv_header
)
return
# Make unified object of counts and feature metadata
output_dict = _merge_counts(
results,
attributes,
additional_attributes,
sparse=sparse,
dtype=dtype,
)
if output_sfx == 'mtx':
_count_table_to_mtx(
output_filename,
output_dict['table'],
output_dict['feature_metadata'],
samples,
)
return
if output_sfx == 'loom':
_count_table_to_loom(
output_filename,
output_dict['table'],
output_dict['feature_metadata'],
samples,
)
return
if output_sfx == 'h5ad':
_count_table_to_h5ad(
output_filename,
output_dict['table'],
output_dict['feature_metadata'],
samples,
)
return
raise ValueError(
f'Format not recognized for output count file: {output_sfx}')
|