Commit message:
Uploaded |
added:
convert.xml converter.py |
b |
diff -r 000000000000 -r 37392af48c37 convert.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/convert.xml Thu Mar 19 11:46:50 2015 -0400 |
b |
@@ -0,0 +1,47 @@ +<tool id="phylogeny_converter" name="phylogeny_converter"> + <description></description> + <command interpreter="python"> + converter.py $input $output $filetype.inouttype $filetype.type + </command> + <inputs> + <conditional name="filetype"> + <param name="type" type="select" label="is this a multi-sequence file?"> + <option value="multi">Multi sequence</option> + <option value="single">Single sequence</option> + </param> + <when value="single"> + <param name="input" type="data" label="Original sigle sequence file"/> + <param name="inouttype" type="select" label="conversion"> + <option value="f-g">Fasta to GenBank</option> + <option value="g-f">GenBank to Fasta</option> + </param> + </when> + <when value="multi"> + <param name="input" type="data" label="Original multi sequence file" help="Please note that Phylip and nexus limit sequence names to a maximum of 10 characters"/> + <param name="inouttype" type="select" label="conversion"> + <option value="f-g">Fasta to GenBank</option> + <option value="g-f">GenBank to Fasta</option> + <option value="f-p">Fasta to Phylip</option> + <option value="f-n">Fasta to Nexus</option> + <option value="p-f">Phylip to Fasta</option> + <option value="p-n">Phylip to Nexus</option> + <option value="n-p">Nexus to Phylip</option> + <option value="n-f">Nexus to Fasta</option> + </param> + </when> + </conditional> + </inputs> + <outputs> + <data name="output" format="tabular"/> + </outputs> + <help> + +What it does + +This tool converts different file formats to allow the data-exchange from different philogeny tools. + +Developed by +Iolanda Mangone + + </help> +</tool> |
b |
diff -r 000000000000 -r 37392af48c37 converter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/converter.py Thu Mar 19 11:46:50 2015 -0400 |
[ |
b'@@ -0,0 +1,535 @@\n+\r\n+#!/usr/bin/env python\r\n+\r\n+import sys\r\n+import string\r\n+import os\r\n+\r\n+class convertitori:\r\n+\tdef __init__(self,input,inouttype,type,output):\r\n+\t\tself.input = input\r\n+\t\tself.inouttype = inouttype\r\n+\t\tself.type = type\r\n+\t\tself.output = output\r\n+\r\n+\tdef fp(self):\r\n+\t\tcount = 0\r\n+\t\tcseq = 0\r\n+\t\tfasta = []\r\n+\t\tfor riga in self.input:\r\n+\t\t\tcount += 1\r\n+\t\t if ">" in riga:\r\n+\t\t f = ""\r\n+\t\t p = self.input.index(riga,count-1)\r\n+\t\t c = 1\r\n+ y = riga[1:].replace(" ","_")\r\n+ if y >= 10:\r\n+ f = f + y[:10] + \'\\t\'\r\n+ else:\r\n+ f = f + y + "_"*(10-len(y)) + \'\\t\'\r\n+\t\t\t\ttry:\r\n+\t\t\t while ">" not in self.input[p+c]:\r\n+\t\t\t f = f + (self.input[p+c].strip())\r\n+\t\t\t c += 1\r\n+\t\t\t\texcept:\r\n+\t\t\t\t\tpass\r\n+\t\t\t\tfasta.append(f)\r\n+\t\tnum = str(len(fasta))\r\n+\t\tlun = str(len(fasta[0].split("\\t")[1]))\r\n+\t\tfor sequence in fasta:\r\n+\t\t\tif str(len(sequence.split("\\t")[1])) != lun:\r\n+\t\t\t\tsys.exit("The input file does not contains a multiple alignment in fasta format. Please ensure that all the sequences have the same length")\r\n+\t\tself.output.write(num + \'\\t\' + lun + \'\\n\')\r\n+\t\tfor seq in fasta:\r\n+\t\t self.output.write(seq + \'\\n\')\r\n+\r\n+\tdef fn(self):\r\n+\t\tcount = 0\r\n+\t\tfasta = []\r\n+\t\tfor riga in self.input:\r\n+\t\t\tcount += 1\r\n+\t\t if ">" in riga:\r\n+\t\t f = ""\r\n+\t\t p = self.input.index(riga,count-1)\r\n+\t\t c = 1\r\n+\t\t\t\ty = riga[1:].replace(" ","_")\r\n+\t\t\t\tif y >= 10:\r\n+\t\t\t f = f + y[:10] + \'\\t\'\r\n+\t\t\t\telse:\r\n+\t\t\t\t\tf = f + y + "_"*(10-len(y)) + \'\\t\'\r\n+\t\t\t\ttry:\r\n+ while ">" not in self.input[p+c]:\r\n+ f = f + (self.input[p+c].strip())\r\n+ c += 1\r\n+\t\t\t\texcept:\r\n+\t\t\t\t\tpass\r\n+\t\t\t\tfasta.append(f)\r\n+\t\tnum = str(len(fasta))\r\n+\t\tlun = str(len(fasta[0].split("\\t")[1]))\r\n+\t\tfor sequence in fasta:\r\n+\t\t\tif str(len(sequence.split("\\t")[1])) != lun:\r\n+\t\t\t\tsys.exit("The input file does not contains a multiple alignment in fasta format. Please ensure that all the sequences have the same length")\r\n+\t\tself.output.write("#NEXUS\\n\\nBEGIN DATA;\\nDIMENSIONS NTAX=%s NCHAR=%s;\\nFORMAT DATATYPE=DNA INTERLEAVE MISSING=-;\\n\\nMATRIX\\n"%(num,lun))\r\n+\t\tporzioni = int(lun)/100\r\n+\t\tfor volte in range(porzioni):\r\n+\t\t\tfor seq in fasta:\r\n+\t\t\t\tpart = ""\r\n+\t\t\t\tself.output.write(seq.split("\\t")[0] + \'\\t\')\r\n+\t\t\t\tcont = 0\r\n+\t\t\t\tfor chara in seq.split("\\t")[1][volte*100:(volte+1)*100]:\r\n+\t\t\t\t\tcont += 1\r\n+\t\t\t\t\tpart = part + chara\r\n+\t\t\t\t\tif cont%20.0 == 0:\r\n+\t\t\t\t\t\tpart = part + " "\r\n+\t\t\t\tpart = part + "\\n"\r\n+\t\t\t\tself.output.write(part)\r\n+\t\t\tself.output.write("\\n\\n\\n")\r\n+\t\tfor seq in fasta:\r\n+\t\t\tpart = ""\r\n+\t\t\tcont = 0\r\n+\t\t\tself.output.write(seq.split("\\t")[0] + \'\\t\')\r\n+\t\t\tfor chara in seq.split("\\t")[1][(volte+1)*100:]:\r\n+\t\t\t\tcont += 1\r\n+\t\t\t\tpart = part + chara\r\n+\t\t\t\tif cont%20.0 == 0:\r\n+\t\t\t\t\tpart = part + " "\r\n+\t\t\tpart = part + "\\n"\r\n+\t\t\tself.output.write(part)\r\n+\r\n+\tdef pn(self):\r\n+\t\tnum = int(self.input[0].split()[0])\r\n+\t\tlun = float(self.input[0].split()[1])\r\n+\t\tlunf = float(len(self.input))\r\n+\t\tself.output.write("#NEXUS\\n\\nBEGIN DATA;\\nDIMENSIONS NTAX=%s NCHAR=%s;\\nFORMAT DATATYPE=DNA INTERLEAVE MISSING=-;\\n\\nMATRIX\\n"%(int(num),lun))\r\n+\t\tspia = 0\r\n+\t\tporzioni = int(lun)/100\r\n+\t\tif (lunf-1)/num == 1.0:\r\n+\t\t\tspia = 1\r\n+\t\tif spia == 1:\r\n+\t\t\tfor volte in range(porzioni):\r\n+\t\t\t\tfor seq in self.input[1:]:\r\n+\t part = ""\r\n+\t self.output.write(seq.split("\\t")[0] + \'\\t\')\r\n+\t cont = 0\r\n+\t for chara in seq.split("\\t")[1][volte*100:(volte+1)*100]:\r\n+\t cont += 1\r\n+\t '..b'equence\' option")\r\n+\t\t\t\tif count == 0:\r\n+\t\t\t\t\tsys.exit("The input file is not in fasta format. Please check that the first row starts with > and that the sequence starts from the second line")\r\n+\t\tif self.intype == "g":\r\n+\t\t\tlocus = 0\r\n+\t\t\torigin = 0\r\n+\t\t\tend = 0\r\n+\t\t\tlun = 1\r\n+\t\t\tfor riga in self.infile:\r\n+\t\t\t\tif "LOCUS" in riga:\r\n+\t\t\t\t\tlocus = 1\r\n+\t\t\t\tif "ORIGIN" in riga:\r\n+\t\t\t\t\torigin = 1\r\n+\t\t\t\telif origin == 1 and len(riga.split()) >= 7:\r\n+\t\t\t\t\tlun = 0\r\n+\t\t\t\tif "//" in riga:\r\n+\t\t\t\t\tend = 1\r\n+\t\t\tif locus == 0 or origin == 0 or end == 0 or lun == 1:\r\n+\t\t\t\tsys.exit("The input file is not in GenBank format. Please make sure that the file contains at least the LOCUS and ORIGIN fields. The file must also ends with //")\r\n+\t\t\telse:\r\n+\t\t\t\treturn "ok" \r\n+\tdef multi(self):\r\n+\t\tif self.intype == "p":\r\n+\t\t\tif len(self.infile[0].split()) == 2 or len(self.infile[0].split()) == 3:\r\n+\t\t\t\tif int(self.infile[0].split()[0]) > 1:\r\n+\t\t\t\t\treturn "ok"\r\n+\t\t\t\telse:\r\n+\t\t\t\t\tsys.exit("There is only one sequence in the file")\r\n+\t\t\telse:\r\n+\t\t\t\tsys.exit("the input file is not in phylip format.")\r\n+\t\tif self.intype == "n":\r\n+\t\t\tbegin = 0\r\n+\t\t\tmatrix = 0\r\n+\t\t\tntax = 0\r\n+\t\t\tif "#NEXUS" in self.infile[0]:\r\n+\t\t\t\tfor riga in self.infile:\r\n+\t\t\t\t\tif "begin data;" in riga.lower():\r\n+\t\t\t\t\t\tbegin = 1\r\n+\t\t\t\t\tif "matrix" in riga.lower():\r\n+\t\t\t\t\t\tmatrix = 1\r\n+\t\t\t\t\tif "ntax" in riga.lower():\r\n+\t\t\t\t\t\tr = riga.split()\r\n+\t\t\t\t\t\tntax = int(r[1][5:])\r\n+\t\t\t\tif begin==1 and matrix == 1:\r\n+\t\t\t\t\treturn "ok"\r\n+\t\t\t\telse:\r\n+\t\t\t\t\tsys.exit("the input file is not in nexus format.")\r\n+\t\t\t\tif ntax <= 1:\r\n+\t\t\t\t\t sys.exit("There is only one sequence in the file")\t\t\t\t\r\n+\t\t\telse:\r\n+\t\t\t\tsys.exit("the input file is not in nexus format.")\r\n+\t\tif self.intype == "f":\r\n+\t\t\tcount = 0\r\n+\t\t\tfor riga in self.infile:\r\n+\t\t\t\tif riga[0] == ">":\r\n+\t\t\t\t\tcount += 1\r\n+\t\t\tif count > 1:\r\n+\t\t\t\tif len(self.infile) < 4:\r\n+\t\t\t\t\tsys.exit("The input file is not in fasta format. Please check that the first row starts with > and that the sequence starts from the second line")\r\n+\t\t\t\telse:\r\n+\t\t\t\t\treturn "ok"\r\n+\t\t\telse:\r\n+\t\t\t\tif count == 1:\r\n+\t\t\t\t\tsys.exit("The input file is a single-fasta file. Please resubmit the job using the \'single sequence\' option")\r\n+\t\t\t\tif count == 0:\r\n+\t\t\t\t\tsys.exit("The input file is not in fasta format. Please check that the first row starts with > and that the sequence starts from the second line")\r\n+\t\tif self.intype == "g":\r\n+\t\t\tlocus = 0\r\n+\t\t\torigin = 0\r\n+\t\t\tend = 0\r\n+\t\t\tlun = 1\r\n+\t\t\tfor riga in self.infile:\r\n+\t\t\t\tif "LOCUS" in riga:\r\n+\t\t\t\t\tlocus = 1\r\n+\t\t\t\tif "ORIGIN" in riga:\r\n+\t\t\t\t\torigin = 1\r\n+\t\t\t\tif origin == 1 and len(riga.split()) >= 7:\r\n+\t\t\t\t\tlun = 0\r\n+\t\t\t\tif "//" in riga:\r\n+\t\t\t\t\tend = 1\r\n+\t\t\tif locus == 0 or origin == 0 or end == 0 or lun == 1:\r\n+\t\t\t\tsys.exit("The input file is not in GenBank format. Please make sure that the file contains at least the LOCUS and ORIGIN fields. The file must also ends with //")\r\n+\t\t\telse:\r\n+\t\t\t\treturn "ok"\r\n+\r\n+\r\n+def main(input,output,inouttype,type):\r\n+\tcheck = check_fileformat(inouttype,input)\r\n+\tif type == "single":\r\n+\t\tc = check.single()\r\n+\t\tif c == "ok":\r\n+\t\t\tconv = convertitori(input,inouttype,type,output)\r\n+\t if inouttype == "f-g":\r\n+\t conv.fg()\r\n+\t if inouttype == "g-f":\r\n+\t conv.gf()\r\n+\tif type == "multi":\r\n+\t\tc = check.multi()\r\n+\t\tif c == "ok":\r\n+\t\t\tconv = convertitori(input,inouttype,type,output)\r\n+\t\t\tif inouttype == "f-g":\r\n+\t\t\t\tconv.fg()\r\n+\t\t\tif inouttype == "g-f":\r\n+\t\t\t\tconv.gf()\r\n+\t\t\tif inouttype == "f-p":\r\n+\t\t\t\tconv.fp()\r\n+\t\t\tif inouttype == "f-n":\r\n+\t\t\t\tconv.fn()\r\n+\t\t\tif inouttype == "p-f":\r\n+\t\t\t\tconv.pf()\r\n+\t\t\tif inouttype == "p-n":\r\n+\t\t\t\tconv.pn()\r\n+\t\t\tif inouttype == "n-p":\r\n+\t\t\t\tconv.np()\r\n+\t\t\tif inouttype == "n-f":\r\n+\t\t\t\tconv.nf()\r\n+\toutput.close()\r\n+\r\n+if __name__ == "__main__" : \r\n+\tinput = open(sys.argv[1],"r").readlines()\r\n+\toutput = open(sys.argv[2],"a")\r\n+\tinouttype = sys.argv[3]\r\n+ type = sys.argv[4]\r\n+\tmain(input,output,inouttype,type)\r\n' |