Skip to content

Instantly share code, notes, and snippets.

@suqingdong
Created October 17, 2016 15:12
Show Gist options
  • Save suqingdong/79c45f691b5b3da17f213f75a5f1759c to your computer and use it in GitHub Desktop.
Save suqingdong/79c45f691b5b3da17f213f75a5f1759c to your computer and use it in GitHub Desktop.
convert pos and samples from replaced files
#!/usr/bin/env python
#=======================================================
# convert pos and samples
# row name is sample name, and column name is position
# value is the number of alt (0,1,2)
#=======================================================
# sampledict: {'sample1':['0','2',...], ...}
def convertSNP(infile, outfile):
with open(infile) as f:
header = f.readline().strip().split('\t')
samplenames = header[6:]
sampledict = {}
poslist = []
#print samplenames
for line in f:
pos = line.strip().split('\t')[1]
poslist.append(pos)
sampleinfos = line.strip().split('\t')[6:]
for samplename, snp in zip(samplenames,sampleinfos):
if samplename not in sampledict:
sampledict[samplename] = [snp]
else:
sampledict[samplename].append(snp)
with open(outfile, 'w') as out:
out.write('Sample\t'+'\t'.join(poslist)+'\n')
for samplename, snp in sampledict.items():
out.write(samplename+'\t'+'\t'.join(snp)+'\n')
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print "Usage: python %s <infile> <outfile>" % sys.argv[0]
exit(1)
infile, outfile = sys.argv[1:3]
convertSNP(infile, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment