annotate src/breadcrumbs/scripts/scriptEnvToTable.py @ 0:2f4f6f08c8c4 draft

Uploaded
author george-weingart
date Tue, 13 May 2014 21:58:57 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
2 """
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
3 Author: Timothy Tickle
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
4 Description: Convert Env file to table
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
5 """
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
6
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
7 __author__ = "Timothy Tickle"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
8 __copyright__ = "Copyright 2012"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
9 __credits__ = ["Timothy Tickle"]
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
10 __license__ = ""
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
11 __version__ = ""
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
12 __maintainer__ = "Timothy Tickle"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
13 __email__ = "ttickle@sph.harvard.edu"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
14 __status__ = "Development"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
15
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
16 import sys
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
17 import argparse
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
18 import csv
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
19
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
20
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
21 #Set up arguments reader
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
22 argp = argparse.ArgumentParser( prog = "scriptEnvToTable.py",
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
23 description = """Convert Env file to table""" )
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
24
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
25 #Arguments
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
26 #For table
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
27 argp.add_argument("strEnvFile", metavar = "EnvFile", help ="EnvFile data file")
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
28 argp.add_argument("strOutputFile", metavar = "OutputFile", help ="Output File")
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
29 args = argp.parse_args( )
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
30
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
31 hndlReader = csv.reader(open(args.strEnvFile,'rU'), delimiter="\t")
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
32
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
33 lsListOfIDs = []
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
34 lsListOfFeatures = []
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
35 dictValues = {}
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
36 for lsLine in hndlReader:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
37 print(lsLine)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
38 lsListOfIDs.append(lsLine[1])
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
39 lsListOfFeatures.append(lsLine[0])
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
40 tpleKey = tuple([lsLine[1],lsLine[0]])
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
41 if tpleKey in dictValues:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
42 print("Error:: Duplicate key entries found")
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
43 exit(1)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
44 dictValues[tpleKey] = lsLine[2]
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
45
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
46 lsListOfIDs = list(set(lsListOfIDs))
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
47 lsListOfFeatures = list(set(lsListOfFeatures))
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
48 print(lsListOfIDs)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
49 print(lsListOfFeatures)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
50 hndlWrite = csv.writer(open(args.strOutputFile,'w'), delimiter="\t")
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
51 hndlWrite.writerow(["ID"]+lsListOfIDs)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
52 for sFeature in lsListOfFeatures:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
53 lsFeatureLine = [sFeature]
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
54 for sSample in lsListOfIDs:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
55 lsFeatureLine.append(dictValues.get(tuple([sSample,sFeature]),0))
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
56 hndlWrite.writerow(lsFeatureLine)