changeset 1:07a23652bc2a draft

Uploaded
author davidvanzessen
date Tue, 25 Mar 2014 06:59:26 -0400
parents 03dbb4601b15
children f2c4c7151016
files igblastmerge.py igblastmerge.xml
diffstat 2 files changed, 40 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/igblastmerge.py	Thu Jan 23 08:14:08 2014 -0500
+++ b/igblastmerge.py	Tue Mar 25 06:59:26 2014 -0400
@@ -1,52 +1,44 @@
 import sys
-# error
-def stop_err( msg ):
-	sys.stderr.write( "%s\n" % msg )
-	sys.exit()
-
-# main
-def main():
-    args = sys.argv[1:-2]
-
-    try:
-        o = open(sys.argv[-1], 'w')
-        i = open(args[1], 'r')
-        separator = "\t"
-        newline = "\n"
-        line = i.readline()
-        #write the header
-        o.write(line[:line.rfind(newline)] + separator + "Sample" + separator + "Replicate" + newline)
-        i.close()
-
-        current = 1
-        sampleID = args[0]
-        count = 1
+import pandas as pd
 
-        while True:
-            print str(o)
-            f = open(args[current], 'r')
-            line = f.readline()
-            line = f.readline()
-            while line:
-                o.write(line[:line.rfind(newline)] + separator + sampleID + separator + str(count) + newline)
-                line = f.readline()
-            f.close()
+def main():
+	patients = {}
+	files = []
+	sample_id = sys.argv[1]
+	imgt_files = 0
+	blast_files = 0
+	#organize files
+	for arg in sys.argv[2:-2]:
+		if arg.find("/") is -1:
+			patients[sample_id] = files
+			files = []
+			sample_id = arg
+		else:
+			df = pd.read_csv(arg, sep="\t")
+			if "Functionality" in list(df.columns.values):
+				df["VDJ Frame"][df["Functionality"] != "productive"] = "In-frame with stop codon"
+				imgt_files += 1
+			else:
+				blast_files += 1
+			files.append(df)
+	patients[sample_id] = files
+	columns = [u'ID', u'VDJ Frame', u'Top V Gene', u'Top D Gene', u'Top J Gene', u'CDR1 Seq', u'CDR1 Length', u'CDR2 Seq', u'CDR2 Length', u'CDR3 Seq', u'CDR3 Length', u'CDR3 Seq DNA', u'CDR3 Length DNA', u'Strand', u'CDR3 Found How', u'Functionality', 'V-REGION identity %', 'V-REGION identity nt', 'D-REGION reading frame', 'AA JUNCTION', 'Functionality comment', 'Sequence', 'FR1-IMGT', 'FR2-IMGT', 'FR3-IMGT', 'CDR3-IMGT', 'JUNCTION', 'J-REGION', 'FR4-IMGT', 'P3V-nt nb', 'N1-REGION-nt nb', 'P5D-nt nb', 'P3D-nt nb', 'N2-REGION-nt nb', 'P5J-nt nb', '3V-REGION trimmed-nt nb', '5D-REGION trimmed-nt nb', '3D-REGION trimmed-nt nb', '5J-REGION trimmed-nt nb', u'Sample', u'Replicate']
+	if blast_files is not 0:
+		print "Has a parsed blastn file, using limited columns."
+		columns = [u'ID', u'VDJ Frame', u'Top V Gene', u'Top D Gene', u'Top J Gene', u'CDR1 Seq', u'CDR1 Length', u'CDR2 Seq', u'CDR2 Length', u'CDR3 Seq', u'CDR3 Length', u'CDR3 Seq DNA', u'CDR3 Length DNA', u'Strand', u'CDR3 Found How', u'Sample', u'Replicate']
 
-            if current >= (len(args) - 1):
-                break
-            if args[current + 1].find("/") is -1:
-                sampleID = args[current + 1]
-                current += 1
-                count = 1
-            else:
-                count += 1
-            current += 1
-        o.close()
-
-    except Exception, ex:
-        stop_err('Error running new_column.py\n' + str(ex))
-    sys.exit(0)
+	result = None
+	for patient_id, samples in patients.iteritems():
+		count = 1
+		for sample in samples:
+			sample['Sample'] = patient_id
+			sample['Replicate'] = str(count)
+			count += 1
+			if result is None:
+				result = sample[columns]
+			else:
+				result = result.append(sample[columns])
+	result.to_csv(sys.argv[-1], sep="\t", index=False, index_label="index")
 
 if __name__ == "__main__":
-	print sys.argv
 	main()
--- a/igblastmerge.xml	Thu Jan 23 08:14:08 2014 -0500
+++ b/igblastmerge.xml	Tue Mar 25 06:59:26 2014 -0400
@@ -3,7 +3,7 @@
 	<command interpreter="python">
 		igblastmerge.py 
 		#for $i, $f in enumerate($patients)
-            $f.id
+            "$f.id"
             #for $j, $g in enumerate($f.samples)
             	${g.sample}
             #end for
@@ -12,7 +12,7 @@
 		--output $out_file
 	</command>
 	<inputs>
-		<repeat name="patients" title="Patients" min="1" default="1">
+		<repeat name="patients" title="Patient" min="1" default="1">
             <repeat name="samples" title="Sample" min="1" default="1">
                 <param name="sample" format="tabular" type="data" label="Sample to Process" />
             </repeat>