changeset 1:2f7278120be9 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_fasta_tabular_converter commit 6a93f2809e2939f9d847c3238bfbff8ead367d9f
author drosofff
date Tue, 22 Mar 2016 18:54:00 -0400
parents 951cb6b3979b
children 330dd8a8c31a
files fasta_tabular_converter.py fasta_tabular_converter.xml
diffstat 2 files changed, 66 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/fasta_tabular_converter.py	Sun Jun 21 14:28:49 2015 -0400
+++ b/fasta_tabular_converter.py	Tue Mar 22 18:54:00 2016 -0400
@@ -1,19 +1,37 @@
 #!/usr/bin/python
 #
 import sys
+import string
+import argparse
 from collections import defaultdict
 
-def readfasta_writetabular(fasta, tabular):
-  F = open(fasta, "r")
-  for line in F:
-    if line[0] == ">": continue
-    else:
-      seqdic[line[:-1]] += 1
-  F.close()
-  F = open(tabular, "w")
-  for seq in sorted(seqdic, key=seqdic.get, reverse=True):
-    print >> F, "%s\t%s" % (seq, seqdic[seq])
-  F.close()
+def Parser():
+    the_parser = argparse.ArgumentParser()
+    the_parser.add_argument(
+        '--input', action="store", type=str, help="input file")
+    the_parser.add_argument(
+        '--output', action="store", type=str, help="output converted file")
+    the_parser.add_argument(
+        '--type', action="store", type=str, help="type of convertion")
+    args = the_parser.parse_args()
+    return args
+
+def readfasta_writetabular(fasta, tabular, mode="oneline"):
+    F = open(fasta, "r")
+    for line in F:
+        if line[0] == ">":
+            try:
+                seqdic["".join(stringlist)] += 1 # to dump the sequence of the previous item - try because of first missing stringlist variable
+            except: pass
+            stringlist=[]
+        else:
+            stringlist.append(line[:-1])
+    seqdic["".join(stringlist)] +=  1 # for the last sequence
+    F.close()
+    F = open(tabular, "w")
+    for seq in sorted(seqdic, key=seqdic.get, reverse=True):
+        print >> F, "%s\t%s" % (seq, seqdic[seq])
+    F.close()
     
         
 def readtabular_writefasta(tabular, fasta):
@@ -72,17 +90,19 @@
   F.close()
   Fw.close()
 
-
-seqdic = defaultdict(int)
-option = sys.argv[3]
+def main(input, output, type):
+    if type == "fasta2tabular":
+        readfasta_writetabular(input, output)
+    elif type == "tabular2fasta":
+        readtabular_writefasta(input, output)
+    elif type == "tabular2fastaweight":
+        readtabular_writefastaweighted (input, output)
+    elif type == "fastaweight2fastaweight":
+        readfastaeighted_writefastaweighted(input, output)
+    elif type == "fastaweight2fasta":
+        readfastaeighted_writefasta(input, output)
 
-if option == "fasta2tabular":
-  readfasta_writetabular(sys.argv[1], sys.argv[2])
-elif option == "tabular2fasta":
-  readtabular_writefasta(sys.argv[1], sys.argv[2])
-elif option == "tabular2fastaweight":
-  readtabular_writefastaweighted (sys.argv[1], sys.argv[2])
-elif option == "fastaweight2fastaweight":
-  readfastaeighted_writefastaweighted(sys.argv[1], sys.argv[2])
-elif option == "fastaweight2fasta":
-  readfastaeighted_writefasta(sys.argv[1], sys.argv[2])
+if __name__ == "__main__":
+    seqdic = defaultdict(int)
+    args = Parser()
+    main (args.input, args.output, args.type)
\ No newline at end of file
--- a/fasta_tabular_converter.xml	Sun Jun 21 14:28:49 2015 -0400
+++ b/fasta_tabular_converter.xml	Tue Mar 22 18:54:00 2016 -0400
@@ -1,6 +1,10 @@
-<tool id="fasta_tabular_converter" name="fasta - tabular" version="1.0.2">
+<tool id="fasta_tabular_converter" name="fasta - tabular" version="1.1.0">
   <description>conversions</description>
-  <command interpreter="python">fasta_tabular_converter.py $input $output $switch.conversionType</command>
+  <command interpreter="python">fasta_tabular_converter.py
+                                          --input $input
+                                          --output $output
+                                          --type $switch.conversionType
+  </command>
   <inputs>
     <conditional name="switch">
        <param name="conversionType" type="select" label="conversion option">
@@ -10,23 +14,22 @@
           <option value="fastaweight2fastaweight">recompile weighted fasta to catenated fasta weighted</option>
           <option value="fastaweight2fasta">fasta weighted to fasta</option>
        </param>
-    <when value="fasta2tabular">
-       <param name="input" type="data" format="fasta" label="fasta file to convert to tabular"/>
-    </when>
-    <when value="tabular2fasta">
-       <param name="input" type="data" format="tabular" label="tabular file to convert to fasta"/>
-    </when>
-    <when value="tabular2fastaweight">
-       <param name="input" type="data" format="tabular" label="tabular file to convert to fasta weighted"/>
-    </when>
-    <when value="fastaweight2fastaweight">
-       <param name="input" type="data" format="fasta" label="catenated fasta weighted to recompile" help="Use this option only if you known what you're doing"/>
-    </when>
-    <when value="fastaweight2fasta">
-       <param name="input" type="data" format="fasta" label="fasta weighted file to convert to fasta"/>
-    </when>
-   </conditional>
-
+       <when value="fasta2tabular">
+          <param name="input" type="data" format="fasta" label="fasta file to convert to tabular"/>
+       </when>
+       <when value="tabular2fasta">
+          <param name="input" type="data" format="tabular" label="tabular file to convert to fasta"/>
+       </when>
+       <when value="tabular2fastaweight">
+          <param name="input" type="data" format="tabular" label="tabular file to convert to fasta weighted"/>
+       </when>
+       <when value="fastaweight2fastaweight">
+          <param name="input" type="data" format="fasta" label="catenated fasta weighted to recompile" help="Use this option only if you known what you're doing"/>
+       </when>
+       <when value="fastaweight2fasta">
+          <param name="input" type="data" format="fasta" label="fasta weighted file to convert to fasta"/>
+       </when>
+    </conditional>
    </inputs>
 
  <outputs>