Skip to content

Instantly share code, notes, and snippets.

@jcausey-astate
Last active September 26, 2016 20:15
Show Gist options
  • Save jcausey-astate/10f579694dd3470c4c6b3a4dad1fdee1 to your computer and use it in GitHub Desktop.
Save jcausey-astate/10f579694dd3470c4c6b3a4dad1fdee1 to your computer and use it in GitHub Desktop.
Given a CSV or tab-delimited input file (ASCII-encoding), replace all missing values or all occurances of a specified value with a new marker. Useful for large scientific data where some values are missing (i.e. gene microarrays, etc).
#! /usr/bin/env python
###################################################################################################
# Given a CSV or tab-delimited input file (ASCII-encoding), replace all
# missing values or all occurances of a specified value with a new marker.
# Useful for large scientific data where some values are missing (i.e. gene
# microarrays, etc)
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Jason L Causey, Arkansas State University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###################################################################################################
usage = """
Usage:
{0} ascii_matrix_file [--help] [--missing=MISSING-MARKER] [--fill=FILL-MARKER] [--output=OUTPUT-FILE]
Fill all instances of MISSING-MARKER in an ASCII-format (CSV or tab-delimited)
matrix or table with the value specified by MISSING-FILL
ascii_matrix_file - a CSV or tab-delimited file in ASCII encoding
--help - show this help message and exit
--missing=MISSING-MARKER - MISSING-MARKER is the value in input file that currently
marks "missing value" locations.
Default is '' (no value at all) - literally missing.
--fill=FILL-MARKER - FILL-MARKER is the value to place in output file for
all missing locations.
Default is 0.
--output=OUTPUT-FILE - direct output to OUTPUT-FILE instead of stdout
Default output is to stdout
"""
import sys, csv, os
def usage(code=0):
print(usage.format(os.path.basename(sys.argv[0])))
if code != None:
sys.exit(code)
if len(sys.argv) < 2:
usage(1)
inname = sys.argv[1]
outname = None
missing_ch = ''
fill_value = '0'
ids_file = None
ids = None
if len(sys.argv) > 2:
for i in range(2, 6):
if len(sys.argv) > i and sys.argv[i][0:3] == '--h':
usage(0)
if len(sys.argv) > i and sys.argv[i].split('=')[0] == '--missing':
missing_ch = sys.argv[i].split('=')[1]
if len(sys.argv) > i and sys.argv[i].split('=')[0] == '--fill':
fill_value = sys.argv[i].split('=')[1]
if len(sys.argv) > i and sys.argv[i].split('=')[0] == '--output':
outname = sys.argv[i].split('=')[1]
of = sys.stdout
with open(inname, 'rb') as csvfile:
dialect = csv.Sniffer().sniff(csvfile.read(8*1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
if outname != None:
of = open(outname, 'wb')
writer = csv.writer(of, dialect)
for line in reader:
line = [fill_value if v == missing_ch else v for v in line]
writer.writerow(line)
if outname != None:
of.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment