Sort VCF user Kantale

From PyPedia
Revision as of 01:29, 19 April 2012 by Kantale (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

[edit] Documentation

Sorts a VCF (http://vcftools.sourceforge.net/specs.html) file according to position of the variants. All the sorting happens in memory (tested for 150MB of VCF)


[edit] Parameters

<inputs>
</inputs>


[edit] Return

None

[edit] See also

Annotate_VCF_file_via_ANNOVAR_user_Kantale , Convert_VCF_file_to_TAB_user_Kantale

[edit] Code

def ChrToInt(chr):
        if chr == "X": return 23
        if chr == "Y": return 24
        if chr == "M" or chr == "MT": return 25
        return int(chr)

def sortFunction(x,y):
        splittedX = x.split("\t")
        splittedY = y.split("\t")

        if splittedX[0] != splittedY[0]:
                return ChrToInt(splittedX[0]) - ChrToInt(splittedY[0])

        return int(splittedX[1]) - int(splittedY[1])

import os

def Sort_VCF_user_Kantale(inputFilename, outputFilename):

        print "Removing the header.."
        command = 'cat ' + inputFilename + ' | grep -v "#"  > ' + outputFilename + '.toSort'
        print command
        os.system(command)

        command = 'cat ' + inputFilename + ' | grep "#"  > ' + outputFilename + '.header'
        print command
        os.system(command)
        print "..Done"

        print "Loading.."
        filelines = open(outputFilename + '.toSort').readlines()
        print "..Done"

        print "Sorting.."
        fileLinesSorted = sorted(filelines, sortFunction)
        print "..Done"

        print "Outputing..."
        fileOutput = open(outputFilename + '.sorted', "w")
        for line in fileLinesSorted: fileOutput.write(line)
        fileOutput.close()
        print "..Done"

        print "Merging.."
        command = 'cat ' + outputFilename + '.header' + ' ' + outputFilename + '.sorted > ' + outputFilename
        print command
        os.system(command)
        print "..Done"

        print "Deleting intermediate files.."
        Delete_file_user_Kantale(outputFilename + '.header')
        Delete_file_user_Kantale(outputFilename + '.toSort')
        Delete_file_user_Kantale(outputFilename + '.sorted')
        print "..Done"

[edit] Unit Tests

def uni1():
   return True

[edit] Development Code

def Sort_VCF_user_Kantale():
	pass

[edit] Permissions

[edit] Documentation Permissions

Kantale

[edit] Code Permissions

Kantale

[edit] Unit Tests Permissions

Kantale

[edit] Permissions Permissions

Kantale

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox