diff splitFasta.py @ 4:ae4d5733272f draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/splitfasta commit 03f3cc2000e6ce876a3cb44c55c3fe878a2e7ce3-dirty
author rnateam
date Fri, 16 Oct 2015 16:13:34 -0400
parents bc25ba9d9fcf
children
line wrap: on
line diff
--- a/splitFasta.py	Wed Jul 08 06:23:46 2015 -0400
+++ b/splitFasta.py	Fri Oct 16 16:13:34 2015 -0400
@@ -1,14 +1,13 @@
-import sys, os
+#!/usr/bin/env python
+import os
+import sys
+from Bio import SeqIO
+
 if __name__ == "__main__":
-    #assuming perfect input, read every two lines
     inpath = sys.argv[1]
-    file_contents = open(inpath, 'r').readlines()
-    os.makedirs('splits')
-    inname = os.path.basename(inpath)
-    for i in range(0, len(file_contents), 2):
-        headline = file_contents[i]
-        outname = headline[1:headline.index(' ')]+'.fa'
-        outfile = open(os.path.join('splits',outname), 'w')
-        outfile.write(file_contents[i])
-        outfile.write(file_contents[i+1])
-        outfile.close()
+    os.mkdir('splits')
+    with open(inpath, 'r') as handle:
+        for record in SeqIO.parse(handle, 'fasta'):
+            header = os.path.join('splits', record.id + '.fasta')
+            with open(header, 'w') as handle2:
+                SeqIO.write([record], handle2, 'fasta')