Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • eb7458b
  • /
  • src
  • /
  • reformat_plink_freq.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:665a73bee90e890759318fa4569d678c908fc0d5
directory badge
swh:1:dir:51dc8f21506d29022a61063870940fa7eacebc5c

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
reformat_plink_freq.py
#!python3

'''
    Scalable way to reformat geodist tables   
'''

import click
import gzip as gz
import numpy as np


@click.command()
@click.option('--freq_file', help='Frequency File from Plink')
@click.option('--pop_labels', help='Population Labels')
@click.option('--header', help='Header or not', type=bool, default=False)
@click.option('--sep', help='Field separator string', type=str, default=None)
@click.option('--mac', help='Minor Allele Count', type=bool, default=True)
def main(freq_file, pop_labels, header, sep, mac):
  """Reformat a plink file into a reasonable frequency table """
  # 1. Getting a set of population labels 
  pops = []
  with open(pop_labels,'r') as pf:
    for line in pf:
      pop = line.split()[0]
      if pop not in pops:
        pops.append(pop)
	# 2. Reading through the variants 
  with gz.open(freq_file, 'rt') as f:
    if header:
      next(f)
    print('\t'.join(['CHR','SNP','A1','A2'] + pops))
    cur_k = None
    cur_pop_vec = ['0' for i in range(len(pops))]
    for line in f:
      fields = line.split(sep)
      k = (fields[0], fields[1], fields[3], fields[4])
      pop = fields[2]
      if k != cur_k:
        if cur_k is None:
          cur_k = k
        else:
          s = '\t'.join(list(cur_k) + cur_pop_vec)
          print(s)
          cur_k = k
          cur_pop_vec = ['0' for i in range(len(pops))]
          i = pops.index(pop)
          if mac:
            cur_pop_vec[i] = '%s' % fields[6]
          else:
            cur_pop_vec[i] = '%s' % fields[7]
      else:
        i = pops.index(pop)
        if mac:
          cur_pop_vec[i] = '%s' % fields[6]
        else:
          cur_pop_vec[i] = '%s' % fields[7]

if __name__ =='__main__':
	main()

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API