Advertisement
Guest User

Untitled

a guest
May 27th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. #Name: chr_restore_all
  2. #Autor: Marieke Beltman
  3. #Date: 11-03-2015
  4. #Function: reads map files, selects the first column and changes the first column in the selected chromosome (instead of 0), and writes it in a new file. Needed for .map files from PLINK 1.07, because this version of PLINK doesn't recognize other chromosomes than human.
  5.  
  6. #For every item in chrList, the map file and a new file will be opened and are send to the function restoreChr.
  7. import sys, getopt
  8.  
  9. outname = sys.argv[1]
  10.  
  11.  
  12. def main(outname):
  13.     chrList = ["27", "28", "1A","25LG1", "25LG2","4A", "LGE22","Z"]
  14.     snpChrList ={}
  15.     for i in chrList:  
  16.         snpChrList = openchrFile(i,snpChrList)     
  17.     #print(snpChrList)
  18.     mapFile= openFile(outname)
  19.     newMapFile= newFile(outname)
  20.     restoreChr(snpChrList,mapFile,newMapFile)
  21.  
  22. def openFile(outname):
  23.     with open(outname+".map","r") as mapFile:
  24.         best = mapFile.readlines()
  25.     return best
  26.  
  27. def openchrFile(i,chrom):
  28.     with open("SNPs_per_chr/chr_GT_"+i+".txt","r") as chrFile:
  29.         chrLines = chrFile.readlines()
  30.         for line in chrLines:
  31.             line = line.strip()
  32.             chrom[line]=i
  33.     return chrom
  34.  
  35. def newFile(outname):
  36.     new = open(outname+"_2.map","w")
  37.     return new
  38.  
  39. #Every line in the map file is splitted up and the first item of the line is changed in the chromosome, then the line is written to the new file
  40. def restoreChr(snpChrList,mapFile,newMapFile):
  41.     for line in mapFile:
  42.         if line.startswith('0'):
  43.             line = line.strip().split("\t")
  44.             if line[1] in snpChrList.keys():
  45.                 line[0]= snpChrList.get(line[1])
  46.                 newMapFile.write(("\t".join(line))+"\n")
  47.             elif line[1] not in snpChrList.keys():
  48.                 line[0] = 'Un'
  49.                 newMapFile.write(("\t".join(line))+"\n")
  50.         else:
  51.             newMapFile.write(line)
  52.        
  53.    
  54. main(outname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement